/// <summary> /// Converts <paramref name="wildcardPattern"/> into a DOS wildcard. /// </summary> internal static string Parse(WildcardPattern wildcardPattern) { var parser = new WildcardPatternToDosWildcardParser(); WildcardPatternParser.Parse(wildcardPattern, parser); return(parser._result.ToString()); }
public static void Parse(WildcardPattern pattern, WildcardPatternParser parser) { parser.BeginWildcardPattern(pattern); bool flag = false; bool flag2 = false; bool flag3 = false; StringBuilder builder = null; StringBuilder builder2 = null; foreach (char ch in pattern.Pattern) { if (flag3) { if (((ch == ']') && !flag2) && !flag) { flag3 = false; parser.AppendBracketExpression(builder.ToString(), builder2.ToString(), pattern.Pattern); builder = null; builder2 = null; } else if ((ch != '`') || flag) { builder.Append(ch); builder2.Append(((ch == '-') && !flag) ? '-' : ' '); } flag2 = false; } else if ((ch == '*') && !flag) { parser.AppendAsterix(); } else if ((ch == '?') && !flag) { parser.AppendQuestionMark(); } else if ((ch == '[') && !flag) { flag3 = true; builder = new StringBuilder(); builder2 = new StringBuilder(); flag2 = true; } else if ((ch != '`') || flag) { parser.AppendLiteralCharacter(ch); } flag = (ch == '`') && !flag; } if (flag3) { throw NewWildcardPatternException(pattern.Pattern); } if (flag && !pattern.Pattern.Equals("`", StringComparison.Ordinal)) { parser.AppendLiteralCharacter(pattern.Pattern[pattern.Pattern.Length - 1]); } parser.EndWildcardPattern(); }
public static WildcardPatternMatcher.PatternElement[] Parse(WildcardPattern pattern, WildcardPatternMatcher.CharacterNormalizer characterNormalizer) { WildcardPatternMatcher.MyWildcardPatternParser parser = new WildcardPatternMatcher.MyWildcardPatternParser { _characterNormalizer = characterNormalizer, _regexOptions = WildcardPatternToRegexParser.TranslateWildcardOptionsIntoRegexOptions(pattern.Options) }; WildcardPatternParser.Parse(pattern, parser); return(parser._patternElements.ToArray()); }
/// <summary> /// Parses a <paramref name="wildcardPattern"/> into a <see cref="Regex"/> /// </summary> /// <param name="wildcardPattern">Wildcard pattern to parse.</param> /// <returns>Regular expression equivalent to <paramref name="wildcardPattern"/></returns> public static Regex Parse(WildcardPattern wildcardPattern) { WildcardPatternToRegexParser parser = new WildcardPatternToRegexParser(); WildcardPatternParser.Parse(wildcardPattern, parser); try { return(new Regex(parser._regexPattern.ToString(), parser._regexOptions)); } catch (ArgumentException) { throw WildcardPatternParser.NewWildcardPatternException(wildcardPattern.Pattern); } }
/// <summary> /// Parses <paramref name="pattern"/>, calling appropriate overloads /// in <paramref name="parser"/> /// </summary> /// <param name="pattern">Pattern to parse.</param> /// <param name="parser">Parser to call back.</param> public static void Parse(WildcardPattern pattern, WildcardPatternParser parser) { parser.BeginWildcardPattern(pattern); bool previousCharacterIsAnEscape = false; bool previousCharacterStartedBracketExpression = false; bool insideCharacterRange = false; StringBuilder characterRangeContents = null; StringBuilder characterRangeOperators = null; foreach (char c in pattern.Pattern) { if (insideCharacterRange) { if (c == ']' && !previousCharacterStartedBracketExpression && !previousCharacterIsAnEscape) { // An unescaped closing square bracket closes the character set. In other // words, there are no nested square bracket expressions // This is different than the POSIX spec // (at https://www.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html), // but we are keeping this behavior for back-compatibility. insideCharacterRange = false; parser.AppendBracketExpression(characterRangeContents.ToString(), characterRangeOperators.ToString(), pattern.Pattern); characterRangeContents = null; characterRangeOperators = null; } else if (c != '`' || previousCharacterIsAnEscape) { characterRangeContents.Append(c); characterRangeOperators.Append((c == '-') && !previousCharacterIsAnEscape ? '-' : ' '); } previousCharacterStartedBracketExpression = false; } else { if (c == '*' && !previousCharacterIsAnEscape) { parser.AppendAsterix(); } else if (c == '?' && !previousCharacterIsAnEscape) { parser.AppendQuestionMark(); } else if (c == '[' && !previousCharacterIsAnEscape) { insideCharacterRange = true; characterRangeContents = new StringBuilder(); characterRangeOperators = new StringBuilder(); previousCharacterStartedBracketExpression = true; } else if (c != '`' || previousCharacterIsAnEscape) { parser.AppendLiteralCharacter(c); } } previousCharacterIsAnEscape = (c == '`') && (!previousCharacterIsAnEscape); } if (insideCharacterRange) { throw NewWildcardPatternException(pattern.Pattern); } if (previousCharacterIsAnEscape) { if (!pattern.Pattern.Equals("`", StringComparison.Ordinal)) // Win7 backcompatibility requires treating '`' pattern as '' pattern { parser.AppendLiteralCharacter(pattern.Pattern[pattern.Pattern.Length - 1]); } } parser.EndWildcardPattern(); }
/// <summary> /// Parses <paramref name="pattern"/>, calling appropriate overloads /// in <paramref name="parser"/> /// </summary> /// <param name="pattern">Pattern to parse</param> /// <param name="parser">Parser to call back</param> public static void Parse(WildcardPattern pattern, WildcardPatternParser parser) { parser.BeginWildcardPattern(pattern); bool previousCharacterIsAnEscape = false; bool previousCharacterStartedBracketExpression = false; bool insideCharacterRange = false; StringBuilder characterRangeContents = null; StringBuilder characterRangeOperators = null; foreach (char c in pattern.Pattern) { if (insideCharacterRange) { if (c == ']' && !previousCharacterStartedBracketExpression && !previousCharacterIsAnEscape) { // An unescaped closing square bracket closes the character set. In other // words, there are no nested square bracket expressions // This is different than the POSIX spec // (at http://www.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html), // but we are keeping this behavior for back-compatibility. insideCharacterRange = false; parser.AppendBracketExpression(characterRangeContents.ToString(), characterRangeOperators.ToString(), pattern.Pattern); characterRangeContents = null; characterRangeOperators = null; } else if (c != '`' || previousCharacterIsAnEscape) { characterRangeContents.Append(c); characterRangeOperators.Append((c == '-') && !previousCharacterIsAnEscape ? '-' : ' '); } previousCharacterStartedBracketExpression = false; } else { if (c == '*' && !previousCharacterIsAnEscape) { parser.AppendAsterix(); } else if (c == '?' && !previousCharacterIsAnEscape) { parser.AppendQuestionMark(); } else if (c == '[' && !previousCharacterIsAnEscape) { insideCharacterRange = true; characterRangeContents = new StringBuilder(); characterRangeOperators = new StringBuilder(); previousCharacterStartedBracketExpression = true; } else if (c != '`' || previousCharacterIsAnEscape) { parser.AppendLiteralCharacter(c); } } previousCharacterIsAnEscape = (c == '`') && (!previousCharacterIsAnEscape); } if (insideCharacterRange) { throw NewWildcardPatternException(pattern.Pattern); } if (previousCharacterIsAnEscape) { if (!pattern.Pattern.Equals("`", StringComparison.Ordinal)) // Win7 backcompatibility requires treating '`' pattern as '' pattern { parser.AppendLiteralCharacter(pattern.Pattern[pattern.Pattern.Length - 1]); } } parser.EndWildcardPattern(); }