4 ms·
It can be simpler to not make a finite state machine. Instead, try the different alternatives one by one, the longest one first. Instead of if (next == '
by ufo 5y ago
It can be simpler to not make a finite state machine. Instead, try the different alternatives one by one, the longest one first.
Instead of
if (next == '>') {
advance()
if (next == '=') {
advance()
return GREATER_EQUAL
} else {
return GREATER
}
}
it can be simpler to do
if (next is ">=")
return GREATER_EQUAL
if (next is ">")
return GREATER
The price is a little bit of performance. It needs more than one character of "lookahead" and may have to re-scan some parts of the input.