From 8d2192b204fb2fb233c7b6329c2238093429ae2a Mon Sep 17 00:00:00 2001 From: IIHOSHI Yoshinori Date: Thu, 2 Jan 2025 00:05:26 +0900 Subject: [PATCH] Fix to use IntegerParser for th10 --- ThScoreFileConverter/Models/Th10/CardReplacerBase.cs | 5 +++-- ThScoreFileConverter/Models/Th10/CareerReplacerBase.cs | 9 ++++++--- ThScoreFileConverter/Models/Th10/CharaExReplacerBase.cs | 6 ++++-- ThScoreFileConverter/Models/Th10/CharaReplacerBase.cs | 6 ++++-- .../Models/Th10/CollectRateReplacerBase.cs | 6 ++++-- ThScoreFileConverter/Models/Th10/ScoreReplacerBase.cs | 9 ++++++--- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/ThScoreFileConverter/Models/Th10/CardReplacerBase.cs b/ThScoreFileConverter/Models/Th10/CardReplacerBase.cs index 556c5e14..0af01b68 100644 --- a/ThScoreFileConverter/Models/Th10/CardReplacerBase.cs +++ b/ThScoreFileConverter/Models/Th10/CardReplacerBase.cs @@ -39,11 +39,12 @@ protected CardReplacerBase( Func levelToString) { var numDigits = IntegerHelper.GetNumDigits(cardTable.Count); + var cardNumberParser = new IntegerParser($@"\d{{{numDigits}}}"); - this.pattern = StringHelper.Create($@"{formatPrefix}CARD(\d{{{numDigits}}})([NR])"); + this.pattern = StringHelper.Create($@"{formatPrefix}CARD({cardNumberParser.Pattern})([NR])"); this.evaluator = new MatchEvaluator(match => { - var number = IntegerHelper.Parse(match.Groups[1].Value); + var number = cardNumberParser.Parse(match.Groups[1]); var type = match.Groups[2].Value.ToUpperInvariant(); if (cardTable.TryGetValue(number, out var cardInfo)) diff --git a/ThScoreFileConverter/Models/Th10/CareerReplacerBase.cs b/ThScoreFileConverter/Models/Th10/CareerReplacerBase.cs index 0c2217bd..66096277 100644 --- a/ThScoreFileConverter/Models/Th10/CareerReplacerBase.cs +++ b/ThScoreFileConverter/Models/Th10/CareerReplacerBase.cs @@ -22,6 +22,9 @@ internal class CareerReplacerBase : IStringRepl where TStage : struct, Enum where TLevel : struct, Enum { + private static readonly IntegerParser CardNumberParser = new(@"\d{3}"); + private static readonly IntegerParser TypeParser = new(@"[12]"); + private readonly string pattern; private readonly MatchEvaluator evaluator; @@ -32,12 +35,12 @@ protected CareerReplacerBase( IReadOnlyDictionary> cardTable, INumberFormatter formatter) { - this.pattern = StringHelper.Create($@"{formatPrefix}C(\d{{3}})({charaWithTotalParser.Pattern})([12])"); + this.pattern = StringHelper.Create($@"{formatPrefix}C({CardNumberParser.Pattern})({charaWithTotalParser.Pattern})({TypeParser.Pattern})"); this.evaluator = new MatchEvaluator(match => { - var number = IntegerHelper.Parse(match.Groups[1].Value); + var number = CardNumberParser.Parse(match.Groups[1]); var chara = charaWithTotalParser.Parse(match.Groups[2]); - var type = IntegerHelper.Parse(match.Groups[3].Value); + var type = TypeParser.Parse(match.Groups[3]); Func, int> getCount = type switch { diff --git a/ThScoreFileConverter/Models/Th10/CharaExReplacerBase.cs b/ThScoreFileConverter/Models/Th10/CharaExReplacerBase.cs index edd299bf..8980d814 100644 --- a/ThScoreFileConverter/Models/Th10/CharaExReplacerBase.cs +++ b/ThScoreFileConverter/Models/Th10/CharaExReplacerBase.cs @@ -19,6 +19,8 @@ namespace ThScoreFileConverter.Models.Th10; internal class CharaExReplacerBase : IStringReplaceable where TCharaWithTotal : struct, Enum { + private static readonly IntegerParser TypeParser = new(@"[1-3]"); + private readonly string pattern; private readonly MatchEvaluator evaluator; @@ -32,12 +34,12 @@ protected CharaExReplacerBase( INumberFormatter formatter) { this.pattern = StringHelper.Create( - $"{formatPrefix}CHARAEX({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})([1-3])"); + $"{formatPrefix}CHARAEX({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})({TypeParser.Pattern})"); this.evaluator = new MatchEvaluator(match => { var level = levelWithTotalParser.Parse(match.Groups[1]); var chara = charaWithTotalParser.Parse(match.Groups[2]); - var type = IntegerHelper.Parse(match.Groups[3].Value); + var type = TypeParser.Parse(match.Groups[3]); Func, long> getValueByType = (level, type) switch { diff --git a/ThScoreFileConverter/Models/Th10/CharaReplacerBase.cs b/ThScoreFileConverter/Models/Th10/CharaReplacerBase.cs index 92db5c57..e54a68f0 100644 --- a/ThScoreFileConverter/Models/Th10/CharaReplacerBase.cs +++ b/ThScoreFileConverter/Models/Th10/CharaReplacerBase.cs @@ -19,6 +19,8 @@ namespace ThScoreFileConverter.Models.Th10; internal class CharaReplacerBase : IStringReplaceable where TCharaWithTotal : struct, Enum { + private static readonly IntegerParser TypeParser = new(@"[1-3]"); + private readonly string pattern; private readonly MatchEvaluator evaluator; @@ -29,11 +31,11 @@ protected CharaReplacerBase( IReadOnlyDictionary> clearDataDictionary, INumberFormatter formatter) { - this.pattern = StringHelper.Create($"{formatPrefix}CHARA({charaWithTotalParser.Pattern})([1-3])"); + this.pattern = StringHelper.Create($"{formatPrefix}CHARA({charaWithTotalParser.Pattern})({TypeParser.Pattern})"); this.evaluator = new MatchEvaluator(match => { var chara = charaWithTotalParser.Parse(match.Groups[1]); - var type = IntegerHelper.Parse(match.Groups[2].Value); + var type = TypeParser.Parse(match.Groups[2]); Func, long> getValueByType = type switch { diff --git a/ThScoreFileConverter/Models/Th10/CollectRateReplacerBase.cs b/ThScoreFileConverter/Models/Th10/CollectRateReplacerBase.cs index 9aac8ecf..5a7fbe70 100644 --- a/ThScoreFileConverter/Models/Th10/CollectRateReplacerBase.cs +++ b/ThScoreFileConverter/Models/Th10/CollectRateReplacerBase.cs @@ -19,6 +19,8 @@ namespace ThScoreFileConverter.Models.Th10; internal class CollectRateReplacerBase : IStringReplaceable where TCharaWithTotal : struct, Enum { + private static readonly IntegerParser TypeParser = new(@"[12]"); + private readonly string pattern; private readonly MatchEvaluator evaluator; @@ -32,13 +34,13 @@ protected CollectRateReplacerBase( INumberFormatter formatter) { this.pattern = StringHelper.Create( - $"{formatPrefix}CRG({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})({stageWithTotalParser.Pattern})([12])"); + $"{formatPrefix}CRG({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})({stageWithTotalParser.Pattern})({TypeParser.Pattern})"); this.evaluator = new MatchEvaluator(match => { var level = levelWithTotalParser.Parse(match.Groups[1]); var chara = charaWithTotalParser.Parse(match.Groups[2]); var stage = stageWithTotalParser.Parse(match.Groups[3]); - var type = IntegerHelper.Parse(match.Groups[4].Value); + var type = TypeParser.Parse(match.Groups[4]); if (stage == StageWithTotal.Extra) return match.ToString(); diff --git a/ThScoreFileConverter/Models/Th10/ScoreReplacerBase.cs b/ThScoreFileConverter/Models/Th10/ScoreReplacerBase.cs index 32abeda2..895d5167 100644 --- a/ThScoreFileConverter/Models/Th10/ScoreReplacerBase.cs +++ b/ThScoreFileConverter/Models/Th10/ScoreReplacerBase.cs @@ -19,6 +19,9 @@ namespace ThScoreFileConverter.Models.Th10; internal class ScoreReplacerBase : IStringReplaceable where TChara : struct, Enum { + private static readonly IntegerParser RankParser = new(@"\d"); + private static readonly IntegerParser TypeParser = new(@"[1-5]"); + private readonly string pattern; private readonly MatchEvaluator evaluator; @@ -30,13 +33,13 @@ protected ScoreReplacerBase( INumberFormatter formatter) { this.pattern = StringHelper.Create( - $@"{formatPrefix}SCR({levelParser.Pattern})({charaParser.Pattern})(\d)([1-5])"); + $@"{formatPrefix}SCR({levelParser.Pattern})({charaParser.Pattern})({RankParser.Pattern})({TypeParser.Pattern})"); this.evaluator = new MatchEvaluator(match => { var level = levelParser.Parse(match.Groups[1]); var chara = charaParser.Parse(match.Groups[2]); - var rank = IntegerHelper.ToZeroBased(IntegerHelper.Parse(match.Groups[3].Value)); - var type = IntegerHelper.Parse(match.Groups[4].Value); + var rank = IntegerHelper.ToZeroBased(RankParser.Parse(match.Groups[3])); + var type = TypeParser.Parse(match.Groups[4]); var score = getScore(level, chara, rank);