VirtualBox

Ignore:
Timestamp:
Jun 27, 2017 4:36:58 PM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
116419
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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    }
Note: See TracChangeset for help on using the changeset viewer.

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