treeSitterCharacterRangeSetContains function
Exact binary-search contract of Tree-sitter 0.25.10 set_contains.
Native callers supply a non-empty array sorted by both endpoints. Invalid inputs are rejected rather than reproducing native out-of-bounds access.
Implementation
bool treeSitterCharacterRangeSetContains(
List<TreeSitterCharacterRange> ranges,
int lookahead,
) {
if (ranges.isEmpty) {
throw ArgumentError.value(ranges, 'ranges', 'must not be empty');
}
var index = 0;
var size = ranges.length;
while (size > 1) {
final halfSize = size ~/ 2;
final midIndex = index + halfSize;
final range = ranges[midIndex];
if (lookahead >= range.start && lookahead <= range.end) {
return true;
} else if (lookahead > range.end) {
index = midIndex;
}
size -= halfSize;
}
final range = ranges[index];
return lookahead >= range.start && lookahead <= range.end;
}