Ignore:
Timestamp:
Jul 30, 2008, 1:16:06 PM (17 years ago)
Author:
[email protected]
Message:

Reviewed by Geoff Garen.

Add consistency checks to UString to document and enforce its design.

  • kjs/ustring.cpp: (KJS::UString::Rep::create): (KJS::UString::Rep::destroy): (KJS::UString::Rep::checkConsistency): (KJS::UString::expandCapacity): (KJS::UString::expandPreCapacity): (KJS::UString::UString): (KJS::UString::spliceSubstringsWithSeparators): (KJS::UString::append):
  • kjs/ustring.h: (KJS::UString::Rep::checkConsistency):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/ustring.cpp

    r35418 r35458  
    200200    r->preCapacity = 0;
    201201
     202    r->checkConsistency();
     203
    202204    // steal the single reference this Rep was created with
    203205    return adoptRef(r);
     
    207209{
    208210    ASSERT(base);
     211    base->checkConsistency();
    209212
    210213    int baseOffset = base->offset;
     
    229232    r->preCapacity = 0;
    230233
     234    r->checkConsistency();
     235
    231236    // steal the single reference this Rep was created with
    232237    return adoptRef(r);
     
    249254void UString::Rep::destroy()
    250255{
     256    checkConsistency();
     257
    251258    // Static null and empty strings can never be destroyed, but we cannot rely on
    252259    // reference counting, because ref/deref are not thread-safe.
     
    356363}
    357364
     365#ifndef NDEBUG
     366void UString::Rep::checkConsistency() const
     367{
     368    // Only base strings have non-zero shared data.
     369    if (this != baseString) {
     370        ASSERT(!buf);
     371        ASSERT(!usedCapacity);
     372        ASSERT(!capacity);
     373        ASSERT(!usedPreCapacity);
     374        ASSERT(!preCapacity);
     375    }
     376
     377    // There is no recursion for base strings.
     378    ASSERT(baseString == baseString->baseString);
     379
     380    if (isStatic()) {
     381        // There are only two static strings: null and empty.
     382        ASSERT(!len);
     383
     384        // Static strings cannot get in identifier tables, because they are globally shared.
     385        ASSERT(!identifierTable());
     386    }
     387
     388    // The string fits in buffer.
     389    ASSERT(baseString->usedPreCapacity <= baseString->preCapacity);
     390    ASSERT(baseString->usedCapacity <= baseString->capacity);
     391    ASSERT(-offset <= baseString->usedPreCapacity);
     392    ASSERT(offset + len <= baseString->usedCapacity);
     393}
     394#endif
     395
    358396// put these early so they can be inlined
    359397inline size_t UString::expandedSize(size_t size, size_t otherSize) const
     
    384422void UString::expandCapacity(int requiredLength)
    385423{
     424    m_rep->checkConsistency();
     425
    386426    Rep* r = m_rep->baseString;
    387427
     
    399439    if (requiredLength > r->usedCapacity)
    400440        r->usedCapacity = requiredLength;
     441
     442    m_rep->checkConsistency();
    401443}
    402444
    403445void UString::expandPreCapacity(int requiredPreCap)
    404446{
     447    m_rep->checkConsistency();
     448
    405449    Rep* r = m_rep->baseString;
    406450
     
    422466    if (requiredPreCap > r->usedPreCapacity)
    423467        r->usedPreCapacity = requiredPreCap;
     468
     469    m_rep->checkConsistency();
    424470}
    425471
     
    476522UString::UString(const UString& a, const UString& b)
    477523{
     524    a.rep()->checkConsistency();
     525    b.rep()->checkConsistency();
     526
    478527    int aSize = a.size();
    479528    int aOffset = a.m_rep->offset;
     
    527576        }
    528577    }
     578    a.rep()->checkConsistency();
     579    b.rep()->checkConsistency();
     580    m_rep->checkConsistency();
    529581}
    530582
     
    679731UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
    680732{
     733    m_rep->checkConsistency();
     734
    681735    if (rangeCount == 1 && separatorCount == 0) {
    682736        int thisSize = size();
     
    719773UString& UString::append(const UString &t)
    720774{
     775    m_rep->checkConsistency();
     776    t.rep()->checkConsistency();
     777
    721778    int thisSize = size();
    722779    int thisOffset = m_rep->offset;
     
    759816    }
    760817
     818    m_rep->checkConsistency();
     819    t.rep()->checkConsistency();
     820
    761821    return *this;
    762822}
     
    764824UString& UString::append(const UChar* tData, int tSize)
    765825{
     826    m_rep->checkConsistency();
     827
    766828    int thisSize = size();
    767829    int thisOffset = m_rep->offset;
     
    803865    }
    804866
     867    m_rep->checkConsistency();
     868
    805869    return *this;
    806870}
     
    808872UString& UString::append(const char* t)
    809873{
     874    m_rep->checkConsistency();
     875
    810876    int thisSize = size();
    811877    int thisOffset = m_rep->offset;
     
    853919    }
    854920
     921    m_rep->checkConsistency();
     922
    855923    return *this;
    856924}
     
    858926UString& UString::append(UChar c)
    859927{
     928    m_rep->checkConsistency();
     929
    860930    int thisOffset = m_rep->offset;
    861931    int length = size();
     
    903973        }
    904974    }
     975
     976    m_rep->checkConsistency();
    905977
    906978    return *this;
Note: See TracChangeset for help on using the changeset viewer.