From 89f0f0f131e2afc65bca5b722b12b891940ae3df Mon Sep 17 00:00:00 2001 From: Nitload Date: Sun, 12 Jul 2026 23:07:16 +0800 Subject: [PATCH] a few add to source gen --- .../Text/RegexAnalyzer.SourceGen.cs | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs b/src/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs index b12b640e..97805e66 100644 --- a/src/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs +++ b/src/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs @@ -20,9 +20,7 @@ namespace StgSharp.RegularAnalysis.Text public partial class RegexAnalyzer { - internal static SequenceEmitter GenerateSource( - TextRegexSource source - ) + internal static SequenceEmitter GenerateSource(TextRegexSource source) { SequenceEmitter se = new(); List> sub_match_list = []; @@ -33,6 +31,8 @@ namespace StgSharp.RegularAnalysis.Text RegexIR[] window = new RegexIR[8]; Span scan_win = window; + const string current_ptr = "current_ptr"; + _ = se.AppendLine(@"public override MatchResult Match(ReadOnlySpan text)")// .AppendLine(@"{"); @@ -44,6 +44,8 @@ namespace StgSharp.RegularAnalysis.Text .AppendLine($@"bool region_{i}_success;"); } + _ = se.AppendLine($@"int {current_ptr} = 0;"); + int index = 0; do { @@ -61,15 +63,19 @@ namespace StgSharp.RegularAnalysis.Text return se; /**/ - int TryGenerateMatch( - Span scan_win, - SequenceEmitter sb - ) + int TryGenerateMatch(Span scan_win, SequenceEmitter sb) { if (scan_win[0] is not RegexMatchSeqIR seq) { return -1; } - _ = sb.Append(string.Empty); + string pattern = seq.Pattern; + int length = pattern.Length; + _ = sb.Append(string.Empty) + .AppendLine($@"if(text[{current_ptr}..({current_ptr} + {length})] == {pattern})") + .AppendLine($@"{{") + .AppendLine($@"{current_ptr} += {length};") + .AppendLine($@"}}"); + return 1; } /**/ @@ -77,3 +83,4 @@ namespace StgSharp.RegularAnalysis.Text } } +