public override bool Match(string input, GlobOptions options) { throw new NotSupportedException(); }
public override bool Match(string input, GlobOptions options) { return(regex.Value.IsMatch(input)); }
public override string RegexSource(GlobOptions options) { return(options.NoGlobStar ? star : options.Dot?twoStarDot : twoStarNoDot); }
public MagicItem(string source, GlobOptions options) { Source = source; regex = new Lazy <Regex>(() => new Regex("^" + source + "$", options.RegexOptions)); }
public override string RegexSource(GlobOptions options) { return(Source); }
public override string RegexSource(GlobOptions options) { return(Regex.Escape(Source)); }
public override bool Match(string input, GlobOptions options) { return(input.Equals(Source, options.NoCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)); }
public abstract bool Match(string input, GlobOptions options);
public abstract string RegexSource(GlobOptions options);
// Brace expansion: // a{b,c}d -> abd acd // a{b,}c -> abc ac // a{0..3}d -> a0d a1d a2d a3d // a{b,c{d,e}f}g -> abg acdfg acefg // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg // // Invalid sets are not expanded. // a{2..}b -> a{2..}b // a{b}c -> a{b}c ///<summary>Expands all brace ranges in a pattern, returning a sequence containing every possible combination.</summary> public static IEnumerable <string> BraceExpand(string pattern, GlobOptions options) { if (options.NoBrace || !hasBraces.IsMatch(pattern)) { // shortcut. no need to expand. return(new[] { pattern }); } bool escaping = false; int i; // examples and comments refer to this crazy pattern: // a{b,c{d,e},{f,g}h}x{y,z} // expected: // abxy // abxz // acdxy // acdxz // acexy // acexz // afhxy // afhxz // aghxy // aghxz // everything before the first \{ is just a prefix. // So, we pluck that off, and work with the rest, // and then prepend it to everything we find. if (pattern[0] != '{') { // console.error(pattern) string prefix = null; for (i = 0; i < pattern.Length; i++) { var c = pattern[i]; // console.error(i, c) if (c == '\\') { escaping = !escaping; } else if (c == '{' && !escaping) { prefix = pattern.Substring(0, i); break; } } // actually no sets, all { were escaped. if (prefix == null) { // console.error("no sets") return(new[] { pattern }); } return(BraceExpand(pattern.Substring(i), options).Select(t => prefix + t)); } // now we have something like: // {b,c{d,e},{f,g}h}x{y,z} // walk through the set, expanding each part, until // the set ends. then, we'll expand the suffix. // If the set only has a single member, then'll put the {} back // first, handle numeric sets, since they're easier var numset = numericSet.Match(pattern); if (numset.Success) { // console.error("numset", numset[1], numset[2]) var suf = BraceExpand(pattern.Substring(numset.Length), options).ToList(); int start = int.Parse(numset.Groups[1].Value), end = int.Parse(numset.Groups[2].Value), inc = start > end ? -1 : 1; var retVal = new List <string>(); for (var w = start; w != (end + inc); w += inc) { // append all the suffixes for (var ii = 0; ii < suf.Count; ii++) { retVal.Add(w.ToString() + suf[ii]); } } return(retVal); } // ok, walk through the set // We hope, somewhat optimistically, that there // will be a } at the end. // If the closing brace isn't found, then the pattern is // interpreted as braceExpand("\\" + pattern) so that // the leading \{ will be interpreted literally. i = 1; // skip the \{ int depth = 1; var set = new List <string>(); string member = ""; for (i = 1; i < pattern.Length && depth > 0; i++) { var c = pattern[i]; // console.error("", i, c) if (escaping) { escaping = false; member += "\\" + c; } else { switch (c) { case '\\': escaping = true; continue; case '{': depth++; member += "{"; continue; case '}': depth--; // if this closes the actual set, then we're done if (depth == 0) { set.Add(member); member = ""; // pluck off the close-brace break; } else { member += c; continue; } case ',': if (depth == 1) { set.Add(member); member = ""; } else { member += c; } continue; default: member += c; continue; } // switch } // else } // for // now we've either finished the set, and the suffix is // pattern.substr(i), or we have *not* closed the set, // and need to escape the leading brace if (depth != 0) { // console.error("didn't close", pattern) return(BraceExpand("\\" + pattern, options)); } // ["b", "c{d,e}","{f,g}h"] -> // ["b", "cd", "ce", "fh", "gh"] var addBraces = set.Count == 1; set = set.SelectMany(p => BraceExpand(p, options)).ToList(); if (addBraces) { set = set.Select(s => "{" + s + "}").ToList(); } // now attach the suffixes. // x{y,z} -> ["xy", "xz"] // console.error("set", set) // console.error("suffix", pattern.substr(i)) return(BraceExpand(pattern.Substring(i), options).SelectMany(s1 => set.Select(s2 => s2 + s1))); }
///<summary>Compiles a pattern into a single regular expression.</summary> public static Regex CreateRegex(string pattern, GlobOptions options = null) { return(new GlobHelper(pattern, options).MakeRegex()); }
///<summary>Filters a list of inputs against a single pattern.</summary> ///<remarks>This function reparses this input on each invocation. For performance, avoid this function and reuse a Minimatcher instance instead.</remarks> public static IEnumerable <string> Filter(IEnumerable <string> list, string pattern, GlobOptions options = null) { var mm = new GlobHelper(pattern, options); list = list.Where(mm.IsMatch); if (options != null && options.NoNull) { list = list.DefaultIfEmpty(pattern); } return(list); }