Skip to content

Commit

Permalink
Changed wild card pattern class back to original
Browse files Browse the repository at this point in the history
  • Loading branch information
twoody0 committed Dec 11, 2024
1 parent 8c220e5 commit bdba3bc
Showing 1 changed file with 85 additions and 39 deletions.
124 changes: 85 additions & 39 deletions Assignment.Tests/WildCardPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ public enum WildcardOptions
/// <summary>
/// Represents a wildcard pattern.
/// </summary>
/// <remarks>
/// Initializes an instance of the WildcardPattern class for
/// the specified wildcard pattern expression, with options
/// that modify the pattern.
/// </remarks>
/// <param name="pattern">The wildcard pattern to match.</param>
/// <param name="options">Wildcard options</param>
/// <returns>The constructed WildcardPattern object</returns>
/// <remarks> if wildCardType == None, the pattern does not have wild cards </remarks>
public sealed partial class WildcardPattern(string pattern, WildcardOptions options)
public sealed partial class WildcardPattern
{
//
// char that escapes special chars
Expand All @@ -67,12 +58,12 @@ public sealed partial class WildcardPattern(string pattern, WildcardOptions opti
//
// wildcard pattern
//
internal string Pattern { get; } = pattern ?? throw new ArgumentNullException(nameof(pattern));
internal string Pattern { get; }

//
// options that control match behavior
//
internal WildcardOptions Options { get; } = options;
internal WildcardOptions Options { get; } = WildcardOptions.None;

/// <summary>
/// wildcard pattern converted to regex pattern.
Expand All @@ -81,7 +72,7 @@ internal string PatternConvertedToRegex
{
get
{
Regex patternRegex = WildcardPatternToRegexParser.Parse(this);
var patternRegex = WildcardPatternToRegexParser.Parse(this);
return patternRegex.ToString();
}
}
Expand Down Expand Up @@ -110,6 +101,21 @@ public WildcardPattern(string pattern, char escapeCharacter) :
this(pattern, escapeCharacter, WildcardOptions.None)
{ }

/// <summary>
/// Initializes an instance of the WildcardPattern class for
/// the specified wildcard pattern expression, with options
/// that modify the pattern.
/// </summary>
/// <param name="pattern">The wildcard pattern to match.</param>
/// <param name="options">Wildcard options</param>
/// <returns>The constructed WildcardPattern object</returns>
/// <remarks> if wildCardType == None, the pattern does not have wild cards </remarks>
public WildcardPattern(string pattern, WildcardOptions options)
{
Pattern = pattern ?? throw new ArgumentNullException(nameof(pattern));
Options = options;
}

/// <summary>
/// Initializes an instance of the WildcardPattern class for
/// the specified wildcard pattern expression, with options
Expand Down Expand Up @@ -160,7 +166,10 @@ public static WildcardPattern Get(string pattern, WildcardOptions options)
{
ArgumentNullException.ThrowIfNull(pattern);

return pattern.Length == 1 && pattern[0] == '*' ? s_matchAllIgnoreCasePattern : new WildcardPattern(pattern, options);
if (pattern.Length == 1 && pattern[0] == '*')
return s_matchAllIgnoreCasePattern;

return new WildcardPattern(pattern, options);
}

/// <summary>
Expand All @@ -181,7 +190,7 @@ private void Init()
}
else
{
WildcardPatternMatcher matcher = new(this);
var matcher = new WildcardPatternMatcher(this);
_isMatch = matcher.IsMatch;
}
}
Expand Down Expand Up @@ -235,7 +244,14 @@ internal static string Escape(string pattern,
temp[tempIndex++] = ch;
}

return tempIndex > 0 ? new string(temp, 0, tempIndex) : string.Empty;
if (tempIndex > 0)
{
return new string(temp, 0, tempIndex);
}
else
{
return String.Empty;
}

#pragma warning restore 56506
}
Expand All @@ -251,7 +267,7 @@ internal static string Escape(string pattern,
public static string Escape(
string pattern, char escapeCharacter)
{
return Escape(pattern, [], escapeCharacter);
return Escape(pattern, Array.Empty<char>(), escapeCharacter);
}

/// <summary>
Expand Down Expand Up @@ -356,7 +372,14 @@ public static string Unescape(
temp[tempIndex++] = escapeCharacter;
}

return tempIndex > 0 ? new string(temp, 0, tempIndex) : string.Empty;
if (tempIndex > 0)
{
return new string(temp, 0, tempIndex);
}
else
{
return String.Empty;
}
} // Unescape

public static bool IsWildcardChar(char ch)
Expand All @@ -374,12 +397,12 @@ public static bool IsWildcardChar(char ch)
/// <returns>The normalized input.</returns>
public static string NormalizeLineEndings(string input, bool trimTrailingNewline = false)
{
// https://stackoverflow.com/questions/140926/normalize-newlines-in-c-sharp
// Normalize all newlines to Environment.NewLine
input = NormalizeNewLines().Replace(input, Environment.NewLine);

if (trimTrailingNewline && input.EndsWith(Environment.NewLine, StringComparison.OrdinalIgnoreCase))
if (trimTrailingNewline && input.EndsWith(Environment.NewLine, StringComparison.Ordinal)) // Fixed here
{
input = input[..^Environment.NewLine.Length];
input = input.Substring(0, input.Length - Environment.NewLine.Length);
}

return input;
Expand Down Expand Up @@ -593,7 +616,7 @@ public static void Parse(
{
if (!pattern.Pattern.Equals($"{pattern.EscapeCharacter}", StringComparison.Ordinal)) // Win7 backcompatibility requires treating '`' pattern as '' pattern when this code was used with PowerShell.
{
parser.AppendLiteralCharacter(pattern.Pattern[^1]);
parser.AppendLiteralCharacter(pattern.Pattern[pattern.Pattern.Length - 1]);
}
}

Expand All @@ -603,7 +626,7 @@ public static void Parse(
internal static Exception NewWildcardPatternException(string invalidPattern)
{
return new ArgumentException(
$"The wildcard pattern, '{invalidPattern}', is invalid.");
$"The wildcard pattern, '{invalidPattern}', is invalid.", nameof(invalidPattern));
}
};

Expand Down Expand Up @@ -822,12 +845,12 @@ internal bool IsMatch(string str)
// - Wikipedia calls this algorithm the "NFA" algorithm at
// http://en.wikipedia.org/wiki/Regular_expression#Implementations_and_running_times

PatternPositionsVisitor patternPositionsForCurrentStringPosition =
new(_patternElements.Length);
var patternPositionsForCurrentStringPosition =
new PatternPositionsVisitor(_patternElements.Length);
patternPositionsForCurrentStringPosition.Add(0);

PatternPositionsVisitor patternPositionsForNextStringPosition =
new(_patternElements.Length);
var patternPositionsForNextStringPosition =
new PatternPositionsVisitor(_patternElements.Length);

for (int currentStringPosition = 0;
currentStringPosition < str.Length;
Expand All @@ -848,7 +871,9 @@ internal bool IsMatch(string str)

// swap patternPositionsForCurrentStringPosition
// with patternPositionsForNextStringPosition
(patternPositionsForNextStringPosition, patternPositionsForCurrentStringPosition) = (patternPositionsForCurrentStringPosition, patternPositionsForNextStringPosition);
var tmp = patternPositionsForCurrentStringPosition;
patternPositionsForCurrentStringPosition = patternPositionsForNextStringPosition;
patternPositionsForNextStringPosition = tmp;
}

while (patternPositionsForCurrentStringPosition.MoveNext(out int patternPosition2))
Expand Down Expand Up @@ -927,7 +952,13 @@ public void Add(int patternPosition)
}
}

public bool ReachedEndOfPattern => _isPatternPositionVisitedMarker[_lengthOfPattern] >= this.StringPosition;
public bool ReachedEndOfPattern
{
get
{
return _isPatternPositionVisitedMarker[_lengthOfPattern] >= this.StringPosition;
}
}

// non-virtual MoveNext is more performant
// than implementing IEnumerable / virtual MoveNext
Expand Down Expand Up @@ -983,9 +1014,14 @@ public override void ProcessEndOfString(
}
}

private sealed class LiteralCharacterElement(char literalCharacter) : QuestionMarkElement
private sealed class LiteralCharacterElement : QuestionMarkElement
{
private readonly char _literalCharacter = literalCharacter;
private readonly char _literalCharacter;

public LiteralCharacterElement(char literalCharacter)
{
_literalCharacter = literalCharacter;
}

public override void ProcessStringCharacter(
char currentStringCharacter,
Expand All @@ -1004,9 +1040,14 @@ public override void ProcessStringCharacter(
}
}

private sealed class BracketExpressionElement(Regex regex) : QuestionMarkElement
private sealed class BracketExpressionElement : QuestionMarkElement
{
private readonly Regex _Regex = regex ?? throw new ArgumentNullException(nameof(regex));
private readonly Regex _Regex;

public BracketExpressionElement(Regex regex)
{
_Regex = regex ?? throw new ArgumentNullException(nameof(regex));
}

public override void ProcessStringCharacter(
char currentStringCharacter,
Expand Down Expand Up @@ -1049,7 +1090,7 @@ public override void ProcessEndOfString(

private sealed class MyWildcardPatternParser : WildcardPatternParser
{
private readonly List<PatternElement> _patternElements = [];
private readonly List<PatternElement> _patternElements = new();
private CharacterNormalizer _characterNormalizer;
private RegexOptions _regexOptions;
private StringBuilder _bracketExpressionBuilder;
Expand All @@ -1058,13 +1099,13 @@ public static PatternElement[] Parse(
WildcardPattern pattern,
CharacterNormalizer characterNormalizer)
{
MyWildcardPatternParser parser = new()
var parser = new MyWildcardPatternParser
{
_characterNormalizer = characterNormalizer,
_regexOptions = WildcardPatternToRegexParser.TranslateWildcardOptionsIntoRegexOptions(pattern.Options),
};
WildcardPatternParser.Parse(pattern, parser);
return [.. parser._patternElements];
return parser._patternElements.ToArray();
}

protected override void AppendLiteralCharacter(char c)
Expand Down Expand Up @@ -1114,7 +1155,7 @@ protected override void EndBracketExpression()
}
}

private readonly struct CharacterNormalizer
private struct CharacterNormalizer
{
private readonly CultureInfo _cultureInfo;
private readonly bool _caseInsensitive;
Expand All @@ -1137,7 +1178,12 @@ public CharacterNormalizer(WildcardOptions options)

public char Normalize(char x)
{
return _caseInsensitive ? _cultureInfo.TextInfo.ToLower(x) : x;
if (_caseInsensitive)
{
return _cultureInfo.TextInfo.ToLower(x);
}

return x;
}
}
}
Expand Down Expand Up @@ -1186,7 +1232,7 @@ protected override void EndBracketExpression()
/// </summary>
internal static string Parse(WildcardPattern wildcardPattern)
{
WildcardPatternToDosWildcardParser parser = new();
var parser = new WildcardPatternToDosWildcardParser();
WildcardPatternParser.Parse(wildcardPattern, parser);
return parser._result.ToString();
}
Expand Down

0 comments on commit bdba3bc

Please sign in to comment.