Logo Search packages:      
Sourcecode: osb-jscore version File versions  Download package

UString & KJS::UString::append ( const UString t  ) 

Append another string.

Definition at line 596 of file ustring.cpp.

References KJS::UString::Rep::_hash, KJS::UString::Rep::baseString, KJS::UString::Rep::capacity, data(), KJS::UString::Rep::len, KJS::UString::Rep::offset, KJS::UString::Rep::rc, and size().

{
  int thisSize = size();
  int thisOffset = rep->offset;
  int tSize = t.size();
  int length = thisSize + tSize;

  // possible cases:
  if (thisSize == 0) {
    // this is empty
    *this = t;
  } else if (tSize == 0) {
    // t is empty
  } else if (!rep->baseString && rep->rc == 1) {
    // this is direct and has refcount of 1 (so we can just alter it directly)
    expandCapacity(thisOffset + length);
    memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar));
    rep->len = length;
    rep->_hash = 0;
  } else if (thisOffset + thisSize == usedCapacity()) {
    // this reaches the end of the buffer - extend it
    expandCapacity(length);
    memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar));
    Rep *newRep = Rep::create(rep, 0, length);
    release();
    rep = newRep;
  } else {
    // this is shared with someone using more capacity, gotta make a whole new string
    int newCapacity = expandedSize(length, 0);
    UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * newCapacity));
    memcpy(d, data(), thisSize * sizeof(UChar));
    memcpy(const_cast<UChar *>(d + thisSize), t.data(), tSize * sizeof(UChar));
    release();
    rep = Rep::create(d, length);
    rep->capacity = newCapacity;
  }

  return *this;
}


Generated by  Doxygen 1.6.0   Back to index