Logo Search packages:      
Sourcecode: xulrunner version File versions

void nsTextFragment::Append ( const PRUnichar *  aBuffer,
PRUint32  aLength 
)

Append aData to the end of this fragment.

Definition at line 290 of file nsTextFragment.cpp.

References SetTo().

{
  // This is a common case because some callsites create a textnode
  // with a value by creating the node and then calling AppendData.
  if (mState.mLength == 0) {
    SetTo(aBuffer, aLength);

    return;
  }

  // Should we optimize for aData.Length() == 0?

  if (mState.mIs2b) {
    // Already a 2-byte string so the result will be too
    PRUnichar* buff = (PRUnichar*)nsMemory::Realloc(m2b, (mState.mLength + aLength) * sizeof(PRUnichar));
    if (!buff) {
      return;
    }
    
    memcpy(buff + mState.mLength, aBuffer, aLength * sizeof(PRUnichar));
    mState.mLength += aLength;
    m2b = buff;

    return;
  }

  // Current string is a 1-byte string, check if the new data fits in one byte too.

  const PRUnichar* ucp = aBuffer;
  const PRUnichar* uend = ucp + aLength;
  PRBool need2 = PR_FALSE;
  while (ucp < uend) {
    PRUnichar ch = *ucp++;
    if (ch >= 256) {
      need2 = PR_TRUE;
      break;
    }
  }

  if (need2) {
    // The old data was 1-byte, but the new is not so we have to expand it
    // all to 2-byte
    PRUnichar* buff = (PRUnichar*)nsMemory::Alloc((mState.mLength + aLength) *
                                                  sizeof(PRUnichar));
    if (!buff) {
      return;
    }

    // Copy data
    for (PRUint32 i = 0; i < mState.mLength; ++i) {
      buff[i] = (unsigned char)m1b[i];
    }
    
    memcpy(buff + mState.mLength, aBuffer, aLength * sizeof(PRUnichar));

    mState.mLength += aLength;
    mState.mIs2b = PR_TRUE;

    if (mState.mInHeap) {
      nsMemory::Free(m2b);
    }
    m2b = buff;

    mState.mInHeap = PR_TRUE;

    return;
  }

  // The new and the old data is all 1-byte
  char* buff;
  if (mState.mInHeap) {
    buff = (char*)nsMemory::Realloc(const_cast<char*>(m1b),
                                    (mState.mLength + aLength) * sizeof(char));
    if (!buff) {
      return;
    }
  }
  else {
    buff = (char*)nsMemory::Alloc((mState.mLength + aLength) * sizeof(char));
    if (!buff) {
      return;
    }

    memcpy(buff, m1b, mState.mLength);
    mState.mInHeap = PR_TRUE;
  }
    
  for (PRUint32 i = 0; i < aLength; ++i) {
    buff[mState.mLength + i] = (char)aBuffer[i];
  }

  m1b = buff;
  mState.mLength += aLength;

}


Generated by  Doxygen 1.6.0   Back to index