Ignore:
Timestamp:
Jan 9, 2009, 9:53:36 AM (16 years ago)
Author:
[email protected]
Message:

2009-01-09 David Levin <[email protected]>

Reviewed by Oliver Hunt.

https://bugs.webkit.org/show_bug.cgi?id=23175

Added a template to make the pointer and flags combination
in UString more readable and less error prone.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/UString.cpp

    r38390 r39747  
    33 *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
    44 *  Copyright (C) 2007 Cameron Zwarich ([email protected])
     5 *  Copyright (c) 2009, Google Inc. All rights reserved.
    56 *
    67 *  This library is free software; you can redistribute it and/or
     
    187188// reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
    188189static UChar sharedEmptyChar;
    189 UString::Rep UString::Rep::null = { 0, 0, INT_MAX / 2, 0, 1, &UString::Rep::null, 0, 0, 0, 0, 0, 0 };
    190 UString::Rep UString::Rep::empty = { 0, 0, INT_MAX / 2, 0, 1, &UString::Rep::empty, 0, &sharedEmptyChar, 0, 0, 0, 0 };
     190UString::Rep* UString::Rep::nullBaseString;
     191UString::Rep* UString::Rep::emptyBaseString;
     192UString* UString::nullUString;
     193
     194void initializeStaticBaseString(int len, UChar* buf, UString::Rep& base)
     195{
     196    base.offset = 0;
     197    base.len = len;
     198    base.rc = INT_MAX / 2;
     199    base._hash = 0;
     200    base.m_identifierTableAndFlags.setFlag(UString::Rep::StaticFlag);
     201    base.baseString = &base;
     202    base.buf = buf;
     203    base.preCapacity = 0;
     204    base.usedPreCapacity = 0;
     205    base.capacity = 0;
     206    base.usedCapacity = 0;
     207    base.reportedCost = 0;
     208}
     209
     210void initializeUString()
     211{
     212    UString::Rep::nullBaseString = new UString::Rep;
     213    initializeStaticBaseString(0, 0, *UString::Rep::nullBaseString);
     214
     215    UString::Rep::emptyBaseString = new UString::Rep;
     216    initializeStaticBaseString(0, &sharedEmptyChar, *UString::Rep::emptyBaseString);
     217
     218    UString::nullUString = new UString;
     219}
    191220
    192221static char* statBuffer = 0; // Only used for debugging via UString::ascii().
     
    206235    r->rc = 1;
    207236    r->_hash = 0;
    208     r->m_identifierTable = 0;
    209237    r->baseString = r;
    210238    r->reportedCost = 0;
     
    238266    r->rc = 1;
    239267    r->_hash = 0;
    240     r->m_identifierTable = 0;
    241268    r->baseString = base.releaseRef();
    242269    r->reportedCost = 0;
     
    256283{
    257284    if (!string)
    258         return &UString::Rep::null;
     285        return &UString::Rep::null();
    259286
    260287    size_t length = strlen(string);
     
    262289    UChar* p = buffer.data();
    263290    if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
    264         return &UString::Rep::null;
     291        return &UString::Rep::null();
    265292
    266293    return UString::Rep::createCopying(buffer.data(), p - buffer.data());
     
    495522{
    496523    if (!c)
    497         return &UString::Rep::null;
     524        return &UString::Rep::null();
    498525
    499526    if (!c[0])
    500         return &UString::Rep::empty;
     527        return &UString::Rep::empty();
    501528
    502529    size_t length = strlen(c);
    503530    UChar* d = allocChars(length);
    504531    if (!d)
    505         return &UString::Rep::null;
     532        return &UString::Rep::null();
    506533    else {
    507534        for (size_t i = 0; i < length; i++)
     
    520547{
    521548    if (length == 0)
    522         m_rep = &Rep::empty;
     549        m_rep = &Rep::empty();
    523550    else
    524551        m_rep = Rep::createCopying(c, length);
     
    528555{
    529556    if (length == 0)
    530         m_rep = &Rep::empty;
     557        m_rep = &Rep::empty();
    531558    else if (copy)
    532559        m_rep = Rep::createCopying(c, length);
     
    538565{
    539566    if (!buffer.size())
    540         m_rep = &Rep::empty;
     567        m_rep = &Rep::empty();
    541568    else
    542569        m_rep = Rep::createCopying(buffer.data(), buffer.size());
     
    562589        // this is direct and has refcount of 1 (so we can just alter it directly)
    563590        if (!expandCapacity(rep.get(), thisOffset + length))
    564             rep = &UString::Rep::null;
     591            rep = &UString::Rep::null();
    565592        if (rep->data()) {
    566593            copyChars(rep->data() + thisSize, tData, tSize);
     
    571598        // this reaches the end of the buffer - extend it if it's long enough to append to
    572599        if (!expandCapacity(rep.get(), thisOffset + length))
    573             rep = &UString::Rep::null;
     600            rep = &UString::Rep::null();
    574601        if (rep->data()) {
    575602            copyChars(rep->data() + thisSize, tData, tSize);
     
    581608        UChar* d = allocChars(newCapacity);
    582609        if (!d)
    583             rep = &UString::Rep::null;
     610            rep = &UString::Rep::null();
    584611        else {
    585612            copyChars(d, rep->data(), thisSize);
     
    636663        UChar* d = allocChars(newCapacity);
    637664        if (!d)
    638             rep = &UString::Rep::null;
     665            rep = &UString::Rep::null();
    639666        else {
    640667            copyChars(d, rep->data(), thisSize);
     
    832859}
    833860
    834 const UString& UString::null()
    835 {
    836     static UString* n = new UString; // Should be called from main thread at least once to be safely initialized.
    837     return *n;
    838 }
    839 
    840861UString UString::from(int i)
    841862{
     
    11861207{
    11871208    if (!c) {
    1188         m_rep = &Rep::null;
     1209        m_rep = &Rep::null();
    11891210        return *this;
    11901211    }
    11911212
    11921213    if (!c[0]) {
    1193         m_rep = &Rep::empty;
     1214        m_rep = &Rep::empty();
    11941215        return *this;
    11951216    }
     
    16271648NEVER_INLINE void UString::makeNull()
    16281649{
    1629     m_rep = &Rep::null;
     1650    m_rep = &Rep::null();
    16301651}
    16311652
     
    16331654NEVER_INLINE UString::Rep* UString::nullRep()
    16341655{
    1635     return &Rep::null;
     1656    return &Rep::null();
    16361657}
    16371658
Note: See TracChangeset for help on using the changeset viewer.