37 lines
1.1 KiB
Dart
37 lines
1.1 KiB
Dart
final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');
|
|
|
|
RegExp patternToRegExp(String pattern, List<String> parameters) {
|
|
final StringBuffer buffer = StringBuffer('^');
|
|
int start = 0;
|
|
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern)) {
|
|
if (match.start > start) {
|
|
buffer.write(RegExp.escape(pattern.substring(start, match.start)));
|
|
}
|
|
final String name = match[1]!;
|
|
final String? optionalPattern = match[2];
|
|
final String regex = optionalPattern != null
|
|
? _escapeGroup(optionalPattern, name)
|
|
: '(?<$name>[^/]+)';
|
|
buffer.write(regex);
|
|
parameters.add(name);
|
|
start = match.end;
|
|
}
|
|
|
|
if (start < pattern.length) {
|
|
buffer.write(RegExp.escape(pattern.substring(start)));
|
|
}
|
|
|
|
if (!pattern.endsWith('/')) {
|
|
buffer.write(r'(?=/|$)');
|
|
}
|
|
return RegExp(buffer.toString(), caseSensitive: false);
|
|
}
|
|
|
|
String _escapeGroup(String group, [String? name]) {
|
|
final String escapedGroup = group.replaceFirstMapped(
|
|
RegExp(r'[:=!]'), (Match match) => '\\${match[0]}');
|
|
if (name != null) {
|
|
return '(?<$name>$escapedGroup)';
|
|
}
|
|
return escapedGroup;
|
|
} |