add more ir opt code, clearified what to opt, so current opt code may need rewrite
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -36,19 +37,22 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
public partial class RegexAnalyzer
|
||||
{
|
||||
|
||||
internal static RegexInfo ScanAndOptimizeIR(
|
||||
List<RegexIR> baseIR,
|
||||
out List<RegexIR> optIR
|
||||
// this array is not thread safe so opt process must be done in single thread.
|
||||
private RegexIR[] _opt_buffer = new RegexIR[16];
|
||||
|
||||
internal RegexInfo ScanAndOptimizeIR(
|
||||
List<RegexIR> baseIR,
|
||||
out List<RegexIR> optIR
|
||||
)
|
||||
{
|
||||
int index = 0;
|
||||
Span<RegexIR> origin_span = CollectionsMarshal.AsSpan(baseIR);
|
||||
List<RegexIR> optimizedIR = new(baseIR.Count);
|
||||
List<RegexIR> segment = [];
|
||||
RegexInfo info = new();
|
||||
do
|
||||
{
|
||||
// TODO scan and opt here
|
||||
OptimizeIR(baseIR, ref index, segment, info);
|
||||
OptimizeIR(origin_span, ref index, segment, info);
|
||||
optimizedIR.AddRange(segment);
|
||||
index++;
|
||||
} while (index < baseIR.Count);
|
||||
@@ -56,37 +60,106 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
return info;
|
||||
}
|
||||
|
||||
private static void OptimizeDot(
|
||||
RegexCountSetIR root,
|
||||
List<RegexIR> ir,
|
||||
ref int index,
|
||||
List<RegexIR> optimizedIR,
|
||||
RegexInfo info
|
||||
private int ExpandCount(
|
||||
string pattern,
|
||||
int min,
|
||||
int max,
|
||||
bool is_greedy,
|
||||
in Span<RegexIR> span
|
||||
)
|
||||
{
|
||||
// expand count operation to less cycles.
|
||||
|
||||
const int fast_seq_length = 32;
|
||||
|
||||
int fast_count = fast_seq_length / pattern.Length;
|
||||
fast_count = int.Min(fast_count, min);
|
||||
if (fast_count > 0)
|
||||
{
|
||||
string fast_pattern = string.Concat(Enumerable.Repeat(pattern, fast_count));
|
||||
|
||||
int fast_min = min / fast_count;
|
||||
int consumed_count = fast_min * fast_count;
|
||||
int remain_min = min - consumed_count;
|
||||
int remain_max = max - consumed_count;
|
||||
remain_max = int.Max(-1, remain_max); //for negative stands for infinitive
|
||||
if (remain_max == 0)
|
||||
{
|
||||
// a{2}
|
||||
RegexCountSeqIR fast = new(fast_min, fast_min, is_greedy, fast_pattern);
|
||||
span[0] = fast;
|
||||
return 1;
|
||||
} else
|
||||
{
|
||||
// a{2,5}
|
||||
RegexCountSeqIR fast = new(fast_min, fast_min, is_greedy, fast_pattern);
|
||||
RegexCountSeqIR tail = new(remain_min, remain_max, is_greedy, pattern);
|
||||
span[0] = fast;
|
||||
span[1] = tail;
|
||||
return 2;
|
||||
}
|
||||
} else if (fast_count == 0)
|
||||
{
|
||||
// least count is 0
|
||||
span[0] = new RegexCountSeqIR(0, max, is_greedy, pattern);
|
||||
return 1;
|
||||
} else
|
||||
{
|
||||
// least count is infinitive
|
||||
span[0] = new RegexCountSeqIR(-1, -1, is_greedy, pattern);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
private void OptimizeDot(
|
||||
RegexCountSetIR root,
|
||||
Span<RegexIR> ir,
|
||||
ref int index,
|
||||
List<RegexIR> optimizedIR,
|
||||
RegexInfo info
|
||||
)
|
||||
{
|
||||
int next = index + 1;
|
||||
if (next >= ir.Count)
|
||||
optimizedIR.Clear();
|
||||
if (next >= ir.Length)
|
||||
{
|
||||
optimizedIR.Clear();
|
||||
optimizedIR.Add(root);
|
||||
return;
|
||||
}
|
||||
RegexIR nextIR = ir[next];
|
||||
if (nextIR.Command == RegexIRCommand.Match_Seq)
|
||||
if (nextIR is RegexMatchSeqIR match_seq)
|
||||
{
|
||||
// opt .*abc style
|
||||
RegexFindSeqIR find = new((nextIR as RegexMatchSeqIR)!.Pattern);
|
||||
RegexFindSeqIR find = new(match_seq.Pattern);
|
||||
optimizedIR.Add(find);
|
||||
info.MinLength += find.Pattern.Length;
|
||||
return;
|
||||
} else { }
|
||||
} else if (nextIR is RegexCountSeqIR count_seq)
|
||||
{
|
||||
// opt .* (abc)* or .*(abc){2,5} style
|
||||
string pattern = count_seq.Sequence;
|
||||
int min = count_seq.Min;
|
||||
int max = count_seq.Max;
|
||||
switch ((min, max))
|
||||
{
|
||||
case (0, _):
|
||||
|
||||
break;
|
||||
default:
|
||||
RegexFindSeqIR find = new(pattern);
|
||||
|
||||
// RegexCountSeqIR
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void OptimizeIR(
|
||||
List<RegexIR> ir,
|
||||
ref int index,
|
||||
List<RegexIR> optimizedIR,
|
||||
RegexInfo info
|
||||
private void OptimizeIR(
|
||||
Span<RegexIR> ir,
|
||||
ref int index,
|
||||
List<RegexIR> optimizedIR,
|
||||
RegexInfo info
|
||||
)
|
||||
{
|
||||
optimizedIR.Clear();
|
||||
@@ -120,6 +193,8 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
|
||||
public int TryDepth { get; set; }
|
||||
|
||||
public int GroupCount { get; set; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user