/** Returns previous character */ private int getPrevious() { char c2 = text.previous(); if (Character.isLowSurrogate(c2) && text.getIndex() > text.getBeginIndex()) { char c1 = text.previous(); if (Character.isHighSurrogate(c1)) { return Character.toCodePoint(c1, c2); } else { text.next(); } } return (int) c2; }
public int previous() { int i = iterator.previous(); if (i == CharacterIterator.DONE) { return DONE; } return i; }
/** * For the given string, returns the number of UTF-8 bytes required to encode the string. * * @param string text to encode * @return number of UTF-8 bytes required to encode */ public static int utf8Length(String string) { CharacterIterator iter = new StringCharacterIterator(string); char ch = iter.first(); int size = 0; while (ch != CharacterIterator.DONE) { if ((ch >= 0xD800) && (ch < 0xDC00)) { // surrogate pair? char trail = iter.next(); if ((trail > 0xDBFF) && (trail < 0xE000)) { // valid pair size += 4; } else { // invalid pair size += 3; iter.previous(); // rewind one } } else if (ch < 0x80) { size++; } else if (ch < 0x800) { size += 2; } else { // ch < 0x10000, that is, the largest char value size += 3; } ch = iter.next(); } return size; }
/** Returns the count of next character. */ private int getCurrentCodePointCount() { char c1 = text.current(); if (Character.isHighSurrogate(c1) && text.getIndex() < text.getEndIndex()) { char c2 = text.next(); text.previous(); if (Character.isLowSurrogate(c2)) { return 2; } } return 1; }
/** Returns current character */ int getCurrent() { char c1 = text.current(); if (Character.isHighSurrogate(c1) && text.getIndex() < text.getEndIndex()) { char c2 = text.next(); text.previous(); if (Character.isLowSurrogate(c2)) { return Character.toCodePoint(c1, c2); } } return (int) c1; }
private Object read() { skipWhiteSpace(); char ch = c; next(); switch (ch) { case '"': token = string(); break; case '[': token = array(); break; case ']': token = ARRAY_END; break; case ',': token = COMMA; break; case '{': token = object(); break; case '}': token = OBJECT_END; break; case ':': token = COLON; break; case 't': if (c != 'r' || next() != 'u' || next() != 'e') throw new RuntimeException("Invalid JSON token: expected 'true' keyword."); next(); token = Boolean.TRUE; break; case 'f': if (c != 'a' || next() != 'l' || next() != 's' || next() != 'e') throw new RuntimeException("Invalid JSON token: expected 'false' keyword."); next(); token = Boolean.FALSE; break; case 'n': if (c != 'u' || next() != 'l' || next() != 'l') throw new RuntimeException("Invalid JSON token: expected 'null' keyword."); next(); token = null; break; default: c = it.previous(); if (Character.isDigit(c) || c == '-') { token = number(); } } // System.out.println("token: " + token); // enable this line to see the token stream return token; }
private Object read() { skipWhiteSpace(); char ch = c; next(); Object value = null; switch (ch) { case '"': value = string(); break; case '[': value = array(); break; case ']': value = ARRAY_END; break; case ',': value = COMMA; break; case '{': value = object(); break; case '}': value = OBJECT_END; break; case ':': value = COLON; break; case 't': if (c != 'r' || next() != 'u' || next() != 'e') error("Invalid JSON token: expected 'true' keyword."); next(); value = Boolean.TRUE; break; case 'f': if (c != 'a' || next() != 'l' || next() != 's' || next() != 'e') error("Invalid JSON token: expected 'false' keyword."); next(); value = Boolean.FALSE; break; case 'n': if (c != 'u' || next() != 'l' || next() != 'l') error("Invalid JSON token: expected 'null' keyword."); next(); value = null; break; default: c = it.previous(); if (Character.isDigit(c) || c == '-') value = number(); else error("Invalid character '" + c + "', expecting a JSON token."); } return value; }
private char previous() { c = it.previous(); return c; }