VirtualBox

Changeset 16324 in vbox for trunk/src/VBox/Main/glue


Ignore:
Timestamp:
Jan 28, 2009 6:06:59 PM (16 years ago)
Author:
vboxsync
Message:

COM: teach Utf8Str substr() like std::string

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/string.cpp

    r14948 r16324  
    3535/* static */
    3636const Utf8Str Utf8Str::Null; /* default ctor is OK */
     37
     38const size_t Utf8Str::npos = (size_t)-1;
     39
     40Utf8Str Utf8Str::substr(size_t pos /*= 0*/, size_t n /*= npos*/) const
     41{
     42    Utf8Str ret;
     43
     44    if (n)
     45    {
     46        const char *psz = c_str();
     47        RTUNICP cp;
     48
     49        // walk the UTF-8 characters until where the caller wants to start
     50        size_t i = pos;
     51        while (i--)
     52            RTStrGetCpEx(&psz, &cp);
     53
     54        const char *pFirst = psz;
     55
     56        if (n == npos)
     57            // all the rest:
     58            ret = pFirst;
     59        else
     60        {
     61            i = n;
     62            while (i--)
     63                RTStrGetCpEx(&psz, &cp);
     64
     65            size_t len = psz - pFirst;
     66            char *psz = (char*)RTMemAlloc(len + 1);
     67            memcpy(psz, pFirst, len);
     68            psz[len] = '\0';
     69            ret = psz;
     70            RTMemFree(psz);
     71        }
     72    }
     73
     74    return ret;
     75}
    3776
    3877struct FormatData
     
    99138}
    100139
     140
    101141} /* namespace com */
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