repeat method

Pattern repeat(
  1. int min, [
  2. int? max
])

Makes this pattern repeat between min and max times.

If max is null, repeats exactly min times (equivalent to {min}). If max is 0, repeats min or more times (unbounded, equivalent to {min,}). Otherwise, repeats between min and max times (inclusive, equivalent to {min,max}).

Examples:

  • repeat(3) - exactly 3 times {3}
  • repeat(2, 5) - between 2 and 5 times {2,5}
  • repeat(1, 0) - 1 or more times {1,}

Implementation

Pattern repeat(int min, [int? max]) {
  if (min < 0 || (max != null && max < 0)) {
    throw STRlingError(
        'Pattern.repeat(min, max): min and max must be 0 or greater.');
  }

  if (max != null && max != 0 && max < min) {
    throw STRlingError(
        'Pattern.repeat(min, max): max must be greater than or equal to min (use 0 for unbounded).');
  }

  // Convert max to actual value for validation
  final actualMax = max == null ? min : (max == 0 ? double.infinity : max);
  if (_namedGroups.isNotEmpty && actualMax > 1) {
    throw STRlingError(
        'Pattern.repeat(min, max): Named groups cannot be repeated more than once as they must be unique. '
        'Consider using asCapture() or Simply.merge() instead.');
  }

  final qMax = max == null ? min : (max == 0 ? 'Inf' : max);

  return Pattern._(
    nodes.Quantifier(
      target: _node,
      min: min,
      max: qMax,
      greedy: true,
      lazy: false,
      possessive: false,
    ),
    namedGroups: _namedGroups,
  );
}