using System.Collections.Generic;
using System.Linq;
namespace BinaryObjectScanner.Matching
{
///
/// Path matching criteria
///
public class PathMatch : IMatch
{
///
/// String to match
///
public string Needle { get; set; }
///
/// Match exact casing instead of invariant
///
public bool MatchExact { get; set; }
///
/// Match that values end with the needle and not just contains
///
public bool UseEndsWith { get; set; }
///
/// Constructor
///
/// String representing the search
/// True to match exact casing, false otherwise
/// True to match the end only, false for all contents
public PathMatch(string needle, bool matchExact = false, bool useEndsWith = false)
{
Needle = needle;
MatchExact = matchExact;
UseEndsWith = useEndsWith;
}
#region Matching
///
/// Get if this match can be found in a stack
///
/// List of strings to search for the given content
/// Tuple of success and matched item
public (bool, string) Match(IEnumerable stack)
{
// If either array is null or empty, we can't do anything
if (stack == null || !stack.Any() || Needle == null || Needle.Length == 0)
return (false, null);
// Preprocess the needle, if necessary
string procNeedle = MatchExact ? Needle : Needle.ToLowerInvariant();
foreach (string stackItem in stack)
{
// Preprocess the stack item, if necessary
string procStackItem = MatchExact ? stackItem : stackItem.ToLowerInvariant();
if (UseEndsWith && procStackItem.EndsWith(procNeedle))
return (true, stackItem);
else if (!UseEndsWith && procStackItem.Contains(procNeedle))
return (true, stackItem);
}
return (false, null);
}
#endregion
}
}