| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Text |
| | | 7 | | { |
| | | 8 | | internal static class SimpleRegex |
| | | 9 | | { |
| | | 10 | | // Based on wildcmp written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A> |
| | | 11 | | // https://www.codeproject.com/Articles/1088/Wildcard-string-compare-globbing |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Perform a match between an input string and a pattern in which the only special character |
| | | 15 | | /// is an asterisk, which can map to zero or more of any character in the input. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="input">The input to match.</param> |
| | | 18 | | /// <param name="pattern">The pattern to match against.</param> |
| | | 19 | | /// <returns>true if the input matches the pattern; otherwise, false.</returns> |
| | | 20 | | public static bool IsMatchWithStarWildcard(ReadOnlySpan<char> input, ReadOnlySpan<char> pattern) |
| | 0 | 21 | | { |
| | 0 | 22 | | int inputPos = 0, inputPosSaved = -1; |
| | 0 | 23 | | int patternPos = 0, patternPosSaved = -1; |
| | | 24 | | |
| | | 25 | | // Loop through each character in the input. |
| | 0 | 26 | | while (inputPos < input.Length) |
| | 0 | 27 | | { |
| | 0 | 28 | | if (patternPos < pattern.Length && pattern[patternPos] == '*') |
| | 0 | 29 | | { |
| | | 30 | | // If we're currently positioned on a wildcard in the pattern, |
| | | 31 | | // move past it and remember where we are to backtrack to. |
| | 0 | 32 | | inputPosSaved = inputPos; |
| | 0 | 33 | | patternPosSaved = ++patternPos; |
| | 0 | 34 | | } |
| | 0 | 35 | | else if (patternPos < pattern.Length && |
| | 0 | 36 | | (pattern[patternPos] == input[inputPos] || |
| | 0 | 37 | | char.ToUpperInvariant(pattern[patternPos]) == char.ToUpperInvariant(input[inputPos]))) |
| | 0 | 38 | | { |
| | | 39 | | // If the characters in the pattern and the input match, advance both. |
| | 0 | 40 | | inputPos++; |
| | 0 | 41 | | patternPos++; |
| | 0 | 42 | | } |
| | 0 | 43 | | else if (patternPosSaved == -1) |
| | 0 | 44 | | { |
| | | 45 | | // If we're not on a wildcard and the current characters don't match and we don't have |
| | | 46 | | // any wildcard to backtrack to, this is not a match. |
| | 0 | 47 | | return false; |
| | | 48 | | } |
| | | 49 | | else |
| | 0 | 50 | | { |
| | | 51 | | // Otherwise, this is not a wildcard, the characters don't match, but we do have a |
| | | 52 | | // wildcard saved, so backtrack to it and use it to consume the next input character. |
| | 0 | 53 | | inputPos = ++inputPosSaved; |
| | 0 | 54 | | patternPos = patternPosSaved; |
| | 0 | 55 | | } |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | // We've reached the end of the input. Eat all wildcards immediately after where we are |
| | | 59 | | // in the pattern, as if they're at the end, they'll all just map to nothing (and if it |
| | | 60 | | // turns out there's something after them, eating them won't matter). |
| | 0 | 61 | | while (patternPos < pattern.Length && pattern[patternPos] == '*') |
| | 0 | 62 | | { |
| | 0 | 63 | | patternPos++; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | // If we are in fact at the end of the pattern, then we successfully matched. |
| | | 67 | | // If there's anything left, it's not a wildcard, so it doesn't match. |
| | 0 | 68 | | Debug.Assert(patternPos <= pattern.Length); |
| | 0 | 69 | | return patternPos == pattern.Length; |
| | 0 | 70 | | } |
| | | 71 | | } |
| | | 72 | | } |