tryParse static method

HttpRouteParam? tryParse(
  1. String segment
)

Implementation

static HttpRouteParam? tryParse(String segment) {
  /// route param is of the form ":name" or ":name:pattern"
  /// the ":pattern" part can be a regular expression
  /// or a param type name
  if (!segment.startsWith(':')) return null;
  var pattern = '';
  var name = segment.substring(1);
  HttpRouteParamType? type;
  final idx = name.indexOf(':');
  if (idx > 0) {
    pattern = name.substring(idx + 1);
    name = name.substring(0, idx);
    final typeName = pattern.toLowerCase();
    type = paramTypes.cast<HttpRouteParamType?>().firstWhere((t) => t!.name == typeName, orElse: () => null);
    if (type != null) {
      // the pattern matches a param type name
      pattern = type.pattern;
    }
  } else {
    // anything but a slash
    pattern = r'[^/]+?';
  }
  return HttpRouteParam(name, pattern, type);
}