/**
         * Reads in translated plurals forms that are separated by NULL.
         */
        private ArrayList <StringValue> readPluralForms(int length)

        {
            ArrayList <StringValue> list = new ArrayList <StringValue>();
            StringValue             sb   = new UnicodeBuilderValue();

            for (; length > 0; length--)
            {
                int ch = _in.readChar();

                if (ch > 0)
                {
                    sb.append((char)ch);
                }

                else if (ch == 0)
                {
                    list.add(sb);
                    sb = new UnicodeBuilderValue();
                }
                else
                {
                    break;
                }
            }

            list.add(sb);
            return(list);
        }
Ejemplo n.º 2
0
        public StringValue decodeUnicode(StringValue str)
        {
            UnicodeBuilderValue sb = new UnicodeBuilderValue();

            decodeUnicode(str, sb);

            return(sb);
        }
Ejemplo n.º 3
0
        public CharSequence decode(Env env, StringValue str)
        {
            if (str.isUnicode())
            {
                return(str);
            }

            UnicodeBuilderValue sb = new UnicodeBuilderValue();

            decodeUnicode(str, sb);

            return(sb);
        }
        public override void decodeUnicode(StringValue str, UnicodeBuilderValue sb)
        {
            ByteBuffer in = ByteBuffer.wrap(str.toBytes());

            TempCharBuffer tempBuf = TempCharBuffer.allocate();

            try  {
                CharBuffer @out = CharBuffer.wrap(tempBuf.getBuffer());

                while (in.hasRemaining())
                {
                    CoderResult coder = _decoder.decode(in, @out, false);
                    if (!fill(sb, in, @out, coder))
                    {
                        return;
                    }

                    @out.clear();
                }

                CoderResult coder = _decoder.decode(in, @out, true);
                if (!fill(sb, in, @out, coder))
                {
                    return;
                }

                @out.clear();

                coder = _decoder.flush(out);
                fill(sb, in, @out, coder);

                return;
            }
            finally {
                TempCharBuffer.free(tempBuf);
            }
        }
Ejemplo n.º 5
0
 public abstract void decodeUnicode(StringValue str, UnicodeBuilderValue sb);
Ejemplo n.º 6
0
        static UnicodeValue fromUtf8(StringValue source)
        {
            UnicodeValue target = new UnicodeBuilderValue();
            int          len    = source.length();

            for (int i = 0; i < len; i++)
            {
                char ch = source[i];

                if (ch < 0x80)
                {
                    target.append(ch);
                }
                else if ((ch & 0xe0) == 0xc0)
                {
                    if (len <= i + 1)
                    {
                        log.fine(L.l("Regexp: bad UTF-8 sequence, saw EOF"));
                        return(null);
                    }

                    char ch2 = source[++i];

                    target.append((char)(((ch & 0x1f) << 6)
                                         + (ch2 & 0x3f)));
                }
                else if ((ch & 0xf0) == 0xe0)
                {
                    if (len <= i + 2)
                    {
                        log.fine(L.l("Regexp: bad UTF-8 sequence, saw EOF"));
                        return(null);
                    }

                    char ch2 = source[++i];
                    char ch3 = source[++i];

                    target.append((char)(((ch & 0x0f) << 12)
                                         + ((ch2 & 0x3f) << 6)
                                         + (ch3 & 0x3f)));
                }
                else
                {
                    if (i + 3 >= len)
                    {
                        log.fine(L.l("Regexp: bad UTF-8 sequence, saw EOF"));
                        return(null);
                    }

                    char ch2 = source[++i];
                    char ch3 = source[++i];
                    char ch4 = source[++i];

                    int codePoint = ((ch & 0x07) << 18)
                                    + ((ch2 & 0x3F) << 12)
                                    + ((ch3 & 0x3F) << 6)
                                    + (ch4 & 0x3F);

                    int high = ((codePoint - 0x10000) >> 10) + 0xD800;
                    int low  = (codePoint & 0x3FF) + 0xDC00;

                    target.append((char)high);
                    target.append((char)low);
                }
            }

            return(target);
        }
 protected bool fill(UnicodeBuilderValue sb, ByteBuffer in,
        public override void decodeUnicode(StringValue str, UnicodeBuilderValue sb)
        {
            int len = str.length();

            for (int i = 0; i < len; i++)
            {
                int ch = str[i];

                if (ch <= 0x7F)
                {
                    sb.append((char)ch);
                }
                else if (0xC2 <= ch && ch <= 0xDF)
                {
                    int ch2;
                    if (i + 1 < len &&
                        0x80 <= (ch2 = str[i + 1]) && ch2 <= 0xBF)
                    {
                        i++;

                        int code = ((ch - 0xC0) << 6) + (ch2 - 0x80);

                        sb.append((char)code);
                    }
                    else if (_isIgnoreErrors)
                    {
                    }
                    else if (_replacement != null)
                    {
                        sb.append(_replacement);
                    }
                    else if (_isAllowMalformedOut)
                    {
                        sb.append((char)ch);
                    }
                    else
                    {
                        return;
                    }
                }
                else if (0xE0 <= ch && ch <= 0xEF)
                {
                    int ch2;
                    int ch3;
                    if (i + 2 < len &&
                        0x80 <= (ch2 = str[i + 1]) && ch2 <= 0xBF &&
                        0x80 <= (ch3 = str[i + 2]) && ch3 <= 0xBF)
                    {
                        i += 2;

                        int code = ((ch - 0xE0) << 12)
                                   + ((ch2 - 0x80) << 6)
                                   + (ch3 - 0x80);

                        if (0xD800 <= code && code <= 0xDBFF)
                        {
                            code &= 0xFFFFF;

                            int high = 0xD800 + (code >> 10);
                            int low  = 0xDC00 + (code & 0x3FF);

                            sb.append((char)high);
                            sb.append((char)low);
                        }
                        else
                        {
                            sb.append((char)code);
                        }
                    }
                    else if (_isIgnoreErrors)
                    {
                    }
                    else if (_replacement != null)
                    {
                        sb.append(_replacement);
                    }
                    else if (_isAllowMalformedOut)
                    {
                        sb.append((char)ch);
                    }
                    else
                    {
                        return;
                    }
                }
                else if (0xF0 <= ch && ch <= 0xF4)
                {
                    int ch2;
                    int ch3;
                    int ch4;

                    if (i + 3 < len &&
                        0x80 <= (ch2 = str[i + 1]) && ch2 <= 0xBF &&
                        0x80 <= (ch3 = str[i + 2]) && ch3 <= 0xBF &&
                        0x80 <= (ch4 = str[i + 3]) && ch4 <= 0xBF)
                    {
                        i += 3;

                        int code = ((ch - 0xF0) << 18)
                                   + ((ch2 - 0x80) << 12)
                                   + ((ch3 - 0x80) << 6)
                                   + (ch4 - 0x80);

                        if (code > 0xFFFF || 0xD800 <= code && code <= 0xDBFF)
                        {
                            code &= 0xFFFFF;

                            int high = 0xD800 + code >> 10;
                            int low  = 0xDC00 + code & 0x3FF;

                            sb.append((char)high);
                            sb.append((char)low);
                        }
                        else
                        {
                            sb.append((char)code);
                        }
                    }
                    else if (_isIgnoreErrors)
                    {
                    }
                    else if (_replacement != null)
                    {
                        sb.append(_replacement);
                    }
                    else if (_isAllowMalformedOut)
                    {
                        sb.append((char)ch);
                    }
                    else
                    {
                        return;
                    }
                }
                else if (_isIgnoreErrors)
                {
                }
                else if (_replacement != null)
                {
                    sb.append(_replacement);
                }
                else if (_isAllowMalformedOut)
                {
                    sb.append((char)ch);
                }
                else
                {
                    return;
                }
            }

            /*
             * Utf8Reader reader = new Utf8Reader(str);
             *
             * int ch;
             *
             * while ((ch = reader.read()) >= 0) {
             * if (ch == ERROR_CHARACTER) {
             *  _hasError = true;
             *
             *  if (_isIgnoreErrors) {
             *  }
             *  else if (_replacement != null)
             *    sb.append(_replacement);
             *  else
             *    return sb;
             * }
             * else
             *  sb.append((char) ch);
             * }
             */

            return;
        }