protected static char[] byte2Char( final byte[] a, char[] s ) { final int length = a.length; if ( s == null ) s = new char[ countUTF8Chars( a ) ]; int b, c, d; for( int i = 0, j = 0; i < length; i++ ) { b = a[ i ] & 0xFF; if ( ( b >> 4 ) < 8 ) s[ j++ ] = (char)b; else if ( ( b >> 4 ) < 14 ) { c = a[ ++i ] & 0xFF; if ( ( c & 0xC0 ) != 0x80 ) throw new IllegalStateException( "Malformed internal UTF-8 encoding" ); s[ j++ ] = (char)( ( ( b & 0x1F) << 6 ) | ( c & 0x3F ) ); } else { c = a[ ++i ] & 0xFF; d = a[ ++i ]; if ( ( c & 0xC0 ) != 0x80 || ( d & 0xC0 ) != 0x80 ) throw new IllegalStateException( "Malformed internal UTF-8 encoding" ); s[ j++ ] = (char)( ( ( b & 0x0F ) << 12 ) | ( ( c & 0x3F ) << 6 ) | ( ( d & 0x3F ) << 0 ) ); } } return s; }
protected static char[] byte2Char( final byte[] a, char[] s ) { final int length = a.length; if ( s == null ) s = new char[ countUTF8Chars( a ) ]; int b, c, d; for( int i = 0, j = 0; i < length; i++ ) { b = a[ i ] & 0xFF; if ( ( b >> 4 ) < 8 ) s[ j++ ] = (char)b; else if ( ( b >> 4 ) < 14 ) { c = a[ ++i ] & 0xFF; if ( ( c & 0xC0 ) != 0x80 ) throw new IllegalStateException( "Malformed internal UTF-8 encoding" ); s[ j++ ] = (char)( ( ( b & 0x1F) << 6 ) | ( c & 0x3F ) ); } else { c = a[ ++i ] & 0xFF; d = a[ ++i ]; if ( ( c & 0xC0 ) != 0x80 || ( d & 0xC0 ) != 0x80 ) throw new IllegalStateException( "Malformed internal UTF-8 encoding" ); s[ j++ ] = (char)( ( ( b & 0x0F ) << 12 ) | ( ( c & 0x3F ) << 6 ) | ( ( d & 0x3F ) << 0 ) ); } } return s; }
protected static char[] byte2Char(final byte[] a, char[] s) { final int length = a.length; if (s == null) s = new char[countUTF8Chars(a)]; int b, c, d, t; for(int i = 0, j = 0; i < length; i++) { b = a[i] & 0xFF; t = b >> 4; if (t < 8) s[j++] = (char)b; else if (t < 14) { c = a[++i] & 0xFF; if ((c & 0xC0) != 0x80) throw new IllegalStateException("Malformed internal UTF-8 encoding"); s[j++] = (char)(((b & 0x1F) << 6) | (c & 0x3F)); } else if (t < 15){ c = a[++i] & 0xFF; d = a[++i]; if ((c & 0xC0) != 0x80 || (d & 0xC0) != 0x80) throw new IllegalStateException("Malformed internal UTF-8 encoding"); s[j++] = (char)(((b & 0x0F) << 12) | ((c & 0x3F) << 6) | ((d & 0x3F) << 0)); } else { // Surrogate pair (yuck!) final String surrogatePair = new String(a, i, 4, Charsets.UTF_8); s[j++] = surrogatePair.charAt(0); s[j++] = surrogatePair.charAt(1); i += 3; } } return s; }
/** Returns the element at the specified position in this front-coded list by storing it in a mutable string. * * @param index an index in the list. * @param s a mutable string that will contain the string at the specified position. */ public void get( final int index, MutableString s ) { if ( utf8 ) { final byte[] a = byteFrontCodedList.getArray( index ); s.length( countUTF8Chars( a ) ); byte2Char( a, s.array() ); } else { int res = charFrontCodedList.get( index, s.array() ); if ( res < 0 ) { s.length( s.array().length - res ); res = charFrontCodedList.get( index, s.array() ); } else s.length( res ); } }
/** Returns the element at the specified position in this front-coded list by storing it in a mutable string. * * @param index an index in the list. * @param s a mutable string that will contain the string at the specified position. */ public void get( final int index, MutableString s ) { if ( utf8 ) { final byte[] a = byteFrontCodedList.getArray( index ); s.length( countUTF8Chars( a ) ); byte2Char( a, s.array() ); } else { int res = charFrontCodedList.get( index, s.array() ); if ( res < 0 ) { s.length( s.array().length - res ); res = charFrontCodedList.get( index, s.array() ); } else s.length( res ); } }
/** Returns the element at the specified position in this front-coded list by storing it in a mutable string. * * @param index an index in the list. * @param s a mutable string that will contain the string at the specified position. */ public void get(final int index, MutableString s) { if (utf8) { final byte[] a = byteFrontCodedList.getArray(index); s.length(countUTF8Chars(a)); byte2Char(a, s.array()); } else { s.length(s.array().length); int res = charFrontCodedList.get(index, s.array()); if (res < 0) { s.length(s.array().length - res); res = charFrontCodedList.get(index, s.array()); } else s.length(res); } }