/// <summary> /// Perform a string comparison or a regex match and returns first found token index and length /// </summary> /// <param name="text"></param> /// <param name="filterText"></param> /// <returns>index and length of found token in text. (-1, N) if token is not found</returns> public static Tuple <int, int> GetMatchTextIndex(string text, string filterText) { if (text is null) { throw new ArgumentNullException(nameof(text)); } if (string.IsNullOrEmpty(filterText)) { throw new ArgumentException(@"must be non empty", nameof(filterText)); } if (Configs.Regex) { var regex = RegexObjects.GetRegexObject(filterText, () => new Regex(filterText, RegexOptions.Compiled)); var matches = regex.Matches(text); return(matches.Count > 0 ? new Tuple <int, int>(matches[0].Index, matches[0].Length) : new Tuple <int, int>(-1, 0)); } return(new Tuple <int, int>( text.IndexOf(filterText, 0, Configs.ComparisonOptions), filterText.Length)); }
public static bool IsMatchMask(string path, string mask) { if (string.IsNullOrEmpty(mask)) { return(true); } if (path is null) { throw new ArgumentNullException(nameof(path)); } if (path.Length < mask.Length) { return(false); } var normalizedMask = mask.Replace('\\', '/').ToUpper(CultureInfo.InvariantCulture); var normalizedPath = path.Replace('\\', '/').ToUpper(CultureInfo.InvariantCulture); // regex is slow, tries string comparison first if (normalizedMask.IndexOfAny(new[] { '*', '?' }) == -1) { return(normalizedPath.IndexOf(normalizedMask, StringComparison.InvariantCulture) == normalizedPath.Length - normalizedMask.Length); } var rg = RegexObjects.GetRegexObject(normalizedMask, () => new Regex(normalizedMask .Replace(@".", @"[.]", StringComparison.OrdinalIgnoreCase) .Replace(@"*", @".*", StringComparison.OrdinalIgnoreCase) .Replace(@"?", @".", StringComparison.OrdinalIgnoreCase) , RegexOptions.Compiled)); var matches = rg.Matches(normalizedPath); // check that no chars excepting wildcards remains after last math if (matches.Count <= 0) { return(false); } var lastMatch = matches[^ 1];