diff --git a/src/WCTDataMiner.Core/Parsers/PlossParser.cs b/src/WCTDataMiner.Core/Parsers/PlossParser.cs index f8e13ed..9821a9b 100644 --- a/src/WCTDataMiner.Core/Parsers/PlossParser.cs +++ b/src/WCTDataMiner.Core/Parsers/PlossParser.cs @@ -29,47 +29,55 @@ public class PlossParser : IParser RegexOptions.Compiled ); + /// + /// 匹配类型枚举 + /// + public enum MatchType { None, Header, Fod, Legacy } + + /// + /// 单次匹配结果(避免重复正则匹配) + /// + public (MatchType Type, Match Match) TryMatch(string line) + { + var headerMatch = HeaderPattern.Match(line); + if (headerMatch.Success) + return (MatchType.Header, headerMatch); + + var fodMatch = FodPattern.Match(line); + if (fodMatch.Success) + return (MatchType.Fod, fodMatch); + + var legacyMatch = LegacyPattern.Match(line); + if (legacyMatch.Success) + return (MatchType.Legacy, legacyMatch); + + return (MatchType.None, Match.Empty); + } + /// /// 判断是否可以解析该行(任意一行) /// - public bool CanParse(string line) => - HeaderPattern.IsMatch(line) || FodPattern.IsMatch(line) || LegacyPattern.IsMatch(line); + public bool CanParse(string line) => TryMatch(line).Type != MatchType.None; /// /// 判断是否为多行记录的起始行(header行) /// - public bool IsMultiLineStart(string line) => HeaderPattern.IsMatch(line); + public bool IsMultiLineStart(string line) => TryMatch(line).Type == MatchType.Header; /// /// 单行解析(兼容旧格式) /// public ParseResult Parse(string line, Guid scenarioId) { - // 尝试匹配旧格式 - var legacyMatch = LegacyPattern.Match(line); - if (legacyMatch.Success) - { - return ParseLegacyFormat(legacyMatch, scenarioId); - } + var (type, match) = TryMatch(line); - // 尝试匹配第二行(新格式) - var fodMatch = FodPattern.Match(line); - if (fodMatch.Success) + return type switch { - return ParseFodLine(fodMatch, scenarioId); - } - - // 尝试匹配第一行 - var headerMatch = HeaderPattern.Match(line); - if (headerMatch.Success) - { - return ParseHeaderLine(headerMatch, scenarioId); - } - - return ParseResult.Failure( - "FORMAT_MISMATCH", - "Line does not match Ploss pattern" - ); + MatchType.Legacy => ParseLegacyFormat(match, scenarioId), + MatchType.Fod => ParseFodLine(match, scenarioId), + MatchType.Header => ParseHeaderLine(match, scenarioId), + _ => ParseResult.Failure("FORMAT_MISMATCH", "Line does not match Ploss pattern") + }; } /// @@ -77,7 +85,7 @@ public class PlossParser : IParser /// public ParseResult? ParseMultiLine(string[] lines, Guid scenarioId) { - if (lines.Length < 2) + if (lines.Length < 2 || lines[0] == null || lines[1] == null) return null; var headerMatch = HeaderPattern.Match(lines[0].Trim());