VirtualBox

儲存庫 vbox 的更動 67645


忽略:
時間撮記:
2017-6-27 下午04:36:58 (7 年 以前)
作者:
vboxsync
訊息:

IPRT: RTCString::find: Made it return npos if the string to search for is empty or NULL (strstr will return the first string if empty and crash if NULL). Nobody seems to try search for empty strings. Added a find method taking a string object.

位置:
trunk
檔案:
修改 2 筆資料

圖例:

未更動
新增
刪除
  • trunk/include/iprt/cpp/ministring.h

    r65811 r67645  
    694694     * Find the given substring.
    695695     *
    696      * Looks for pcszFind in "this" starting at "pos" and returns its position
    697      * as a byte (not codepoint) offset, counting from the beginning of "this" at 0.
    698      *
    699      * @param   pcszFind        The substring to find.
    700      * @param   pos             The (byte) offset into the string buffer to start
     696     * Looks for @a pszNeedle in @a this starting at @a offStart and returns its
     697     * position as a byte (not codepoint) offset, counting from the beginning of
     698     * @a this as 0.
     699     *
     700     * @param   pszNeedle       The substring to find.
     701     * @param   offStart        The (byte) offset into the string buffer to start
    701702     *                          searching.
    702703     *
    703      * @returns 0 based position of pcszFind. npos if not found.
    704      */
    705     size_t find(const char *pcszFind, size_t pos = 0) const;
     704     * @returns 0 based position of pszNeedle. npos if not found.
     705     */
     706    size_t find(const char *pszNeedle, size_t offStart = 0) const;
     707
     708    /**
     709     * Find the given substring.
     710     *
     711     * Looks for @a pStrNeedle in @a this starting at @a offStart and returns its
     712     * position as a byte (not codepoint) offset, counting from the beginning of
     713     * @a this as 0.
     714     *
     715     * @param   pStrNeedle      The substring to find.
     716     * @param   offStart        The (byte) offset into the string buffer to start
     717     *                          searching.
     718     *
     719     * @returns 0 based position of pStrNeedle. npos if not found or pStrNeedle is
     720     *          NULL or an empty string.
     721     */
     722    size_t find(const RTCString *pStrNeedle, size_t offStart = 0) const;
    706723
    707724    /**
  • trunk/src/VBox/Runtime/common/string/ministring.cpp

    r62477 r67645  
    200200}
    201201
    202 size_t RTCString::find(const char *pcszFind, size_t pos /*= 0*/) const
    203 {
    204     if (pos < length())
     202size_t RTCString::find(const char *pszNeedle, size_t offStart /*= 0*/) const
     203{
     204    if (offStart < length())
    205205    {
    206206        const char *pszThis = c_str();
    207207        if (pszThis)
    208208        {
    209             const char *pszHit = strstr(pszThis + pos, pcszFind);
    210             if (pszHit)
    211                 return pszHit - pszThis;
     209            if (pszNeedle && *pszNeedle != '\0')
     210            {
     211                const char *pszHit = strstr(pszThis + offStart, pszNeedle);
     212                if (pszHit)
     213                    return pszHit - pszThis;
     214            }
     215        }
     216    }
     217
     218    return npos;
     219}
     220
     221size_t RTCString::find(const RTCString *pStrNeedle, size_t offStart /*= 0*/) const
     222{
     223    if (offStart < length())
     224    {
     225        const char *pszThis = c_str();
     226        if (pszThis)
     227        {
     228            if (pStrNeedle)
     229            {
     230                const char *pszNeedle = pStrNeedle->c_str();
     231                if (pszNeedle && *pszNeedle != '\0')
     232                {
     233                    const char *pszHit = strstr(pszThis + offStart, pszNeedle);
     234                    if (pszHit)
     235                        return pszHit - pszThis;
     236                }
     237            }
    212238        }
    213239    }
注意: 瀏覽 TracChangeset 來幫助您使用更動檢視器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette