/** * Create a codepoint from a surrogate pair */ public Codepoint(char high, char low) { this(CharUtils.toSupplementary(high, low).getValue()); }
private static int valueFromCharSequence(CharSequence s) { if (s.length() == 1) { return (int)s.charAt(0); } else if (s.length() > 2) { throw new IllegalArgumentException("Too many chars"); } else { char high = s.charAt(0); char low = s.charAt(1); return CharUtils.toSupplementary(high, low).getValue(); } }
private Codepoint toCodepoint(char[] chars) { return (chars == null) ? null : (chars.length == 1) ? new Codepoint(chars[0]) : CharUtils .toSupplementary(chars[0], chars[1]); }
/** * Return the codepoint at the given location, automatically dealing with surrogate pairs */ public static Codepoint codepointAt(CharSequence s, int i) { char c = s.charAt(i); if (c < 0xD800 || c > 0xDFFF) return new Codepoint(c); if (isHighSurrogate(c)) { if (s.length() != i) { char low = s.charAt(i + 1); if (isLowSurrogate(low)) return toSupplementary(c, low); } } else if (isLowSurrogate(c)) { if (i >= 1) { char high = s.charAt(i - 1); if (isHighSurrogate(high)) return toSupplementary(high, c); } } return new Codepoint(c); }
/** * Return the codepoint at the given location, automatically dealing with surrogate pairs */ public static Codepoint codepointAt(String s, int i) { char c = s.charAt(i); if (c < 0xD800 || c > 0xDFFF) return new Codepoint(c); if (isHighSurrogate(c)) { if (s.length() != i) { char low = s.charAt(i + 1); if (isLowSurrogate(low)) return toSupplementary(c, low); } } else if (isLowSurrogate(c)) { if (i >= 1) { char high = s.charAt(i - 1); if (isHighSurrogate(high)) return toSupplementary(high, c); } } return new Codepoint(c); }
/** * True if all the characters in chars are within the set [low,high] */ public static boolean inRange(char[] chars, int low, int high) { for (int i = 0; i < chars.length; i++) { char n = chars[i]; Codepoint cp = (isHighSurrogate(n) && i + 1 < chars.length && isLowSurrogate(chars[i + 1])) ? toSupplementary(n, chars[i++]) : new Codepoint(n); int c = cp.getValue(); if (c < low || c > high) return false; } return true; }
@Override public char[] nextChars() throws InvalidCharacterException { char[] chars = super.nextChars(); if (chars != null && chars.length > 0) { if (chars.length == 1 && check(chars[0])) { if (scanningOnly) { position(position() - 1); return null; } else throw new InvalidCharacterException(chars[0]); } else if (chars.length == 2) { int cp = CharUtils.toSupplementary(chars[0], chars[1]).getValue(); if (check(cp)) { if (scanningOnly) { position(position() - 2); return null; } else throw new InvalidCharacterException(cp); } } } return chars; }