VirtualBox

Changeset 16324 in vbox


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

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/string.h

    r15051 r16324  
    442442    const char *raw() const { return str; }
    443443
     444    /** The same as operator const char *(), but for situations where the compiler
     445        cannot typecast implicitly (for example, in printf() argument list). */
     446    const char *c_str() const { return str; }
     447
    444448    /**
    445449     *  Returns a non-const raw pointer that allows to modify the string directly.
     
    495499        return *this;
    496500    }
     501
     502    static const size_t npos;
     503
     504    Utf8Str substr(size_t pos = 0, size_t n = npos) const;
    497505
    498506    /**
  • 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