VirtualBox

Changeset 24656 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Nov 14, 2009 10:36:32 PM (15 years ago)
Author:
vboxsync
Message:

iprt/ministring: bird review - addressed object state after throwing std::bad_alloc.
Because of the cleanup() + copyFrom() approach to changing the string, there is
no way to preserve the original string value without rewriting the code
fundamentally. I would strongly recommend doing so. (The rewrite is to not
cleanup() first, but use RTMemRealloc() to extend the buffer.)

Left a few review @todos in the startsWith, endsWith & contains
implementations. They are related and all depends on a policy decision wrt
matching empty strings.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/ministring.cpp

    r23223 r24656  
    3636using namespace iprt;
    3737
    38 const size_t MiniString::npos = (size_t)-1;
    39 
    40 /**
    41  * Appends a copy of @a that to "this".
    42  * @param that
    43  */
    44 MiniString& MiniString::append(const MiniString &that)
     38const size_t MiniString::npos = ~(size_t)0;
     39
     40MiniString &MiniString::append(const MiniString &that)
    4541{
    4642    size_t lenThat = that.length();
     
    5147
    5248        reserve(cbBoth);
    53             // calls realloc(cbBoth) and sets m_cbAllocated
     49            // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
     50#ifndef RT_EXCEPTIONS_ENABLED
     51        AssertRelease(capacity() >= cbBoth);
     52#endif
    5453
    5554        memcpy(m_psz + lenThis, that.m_psz, lenThat);
     
    6059}
    6160
    62 /**
    63  * Appends the given character to "this".
    64  * @param c
    65  * @return
    66  */
    6761MiniString& MiniString::append(char c)
    6862{
     
    7165        // allocate in chunks of 20 in case this gets called several times
    7266        if (m_cbLength + 1 >= m_cbAllocated)
     67        {
    7368            reserve(m_cbLength + 10);
    74             // calls realloc() and sets m_cbAllocated
     69            // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
     70#ifndef RT_EXCEPTIONS_ENABLED
     71            AssertRelease(capacity() >= m_cbLength + 1);
     72#endif
     73        }
    7574
    7675        m_psz[m_cbLength] = c;
     
    8180}
    8281
    83 size_t MiniString::find(const char *pcszFind,
    84                         size_t pos /*= 0*/)
     82size_t MiniString::find(const char *pcszFind, size_t pos /*= 0*/)
    8583    const
    8684{
     
    128126
    129127                size_t cbCopy = psz - pFirst;
    130                 ret.reserve(cbCopy + 1);
     128                ret.reserve(cbCopy + 1); // may throw bad_alloc
     129#ifndef RT_EXCEPTIONS_ENABLED
     130                AssertRelease(capacity() >= cbCopy + 1);
     131#endif
    131132                memcpy(ret.m_psz, pFirst, cbCopy);
    132133                ret.m_cbLength = cbCopy;
     
    148149    if (l1 < l2)
    149150        return false;
     151    /** @todo r=bird: If l2 is 0, then m_psz can be NULL and we will crash. See
     152     *        also handling of l2 == in startsWith. */
    150153
    151154    size_t l = l1 - l2;
     
    160163    size_t l1 = length();
    161164    size_t l2 = that.length();
    162     if (l1 == 0 || l2 == 0)
     165    if (l1 == 0 || l2 == 0) /** @todo r=bird: this differs from endsWith, and I think other IPRT code. If l2 == 0, it matches anything. */
    163166        return false;
    164167
     
    174177bool MiniString::contains(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
    175178{
     179    /** @todo r-bird: Not checking for NULL strings like startsWith does (and
     180     *        endsWith only does half way). */
    176181    if (cs == CaseSensitive)
    177182        return ::RTStrStr(m_psz, that.m_psz) != NULL;
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