Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Private Attributes
ConvertUTF16toUTF8 Class Reference

A character sink (see |copy_string| in nsAlgorithm.h) for converting UTF-16 to UTF-8. More...

#include <nsUTF8Utils.h>

List of all members.

Public Types

typedef nsAString::char_type value_type
typedef nsACString::char_type buffer_type

Public Member Functions

 ConvertUTF16toUTF8 (buffer_type *aBuffer)
size_t Size () const
PRUint32 NS_ALWAYS_INLINE write (const value_type *start, PRUint32 N)
void write_terminator ()

Private Attributes

buffer_type *const mStart
buffer_typemBuffer

Detailed Description

A character sink (see |copy_string| in nsAlgorithm.h) for converting UTF-16 to UTF-8.

Definition at line 285 of file nsUTF8Utils.h.


Member Typedef Documentation

typedef nsACString::char_type ConvertUTF16toUTF8::buffer_type

Definition at line 289 of file nsUTF8Utils.h.

typedef nsAString::char_type ConvertUTF16toUTF8::value_type

Definition at line 288 of file nsUTF8Utils.h.


Constructor & Destructor Documentation

Definition at line 295 of file nsUTF8Utils.h.

        : mStart(aBuffer), mBuffer(aBuffer) {}

Member Function Documentation

size_t ConvertUTF16toUTF8::Size ( void  ) const [inline]

Definition at line 298 of file nsUTF8Utils.h.

{ return mBuffer - mStart; }

Here is the caller graph for this function:

Definition at line 300 of file nsUTF8Utils.h.

      {
        buffer_type *out = mBuffer; // gcc isn't smart enough to do this!

        for (const value_type *p = start, *end = start + N; p < end; ++p )
          {
            value_type c = *p;
            if (! (c & 0xFF80)) // U+0000 - U+007F
              {
                *out++ = (char)c;
              }
            else if (! (c & 0xF800)) // U+0100 - U+07FF
              {
                *out++ = 0xC0 | (char)(c >> 6);
                *out++ = 0x80 | (char)(0x003F & c);
              }
            else if (!IS_SURROGATE(c)) // U+0800 - U+D7FF,U+E000 - U+FFFF
              {
                *out++ = 0xE0 | (char)(c >> 12);
                *out++ = 0x80 | (char)(0x003F & (c >> 6));
                *out++ = 0x80 | (char)(0x003F & c );
              }
            else if (IS_HIGH_SURROGATE(c)) // U+D800 - U+DBFF
              {
                // D800- DBFF - High Surrogate
                value_type h = c;

                ++p;
                if (p == end)
                  {
                    NS_ERROR("Surrogate pair split between fragments");
                    mBuffer = out;
                    return N;
                  }
                c = *p;

                if (IS_LOW_SURROGATE(c))
                  {
                    // DC00- DFFF - Low Surrogate
                    // N = (H - D800) *400 + 10000 + ( L - DC00 )
                    PRUint32 ucs4 = SURROGATE_TO_UCS4(h, c);

                    // 0001 0000-001F FFFF
                    *out++ = 0xF0 | (char)(ucs4 >> 18);
                    *out++ = 0x80 | (char)(0x003F & (ucs4 >> 12));
                    *out++ = 0x80 | (char)(0x003F & (ucs4 >> 6));
                    *out++ = 0x80 | (char)(0x003F & ucs4);
                  }
                else
                  {
                    NS_ERROR("got a High Surrogate but no low surrogate");
                    // output nothing.
                  }
              }
            else // U+DC00 - U+DFFF
              {
                // DC00- DFFF - Low Surrogate
                NS_ERROR("got a low Surrogate but no high surrogate");
                // output nothing.
              }
          }

        mBuffer = out;
        return N;
      }

Here is the caller graph for this function:

Definition at line 366 of file nsUTF8Utils.h.

      {
        *mBuffer = buffer_type(0);
      }

Member Data Documentation

Definition at line 373 of file nsUTF8Utils.h.

Definition at line 372 of file nsUTF8Utils.h.


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