repeatString method
Returns a parser that accepts the receiver between min
and max
times.
The resulting parser returns the consumed input string.
This implementation is equivalent to PossessiveRepeatingParserExtension.repeat, but particularly performant when used on character parsers. Instead of a List it returns the parsed sub-String.
For example, the parser letter().repeatString(2, 4)
accepts a sequence of
two, three, or four letters and returns the accepted letters as a string.
Implementation
@useResult
Parser<String> repeatString(int min, [int? max, String? message]) {
final self = this;
if (self is SingleCharacterParser) {
return RepeatingCharacterParser(
self.predicate, message ?? self.message, min, max ?? min);
} else if (self is AnyCharacterParser) {
return RepeatingCharacterParser(const ConstantCharPredicate(true),
message ?? self.message, min, max ?? min);
} else {
return self.repeat(min, max).flatten(message);
}
}