match method
Matches a route for the given method and path. Returns the first route that fits the given method and path.
Performance:
- Static routes: O(1) hash map lookup
- Dynamic routes: O(n) where n = number of dynamic routes only
Implementation
RouteMatchResult? match(String method, String path) {
// Normalize the incoming path for consistent matching
final normalizedPath = _normalizePath(path);
// Try static routes first (O(1) lookup)
if (_staticRoutes[method]?.containsKey(normalizedPath) ?? false) {
final route = _staticRoutes[method]![normalizedPath]!;
return RouteMatchResult(
handler: route.handler,
params: {}, // Static routes have no params
middleware: route.middleware,
);
}
// Fallback to dynamic routes (O(n) but smaller n)
for (final route in _dynamicRoutes) {
if (route.matches(method, path)) {
return RouteMatchResult(
handler: route.handler,
params: route.extractParams(path),
middleware: route.middleware,
);
}
}
return null;
}