refactor(PlossParser): 优化正则匹配逻辑避免重复匹配
This commit is contained in:
@@ -29,47 +29,55 @@ public class PlossParser : IParser<PlossRecord>
|
||||
RegexOptions.Compiled
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 匹配类型枚举
|
||||
/// </summary>
|
||||
public enum MatchType { None, Header, Fod, Legacy }
|
||||
|
||||
/// <summary>
|
||||
/// 单次匹配结果(避免重复正则匹配)
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否可以解析该行(任意一行)
|
||||
/// </summary>
|
||||
public bool CanParse(string line) =>
|
||||
HeaderPattern.IsMatch(line) || FodPattern.IsMatch(line) || LegacyPattern.IsMatch(line);
|
||||
public bool CanParse(string line) => TryMatch(line).Type != MatchType.None;
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否为多行记录的起始行(header行)
|
||||
/// </summary>
|
||||
public bool IsMultiLineStart(string line) => HeaderPattern.IsMatch(line);
|
||||
public bool IsMultiLineStart(string line) => TryMatch(line).Type == MatchType.Header;
|
||||
|
||||
/// <summary>
|
||||
/// 单行解析(兼容旧格式)
|
||||
/// </summary>
|
||||
public ParseResult<PlossRecord> 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<PlossRecord>.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<PlossRecord>.Failure("FORMAT_MISMATCH", "Line does not match Ploss pattern")
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -77,7 +85,7 @@ public class PlossParser : IParser<PlossRecord>
|
||||
/// </summary>
|
||||
public ParseResult<PlossRecord>? 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());
|
||||
|
||||
Reference in New Issue
Block a user