using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using ParadoxSaveParser.Lib; namespace ParadoxSaveParser.Benchmarks; /// /// Checks whether the regex approach associates the right country tag with the right /// technology block. It cannot: 933 of the 2803 country blocks in the test save have no /// technology block at all, so a lazy "tag ... technology" pattern silently pairs a tag /// with a technology block belonging to a later country. /// public static class Mispairing { public static Dictionary FromParser(byte[] pdx) { var result = new Dictionary(); using var stream = new MemoryStream(pdx, false); var root = new SaveParserEU4(stream, SearchExpressionCompiler.Compile(BenchData.PdxQuery)).Parse(); foreach (var (tag, value) in (Dictionary)root["countries"]) { if (value is Dictionary c && c.TryGetValue("technology", out var t) && t is Dictionary tech && tech.TryGetValue("adm_tech", out var adm)) result[tag] = (long)adm; } return result; } public static Dictionary FromRegex(string text, Regex pathAware) { var result = new Dictionary(); foreach (Match m in pathAware.Matches(text)) result[m.Groups[1].Value] = long.Parse(m.Groups[2].ValueSpan); return result; } public static void Report(byte[] pdx, string text) { var expected = FromParser(pdx); var actual = FromRegex(text, new Regex(Extractors.PathAwarePattern, RegexOptions.Compiled)); int missing = 0, wrong = 0, extra = 0; foreach (var (tag, adm) in expected) { if (!actual.TryGetValue(tag, out long got)) { missing++; Console.WriteLine($" missing tag: {tag}"); } else if (got != adm) wrong++; } foreach (var tag in actual.Keys) if (!expected.ContainsKey(tag)) extra++; Console.WriteLine($"path aware regex pairing: {expected.Count} countries have technology, " + $"regex reported {actual.Count}; missing={missing} wrong_value={wrong} invented={extra}"); } }