parseTokenBudget function

int? parseTokenBudget(
  1. String text
)

Parse a token budget from text. Returns null if no budget is found.

Supports:

  • Shorthand at start: +500k fix the bug
  • Shorthand at end: fix the bug +500k
  • Verbose anywhere: use 2M tokens to fix the bug

Implementation

int? parseTokenBudget(String text) {
  final startMatch = _shorthandStartRe.firstMatch(text);
  if (startMatch != null) {
    return _parseBudgetMatch(
      startMatch.group(1)!,
      startMatch.group(2)!,
    ).round();
  }
  final endMatch = _shorthandEndRe.firstMatch(text);
  if (endMatch != null) {
    return _parseBudgetMatch(endMatch.group(1)!, endMatch.group(2)!).round();
  }
  final verboseMatch = _verboseRe.firstMatch(text);
  if (verboseMatch != null) {
    return _parseBudgetMatch(
      verboseMatch.group(1)!,
      verboseMatch.group(2)!,
    ).round();
  }
  return null;
}