using System.Collections.Generic; using System.Linq; using DotNet.Globbing; namespace Kavita.Common.Helpers; /** * Matches against strings using Glob syntax */ public class GlobMatcher { private readonly IList _includes = new List(); private readonly IList _excludes = new List(); public void AddInclude(string pattern) { _includes.Add(Glob.Parse(pattern)); } public void AddExclude(string pattern) { _excludes.Add(Glob.Parse(pattern)); } public bool ExcludeMatches(string file) { // NOTE: Glob.IsMatch() returns the opposite of what you'd expect return _excludes.Any(p => p.IsMatch(file)); } /// /// /// /// /// /// True if any public bool IsMatch(string file, bool mustMatchIncludes = false) { // NOTE: Glob.IsMatch() returns the opposite of what you'd expect if (_excludes.Any(p => p.IsMatch(file))) return true; if (mustMatchIncludes) { return _includes.Any(p => p.IsMatch(file)); } return false; } public void Merge(GlobMatcher matcher) { if (matcher == null) return; foreach (var glob in matcher._excludes) { _excludes.Add(glob); } foreach (var glob in matcher._includes) { _includes.Add(glob); } } }