Pattern constructor

Pattern(
  1. String pattern
)

Constructor from a string representation of a path pattern.

Implementation

Pattern(String pattern) : _segments = pattern.split(_pathSeparator) {
  // Check for some prohibited values

  if (_segments.length == 1 && _segments.first.isEmpty) {
    // i.e. "" is not allowed as a pattern
    throw ArgumentError.value(pattern, 'pattern', 'empty string');
  }
  if (_segments.first != _prefix) {
    // The string representation of a path pattern must start with "~/".
    // e.g. "/", "/foo", "bar" or "bar/foo" is not allowed as a pattern
    throw ArgumentError.value(
        pattern, 'pattern', 'does not start with "$_prefix/"');
  }

  // Clean up the segments

  _segments.removeAt(0); // remove the leading "~".

  while (_segments.isNotEmpty && _segments[0].isEmpty) {
    _segments.removeAt(0); // remove leading slashes "/", "//", "/////"
  }

  // Check for invalid combinations of segment types

  for (final seg in _segments) {
    // Check that each segment contains at most one special type
    // e.g. :foo?, :*, *? or :foo? are not permitted

    var numSpecials = 0;
    var name = seg;
    if (_isVariable(name)) {
      numSpecials++;
      name = _variableName(name);
    }
    if (_isOptional(name)) {
      numSpecials++;
      name = _optionalName(name);
    }
    if (name == wildcard) {
      numSpecials++;
    }

    if (1 < numSpecials) {
      throw ArgumentError.value(pattern, 'pattern', 'invalid segment: $seg');
    }
  }
}