public char charAt(int index) { int currentIndex = 0; for (int i = 0; i < this.codePoints.size(); i++) { int codePoint = this.codePoints.get(i); int charCount = Character.charCount(codePoint); if (index < currentIndex + charCount) { if (charCount == 1) { return (char) codePoint; } if (index == currentIndex) { return Character.highSurrogate(codePoint); } return Character.lowSurrogate(codePoint); } currentIndex += charCount; } throw new IndexOutOfBoundsException("Char value at " + index + " is out of bounds for length " + currentIndex); }
@Override public char charAt(int index) { int currentIndex = 0; for (int i = 0; i < this.codePoints.size(); i++) { int codePoint = this.codePoints.get(i); int charCount = Character.charCount(codePoint); if (index < currentIndex + charCount) { if (charCount == 1) { return (char) codePoint; } if (index == currentIndex) { return Character.highSurrogate(codePoint); } return Character.lowSurrogate(codePoint); } currentIndex += charCount; } throw new IndexOutOfBoundsException("Char value at " + index + " is out of bounds for length " + currentIndex); }
@Override public char charAt(int index) { int currentIndex = 0; for (int i = 0; i < this.codePoints.size(); i++) { int codePoint = this.codePoints.get(i); int charCount = Character.charCount(codePoint); if (index < currentIndex + charCount) { if (charCount == 1) { return (char) codePoint; } if (index == currentIndex) { return Character.highSurrogate(codePoint); } return Character.lowSurrogate(codePoint); } currentIndex += charCount; } throw new IndexOutOfBoundsException("Char value at " + index + " is out of bounds for length " + currentIndex); }
outArray[outPos++] = Character.lowSurrogate(code); } else { --inPos;
static void toSurrogates(int codePoint, char[] dst, int index) { // We write elements "backwards" to guarantee all-or-nothing dst[index+1] = lowSurrogate(codePoint); dst[index] = highSurrogate(codePoint); }
static void toSurrogates(int codePoint, char[] dst, int index) { // We write elements "backwards" to guarantee all-or-nothing dst[index + 1] = lowSurrogate(codePoint); dst[index] = highSurrogate(codePoint); } }
static void toSurrogates(int codePoint, char[] dst, int index) { // We write elements "backwards" to guarantee all-or-nothing dst[index + 1] = lowSurrogate(codePoint); dst[index] = highSurrogate(codePoint); } }
// byte[] bytes = new byte[] { (byte) 0xD8, 0x34, (byte) 0xDD, 0x1E }; String text = ""; // new String(bytes, StandardCharsets.UTF_16); int codePoint = text.codePointAt(0); ByteBuffer buf = ByteBuffer.allocate(4); buf.putChar(Character.highSurrogate(codePoint)); buf.putChar(Character.lowSurrogate(codePoint)); byte[] data = buf.array(); // recreated String rep = new String(data, StandardCharsets.UTF_16);
static void toSurrogates(int codePoint, char[] dst, int index) { // We write elements "backwards" to guarantee all-or-nothing dst[index+1] = lowSurrogate(codePoint); dst[index] = highSurrogate(codePoint); }
static public void appendJavaStyleEscapedCodePoint(int codePoint, StringBuilder sb) { if (Character.isSupplementaryCodePoint(codePoint)) { // char is not an 'integral' type, so we have to explicitly convert // to int before passing to the %X formatter or else it throws. sb.append(String.format("\\u%04X", (int)Character.highSurrogate(codePoint))); sb.append(String.format("\\u%04X", (int)Character.lowSurrogate(codePoint))); } else { sb.append(String.format("\\u%04X", codePoint)); } }
static public void appendJavaStyleEscapedCodePoint(int codePoint, StringBuilder sb) { if (Character.isSupplementaryCodePoint(codePoint)) { // char is not an 'integral' type, so we have to explicitly convert // to int before passing to the %X formatter or else it throws. sb.append(String.format("\\u%04X", (int)Character.highSurrogate(codePoint))); sb.append(String.format("\\u%04X", (int)Character.lowSurrogate(codePoint))); } else { sb.append(String.format("\\u%04X", codePoint)); } }
Character.highSurrogate(codePoint); Character.lowSurrogate(codePoint);
/** * Appends the characters from codepoint into the string builder. This is the same as Character#toChars * but prevents the additional char array garbage for BMP codepoints. * * @param dst * the destination in which to append the characters * @param codePoint * the codepoint to be appended */ private static void appendCodepoint(StringBuilder dst, int codePoint) { if (Character.isBmpCodePoint(codePoint)) { dst.append((char)codePoint); } else if (Character.isValidCodePoint(codePoint)) { dst.append(Character.highSurrogate(codePoint)); dst.append(Character.lowSurrogate(codePoint)); } else { throw new IllegalArgumentException("Invalid codepoint " + codePoint); } } }
/** * Appends the given code point to the underlying {@link #out} stream or buffer. * * @param c the code point to append. * @throws IOException if an error occurred while appending the code point. */ final void appendCodePoint(final int c) throws IOException { if (Character.isBmpCodePoint(c)) { out.append((char) c); } else if (Character.isSupplementaryCodePoint(c)) { out.append(Character.highSurrogate(c)) .append(Character. lowSurrogate(c)); } else { throw new CharConversionException(); } }
@Override public int codeToMbc(int code, byte[] bytes, int p) { if (Character.isBmpCodePoint(code)) { bytes[p + 0] = (byte) (code >>> 8); bytes[p + 1] = (byte) (code >>> 0); return 2; } char high = Character.highSurrogate(code); char low = Character.lowSurrogate(code); bytes[p + 0] = (byte) (high >>> 8); bytes[p + 1] = (byte) (high >>> 0); bytes[p + 2] = (byte) (low >>> 8); bytes[p + 3] = (byte) (low >>> 0); return 4; }
/** * Appends the given code point to the underlying {@link #out} stream or buffer. * * @param c the code point to append. * @throws IOException if an error occurred while appending the code point. */ final void appendCodePoint(final int c) throws IOException { if (Character.isBmpCodePoint(c)) { out.append((char) c); } else if (Character.isSupplementaryCodePoint(c)) { out.append(Character.highSurrogate(c)) .append(Character. lowSurrogate(c)); } else { throw new CharConversionException(); } }
public char charAt(int index) { int currentIndex = 0; for (int i = 0; i < this.codePoints.size(); i++) { int codePoint = this.codePoints.get(i); int charCount = Character.charCount(codePoint); if (index < currentIndex + charCount) { if (charCount == 1) { return (char) codePoint; } if (index == currentIndex) { return Character.highSurrogate(codePoint); } return Character.lowSurrogate(codePoint); } currentIndex += charCount; } throw new IndexOutOfBoundsException("Char value at " + index + " is out of bounds for length " + currentIndex); }
void appendCodepoint(int codepoint) { if (Character.charCount(codepoint) == 1) { appendChar((char)codepoint); } else { appendChar(Character.highSurrogate(codepoint)); appendChar(Character.lowSurrogate(codepoint)); } } }
private static int utf8Decode4Bytes(long lo, int b, long hi, CharSink sink) { if (b >> 3 != -2) { return utf8error(); } if (hi - lo > 3) { byte b2 = Unsafe.getUnsafe().getByte(lo + 1); byte b3 = Unsafe.getUnsafe().getByte(lo + 2); byte b4 = Unsafe.getUnsafe().getByte(lo + 3); int codePoint = b << 18 ^ b2 << 12 ^ b3 << 6 ^ b4 ^ 3678080; if (!isMalformed4(b2, b3, b4) && Character.isSupplementaryCodePoint(codePoint)) { sink.put(Character.highSurrogate(codePoint)); sink.put(Character.lowSurrogate(codePoint)); return 4; } } return utf8error(); }
/** * Appends the code point to the buffer. * * @param c * the code point */ public void appendCodePoint(int c) { if (Character.isBmpCodePoint(c)) { append(c); } else { append(Character.highSurrogate(c)); append(Character.lowSurrogate(c)); } }