Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions
nsUnicodeToUTF32.cpp File Reference
#include <string.h>
#include "nsUCSupport.h"
#include "nsUnicodeToUTF32.h"

Go to the source code of this file.

Defines

#define UCS4_TO_LE_STRING(u, s)
#define UCS4_TO_BE_STRING(u, s)

Functions

static nsresult ConvertCommon (const PRUnichar *aSrc, PRInt32 *aSrcLength, char *aDest, PRInt32 *aDestLength, PRUnichar *aHighSurrogate, PRBool aIsLE)
static nsresult FinishCommon (char *aDest, PRInt32 *aDestLength, PRUnichar *aHighSurrogate, PRBool aIsLE)

Define Documentation

#define UCS4_TO_BE_STRING (   u,
  s 
)
Value:
PR_BEGIN_MACRO                          \
    s[0] = PRUint8(((u) >> 24) & 0xffL);  \
    s[1] = PRUint8(((u) >> 16) & 0xffL);  \
    s[2] = PRUint8(((u) >> 8) & 0xffL);   \
    s[3] = PRUint8((u) & 0xffL);          \
  PR_END_MACRO

Definition at line 66 of file nsUnicodeToUTF32.cpp.

#define UCS4_TO_LE_STRING (   u,
  s 
)
Value:
PR_BEGIN_MACRO                          \
    *((PRUint32*)(s)) = (u);              \
  PR_END_MACRO

Definition at line 54 of file nsUnicodeToUTF32.cpp.


Function Documentation

static nsresult ConvertCommon ( const PRUnichar aSrc,
PRInt32 aSrcLength,
char *  aDest,
PRInt32 aDestLength,
PRUnichar aHighSurrogate,
PRBool  aIsLE 
) [static]

Definition at line 78 of file nsUnicodeToUTF32.cpp.

{
  const PRUnichar * src = aSrc;
  const PRUnichar * srcEnd = aSrc + *aSrcLength;
  char * dest = aDest;
  const char * destEnd = aDest + *aDestLength; 
  PRUint32 ucs4;


  // left-over high surroage code point from the prev. run.
  if (*aHighSurrogate) 
  {
    if (! *aSrcLength)
    {
      *aDestLength = 0;
      return NS_OK_UENC_MOREINPUT;
    }
    if (*aDestLength < 4) 
    {
      *aSrcLength = 0;
      *aDestLength = 0;
      return NS_OK_UENC_MOREOUTPUT;
    }
    if ((*src & 0xfc00) != 0xdc00) // Not a low surrogate codepoint. Unpaird.
      ucs4 = PRUint32(*aHighSurrogate);
    else 
      ucs4 = (((*aHighSurrogate & 0x3ffL) << 10) | (*src & 0x3ffL)) + 0x10000;

    ++src;
    if (aIsLE)
      UCS4_TO_LE_STRING(ucs4, dest);
    else
      UCS4_TO_BE_STRING(ucs4, dest);
    dest += 4;
    *aHighSurrogate = 0;
  }

  while (src < srcEnd) {
    // regular codepoint or an unpaired low surrogate
    if ((src[0] & 0xfc00) != 0xd800) 
    {
      if (destEnd - dest < 4)
        goto error_more_output;
      ucs4 = PRUint32(src[0]);  
    }
    else  // high surrogate
    {
      if ((src+1) >= srcEnd) {
        //we need another surrogate to complete this unicode char
        *aHighSurrogate = src[0];
        *aDestLength = dest - aDest;
        return NS_OK_UENC_MOREINPUT;
      }
      //handle surrogate
      if (destEnd - dest < 4)
        goto error_more_output;
      if ((src[1] & 0xfc00) != 0xdc00)  // unpaired 
        ucs4 = PRUint32(src[0]);  
      else 
      {  // convert surrogate pair to UCS4
        ucs4 = (((src[0] & 0x3ffL) << 10) | (src[1] & 0x3ffL)) + 0x10000;
        *aHighSurrogate = 0;
        ++src;
      }
    }
    if (aIsLE)
      UCS4_TO_LE_STRING(ucs4, dest);
    else
      UCS4_TO_BE_STRING(ucs4, dest);
    dest += 4;
    ++src;
  }

  *aDestLength = dest - aDest;
  return NS_OK;

error_more_output:
  *aSrcLength = src - aSrc;
  *aDestLength = dest - aDest;
  return NS_OK_UENC_MOREOUTPUT;

}

Here is the caller graph for this function:

static nsresult FinishCommon ( char *  aDest,
PRInt32 aDestLength,
PRUnichar aHighSurrogate,
PRBool  aIsLE 
) [static]

Definition at line 166 of file nsUnicodeToUTF32.cpp.

{
  char * dest = aDest;

  if (*aHighSurrogate) {
    if (*aDestLength < 4) {
      *aDestLength = 0;
      return NS_OK_UENC_MOREOUTPUT;
    }
    PRUint32 high = PRUint32(*aHighSurrogate);
    if (aIsLE)
      UCS4_TO_LE_STRING(high, dest);
    else
      UCS4_TO_BE_STRING(high, dest);
    *aHighSurrogate = 0;
    *aDestLength = 4;
    return NS_OK;
  } 

  *aDestLength  = 0;
  return NS_OK;
}

Here is the caller graph for this function: