mainly theoratical work

This commit is contained in:
Youmu Liu
2026-07-17 18:03:53 +08:00
parent 4efe6c4947
commit b9dab20752
2 changed files with 141 additions and 8 deletions
@@ -5,6 +5,7 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StgSharp.RegularAnalysis.Abstraction;
using System;
@@ -90,6 +91,20 @@ namespace StgSharp.RegularAnalysis.Text
return se;
}
private static string FormatStringLiteral(
string str
)
{
return SyntaxFactory.Literal(str).Text;
}
private static string FormatStringLiteral(
char str
)
{
return SyntaxFactory.Literal(str).Text;
}
#region sub generator
private static int TryGenerateFindSeq(
@@ -122,7 +137,7 @@ namespace StgSharp.RegularAnalysis.Text
if (scan_win[0] is not RegexFindSeqIR seq) {
return -1;
}
string pattern = seq.Pattern;
string pattern = FormatStringLiteral(seq.Pattern);
int length = pattern.Length;
_ = se.AppendLine()
@@ -189,13 +204,14 @@ namespace StgSharp.RegularAnalysis.Text
{
// Handle single character
case CharSetType.Single:
string literal = FormatStringLiteral(c.Value);
caller = first ? "IndexOfAny" : "LastIndexOfAny";
return $@"({offset} = {remain}.{caller}(""{c.Value}"") != -1)";
return $@"({offset} = {remain}.{caller}(""{literal}"") != -1)";
// Handle range of characters
case CharSetType.Range:
caller = first ? "IndexOfAnyInRange" : "LastIndexOfAnyInRange";
return $@"({offset} = {remain}.{caller}('{c.Value[0]}', '{c.Value[1]}') != -1)";
return $@"({offset} = {remain}.{caller}('{FormatStringLiteral(c.Value[0])}', '{FormatStringLiteral(c.Value[1])}') != -1)";
// Handle set of characters
case CharSetType.Set:
@@ -384,7 +400,7 @@ namespace StgSharp.RegularAnalysis.Text
ReadOnlySpan<char> seq = source.Slice(idx, source.Length - idx - 1);
if (seq.IndexOf('-') == -1)
{
return [ new RegexCharSet(seq.ToString(), CharSetType.Single, true) ];
return [new RegexCharSet(seq.ToString(), CharSetType.Single, true)];
} else
{
List<RegexCharSet> list = [];
@@ -404,7 +420,7 @@ namespace StgSharp.RegularAnalysis.Text
list.Add(new($"{seq[pos - 1]}{seq[pos + 1]}", CharSetType.Range, true));
seq = seq[(pos + 2)..];
}
return [ .. list ];
return [.. list];
}
} else if (source[0] == '\\')
{
@@ -413,9 +429,9 @@ namespace StgSharp.RegularAnalysis.Text
return char.ToLower(source[0], CultureInfo.InvariantCulture)
switch
{
's' => [ new("s", CharSetType.Set, accept) ],
'w' => [ new("w", CharSetType.Set, accept) ],
'd' => [ new("d", CharSetType.Set, accept) ],
's' => [new("s", CharSetType.Set, accept)],
'w' => [new("w", CharSetType.Set, accept)],
'd' => [new("d", CharSetType.Set, accept)],
_ => throw new NotSupportedException()
};
} else