public bool TryMatch( CompiledPattern compiledPattern, PathString path, [NotNullWhen(true)] out PatternMatchResult?match, [NotNullWhen(false)] out string?why) { return(matcher.TryMatch(compiledPattern, path, out match, out why)); }
/// <inheritdoc/> public bool TryMatch( CompiledPattern compiledPattern, PathString path, [NotNullWhen(true)] out PatternMatchResult?match, [NotNullWhen(false)] out string?why) { CheckValue(compiledPattern, nameof(compiledPattern)); CheckParam(path.HasValue, nameof(path)); var pathValue = path.Value; Match regexMatch; try { regexMatch = compiledPattern.Regex.Match(pathValue); } catch (RegexMatchTimeoutException) { why = $"The attempt to match path '{pathValue}' with pattern '{compiledPattern.Pattern}' timed out."; match = null; return(false); } if (!regexMatch.Success) { why = $"The path '{pathValue}' is not a match for pattern '{compiledPattern.Pattern}'"; match = null; return(false); } var variables = GetVariables(regexMatch, compiledPattern.VariableNames); var pathSuffix = GetPathSuffix(regexMatch, pathValue); why = null; match = new PatternMatchResult(pathSuffix, variables); return(true); }
public bool TryCompile( string pattern, [NotNullWhen(true)] out CompiledPattern?compiledPattern, [NotNullWhen(false)] out IReadOnlyList <PatternCompilerError>?errors) { CheckValue(pattern, nameof(pattern)); var errorsSink = new PatternCompilerErrorsSink(); // 1- Resolve the type of the pattern var resolvedPattern = resolver.Resolve(pattern); var type = resolvedPattern.Type; pattern = resolvedPattern.Pattern; Regex regex; ISet <string> variables; if (type == PatternType.Regex) { // 2- Build regex regex = RegexFactory.Create(pattern); // 3- Get variable names var names = regex.GetGroupNames().Where(n => !short.TryParse(n, NumberStyles.None, null, out var _)); variables = new HashSet <string>(names, StringComparer.OrdinalIgnoreCase); } else { // 2- Parse if (!parser.TryParse(pattern, errorsSink, out var parsedPattern)) { compiledPattern = null; errors = errorsSink.Errors; return(false); } // 3- Validate if (!validator.Validate(parsedPattern, errorsSink)) { compiledPattern = null; errors = errorsSink.Errors; return(false); } // 4- Build regex regex = PatternRegexBuilder.Build(parsedPattern, type == PatternType.ExactMatch); // 5- Get variable names variables = VariableNamesExtractor.Extract(parsedPattern); } compiledPattern = new CompiledPattern( pattern, type, regex, variables); errors = null; return(true); }
internal CompiledPatternInfo(CompiledPattern compiledPattern) { CompiledPattern = compiledPattern; }