findMatch method
Find matching route and extract parameters
Implementation
RouteMatch? findMatch(Map<String, NyRouterRoute> routeMappings) {
// Normalize the input path but keep leading slash
final normalizedPath = _normalizePath(path);
final pathSegments = normalizedPath.split('/');
// Try each registered route
for (final entry in routeMappings.entries) {
final routePattern = _normalizePath(entry.key);
final route = entry.value;
final patternSegments = routePattern.split('/');
// Quick check for segment count (including empty segments from leading slash)
if (pathSegments.length != patternSegments.length) {
continue;
}
final params = <String, String>{};
var isMatch = true;
// Check each segment
for (var i = 0; i < patternSegments.length; i++) {
final patternSegment = patternSegments[i];
final pathSegment = pathSegments[i];
// Skip empty segments from leading slash
if (patternSegment.isEmpty && pathSegment.isEmpty) {
continue;
}
if (patternSegment.startsWith('{') && patternSegment.endsWith('}')) {
// Extract parameter
final paramName =
patternSegment.substring(1, patternSegment.length - 1);
params[paramName] = pathSegment;
} else if (patternSegment != pathSegment) {
// Static segment doesn't match
isMatch = false;
break;
}
}
if (isMatch) {
return RouteMatch(
pattern: entry.key, // Keep original pattern
route: route,
parameters: params,
);
}
}
return null;
}