Files
ParadoxSaveParser/ParadoxSaveParser.Lib/SearchExpression/ExactMatchExpression.cs
T
2026-09-15 13:18:09 +02:00

37 lines
1.1 KiB
C#

namespace ParadoxSaveParser.Lib;
/// <summary>A literal key, matched byte for byte.</summary>
internal sealed class ExactMatchExpression : ISearchExpression
{
public string Key { get; }
// the key as it appears in the save, so matching needs no encoding at all
public byte[] KeyBytes { get; private set; }
public ISearchExpression? Next { get; }
public ExactMatchExpression(string key, Encoding encoding, ISearchExpression? next)
{
Key = key;
Next = next;
KeyBytes = encoding.GetBytes(key);
}
public bool DoesMatch(MatchCandidate candidate, out ISearchExpression? nextSearchExpression)
{
// comparing bytes keeps the candidate's key from becoming a string
if (candidate.Key.SequenceEqual(KeyBytes))
{
nextSearchExpression = Next;
return true;
}
nextSearchExpression = null;
return false;
}
public void ReEncode(Encoding encoding)
{
KeyBytes = encoding.GetBytes(Key);
Next?.ReEncode(encoding);
}
}