/** * True when a valid surrogate pair starts at the given {@code index} in the given {@code string}. * Out-of-range indexes return false. */ @VisibleForTesting static boolean validSurrogatePairAt(CharSequence string, int index) { return index >= 0 && index <= (string.length() - 2) && Character.isHighSurrogate(string.charAt(index)) && Character.isLowSurrogate(string.charAt(index + 1)); }
/** * True when a valid surrogate pair starts at the given {@code index} in the given {@code string}. * Out-of-range indexes return false. */ @VisibleForTesting static boolean validSurrogatePairAt(CharSequence string, int index) { return index >= 0 && index <= (string.length() - 2) && Character.isHighSurrogate(string.charAt(index)) && Character.isLowSurrogate(string.charAt(index + 1)); } }
/** * Get the length for UTF8-encoding a string without encoding it first * * @param s The string to calculate the length for * @return The length when serialized */ public static int utf8Length(CharSequence s) { int count = 0; for (int i = 0, len = s.length(); i < len; i++) { char ch = s.charAt(i); if (ch <= 0x7F) { count++; } else if (ch <= 0x7FF) { count += 2; } else if (Character.isHighSurrogate(ch)) { count += 4; ++i; } else { count += 3; } } return count; }
/** * True when a valid surrogate pair starts at the given {@code index} in the given {@code string}. * Out-of-range indexes return false. */ @VisibleForTesting static boolean validSurrogatePairAt(CharSequence string, int index) { return index >= 0 && index <= (string.length() - 2) && Character.isHighSurrogate(string.charAt(index)) && Character.isLowSurrogate(string.charAt(index + 1)); } }
/** * True when a valid surrogate pair starts at the given {@code index} in the given {@code string}. * Out-of-range indexes return false. */ @VisibleForTesting static boolean validSurrogatePairAt(CharSequence string, int index) { return index >= 0 && index <= (string.length() - 2) && Character.isHighSurrogate(string.charAt(index)) && Character.isLowSurrogate(string.charAt(index + 1)); } }
public static boolean isSurrogate(String string, int i) { return Character.isLowSurrogate(string.charAt(i - 1)) && Character.isHighSurrogate(string.charAt(i - 2)); }
private static boolean validSurrogatePairAt(CharSequence string, int index) { return index >= 0 && index <= (string.length() - 2) && isHighSurrogate(string.charAt(index)) && isLowSurrogate(string.charAt(index + 1)); } }
int nextCodePoint() { assert pos < end; final char c1 = str.charAt(pos++); if (Character.isHighSurrogate(c1) && pos < end) { final char c2 = str.charAt(pos); if (Character.isLowSurrogate(c2)) { pos++; return Character.toCodePoint(c1, c2); } } return c1; } }
public static boolean isSurrogate(String string, int i) { return Character.isLowSurrogate(string.charAt(i - 1)) && Character.isHighSurrogate(string.charAt(i - 2)); }
if (!Character.isHighSurrogate(c)) { encodedLength++;
private boolean wideDisplayCharacterStartingAt(int column) { for (int currentCharIndex = 0, currentColumn = 0; currentCharIndex < mSpaceUsed; ) { char c = mText[currentCharIndex++]; int codePoint = Character.isHighSurrogate(c) ? Character.toCodePoint(c, mText[currentCharIndex++]) : c; int wcwidth = WcWidth.width(codePoint); if (wcwidth > 0) { if (currentColumn == column && wcwidth == 2) return true; currentColumn += wcwidth; if (currentColumn > column) return false; } } return false; }
/** The width at an index position in a java char array. */ public static int width(char[] chars, int index) { char c = chars[index]; return Character.isHighSurrogate(c) ? width(Character.toCodePoint(c, chars[index + 1])) : width(c); }
private static int writeUtf8Surrogate(AbstractByteBuf buffer, int writerIndex, char c, char c2) { if (!Character.isLowSurrogate(c2)) { buffer._setByte(writerIndex++, WRITE_UTF_UNKNOWN); buffer._setByte(writerIndex++, Character.isHighSurrogate(c2) ? WRITE_UTF_UNKNOWN : c2); return writerIndex; } int codePoint = Character.toCodePoint(c, c2); // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G2630. buffer._setByte(writerIndex++, (byte) (0xf0 | (codePoint >> 18))); buffer._setByte(writerIndex++, (byte) (0x80 | ((codePoint >> 12) & 0x3f))); buffer._setByte(writerIndex++, (byte) (0x80 | ((codePoint >> 6) & 0x3f))); buffer._setByte(writerIndex++, (byte) (0x80 | (codePoint & 0x3f))); return writerIndex; }
private static int writeUtf8Surrogate(AbstractByteBuf buffer, int writerIndex, char c, char c2) { if (!Character.isLowSurrogate(c2)) { buffer._setByte(writerIndex++, WRITE_UTF_UNKNOWN); buffer._setByte(writerIndex++, Character.isHighSurrogate(c2) ? WRITE_UTF_UNKNOWN : c2); return writerIndex; } int codePoint = Character.toCodePoint(c, c2); // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G2630. buffer._setByte(writerIndex++, (byte) (0xf0 | (codePoint >> 18))); buffer._setByte(writerIndex++, (byte) (0x80 | ((codePoint >> 12) & 0x3f))); buffer._setByte(writerIndex++, (byte) (0x80 | ((codePoint >> 6) & 0x3f))); buffer._setByte(writerIndex++, (byte) (0x80 | (codePoint & 0x3f))); return writerIndex; }
private static String composeString(int len, Random r) { char[] cc = new char[len]; char ch; for (int i = 0; i<len; i++) { do { ch = (char)r.nextInt(); } while (!Character.isDefined(ch) || Character.isHighSurrogate(ch) || Character.isLowSurrogate(ch)); cc[i] = ch; } return new String(cc); }
private void assertLineStartsWith(int... codePoints) { char[] chars = row.mText; int charIndex = 0; for (int i = 0; i < codePoints.length; i++) { int lineCodePoint = chars[charIndex++]; if (Character.isHighSurrogate((char) lineCodePoint)) { lineCodePoint = Character.toCodePoint((char) lineCodePoint, chars[charIndex++]); } assertEquals("Differing a code point index=" + i, codePoints[i], lineCodePoint); } }
@FastMethod @Signature(@Arg("char")) public static Memory isHighSurrogate(Environment env, Memory... args) { return Character.isHighSurrogate(chr(args[0])) ? Memory.TRUE : Memory.FALSE; }
buffer._setByte(writerIndex++, (byte) (0x80 | (c & 0x3f))); } else if (isSurrogate(c)) { if (!Character.isHighSurrogate(c)) { buffer._setByte(writerIndex++, WRITE_UTF_UNKNOWN); continue;
@Test public void testBackwardsCompatibility() throws UnsupportedEncodingException, ClassNotFoundException { StringSerializer serializer = new StringSerializer(); int codepoint = 65536; do { if (Character.isValidCodePoint(codepoint) && !(Character.isHighSurrogate((char) codepoint) || Character.isLowSurrogate((char) codepoint))) { String s = new String(Character.toChars(codepoint)); ByteBuffer bytes = ByteBuffer.wrap(s.getBytes("UTF-8")); assertThat("Codepoint : 0x" + Integer.toHexString(codepoint), serializer.read(bytes), is(s)); assertThat("Codepoint : 0x" + Integer.toHexString(codepoint), serializer.equals(s, bytes), is(true)); } } while (++codepoint != Integer.MIN_VALUE); }
protected TerminalTestCase assertLineStartsWith(int line, int... codePoints) { char[] chars = mTerminal.getScreen().mLines[mTerminal.getScreen().externalToInternalRow(line)].mText; int charIndex = 0; for (int i = 0; i < codePoints.length; i++) { int lineCodePoint = chars[charIndex++]; if (Character.isHighSurrogate((char) lineCodePoint)) { lineCodePoint = Character.toCodePoint((char) lineCodePoint, chars[charIndex++]); } assertEquals("Differing a code point index=" + i, codePoints[i], lineCodePoint); } return this; }