Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Member Functions | Protected Member Functions | Protected Attributes
nsTextFragment Class Reference

A fragment of text. More...

#include <nsTextFragment.h>

List of all members.

Classes

struct  FragmentBits

Public Member Functions

 nsTextFragment ()
 Default constructor.
 ~nsTextFragment ()
 nsTextFragment (const nsTextFragment &aOther)
 Initialize the contents of this fragment to be a copy of the argument fragment.
 nsTextFragment (const char *aString)
 Initialize the contents of this fragment to be a copy of the argument 7bit ascii string.
 nsTextFragment (const PRUnichar *aString)
 Initialize the contents of this fragment to be a copy of the argument ucs2 string.
 nsTextFragment (const nsString &aString)
 Initialize the contents of this fragment to be a copy of the argument string.
nsTextFragmentoperator= (const nsTextFragment &aOther)
 Change the contents of this fragment to be a copy of the the argument fragment.
nsTextFragmentoperator= (const char *aString)
 Change the contents of this fragment to be a copy of the the argument 7bit ascii string.
nsTextFragmentoperator= (const PRUnichar *aString)
 Change the contents of this fragment to be a copy of the the argument ucs2 string.
nsTextFragmentoperator= (const nsAString &aString)
 Change the contents of this fragment to be a copy of the the argument string.
PRBool Is2b () const
 Return PR_TRUE if this fragment is represented by PRUnichar data.
PRBool IsBidi () const
 Return PR_TRUE if this fragment contains Bidi text For performance reasons this flag is not set automatically, but requires an explicit call to SetBidiFlag()
const PRUnicharGet2b () const
 Get a pointer to constant PRUnichar data.
const char * Get1b () const
 Get a pointer to constant char data.
PRInt32 GetLength () const
 Get the length of the fragment.
void SetTo (PRUnichar *aBuffer, PRInt32 aLength, PRBool aRelease)
 Change the contents of this fragment to be the given buffer and length.
void SetTo (const PRUnichar *aBuffer, PRInt32 aLength)
 Change the contents of this fragment to be a copy of the given buffer.
void SetTo (const char *aBuffer, PRInt32 aLength)
 Change the contents of this fragment to be a copy of the given buffer.
void AppendTo (nsAString &aString) const
 Append the contents of this string fragment to aString.
void AppendTo (nsACString &aCString) const
 Append the contents of this string fragment to aCString.
void CopyTo (PRUnichar *aDest, PRInt32 aOffset, PRInt32 aCount)
 Make a copy of the fragments contents starting at offset for count characters.
void CopyTo (char *aDest, PRInt32 aOffset, PRInt32 aCount)
 Make a copy of the fragments contents starting at offset for count characters.
PRUnichar CharAt (PRInt32 aIndex) const
 Return the character in the text-fragment at the given index.
void SetBidiFlag ()
 Scan the contents of the fragment and turn on mState.mIsBidi if it includes any Bidi characters.

Protected Member Functions

void ReleaseText ()

Protected Attributes

union {
const PRUnicharm2b
const unsigned char * m1b
}; 
union {
PRUint32 mAllBits
FragmentBits mState
}; 

Detailed Description

A fragment of text.

If mIs2b is 1 then the m2b pointer is valid otherwise the m1b pointer is valid. If m1b is used then each byte of data represents a single ucs2 character with the high byte being zero.

This class does not have a virtual destructor therefore it is not meant to be subclassed.

Definition at line 73 of file nsTextFragment.h.


Class Documentation

struct nsTextFragment::FragmentBits

Definition at line 243 of file nsTextFragment.h.

Class Members
PRBool mInHeap: 1
PRBool mIs2b: 1
PRBool mIsBidi: 1
PRUint32 mLength: 29

Constructor & Destructor Documentation

Default constructor.

Initialize the fragment to be empty.

Definition at line 78 of file nsTextFragment.h.

    : m1b(nsnull), mAllBits(0)
  {
  }

Definition at line 49 of file nsTextFragment.cpp.

{
  ReleaseText();
}

Here is the call graph for this function:

Initialize the contents of this fragment to be a copy of the argument fragment.

Definition at line 69 of file nsTextFragment.cpp.

  : m1b(nsnull), mAllBits(0)
{
  if (aOther.Is2b()) {
    SetTo(aOther.Get2b(), aOther.GetLength());
  } else {
    SetTo(aOther.Get1b(), aOther.GetLength());
  }
}

Here is the call graph for this function:

Initialize the contents of this fragment to be a copy of the argument 7bit ascii string.

Definition at line 79 of file nsTextFragment.cpp.

  : m1b(nsnull), mAllBits(0)
{
  SetTo(aString, strlen(aString));
}

Here is the call graph for this function:

Initialize the contents of this fragment to be a copy of the argument ucs2 string.

Definition at line 85 of file nsTextFragment.cpp.

  : m1b(nsnull), mAllBits(0)
{
  SetTo(aString, nsCRT::strlen(aString));
}

Here is the call graph for this function:

Initialize the contents of this fragment to be a copy of the argument string.

Definition at line 91 of file nsTextFragment.cpp.

  : m1b(nsnull), mAllBits(0)
{
  SetTo(aString.get(), aString.Length());
}

Here is the call graph for this function:


Member Function Documentation

void nsTextFragment::AppendTo ( nsAString &  aString) const

Append the contents of this string fragment to aString.

Definition at line 270 of file nsTextFragment.cpp.

{
  if (mState.mIs2b) {
    aString.Append(m2b, mState.mLength);
  } else {
    AppendASCIItoUTF16(Substring((char *)m1b, ((char *)m1b) + mState.mLength),
                       aString);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsTextFragment::AppendTo ( nsACString &  aCString) const

Append the contents of this string fragment to aCString.

This method will do a lossy conversion from UTF-16 to ASCII.

Definition at line 281 of file nsTextFragment.cpp.

{
  if (mState.mIs2b) {
    LossyAppendUTF16toASCII(Substring((PRUnichar *)m2b,
                                      (PRUnichar *)m2b + mState.mLength),
                            aCString);
  } else {
    aCString.Append((char *)m1b, mState.mLength);
  }
}

Here is the call graph for this function:

PRUnichar nsTextFragment::CharAt ( PRInt32  aIndex) const [inline]

Return the character in the text-fragment at the given index.

This always returns a PRUnichar.

Definition at line 231 of file nsTextFragment.h.

  {
    NS_ASSERTION(PRUint32(aIndex) < mState.mLength, "bad index");
    return mState.mIs2b ? m2b[aIndex] : PRUnichar(m1b[aIndex]);
  }

Here is the caller graph for this function:

void nsTextFragment::CopyTo ( PRUnichar aDest,
PRInt32  aOffset,
PRInt32  aCount 
)

Make a copy of the fragments contents starting at offset for count characters.

The offset and count will be adjusted to lie within the fragments data. The fragments data is converted if necessary.

Definition at line 293 of file nsTextFragment.cpp.

{
  NS_ASSERTION(aOffset >= 0, "Bad offset passed to nsTextFragment::CopyTo()!");
  NS_ASSERTION(aCount >= 0, "Bad count passed to nsTextFragment::CopyTo()!");

  if (aOffset < 0) {
    aOffset = 0;
  }

  if (aOffset + aCount > GetLength()) {
    aCount = mState.mLength - aOffset;
  }

  if (aCount != 0) {
    if (mState.mIs2b) {
      memcpy(aDest, m2b + aOffset, sizeof(PRUnichar) * aCount);
    } else {
      unsigned const char *cp = m1b + aOffset;
      unsigned const char *end = cp + aCount;
      while (cp < end) {
        *aDest++ = PRUnichar(*cp++);
      }
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsTextFragment::CopyTo ( char *  aDest,
PRInt32  aOffset,
PRInt32  aCount 
)

Make a copy of the fragments contents starting at offset for count characters.

The offset and count will be adjusted to lie within the fragments data. The fragments data is converted if necessary.

Definition at line 320 of file nsTextFragment.cpp.

{
  NS_ASSERTION(aOffset >= 0, "Bad offset passed to nsTextFragment::CopyTo()!");
  NS_ASSERTION(aCount >= 0, "Bad count passed to nsTextFragment::CopyTo()!");

  if (aOffset < 0) {
    aOffset = 0;
  }

  if (aOffset + aCount > GetLength()) {
    aCount = mState.mLength - aOffset;
  }

  if (aCount != 0) {
    if (mState.mIs2b) {
      const PRUnichar *cp = m2b + aOffset;
      const PRUnichar *end = cp + aCount;
      while (cp < end) {
        *aDest++ = (char)(*cp++);
      }
    } else {
      memcpy(aDest, m1b + aOffset, sizeof(char) * aCount);
    }
  }
}

Here is the call graph for this function:

const char* nsTextFragment::Get1b ( ) const [inline]

Get a pointer to constant char data.

Definition at line 163 of file nsTextFragment.h.

  {
    NS_ASSERTION(!Is2b(), "not 1b text"); 
    return (const char *)m1b;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

const PRUnichar* nsTextFragment::Get2b ( ) const [inline]

Get a pointer to constant PRUnichar data.

Definition at line 154 of file nsTextFragment.h.

  {
    NS_ASSERTION(Is2b(), "not 2b text"); 
    return m2b;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

PRInt32 nsTextFragment::GetLength ( ) const [inline]

Get the length of the fragment.

The length is the number of logical characters, not the number of bytes to store the characters.

Definition at line 173 of file nsTextFragment.h.

  {
    return PRInt32(mState.mLength);
  }

Here is the caller graph for this function:

PRBool nsTextFragment::Is2b ( ) const [inline]

Return PR_TRUE if this fragment is represented by PRUnichar data.

Definition at line 136 of file nsTextFragment.h.

  {
    return mState.mIs2b;
  }

Here is the caller graph for this function:

PRBool nsTextFragment::IsBidi ( ) const [inline]

Return PR_TRUE if this fragment contains Bidi text For performance reasons this flag is not set automatically, but requires an explicit call to SetBidiFlag()

Definition at line 146 of file nsTextFragment.h.

  {
    return mState.mIsBidi;
  }

Here is the caller graph for this function:

nsTextFragment & nsTextFragment::operator= ( const nsTextFragment aOther)

Change the contents of this fragment to be a copy of the the argument fragment.

Definition at line 98 of file nsTextFragment.cpp.

{
  if (aOther.Is2b()) {
    SetTo(aOther.Get2b(), aOther.GetLength());
  } else {
    SetTo(aOther.Get1b(), aOther.GetLength());
  }

  if (aOther.mState.mIsBidi) {
    // Carry over BIDI state from aOther
    mState.mIsBidi = PR_TRUE;
  }

  return *this;
}

Here is the call graph for this function:

nsTextFragment & nsTextFragment::operator= ( const char *  aString)

Change the contents of this fragment to be a copy of the the argument 7bit ascii string.

Definition at line 115 of file nsTextFragment.cpp.

{
  SetTo(aString, strlen(aString));

  return *this;
}

Here is the call graph for this function:

nsTextFragment & nsTextFragment::operator= ( const PRUnichar aString)

Change the contents of this fragment to be a copy of the the argument ucs2 string.

Definition at line 123 of file nsTextFragment.cpp.

{
  SetTo(aString, nsCRT::strlen(aString));

  return *this;
}

Here is the call graph for this function:

nsTextFragment & nsTextFragment::operator= ( const nsAString &  aString)

Change the contents of this fragment to be a copy of the the argument string.

Definition at line 131 of file nsTextFragment.cpp.

{
  ReleaseText();

  PRUint32 length = aString.Length();

  if (length > 0) {
    PRBool in_heap = PR_TRUE;

    if (IsASCII(aString)) {
      if (length == 1 && aString.First() == '\n') {
        m1b = &sNewLineCharacter;

        in_heap = PR_FALSE;
      } else {
        m1b = (unsigned char *)ToNewCString(aString);
      }

      mState.mIs2b = PR_FALSE;
    } else {
      m2b = ToNewUnicode(aString);
      mState.mIs2b = PR_TRUE;
    }

    mState.mInHeap = in_heap;
    mState.mLength = length;
  }

  return *this;
}

Here is the call graph for this function:

Definition at line 55 of file nsTextFragment.cpp.

{
  if (mState.mLength && m1b && mState.mInHeap) {
    unsigned char *buf = NS_CONST_CAST(unsigned char *, m1b);

    nsMemory::Free(buf); // m1b == m2b as far as nsMemory is concerned
  }

  m1b = nsnull;

  // Set mState.mIs2b, mState.mInHeap, and mState.mLength = 0 with mAllBits;
  mAllBits = 0;
}

Here is the caller graph for this function:

Scan the contents of the fragment and turn on mState.mIsBidi if it includes any Bidi characters.

Definition at line 349 of file nsTextFragment.cpp.

{
  if (mState.mIs2b && !mState.mIsBidi) {
    const PRUnichar* cp = m2b;
    const PRUnichar* end = cp + mState.mLength;
    while (cp < end) {
      PRUnichar ch1 = *cp++;
      PRUint32 utf32Char = ch1;
      if (IS_HIGH_SURROGATE(ch1) &&
          cp < end &&
          IS_LOW_SURROGATE(*cp)) {
        PRUnichar ch2 = *cp++;
        utf32Char = SURROGATE_TO_UCS4(ch1, ch2);
      }
      if (UTF32_CHAR_IS_BIDI(utf32Char) || IS_BIDI_CONTROL_CHAR(utf32Char)) {
        mState.mIsBidi = PR_TRUE;
        break;
      }
    }
  }
}

Here is the caller graph for this function:

void nsTextFragment::SetTo ( PRUnichar aBuffer,
PRInt32  aLength,
PRBool  aRelease 
)

Change the contents of this fragment to be the given buffer and length.

The memory becomes owned by the fragment. In addition, the memory for aBuffer must have been allocated using the nsIMemory interface.

Definition at line 163 of file nsTextFragment.cpp.

{
  ReleaseText();

  m2b = aBuffer;
  mState.mIs2b = PR_TRUE;
  mState.mInHeap = aRelease;
  mState.mLength = aLength;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsTextFragment::SetTo ( const PRUnichar aBuffer,
PRInt32  aLength 
)

Change the contents of this fragment to be a copy of the given buffer.

Like operator= except a length is specified instead of assuming 0 termination.

Definition at line 174 of file nsTextFragment.cpp.

{
  ReleaseText();

  if (aLength != 0) {
    // See if we need to store the data in ucs2 or not
    PRBool need2 = PR_FALSE;
    const PRUnichar *ucp = aBuffer;
    const PRUnichar *uend = aBuffer + aLength;
    while (ucp < uend) {
      PRUnichar ch = *ucp++;
      if (ch >> 8) {
        need2 = PR_TRUE;
        break;
      }
    }

    if (need2) {
      // Use ucs2 storage because we have to
      m2b = (const PRUnichar *)nsMemory::Clone(aBuffer,
                                               aLength * sizeof(PRUnichar));

      if (!m2b) {
        NS_ERROR("Failed to clone string buffer!");

        return;
      }

      // Setup our fields
      mState.mIs2b = PR_TRUE;
      mState.mInHeap = PR_TRUE;
      mState.mLength = aLength;
    } else {
      // Use 1 byte storage because we can

      PRBool in_heap = PR_TRUE;

      if (aLength == 1 && *aBuffer == '\n') {
        m1b = &sNewLineCharacter;

        in_heap = PR_FALSE;
      } else {
        unsigned char *nt =
          (unsigned char *)nsMemory::Alloc(aLength * sizeof(char));

        if (!nt) {
          NS_ERROR("Failed to allocate string buffer!");

          return;
        }

        // Copy data
        for (PRUint32 i = 0; i < (PRUint32)aLength; ++i) {
          nt[i] = (unsigned char)aBuffer[i];
        }

        m1b = nt;
      }

      // Setup our fields
      mState.mIs2b = PR_FALSE;
      mState.mInHeap = in_heap;
      mState.mLength = aLength;
    }
  }
}

Here is the call graph for this function:

void nsTextFragment::SetTo ( const char *  aBuffer,
PRInt32  aLength 
)

Change the contents of this fragment to be a copy of the given buffer.

Like operator= except a length is specified instead of assuming 0 termination.

Definition at line 242 of file nsTextFragment.cpp.

{
  ReleaseText();
  if (aLength != 0) {
    PRBool in_heap = PR_TRUE;

    if (aLength == 1 && *aBuffer == '\n') {
      m1b = &sNewLineCharacter;

      in_heap = PR_FALSE;
    } else {
      m1b = (unsigned char *)nsMemory::Clone(aBuffer, aLength * sizeof(char));

      if (!m1b) {
        NS_ERROR("Failed to allocate string buffer!");

        return;
      }
    }

    // Setup our fields
    mState.mIs2b = PR_FALSE;
    mState.mInHeap = in_heap;
    mState.mLength = aLength;
  }
}

Here is the call graph for this function:


Member Data Documentation

union { ... } [protected]
union { ... } [protected]

The documentation for this class was generated from the following files: