discuss regex ir opt

This commit is contained in:
Youmu Liu
2026-06-22 18:18:16 +08:00
parent 062ffacf88
commit 7e5e4d1f7a
18 changed files with 1499 additions and 127 deletions
@@ -28,7 +28,7 @@
using StgSharp.RegularAnalysis.Abstraction;
using System.Runtime.CompilerServices;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
internal class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel>
{
@@ -31,7 +31,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
[Flags]
public enum RegexElementLabel
@@ -32,25 +32,24 @@ using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
internal enum RegexIRCommand
{
Search_Seq = 0,
Search_Set = 1,
SearchChar = 2,
ConfigFail = 3,
Condition = 4,
Try = 5,
Count = 6,
Count_Seq = 7,
Count_Set = 8,
Pop = 10
Match_Seq = 0,
Match_Set = 1,
Condition = 3,
Try = 4,
Count = 5,
Count_Seq = 6,
Count_Set = 7,
Pop = 8,
Pack = 9,
}
internal abstract class RegexIR
internal abstract class RegexHIR
{
internal abstract RegexIRCommand Command { get; }
@@ -64,70 +63,46 @@ namespace StgSharp.RegularAnalysis.TextRegex
/// <summary>
/// Searches for a certain string pattern or charset from current position.
/// </summary>
internal sealed class RegexMatchSeqIR(
internal sealed class RegexMatchSeqHIR(
string pattern
) : RegexIR
) : RegexHIR
{
public string Pattern { get; } = pattern;
internal override RegexIRCommand Command => RegexIRCommand.Search_Seq;
internal override RegexIRCommand Command => RegexIRCommand.Match_Seq;
internal override string AsCommand(
int lineNum
)
{
return $"[{lineNum}]\t SEARCH_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\"";
return $"[{lineNum}]\t MATCH_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\"";
}
}
internal sealed class RegexMatchSetIR(
internal sealed class RegexMatchSetHIR(
string charset
) : RegexIR
) : RegexHIR
{
public string Charset { get; } = charset;
internal override RegexIRCommand Command => RegexIRCommand.Search_Set;
internal override RegexIRCommand Command => RegexIRCommand.Match_Set;
internal override string AsCommand(
int lineNum
)
{
return $"[{lineNum}]\t SEARCH_SET \"{Charset}\"";
return $"[{lineNum}]\t MATCH_SET \"{Charset}\"";
}
}
/// <summary>
/// Configures the failure jump of current position. The failMod ranges 0-2
/// </summary>
/// <param name="target">
///
/// </param>
internal sealed class RegexConfigFailIR(
int failMod
) : RegexIR
{
public int FailMod { get; } = failMod & 0x3;
internal override RegexIRCommand Command => RegexIRCommand.ConfigFail;
internal override string AsCommand(
int lineNum
)
{
return $"[{lineNum}]\t CONFIG_FAIL {FailMod}";
}
}
internal sealed class RegexConditionIR(
internal sealed class RegexConditionHIR(
int success,
int fail
) : RegexIR
) : RegexHIR
{
public int SuccessCase { get; } = success;
@@ -145,12 +120,33 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexCountIR(
internal sealed class RegexPackHIR(
bool condition,
string name = ""
) : RegexHIR
{
public bool Condition { get; } = condition;
public string GroupName { get; } = name;
internal override RegexIRCommand Command => RegexIRCommand.Pack;
internal override string AsCommand(
int lineNum
)
{
return $"[{lineNum}]\t PACK {(Condition ? "SUCCESS" : "FAIL")} GROUP: \"{GroupName}\"";
}
}
internal sealed class RegexCountHIR(
int min,
int max,
bool isGreedy,
int operation
) : RegexIR
) : RegexHIR
{
public bool IsGreedy { get; } = isGreedy;
@@ -172,12 +168,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexCountSeqIR(
internal sealed class RegexCountSeqHIR(
int min,
int max,
bool isGreedy,
string seq
) : RegexIR
) : RegexHIR
{
public bool IsGreedy { get; } = isGreedy;
@@ -199,12 +195,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexCountSetIR(
internal sealed class RegexCountSetHIR(
int min,
int max,
bool isGreedy,
string set
) : RegexIR
) : RegexHIR
{
public bool IsGreedy { get; } = isGreedy;
@@ -226,9 +222,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexTryIR(
internal sealed class RegexTryHIR(
int count
) : RegexIR
) : RegexHIR
{
public int CountOfCommand { get; } = count;
@@ -244,14 +240,14 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexPopIR(
internal sealed class RegexPopHIR(
bool isSuccess
) : RegexIR
) : RegexHIR
{
public bool IsSuccess { get; } = isSuccess;
internal override RegexIRCommand Command => RegexIRCommand.Try;
internal override RegexIRCommand Command => RegexIRCommand.Pop;
internal override string AsCommand(
int lineNum
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexIRGenerator"
// file="RegexHIRGenerator"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
@@ -32,15 +32,15 @@ using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
internal class RegexIRGenerator
internal class RegexHIRGenerator
{
private Node _head = new();
private Node _tail;
public RegexIRGenerator()
public RegexHIRGenerator()
{
_tail = _head;
}
@@ -52,15 +52,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int fail
)
{
_tail.Add(new RegexConditionIR(success, fail));
Count++;
}
public void EmitConfigFail(
int fail
)
{
_tail.Add(new RegexConfigFailIR(fail));
_tail.Add(new RegexConditionHIR(success, fail));
Count++;
}
@@ -71,7 +63,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int operation
)
{
_tail.Add(new RegexCountIR(min, max, isGreedy, operation));
_tail.Add(new RegexCountHIR(min, max, isGreedy, operation));
Count++;
}
@@ -82,7 +74,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
string seq
)
{
_tail.Add(new RegexCountSeqIR(min, max, isGreedy, seq));
_tail.Add(new RegexCountSeqHIR(min, max, isGreedy, seq));
Count++;
}
@@ -93,12 +85,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
string set
)
{
_tail.Add(new RegexCountSetIR(min, max, isGreedy, set));
_tail.Add(new RegexCountSetHIR(min, max, isGreedy, set));
Count++;
}
public void EmitIRStream(
RegexIRGenerator stream
RegexHIRGenerator stream
)
{
if (stream is null || stream.Count == 0) {
@@ -113,7 +105,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
string unit
)
{
_tail.Add(new RegexMatchSeqIR(unit));
_tail.Add(new RegexMatchSeqHIR(unit));
Count++;
}
@@ -121,7 +113,16 @@ namespace StgSharp.RegularAnalysis.TextRegex
string unit
)
{
_tail.Add(new RegexMatchSetIR(unit));
_tail.Add(new RegexMatchSetHIR(unit));
Count++;
}
public void EmitPack(
bool condition,
string name = ""
)
{
_tail.Add(new RegexPackHIR(condition, name));
Count++;
}
@@ -129,7 +130,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
bool isSuccess
)
{
_tail.Add(new RegexPopIR(isSuccess));
_tail.Add(new RegexPopHIR(isSuccess));
Count++;
}
@@ -137,13 +138,13 @@ namespace StgSharp.RegularAnalysis.TextRegex
int count
)
{
_tail.Add(new RegexTryIR(count));
_tail.Add(new RegexTryHIR(count));
Count++;
}
public List<RegexIR> ToList()
public List<RegexHIR> ToList()
{
List<RegexIR> result = new(Count);
List<RegexHIR> result = new(Count);
Node current = _head;
while (current != null)
{
@@ -160,11 +161,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
public int Count => IRList.Count;
public List<RegexIR> IRList { get; } = [];
public List<RegexHIR> IRList { get; } = [];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(
RegexIR ir
RegexHIR ir
)
{
IRList.Add(ir);
@@ -37,7 +37,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
public partial class RegexInterpreter
{
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter.IRGenerate"
// file="RegexInterpreter.HIRGenerate"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
@@ -38,13 +38,13 @@ using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
public partial class RegexInterpreter
{
internal static List<RegexIR> GenerateIR(
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
internal static List<RegexHIR> GenerateIR(
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
)
{
return GenerateIRRecursion(tree.Root, 0).ToList();
@@ -53,7 +53,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EmitCount(
int curDepth,
RegexIRGenerator ir,
RegexHIRGenerator ir,
int min,
int max,
bool isGreedy,
@@ -66,7 +66,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
if (isRightOperator) // seq to be count is complex
{
ir.EmitCount(min, max, isGreedy, 1);
RegexIRGenerator rightIR = GenerateIRRecursion(right, curDepth + 1);
RegexHIRGenerator rightIR = GenerateIRRecursion(right, curDepth + 1);
ir.EmitTry(rightIR.Count);
ir.EmitIRStream(rightIR);
} else
@@ -89,9 +89,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
}
private static RegexIRGenerator GenerateIRRecursion(
RegexAstNode node,
int depth
private static RegexHIRGenerator GenerateIRRecursion(
RegexAstNode node,
int depth
)
{
#pragma warning disable IDE0010
@@ -102,8 +102,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
int curDepth = depth;
RegexAstNode left = node.Left;
RegexAstNode right = node.Right;
RegexIRGenerator leftIR;
RegexIRGenerator rightIR;
RegexHIRGenerator leftIR;
RegexHIRGenerator rightIR;
Token<RegexElementLabel> token = node.Source;
@@ -116,15 +116,16 @@ namespace StgSharp.RegularAnalysis.TextRegex
bool is_group = group_source.Length == 1; // simple group: (pattern)
string group_name =
group_source.StartsWith(@"(?<", StringComparison.Ordinal) ?
group_source[3..^2] : /* named group (?<name>) */
group_source[3..^1] : /* named group (?<name>) */
string.Empty; /* unnamed group (?:) */
RegexIRGenerator groupIR = new();
RegexHIRGenerator groupIR = new();
rightIR = GenerateIRRecursion(right, curDepth + 1);
groupIR.EmitTry(rightIR.Count);
groupIR.EmitIRStream(rightIR);
groupIR.EmitCondition(2, 1);
/*generate pack ir here*/
groupIR.EmitPop(false);
groupIR.EmitPack(true, group_name);
return groupIR;
case RegexElementLabel.COUNT:
string count_source = token.Value;
@@ -143,14 +144,14 @@ namespace StgSharp.RegularAnalysis.TextRegex
_ => throw new InvalidOperationException("Unexpected sequence mask for count operator"),
};
ReadOnlySpan<char> count_source_span = count_source;
RegexIRGenerator countIR = new();
RegexHIRGenerator countIR = new();
if (count_source_span[0] == '*') // infinite count
{
EmitCount(curDepth, countIR, 0, int.MaxValue, isGreedy, seq);
EmitCount(curDepth, countIR, 0, -1, isGreedy, seq);
} else if (count_source_span[0] == '+') // at least one
{
EmitCount(curDepth, countIR, 1, int.MaxValue, isGreedy, seq);
EmitCount(curDepth, countIR, 1, -1, isGreedy, seq);
} else if (count_source_span[0] == '?') // zero or one
{
EmitCount(curDepth, countIR, 0, 1, isGreedy, seq);
@@ -159,7 +160,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int comma_pos = count_source_span.IndexOf(',');
if (comma_pos == -1)
{
if (int.TryParse(count_source[1..(isGreedy ? ^3 : ^2)], out int count))
if (int.TryParse(count_source[1..(isGreedy ? ^2 : ^1)], out int count))
{
EmitCount(curDepth, countIR, count, count, isGreedy, seq);
} else
@@ -169,7 +170,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
} else
{
if (int.TryParse(count_source[1..comma_pos], out int min) &&
int.TryParse(count_source[(comma_pos + 1)..(isGreedy ? ^3 : ^2)], out int max))
int.TryParse(count_source[(comma_pos + 1)..(isGreedy ? ^1 : ^2)], out int max))
{
EmitCount(curDepth, countIR, min, max, isGreedy, seq);
} else
@@ -184,7 +185,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
return countIR;
case RegexElementLabel.CONCAT:
RegexIRGenerator concat = new();
RegexHIRGenerator concat = new();
// process left node
if (RegexAstNode.IsNullOrEmpty(left))
@@ -237,13 +238,13 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
return concat;
case RegexElementLabel.ALT:
RegexIRGenerator alt = new();
List<RegexIRGenerator> casesIRList = [];
RegexHIRGenerator alt = new();
List<RegexHIRGenerator> casesIRList = [];
RegexAstNode cur_node = right;
int ir_count = 0;
while (!RegexAstNode.IsNullOrEmpty(cur_node))
{
RegexIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1);
RegexHIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1);
casesIRList.Add(_case);
ir_count += _case.Count;
cur_node = cur_node.Next;
@@ -251,7 +252,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int case_count = casesIRList.Count;
while (case_count != 0)
{
RegexIRGenerator cur_ir = casesIRList[^case_count];
RegexHIRGenerator cur_ir = casesIRList[^case_count];
ir_count -= cur_ir.Count;
case_count--;
alt.EmitTry(cur_ir.Count);
@@ -264,11 +265,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
case RegexElementLabel.UNIT_SPAN:
goto case RegexElementLabel.UNIT;
case RegexElementLabel.UNIT:
RegexIRGenerator unit_seq = new();
RegexHIRGenerator unit_seq = new();
unit_seq.EmitMatchSeq(node.Source.Value);
return unit_seq;
case RegexElementLabel.UNIT_SET:
RegexIRGenerator unit_set = new();
RegexHIRGenerator unit_set = new();
unit_set.EmitMatchSet(node.Source.Value);
return unit_set;
default:
@@ -0,0 +1,69 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter.IRScan"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public partial class RegexInterpreter
{
internal static void ScanAndMarkIROptimization(
RegexHIR ir
)
{
switch (ir.Command)
{
case RegexIRCommand.Match_Seq:
break;
case RegexIRCommand.Match_Set:
break;
case RegexIRCommand.Pack:
break;
case RegexIRCommand.Condition:
break;
case RegexIRCommand.Try:
break;
case RegexIRCommand.Count:
break;
case RegexIRCommand.Count_Seq:
break;
case RegexIRCommand.Count_Set:
break;
case RegexIRCommand.Pop:
break;
default:
break;
}
}
}
}
@@ -0,0 +1,51 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter.SourceGenerate"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public partial class RegexInterpreter
{
internal static string GetSourceCode(
IEnumerable<RegexHIR> irs
)
{
StringBuilder sb = new StringBuilder();
foreach (RegexHIR ir in irs) {
ScanAndMarkIROptimization(ir);
}
return sb.ToString();
}
}
}
@@ -35,7 +35,7 @@ using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
public partial class RegexInterpreter
{
+60
View File
@@ -0,0 +1,60 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexLIR"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
internal enum LIRCommand
{
Search = 0,
Count = 1,
Try = 2,
}
internal abstract class RegexLIR
{
public abstract LIRCommand Command { get; }
}
internal enum RegexSearchMethod
{
Match,
Find,
Assert,
}
}
@@ -34,7 +34,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
internal class RegexTokenParser : TokenParser<RegexElementLabel, RegexElementLabel>
{
@@ -34,7 +34,7 @@ using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
internal class RegexTokenReader : ITokenReader<RegexElementLabel>
{
@@ -32,7 +32,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
namespace StgSharp.RegularAnalysis.Text
{
internal static class RegexTokenUtil
{
@@ -0,0 +1,50 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="TextHelper"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public static class TextHelper
{
public static bool TryLastIndexOf(
this ReadOnlySpan<char> span,
ReadOnlySpan<char> value,
out int index
)
{
index = span.LastIndexOf(value);
return index != -1;
}
}
}
@@ -0,0 +1,47 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="TextRegex"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public abstract class TextRegex
{
public abstract bool IsMatch(
ReadOnlySpan<char> text
);
}
public ref struct MatchResult { }
}