Files
BinaryObjectScanner/BurnOutSharp/Matching/PathMatch.cs

71 lines
2.3 KiB
C#
Raw Normal View History

2021-03-22 01:11:59 -07:00
using System.Collections.Generic;
2021-03-22 22:06:55 -07:00
using System.Linq;
2021-03-22 01:11:59 -07:00
namespace BurnOutSharp.Matching
{
/// <summary>
/// Path matching criteria
/// </summary>
public class PathMatch : IMatch<string>
2021-03-22 01:11:59 -07:00
{
/// <summary>
/// String to match
/// </summary>
public string Needle { get; set; }
2021-03-22 01:26:33 -07:00
/// <summary>
/// Match exact casing instead of invariant
/// </summary>
public bool MatchExact { get; set; }
2021-03-22 01:18:49 -07:00
/// <summary>
/// Match that values end with the needle and not just contains
/// </summary>
public bool UseEndsWith { get; set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="needle">String representing the search</param>
/// <param name="matchExact">True to match exact casing, false otherwise</param>
/// <param name="useEndsWith">True to match the end only, false for all contents</param>
2021-03-22 01:26:33 -07:00
public PathMatch(string needle, bool matchExact = false, bool useEndsWith = false)
2021-03-22 01:11:59 -07:00
{
Needle = needle;
2021-03-22 01:26:33 -07:00
MatchExact = matchExact;
2021-03-22 01:18:49 -07:00
UseEndsWith = useEndsWith;
2021-03-22 01:11:59 -07:00
}
#region Matching
/// <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>Tuple of success and matched item</returns>
2021-03-22 22:06:55 -07:00
public (bool, string) Match(IEnumerable<string> stack)
2021-03-22 01:11:59 -07:00
{
// If either array is null or empty, we can't do anything
2021-03-22 22:06:55 -07:00
if (stack == null || !stack.Any() || Needle == null || Needle.Length == 0)
2021-03-22 01:11:59 -07:00
return (false, null);
2021-03-22 01:26:33 -07:00
// Preprocess the needle, if necessary
string procNeedle = MatchExact ? Needle : Needle.ToLowerInvariant();
2021-03-22 01:11:59 -07:00
foreach (string stackItem in stack)
{
// Preprocess the stack item, if necessary
2021-03-22 01:26:33 -07:00
string procStackItem = MatchExact ? stackItem : stackItem.ToLowerInvariant();
if (UseEndsWith && procStackItem.EndsWith(procNeedle))
2021-03-22 01:18:49 -07:00
return (true, stackItem);
2021-03-22 01:26:33 -07:00
else if (!UseEndsWith && procStackItem.Contains(procNeedle))
2021-03-22 01:11:59 -07:00
return (true, stackItem);
}
return (false, null);
}
#endregion
}
}