From ac9f75303e215dbc12937eba68089151aef6f874 Mon Sep 17 00:00:00 2001 From: Niels Basjes Date: Wed, 2 Aug 2017 14:29:36 +0200 Subject: [PATCH] Fixed empty CodePointCharStream throwing exception on getText. Fixes #1949 --- .../test/org/antlr/v4/runtime/TestCodePointCharStream.java | 1 + .../Java/src/org/antlr/v4/runtime/CodePointCharStream.java | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime-testsuite/test/org/antlr/v4/runtime/TestCodePointCharStream.java b/runtime-testsuite/test/org/antlr/v4/runtime/TestCodePointCharStream.java index 25c4c0919..c40c4048c 100644 --- a/runtime-testsuite/test/org/antlr/v4/runtime/TestCodePointCharStream.java +++ b/runtime-testsuite/test/org/antlr/v4/runtime/TestCodePointCharStream.java @@ -23,6 +23,7 @@ public class TestCodePointCharStream { CodePointCharStream s = CharStreams.fromString(""); assertEquals(0, s.size()); assertEquals(0, s.index()); + assertEquals("", s.toString()); } @Test diff --git a/runtime/Java/src/org/antlr/v4/runtime/CodePointCharStream.java b/runtime/Java/src/org/antlr/v4/runtime/CodePointCharStream.java index 491ef6918..107faa7b1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/CodePointCharStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/CodePointCharStream.java @@ -151,8 +151,8 @@ public abstract class CodePointCharStream implements CharStream { /** Return the UTF-16 encoded string for the given interval */ @Override public String getText(Interval interval) { - int startIdx = Math.min(interval.a, size - 1); - int len = Math.min(interval.b - interval.a + 1, size); + int startIdx = Math.min(interval.a, size); + int len = Math.min(interval.b - interval.a + 1, size - startIdx); // We know the maximum code point in byteArray is U+00FF, // so we can treat this as if it were ISO-8859-1, aka Latin-1,