VirtualBox

Changeset 33563 in vbox for trunk/include/iprt/cpp


Ignore:
Timestamp:
Oct 28, 2010 2:46:26 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
67169
Message:

iprt:ministring: Added the java-style equals() and equalsIgnoreCase() as equals() can optimize the comparison by first checking if the length is the same (compare() cannot as it needs to determin the ordering). Added appendCodePoint() for UTF-8. Fixed the incorrect assumption in toUpper and toLower that the string length remained unchanged - the string might shrink as the folded code points may have a shorter encoding. Added testcase that verifies that a code point will not grow during folding and that have a stable encoding length after it has been changed in a folding.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/ministring.h

    r33074 r33563  
    6565    MiniString()
    6666        : m_psz(NULL),
    67           m_cbLength(0),
     67          m_cch(0),
    6868          m_cbAllocated(0)
    6969    {
     
    118118    size_t length() const
    119119    {
    120         return m_cbLength;
     120        return m_cch;
    121121    }
    122122
     
    149149    {
    150150        if (    cb != m_cbAllocated
    151              && cb > m_cbLength + 1
     151             && cb > m_cch + 1
    152152           )
    153153        {
     
    235235     * Appends the given character to "this".
    236236     *
    237      * @param   c               The character to append.
     237     * @param   ch              The character to append.
    238238     *
    239239     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     
    241241     * @returns Reference to the object.
    242242     */
    243     MiniString &append(char c);
     243    MiniString &append(char ch);
     244
     245    /**
     246     * Appends the given unicode code point to "this".
     247     *
     248     * @param   uc              The unicode code point to append.
     249     *
     250     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     251     *
     252     * @returns Reference to the object.
     253     */
     254    MiniString &appendCodePoint(RTUNICP uc);
    244255
    245256    /**
     
    287298    {
    288299        if (length())
     300        {
     301            /* Folding an UTF-8 string may result in a shorter encoding (see
     302               testcase), so recalculate the length afterwars. */
    289303            ::RTStrToUpper(m_psz);
     304            size_t cchNew = strlen(m_psz);
     305            Assert(cchNew <= m_cch);
     306            m_cch = cchNew;
     307        }
    290308        return *this;
    291309    }
     
    299317    {
    300318        if (length())
     319        {
     320            /* Folding an UTF-8 string may result in a shorter encoding (see
     321               testcase), so recalculate the length afterwars. */
    301322            ::RTStrToLower(m_psz);
     323            size_t cchNew = strlen(m_psz);
     324            Assert(cchNew <= m_cch);
     325            m_cch = cchNew;
     326        }
    302327        return *this;
    303328    }
     
    361386        if (m_psz)
    362387        {
    363             m_cbLength = strlen(m_psz);
    364             m_cbAllocated = m_cbLength + 1; /* (Required for the Utf8Str::asOutParam case) */
     388            m_cch = strlen(m_psz);
     389            m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
    365390        }
    366391        else
    367392        {
    368             m_cbLength = 0;
     393            m_cch = 0;
    369394            m_cbAllocated = 0;
    370395        }
     
    434459    }
    435460
     461    /**
     462     * Compares the two strings.
     463     *
     464     * @returns true if equal, false if not.
     465     * @param   that    The string to compare with.
     466     */
     467    bool equals(const MiniString &that) const
     468    {
     469        return that.length() == length()
     470            && memcmp(that.m_psz, m_psz, length()) == 0;
     471    }
     472
     473    /**
     474     * Compares the two strings.
     475     *
     476     * @returns true if equal, false if not.
     477     * @param   pszThat The string to compare with.
     478     */
     479    bool equals(const char *pszThat) const
     480    {
     481        return RTStrCmp(pszThat, m_psz) == 0;
     482    }
     483
     484    /**
     485     * Compares the two strings ignoring differences in case.
     486     *
     487     * @returns true if equal, false if not.
     488     * @param   that    The string to compare with.
     489     */
     490    bool equalsIgnoreCase(const MiniString &that) const
     491    {
     492        /* Unfolded upper and lower case characters may require different
     493           amount of encoding space, so the length optimization doesn't work. */
     494        return RTStrICmp(that.m_psz, m_psz) == 0;
     495    }
     496
     497    /**
     498     * Compares the two strings ignoring differences in case.
     499     *
     500     * @returns true if equal, false if not.
     501     * @param   pszThat The string to compare with.
     502     */
     503    bool equalsIgnoreCase(const char *pszThat) const
     504    {
     505        return RTStrICmp(pszThat, m_psz) == 0;
     506    }
     507
    436508    /** @name Comparison operators.
    437509     * @{  */
    438     bool operator==(const MiniString &that) const { return !compare(that); }
    439     bool operator!=(const MiniString &that) const { return !!compare(that); }
     510    bool operator==(const MiniString &that) const { return equals(that); }
     511    bool operator!=(const MiniString &that) const { return !equals(that); }
    440512    bool operator<( const MiniString &that) const { return compare(that) < 0; }
    441513    bool operator>( const MiniString &that) const { return compare(that) > 0; }
    442514
    443     bool operator==(const char *that) const       { return !compare(that); }
    444     bool operator!=(const char *that) const       { return !!compare(that); }
    445     bool operator<( const char *that) const       { return compare(that) < 0; }
    446     bool operator>( const char *that) const       { return compare(that) > 0; }
     515    bool operator==(const char *pszThat) const    { return equals(pszThat); }
     516    bool operator!=(const char *pszThat) const    { return !equals(pszThat); }
     517    bool operator<( const char *pszThat) const    { return compare(pszThat) < 0; }
     518    bool operator>( const char *pszThat) const    { return compare(pszThat) > 0; }
    447519    /** @} */
    448520
     
    567639            RTStrFree(m_psz);
    568640            m_psz = NULL;
    569             m_cbLength = 0;
     641            m_cch = 0;
    570642            m_cbAllocated = 0;
    571643        }
     
    591663    void copyFrom(const MiniString &s)
    592664    {
    593         if ((m_cbLength = s.m_cbLength))
    594         {
    595             m_cbAllocated = m_cbLength + 1;
     665        if ((m_cch = s.m_cch))
     666        {
     667            m_cbAllocated = m_cch + 1;
    596668            m_psz = (char *)RTStrAlloc(m_cbAllocated);
    597669            if (RT_LIKELY(m_psz))
     
    599671            else
    600672            {
    601                 m_cbLength = 0;
     673                m_cch = 0;
    602674                m_cbAllocated = 0;
    603675#ifdef RT_EXCEPTIONS_ENABLED
     
    631703        if (pcsz && *pcsz)
    632704        {
    633             m_cbLength = strlen(pcsz);
    634             m_cbAllocated = m_cbLength + 1;
     705            m_cch = strlen(pcsz);
     706            m_cbAllocated = m_cch + 1;
    635707            m_psz = (char *)RTStrAlloc(m_cbAllocated);
    636708            if (RT_LIKELY(m_psz))
     
    638710            else
    639711            {
    640                 m_cbLength = 0;
     712                m_cch = 0;
    641713                m_cbAllocated = 0;
    642714#ifdef RT_EXCEPTIONS_ENABLED
     
    647719        else
    648720        {
    649             m_cbLength = 0;
     721            m_cch = 0;
    650722            m_cbAllocated = 0;
    651723            m_psz = NULL;
     
    654726
    655727    char    *m_psz;                     /**< The string buffer. */
    656     size_t  m_cbLength;                 /**< strlen(m_psz) - i.e. no terminator included. */
     728    size_t  m_cch;                      /**< strlen(m_psz) - i.e. no terminator included. */
    657729    size_t  m_cbAllocated;              /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
    658730};
Note: See TracChangeset for help on using the changeset viewer.

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