if ( ucs4 < minUcs4 ) {  *mBuffer++ = UCS2_REPLACEMENT_CHAR; } else if ( ucs4 <= 0xD7FF ) { *mBuffer++ = ucs4; } else if (  ucs4 <= 0xDFFF ) {  *mBuffer++ = UCS2_REPLACEMENT_CHAR; } else if ( ucs4 == 0xFFFE || ucs4 == 0xFFFF ) {  *mBuffer++ = UCS2_REPLACEMENT_CHAR; } else if ( ucs4 >= PLANE1_BASE ) { if ( ucs4 >= 0x00110000 ) *mBuffer++ = UCS2_REPLACEMENT_CHAR; else {  ucs4 -= PLANE1_BASE; *mBuffer++ = (PRUnichar)(ucs4 >> 10) | 0xd800u; *mBuffer++ = (PRUnichar)(ucs4 & 0x3ff) | 0xdc00u; } } else { if ( ucs4 != 0xFEFF ) *mBuffer++ = ucs4; } } return p - start;
return N;
    PRUint32 write( const value_type* start, PRUint32 N )      {        if ( mErrorEncountered )          return N;        // algorithm assumes utf8 units won't        // be spread across fragments        const value_type* p = start;        const value_type* end = start + N;        for ( ; p != end /* && *p */; )          {            char c = *p++;            if ( UTF8traits::isASCII(c) )              {                *mBuffer++ = buffer_type(c);                continue;              }            PRUint32 ucs4;            PRUint32 minUcs4;            PRInt32 state = 0;            if ( UTF8traits::is2byte(c) )              {                ucs4 = (PRUint32(c) << 6) & 0x000007C0L;                state = 1;                minUcs4 = 0x00000080;              }            else if ( UTF8traits::is3byte(c) )              {                ucs4 = (PRUint32(c) << 12) & 0x0000F000L;                state = 2;                minUcs4 = 0x00000800;              }            else if ( UTF8traits::is4byte(c) )              {                ucs4 = (PRUint32(c) << 18) & 0x001F0000L;                state = 3;                minUcs4 = 0x00010000;              }            else if ( UTF8traits::is5byte(c) )              {                ucs4 = (PRUint32(c) << 24) & 0x03000000L;                state = 4;                minUcs4 = 0x00200000;              }            else if ( UTF8traits::is6byte(c) )              {                ucs4 = (PRUint32(c) << 30) & 0x40000000L;                state = 5;                minUcs4 = 0x04000000;              }            else              {                NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings.");                mErrorEncountered = PR_TRUE;                return N;              }            while ( state-- )              {                c = *p++;                if ( UTF8traits::isInSeq(c) )                  {                    PRInt32 shift = state * 6;                    ucs4 |= (PRUint32(c) & 0x3F) << shift;                  }                else                  {                    NS_ERROR("not a UTF8 string");                    mErrorEncountered = PR_TRUE;                    return N;                  }              }            if ( ucs4 < minUcs4 )              {                // Overlong sequence                *mBuffer++ = UCS2_REPLACEMENT_CHAR;              }            else if ( ucs4 <= 0xD7FF )              {                *mBuffer++ = ucs4;              }            else if ( /* ucs4 >= 0xD800 && */ ucs4 <= 0xDFFF )              {                // Surrogates                *mBuffer++ = UCS2_REPLACEMENT_CHAR;              }            else if ( ucs4 == 0xFFFE || ucs4 == 0xFFFF )              {                // Prohibited characters                *mBuffer++ = UCS2_REPLACEMENT_CHAR;              }            else if ( ucs4 >= PLANE1_BASE )              {                if ( ucs4 >= 0x00110000 )                  *mBuffer++ = UCS2_REPLACEMENT_CHAR;                else {                  // surrogate, see unicode specification 3.7 for following math.                  ucs4 -= PLANE1_BASE;                  *mBuffer++ = (PRUnichar)(ucs4 >> 10) | 0xd800u;                  *mBuffer++ = (PRUnichar)(ucs4 & 0x3ff) | 0xdc00u;                }              }            else              {                if ( ucs4 != 0xFEFF ) // ignore BOM                    *mBuffer++ = ucs4;              }          }        return p - start;      }