Files
SabreTools.IO/SabreTools.Matching/PathMatch.cs

96 lines
3.1 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
using System.IO;
2026-03-18 12:50:31 -04:00
namespace SabreTools.Matching
{
/// <summary>
/// Path matching criteria
/// </summary>
public class PathMatch : IMatch<string>
{
/// <summary>
/// String to match
/// </summary>
public string Needle { get; }
/// <summary>
/// Match casing instead of invariant
/// </summary>
private readonly bool _matchCase;
/// <summary>
/// Match that values end with the needle and not just contains
/// </summary>
private readonly bool _useEndsWith;
/// <summary>
/// Constructor
/// </summary>
/// <param name="needle">String representing the search</param>
/// <param name="matchCase">True to match exact casing, false otherwise</param>
/// <param name="useEndsWith">True to match the end only, false for contains</param>
2026-03-10 16:57:22 -04:00
/// <exception cref="InvalidDataException">
/// Thrown if <paramref name="needle"/> has a length of 0.
/// </exception>
public PathMatch(string needle, bool matchCase = false, bool useEndsWith = false)
{
// Validate the inputs
if (needle.Length == 0)
throw new InvalidDataException(nameof(needle));
Needle = needle;
_matchCase = matchCase;
_useEndsWith = useEndsWith;
}
#region Conversion
/// <summary>
/// Allow conversion from string to PathMatch
/// </summary>
2025-11-13 08:56:30 -05:00
public static implicit operator PathMatch(string needle) => new(needle);
#endregion
#region Matching
/// <summary>
/// Get if this match can be found in a stack
/// </summary>
/// <param name="stack">Array of strings to search for the given content</param>
/// <returns>Matched item on success, null on error</returns>
public string? Match(string[]? stack)
2026-01-25 17:04:11 -05:00
=> Match(stack is null ? null : new List<string>(stack));
/// <summary>
/// Get if this match can be found in a stack
/// </summary>
/// <param name="stack">List of strings to search for the given content</param>
/// <returns>Matched item on success, null on error</returns>
public string? Match(List<string>? stack)
{
// If either set is null or empty
2026-01-25 17:04:11 -05:00
if (stack is null || stack.Count == 0 || Needle.Length == 0)
return null;
// Preprocess the needle, if necessary
string procNeedle = _matchCase ? Needle : Needle.ToLowerInvariant();
foreach (string stackItem in stack)
{
// Preprocess the stack item, if necessary
string procStackItem = _matchCase ? stackItem : stackItem.ToLowerInvariant();
if (_useEndsWith && procStackItem.EndsWith(procNeedle))
return stackItem;
else if (!_useEndsWith && procStackItem.Contains(procNeedle))
return stackItem;
}
return null;
}
#endregion
}
}