Skip to content

Commit

Permalink
Change UnbufferedCharStream to use 32-bit Unicode code points and 32-…
Browse files Browse the repository at this point in the history
…bit buffer
  • Loading branch information
bhamiltoncx committed Mar 29, 2017
1 parent 7ec648e commit 8e90f0b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,14 @@ protected int fill(int n) {
add(c);
} else {
char ch = (char) c;
if (Character.isHighSurrogate(ch)) {
if (Character.isLowSurrogate(ch)) {
throw new RuntimeException("Invalid UTF-16 (low surrogate with no preceding high surrogate)");
} else if (Character.isHighSurrogate(ch)) {
int lowSurrogate = nextChar();
if (lowSurrogate > Character.MAX_VALUE) {
throw new RuntimeException("Invalid UTF-16 (high surrogate followed by code point > U+FFFF");
} else if (lowSurrogate == IntStream.EOF) {
throw new RuntimeException("Invalid UTF-16 (dangling high surrogate at end of file)");
} else {
char lowSurrogateChar = (char) lowSurrogate;
if (Character.isLowSurrogate(lowSurrogateChar)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,30 @@ public void testLastChar() {
assertEquals(expecting, tokens.getTokens().toString());
}

@Test public void testUnicodeSMP() throws Exception {
TestingUnbufferedCharStream input = createStream("\uD83C\uDF0E");
assertEquals(0x1F30E, input.LA(1));
assertEquals("\uD83C\uDF0E", input.getBuffer());
input.consume();
assertEquals(IntStream.EOF, input.LA(1));
assertEquals("\uFFFF", input.getBuffer());
}

@Test(expected = RuntimeException.class)
public void testDanglingHighSurrogateAtEOFThrows() throws Exception {
createStream("\uD83C");
}

@Test(expected = RuntimeException.class)
public void testDanglingHighSurrogateThrows() throws Exception {
createStream("\uD83C\u0123");
}

@Test(expected = RuntimeException.class)
public void testDanglingLowSurrogateThrows() throws Exception {
createStream("\uDF0E");
}

protected static TestingUnbufferedCharStream createStream(String text) {
return new TestingUnbufferedCharStream(new StringReader(text));
}
Expand Down

0 comments on commit 8e90f0b

Please sign in to comment.