Example #1
0
        private static GlobMatcherOptions GetMatchOptionsFromItem(FileMappingItem item)
        {
            GlobMatcherOptions options = item?.CaseSensitive ?? false ? GlobMatcherOptions.None : GlobMatcherOptions.IgnoreCase;

            if (item?.AllowDotMatch ?? false)
            {
                options |= GlobMatcherOptions.AllowDotMatch;
            }
            if (!(item?.DisableEscape ?? false))
            {
                options |= GlobMatcherOptions.AllowEscape;
            }
            if (!(item?.DisableExpand ?? false))
            {
                options |= GlobMatcherOptions.AllowExpand;
            }
            if (!(item?.DisableGlobStar ?? false))
            {
                options |= GlobMatcherOptions.AllowGlobStar;
            }
            if (!(item?.DisableNegate ?? false))
            {
                options |= GlobMatcherOptions.AllowNegate;
            }
            return(options);
        }
Example #2
0
 public GlobMatcher(string pattern, GlobMatcherOptions options = DefaultOptions)
 {
     if (pattern == null) throw new ArgumentNullException(nameof(pattern));
     Options = options;
     Raw = pattern;
     _ignoreCase = Options.HasFlag(GlobMatcherOptions.IgnoreCase);
     _negate = ParseNegate(ref pattern, Options);
     _items = Compile(pattern).ToArray();
 }
Example #3
0
        /// <summary>
        /// {a,b}c => [ac, bc]
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        internal static string[] ExpandGroup(string pattern, GlobMatcherOptions options = DefaultOptions)
        {
            GlobUngrouper ungrouper     = new GlobUngrouper();
            bool          escaping      = false;
            bool          disableEscape = !options.HasFlag(GlobMatcherOptions.AllowEscape);

            foreach (char c in pattern)
            {
                if (escaping)
                {
                    if (c != ',' && c != '{' && c != '}')
                    {
                        ungrouper.AddChar('\\');
                    }
                    ungrouper.AddChar(c);
                    escaping = false;
                    continue;
                }
                else if (c == '\\' && !disableEscape)
                {
                    escaping = true;
                    continue;
                }
                switch (c)
                {
                case '{':
                    ungrouper.StartLevel();
                    break;

                case ',':
                    if (ungrouper.Level < 1)
                    {
                        ungrouper.AddChar(c);
                    }
                    else
                    {
                        ungrouper.AddGroup();
                    }
                    break;

                case '}':
                    if (ungrouper.Level < 1)
                    {
                        // Unbalanced closing bracket matches nothing
                        return(EmptyString);
                    }
                    ungrouper.FinishLevel();
                    break;

                default:
                    ungrouper.AddChar(c);
                    break;
                }
            }
            return(ungrouper.Flatten());
        }
Example #4
0
        public static IEnumerable<string> GetFiles(string cwd, IEnumerable<string> patterns, IEnumerable<string> excludePatterns, GlobMatcherOptions options = GlobMatcher.DefaultOptions)
        {
            // If there is no pattern, nothing will be included
            if (patterns == null) return Enumerable.Empty<string>();
            if (string.IsNullOrEmpty(cwd)) cwd = Directory.GetCurrentDirectory();

            IEnumerable<GlobMatcher> globList = patterns.Select(s => new GlobMatcher(s, options));
            IEnumerable<GlobMatcher> excludeGlobList = Enumerable.Empty<GlobMatcher>();
            if (excludePatterns != null)
                excludeGlobList = excludePatterns.Select(s => new GlobMatcher(s, options));
            return GetFilesCore(cwd, globList, excludeGlobList);
        }
Example #5
0
 public GlobMatcher(string pattern, GlobMatcherOptions options = DefaultOptions)
 {
     if (pattern == null)
     {
         throw new ArgumentNullException(nameof(pattern));
     }
     Options     = options;
     Raw         = pattern;
     _ignoreCase = Options.HasFlag(GlobMatcherOptions.IgnoreCase);
     _negate     = ParseNegate(ref pattern, Options);
     _items      = Compile(pattern).ToArray();
 }
        static void TestCase(string pattern, IList <string> expected, GlobMatcherOptions options = null, IEnumerable <string> input = null)
        {
            input = input ?? files;

            var filtered = input;
            var mm       = GlobMatcher.Create(pattern, options);

            filtered = filtered.Where(mm.IsMatch);
            if (options != null && options.NoNull)
            {
                filtered = filtered.DefaultIfEmpty(pattern);
            }

            filtered = filtered.OrderBy(s => s);

            Assert.AreEqual(
                String.Join(Environment.NewLine, expected.OrderBy(s => s)),
                String.Join(Environment.NewLine, filtered),
                "Failure from `" + pattern + "`"
                );
        }
Example #7
0
        internal static bool ParseNegate(ref string pattern, GlobMatcherOptions options = DefaultOptions)
        {
            if (!options.HasFlag(GlobMatcherOptions.AllowNegate))
            {
                return(false);
            }

            bool negate = false;
            int  i      = 0;

            while (i < pattern.Length && pattern[i] == NegateChar)
            {
                negate = !negate;
                i++;
            }

            if (i <= pattern.Length)
            {
                pattern = pattern.Substring(i);
            }

            return(negate);
        }
Example #8
0
        public static IEnumerable <string> GetFiles(string cwd, IEnumerable <string> patterns, IEnumerable <string> excludePatterns, GlobMatcherOptions options = GlobMatcher.DefaultOptions)
        {
            // If there is no pattern, nothing will be included
            if (patterns == null)
            {
                return(Enumerable.Empty <string>());
            }
            if (string.IsNullOrEmpty(cwd))
            {
                cwd = Directory.GetCurrentDirectory();
            }

            IEnumerable <GlobMatcher> globList        = patterns.Select(s => new GlobMatcher(s, options));
            IEnumerable <GlobMatcher> excludeGlobList = Enumerable.Empty <GlobMatcher>();

            if (excludePatterns != null)
            {
                excludeGlobList = excludePatterns.Select(s => new GlobMatcher(s, options));
            }
            return(GetFilesCore(cwd, globList, excludeGlobList));
        }
Example #9
0
        public static IEnumerable <string> GetFiles(string cwd, IEnumerable <string> patterns, IEnumerable <string> excludePatterns, GlobMatcherOptions options = GlobMatcher.DefaultOptions)
        {
            if (patterns == null)
            {
                return(Enumerable.Empty <string>());
            }

            if (string.IsNullOrEmpty(cwd))
            {
                cwd = Directory.GetCurrentDirectory();
            }
            var globArray        = patterns.Select(s => new GlobMatcher(s, options)).ToArray();
            var excludeGlobArray = excludePatterns == null ?
                                   new GlobMatcher[0] :
                                   excludePatterns.Select(s => new GlobMatcher(s, options)).ToArray();

            return(GetFilesCore(cwd, globArray, excludeGlobArray));
        }
Example #10
0
 /// <summary>
 /// {a,b}c => [ac, bc]
 /// </summary>
 /// <param name="pattern"></param>
 /// <returns></returns>
 internal static string[] ExpandGroup(string pattern, GlobMatcherOptions options = DefaultOptions)
 {
     GlobUngrouper ungrouper = new GlobUngrouper();
     bool escaping = false;
     bool disableEscape = !options.HasFlag(GlobMatcherOptions.AllowEscape);
     foreach (char c in pattern)
     {
         if (escaping)
         {
             if (c != ',' && c != '{' && c != '}')
             {
                 ungrouper.AddChar('\\');
             }
             ungrouper.AddChar(c);
             escaping = false;
             continue;
         }
         else if (c == '\\' && !disableEscape)
         {
             escaping = true;
             continue;
         }
         switch (c)
         {
             case '{':
                 ungrouper.StartLevel();
                 break;
             case ',':
                 if (ungrouper.Level < 1)
                 {
                     ungrouper.AddChar(c);
                 }
                 else
                 {
                     ungrouper.AddGroup();
                 }
                 break;
             case '}':
                 if (ungrouper.Level < 1)
                 {
                     // Unbalanced closing bracket matches nothing
                     return EmptyString;
                 }
                 ungrouper.FinishLevel();
                 break;
             default:
                 ungrouper.AddChar(c);
                 break;
         }
     }
     return ungrouper.Flatten();
 }
Example #11
0
        internal static bool ParseNegate(ref string pattern, GlobMatcherOptions options = DefaultOptions)
        {
            if (!options.HasFlag(GlobMatcherOptions.AllowNegate))
            {
                return false;
            }

            bool negate = false;
            int i = 0;
            while (i < pattern.Length && pattern[i] == NegateChar)
            {
                negate = !negate;
                i++;
            }

            if (i <= pattern.Length)
            {
                pattern = pattern.Substring(i);
            }

            return negate;
        }