don't unnecessary fetch all tokens when grabbing an interval of text (match C# logic)

When an exception is thrown, it calls getText to get the text of the tokens invloved but the current C++ and Java implemention first fetches the rest of the tokens instead of  just using the tokens it already has.  C#'s version is correct (fill() is called only when asking for the entire stream's text).
This commit is contained in:
WalterCouto 2019-04-17 20:10:37 -04:00
parent 6195ec749b
commit 31f4dcc7cf
2 changed files with 6 additions and 3 deletions

View File

@ -272,7 +272,7 @@ ssize_t BufferedTokenStream::previousTokenOnChannel(size_t i, size_t channel) {
}
if (i == 0)
break;
return -1;
i--;
}
return i;
@ -358,17 +358,18 @@ std::string BufferedTokenStream::getSourceName() const
}
std::string BufferedTokenStream::getText() {
fill();
return getText(misc::Interval(0U, size() - 1));
}
std::string BufferedTokenStream::getText(const misc::Interval &interval) {
lazyInit();
fill();
size_t start = interval.a;
size_t stop = interval.b;
if (start == INVALID_INDEX || stop == INVALID_INDEX) {
return "";
}
sync(stop);
if (stop >= _tokens.size()) {
stop = _tokens.size() - 1;
}

View File

@ -441,15 +441,17 @@ public class BufferedTokenStream implements TokenStream {
@Override
public String getText() {
fill();
return getText(Interval.of(0,size()-1));
}
@Override
public String getText(Interval interval) {
lazyInit();
int start = interval.a;
int stop = interval.b;
if ( start<0 || stop<0 ) return "";
fill();
sync(stop);
if ( stop>=tokens.size() ) stop = tokens.size()-1;
StringBuilder buf = new StringBuilder();