< Summary

Information
Class: System.Text.SimpleRegex
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\Common\src\System\Text\SimpleRegex.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 72
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
IsMatchWithStarWildcard(...)0%18180%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Text\SimpleRegex.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5
 6namespace 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)
 021        {
 022            int inputPos = 0, inputPosSaved = -1;
 023            int patternPos = 0, patternPosSaved = -1;
 24
 25            // Loop through each character in the input.
 026            while (inputPos < input.Length)
 027            {
 028                if (patternPos < pattern.Length && pattern[patternPos] == '*')
 029                {
 30                    // If we're currently positioned on a wildcard in the pattern,
 31                    // move past it and remember where we are to backtrack to.
 032                    inputPosSaved = inputPos;
 033                    patternPosSaved = ++patternPos;
 034                }
 035                else if (patternPos < pattern.Length &&
 036                    (pattern[patternPos] == input[inputPos] ||
 037                     char.ToUpperInvariant(pattern[patternPos]) == char.ToUpperInvariant(input[inputPos])))
 038                {
 39                    // If the characters in the pattern and the input match, advance both.
 040                    inputPos++;
 041                    patternPos++;
 042                }
 043                else if (patternPosSaved == -1)
 044                {
 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.
 047                    return false;
 48                }
 49                else
 050                {
 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.
 053                    inputPos = ++inputPosSaved;
 054                    patternPos = patternPosSaved;
 055                }
 056            }
 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).
 061            while (patternPos < pattern.Length && pattern[patternPos] == '*')
 062            {
 063                patternPos++;
 064            }
 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.
 068            Debug.Assert(patternPos <= pattern.Length);
 069            return patternPos == pattern.Length;
 070        }
 71    }
 72}