scanning min scan length

This commit is contained in:
Youmu Liu
2026-07-08 18:25:05 +08:00
parent 439128798e
commit 29b24191e1
8 changed files with 125 additions and 93 deletions
@@ -137,7 +137,8 @@ namespace StgSharp.RegularAnalysis.Text
}
private static void OptimizeTree(
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree,
ref RegexInfo info
)
{
// phase0: scan container and remove no parent nodes
@@ -163,11 +164,10 @@ namespace StgSharp.RegularAnalysis.Text
}
}
List<RegexAstNode> cases = [];
// phase3: then ALT union
foreach (RegexAstNode item in altNodes)
{
List<RegexAstNode> cases = [];
RegexAstNode _case = item.Right;
do
{
@@ -194,6 +194,40 @@ namespace StgSharp.RegularAnalysis.Text
}
}
}
info.MinPredictLength = ComputeMinLength(tree.Root);
static int ComputeMinLength(
RegexAstNode node
)
{
if (node.EqualityTypeConvert is RegexElementLabel.UNIT_SPAN or
RegexElementLabel.UNIT)
{
return node.Value.Length;
} else if (node.EqualityTypeConvert == RegexElementLabel.CONCAT)
{
return ComputeMinLength(node.Left) + ComputeMinLength(node.Right);
} else if (node.EqualityTypeConvert == RegexElementLabel.ALT)
{
RegexAstNode case_0 = node.Right;
int length = int.MaxValue;
do
{
length = int.Min(ComputeMinLength(case_0), length);
case_0 = case_0.Next;
} while (!RegexAstNode.IsNullOrEmpty(case_0));
return length;
} else if (node.EqualityTypeConvert == RegexElementLabel.COUNT)
{
string source = node.Source.Value;
int single_count = ComputeMinLength(node.Right);
return 0;
} else
{
return 0;
}
}
}
private static void RotateConcat(