refactored SearchExpression

This commit is contained in:
2026-09-15 13:18:09 +02:00
parent 1d53f6930a
commit 5b1b8f4ce5
21 changed files with 509 additions and 216 deletions
@@ -0,0 +1,37 @@
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);
}
}