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:
parent
6195ec749b
commit
31f4dcc7cf
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue