refactor(PlossParser): 优化正则匹配逻辑避免重复匹配
This commit is contained in:
@@ -29,47 +29,55 @@ public class PlossParser : IParser<PlossRecord>
|
|||||||
RegexOptions.Compiled
|
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>
|
||||||
/// 判断是否可以解析该行(任意一行)
|
/// 判断是否可以解析该行(任意一行)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool CanParse(string line) =>
|
public bool CanParse(string line) => TryMatch(line).Type != MatchType.None;
|
||||||
HeaderPattern.IsMatch(line) || FodPattern.IsMatch(line) || LegacyPattern.IsMatch(line);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 判断是否为多行记录的起始行(header行)
|
/// 判断是否为多行记录的起始行(header行)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsMultiLineStart(string line) => HeaderPattern.IsMatch(line);
|
public bool IsMultiLineStart(string line) => TryMatch(line).Type == MatchType.Header;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 单行解析(兼容旧格式)
|
/// 单行解析(兼容旧格式)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ParseResult<PlossRecord> Parse(string line, Guid scenarioId)
|
public ParseResult<PlossRecord> Parse(string line, Guid scenarioId)
|
||||||
{
|
{
|
||||||
// 尝试匹配旧格式
|
var (type, match) = TryMatch(line);
|
||||||
var legacyMatch = LegacyPattern.Match(line);
|
|
||||||
if (legacyMatch.Success)
|
|
||||||
{
|
|
||||||
return ParseLegacyFormat(legacyMatch, scenarioId);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 尝试匹配第二行(新格式)
|
return type switch
|
||||||
var fodMatch = FodPattern.Match(line);
|
|
||||||
if (fodMatch.Success)
|
|
||||||
{
|
{
|
||||||
return ParseFodLine(fodMatch, scenarioId);
|
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")
|
||||||
var headerMatch = HeaderPattern.Match(line);
|
};
|
||||||
if (headerMatch.Success)
|
|
||||||
{
|
|
||||||
return ParseHeaderLine(headerMatch, scenarioId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ParseResult<PlossRecord>.Failure(
|
|
||||||
"FORMAT_MISMATCH",
|
|
||||||
"Line does not match Ploss pattern"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -77,7 +85,7 @@ public class PlossParser : IParser<PlossRecord>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public ParseResult<PlossRecord>? ParseMultiLine(string[] lines, Guid scenarioId)
|
public ParseResult<PlossRecord>? ParseMultiLine(string[] lines, Guid scenarioId)
|
||||||
{
|
{
|
||||||
if (lines.Length < 2)
|
if (lines.Length < 2 || lines[0] == null || lines[1] == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var headerMatch = HeaderPattern.Match(lines[0].Trim());
|
var headerMatch = HeaderPattern.Match(lines[0].Trim());
|
||||||
|
|||||||
Reference in New Issue
Block a user