public static bool Match(this Route source, Route route, RouteCaseMode caseSensitive = RouteCaseMode.Sensitive) { if (source.IsEmpty()) { return(route.IsEmpty()); } if (route.IsEmpty()) { return(false); } var modeIndex = (int)caseSensitive; if (modeIndex > 1) { return(false); } if (route.IsWild) { var match = Regex.Match(source.Sequences[modeIndex].RegexInput, "^" + route.Sequences[modeIndex].RegexPattern + "$"); return(match.Success); } var routeCount = route.Count; var sourceCount = source.Count; if (routeCount != sourceCount) { return(false); } return(source.Sequences[modeIndex].Hashes[routeCount - 1] == route.Sequences[modeIndex].Hashes[routeCount - 1]); }
public static Route Select(this Route source, Route route, RouteCaseMode caseSensitive = RouteCaseMode.Sensitive) { if (route.IsEmpty() && source.IsEmpty()) { return(Routes.Empty); } var modeIndex = (int)caseSensitive; if (modeIndex > 1) { return(null); } var match = Regex.Match(source.Sequences[modeIndex].RegexInput, route.Sequences[modeIndex].RegexPattern); if (!match.Success) { return(null); } var tokensToSkip = RouteTokenSequenceBuilder.TokenCountInSequence(source.Sequences[modeIndex].RegexInput.Substring(0, match.Index)); var tokensToTake = RouteTokenSequenceBuilder.TokenCountInSequence(match.Value); return(source.Sub(tokensToSkip, tokensToTake)); }
public static bool MatchPartially(this Route source, Route route, RouteCaseMode caseSensitive = RouteCaseMode.Sensitive) { while (!route.IsEmpty()) { if (source.Match(route, caseSensitive)) { return(true); } route = new Route(route.Take(route.Count - 1)); } return(false); }
public static bool StartWith(this Route source, Route route, RouteCaseMode caseSensitive = RouteCaseMode.Sensitive) { if (source.IsEmpty() || route.IsEmpty()) { return(false); } var modeIndex = (int)caseSensitive; if (modeIndex > 1) { return(false); } return(Regex.IsMatch(source.Sequences[modeIndex].RegexInput, "^" + route.Sequences[modeIndex].RegexPattern)); }
public static bool EndWith(this Route source, Route route, RouteCaseMode caseSensitive = RouteCaseMode.Sensitive) { if (source.IsEmpty()) { return(route.IsEmpty()); } if (route.IsEmpty()) { return(true); } var modeIndex = (int)caseSensitive; if (modeIndex > 1) { return(false); } var match = Regex.Match(source.Sequences[modeIndex].RegexInput, route.Sequences[modeIndex].RegexPattern + "$"); return(match.Success); }
public IReadOnlyList <int> GetHashCodes(RouteCaseMode mode) { return(Sequences[(int)mode].Hashes); }
public static RouteRecursiveSplit RecursiveSplit(this Route source, Route mask, RouteCaseMode caseSensitive = RouteCaseMode.Sensitive) { if (source.IsEmpty() || mask.IsEmpty()) { return(null); } var modeIndex = (int)caseSensitive; if (modeIndex > 1) { return(null); } var maskTokenSequence = mask.Sequences[modeIndex]; var match = Regex.Match(source.Sequences[modeIndex].RegexInput, maskTokenSequence.RegexPattern); if (!match.Success) { return(null); } if (!mask.IsWild) { return(new RouteRecursiveSplit(Routes.Empty, Routes.Empty, source)); } // ReSharper disable PossibleInvalidOperationException var recursiveStart = maskTokenSequence.RecursiveStart.Value; var postfixStart = maskTokenSequence.RecursiveEnd.Value; // ReSharper restore PossibleInvalidOperationException var capturedGroups = match.Groups.Enumerate <Group>().Skip(1).ToList(); var prefixTokens = string.Join("/", capturedGroups.Take(recursiveStart).Select(g => g.Value)); var recursiveTokens = string.Join("/", capturedGroups.Skip(recursiveStart).Take(postfixStart - recursiveStart).Select(g => g.Value)); var sourceRecursiveStart = prefixTokens.Occurrences('/') + 1; var sourcePostfixStart = sourceRecursiveStart + recursiveTokens.Occurrences('/') + 1; var prefix = source.Sub(0, sourceRecursiveStart); var recursive = source.Sub(sourceRecursiveStart, sourcePostfixStart - sourceRecursiveStart); var postfix = source.Sub(sourcePostfixStart, source.Count - sourcePostfixStart); return(new RouteRecursiveSplit(prefix, recursive, postfix)); }