findTokenBudgetPositions function
Find all token budget positions in text (for UI highlighting).
Implementation
List<({int start, int end})> findTokenBudgetPositions(String text) {
final positions = <({int start, int end})>[];
final startMatch = _shorthandStartRe.firstMatch(text);
if (startMatch != null) {
final offset =
startMatch.start +
startMatch.group(0)!.length -
startMatch.group(0)!.trimLeft().length;
positions.add((
start: offset,
end: startMatch.start + startMatch.group(0)!.length,
));
}
final endMatch = _shorthandEndRe.firstMatch(text);
if (endMatch != null) {
final endStart = endMatch.start + 1; // +1: regex includes leading \s
// Avoid double-counting when input is just "+500k".
final alreadyCovered = positions.any(
(p) => endStart >= p.start && endStart < p.end,
);
if (!alreadyCovered) {
positions.add((
start: endStart,
end: endMatch.start + endMatch.group(0)!.length,
));
}
}
for (final match in _verboseReG.allMatches(text)) {
positions.add((start: match.start, end: match.end));
}
return positions;
}