Improve Regex performance and change .NET version to .NET 7

This commit is contained in:
Stone_Red
2022-11-17 12:37:32 +01:00
parent e539813314
commit 0f7f63ae9a
8 changed files with 485 additions and 457 deletions
+23 -9
View File
@@ -6,9 +6,9 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities;
namespace YesNt.CodeEditor
{
internal class SyntaxHighlighter
namespace YesNt.CodeEditor;
internal partial class SyntaxHighlighter
{
private readonly ReadOnlyCollection<StatementInformation> statementInformation;
@@ -27,7 +27,7 @@ namespace YesNt.CodeEditor
}
else
{
MatchCollection matches = Regex.Matches(input, @"!!.");
MatchCollection matches = EscapeSequenceRegex().Matches(input);
for (int i = 0; i < matches.Count; i++)
{
input = AddColorInformation(input, matches[i].Value, Console.ForegroundColor, SearchMode.Contains);
@@ -98,19 +98,19 @@ namespace YesNt.CodeEditor
}
}
matches = Regex.Matches(input, @">[a-zA-Z0-9]+");
matches = VariableRegex().Matches(input);
for (int i = 0; i < matches.Count; i++)
{
input = AddColorInformation(input, matches[i].Value, ConsoleColor.Cyan, SearchMode.Contains);
}
matches = Regex.Matches(input, @"^<[a-zA-Z0-9]+");
matches = VariableDeclarationRegex().Matches(input);
for (int i = 0; i < matches.Count; i++)
{
input = AddColorInformation(input, matches[i].Value, ConsoleColor.DarkCyan, SearchMode.StartOfLine);
}
matches = Regex.Matches(input, @"^!<[a-zA-Z0-9]+");
matches = GlobalVariableDeclarationRegex().Matches(input);
for (int i = 0; i < matches.Count; i++)
{
input = AddColorInformation(input, matches[i].Value, ConsoleColor.Blue, SearchMode.StartOfLine);
@@ -122,7 +122,7 @@ namespace YesNt.CodeEditor
ConsoleColor consoleColor;
string messagePart = part;
string stringColor = Regex.Match(messagePart, "(?<=(\\r))(.*)(?=\\r)").Value;
string stringColor = StringColorRegex().Match(messagePart).Value;
bool succ = int.TryParse(stringColor, out int colorIndex);
if (succ && colorIndex >= 0 && colorIndex < 16)
{
@@ -155,5 +155,19 @@ namespace YesNt.CodeEditor
};
return reult;
}
}
[GeneratedRegex("!!.")]
private static partial Regex EscapeSequenceRegex();
[GeneratedRegex("^<[a-zA-Z0-9]+")]
private static partial Regex VariableDeclarationRegex();
[GeneratedRegex("^!<[a-zA-Z0-9]+")]
private static partial Regex GlobalVariableDeclarationRegex();
[GeneratedRegex(">[a-zA-Z0-9]+")]
private static partial Regex VariableRegex();
[GeneratedRegex("(?<=(\\r))(.*)(?=\\r)")]
private static partial Regex StringColorRegex();
}
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
@@ -9,16 +9,14 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements
{
internal class ProcessingStatements : StatementRuntimeInformation
{
private static readonly Regex calculationRegex = new Regex(@"[0-9*+().,^%/-]+[0-9*+ ().,^%/-]+[0-9*+().,^%/-]+");
namespace YesNt.Interpreter.Statements;
internal partial class ProcessingStatements : StatementRuntimeInformation
{
[Statement("!calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High)]
public void Calculate(string args)
{
MatchCollection matches = calculationRegex.Matches(args.FromSaveString());
MatchCollection matches = CalculationRegex().Matches(args.FromSaveString());
for (int i = 0; i < matches.Count; i++)
{
@@ -116,5 +114,7 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.Exit($"Could not find file \"{path}\"", true);
}
}
}
[GeneratedRegex("[0-9*+().,^%/-]+[0-9*+ ().,^%/-]+[0-9*+().,^%/-]+")]
private static partial Regex CalculationRegex();
}
@@ -5,12 +5,10 @@ using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter.Statements
{
internal class VariableStatements : StatementRuntimeInformation
{
private static readonly Regex variableStatementRegex = new Regex(@">[a-zA-Z0-9]+");
namespace YesNt.Interpreter.Statements;
internal partial class VariableStatements : StatementRuntimeInformation
{
[Statement("<", SearchMode.StartOfLine, SpaceAround.None, Priority = Priority.VeryLow)]
public void DefineVariable(string args)
{
@@ -107,7 +105,7 @@ namespace YesNt.Interpreter.Statements
return;
}
MatchCollection matches = variableStatementRegex.Matches(RuntimeInfo.CurrentLine);
MatchCollection matches = VariableStatementRegex().Matches(RuntimeInfo.CurrentLine);
for (int i = 0; i < matches.Count; i++)
{
@@ -119,5 +117,7 @@ namespace YesNt.Interpreter.Statements
}
}
}
}
[GeneratedRegex(">[a-zA-Z0-9]+")]
private static partial Regex VariableStatementRegex();
}
+24 -10
View File
@@ -2,9 +2,9 @@
using System.Linq;
using System.Text.RegularExpressions;
namespace YesNt.Interpreter.Utilities
{
internal static class Evaluator
namespace YesNt.Interpreter.Utilities;
internal static partial class Evaluator
{
public static bool? EvaluateCondition(string input)
{
@@ -70,10 +70,10 @@ namespace YesNt.Interpreter.Utilities
public static string Calculate(string input)
{
input = Regex.Replace(input, @"(\+ +\+)+", "+");
input = Regex.Replace(input, @"(\- +\-)+", "+");
input = Regex.Replace(input, @"(\- +\+)+", "-");
input = Regex.Replace(input, @"(\+ +\-)+", "-");
input = PlusPlusRegex().Replace(input, "+");
input = MinusMinusRegex().Replace(input, "+");
input = MinusPlusRegex().Replace(input, "-");
input = PlusMinusRegex().Replace(input, "-");
string yes = Calculate(input, '+');
return yes;
@@ -88,7 +88,7 @@ namespace YesNt.Interpreter.Utilities
input = input.FromSaveString();
MatchCollection matches = Regex.Matches(input, @"\(([^()]+)\)");
MatchCollection matches = ParenthesesRegex().Matches(input);
while (matches.Count > 0)
{
for (int i = 0; i < matches.Count; i++)
@@ -97,7 +97,7 @@ namespace YesNt.Interpreter.Utilities
string ret = Calculate(calc);
input = input.Replace(matches[i].Value, ret);
}
matches = Regex.Matches(input, @"\(([^()]+)\)");
matches = ParenthesesRegex().Matches(input);
}
string[] parts = input.Split(op);
@@ -174,5 +174,19 @@ namespace YesNt.Interpreter.Utilities
return number.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
}
[GeneratedRegex("\\(([^()]+)\\)")]
private static partial Regex ParenthesesRegex();
[GeneratedRegex("(\\+ +\\+)+")]
private static partial Regex PlusPlusRegex();
[GeneratedRegex("(\\- +\\-)+")]
private static partial Regex MinusMinusRegex();
[GeneratedRegex("(\\- +\\+)+")]
private static partial Regex MinusPlusRegex();
[GeneratedRegex("(\\+ +\\-)+")]
private static partial Regex PlusMinusRegex();
}
+6 -6
View File
@@ -23,14 +23,14 @@ namespace YesNt.Interpreter.Utilities
public new void BeginOutputReadLine()
{
Stream baseStream = StandardOutput.BaseStream;
output = new AsyncStreamReader(this, baseStream, new UserCallBack(FixedOutputReadNotifyUser), StandardOutput.CurrentEncoding);
output = new AsyncStreamReader(baseStream, new UserCallBack(FixedOutputReadNotifyUser), StandardOutput.CurrentEncoding);
output.BeginReadLine();
}
public new void BeginErrorReadLine()
{
Stream baseStream = StandardError.BaseStream;
error = new AsyncStreamReader(this, baseStream, new UserCallBack(FixedErrorReadNotifyUser), StandardError.CurrentEncoding);
error = new AsyncStreamReader(baseStream, new UserCallBack(FixedErrorReadNotifyUser), StandardError.CurrentEncoding);
error.BeginReadLine();
}
@@ -90,17 +90,17 @@ namespace YesNt.Interpreter.Utilities
public virtual Encoding CurrentEncoding => encoding;
public virtual Stream BaseStream => stream;
internal AsyncStreamReader(Process process, Stream stream, UserCallBack callback, Encoding encoding) : this(process, stream, callback, encoding, 1024)
internal AsyncStreamReader(Stream stream, UserCallBack callback, Encoding encoding) : this(stream, callback, encoding, 1024)
{
}
internal AsyncStreamReader(Process process, Stream stream, UserCallBack callback, Encoding encoding, int bufferSize)
internal AsyncStreamReader(Stream stream, UserCallBack callback, Encoding encoding, int bufferSize)
{
Init(process, stream, callback, encoding, bufferSize);
Init(stream, callback, encoding, bufferSize);
messageQueue = new Queue();
}
private void Init(Process process, Stream stream, UserCallBack callback, Encoding encoding, int bufferSize)
private void Init(Stream stream, UserCallBack callback, Encoding encoding, int bufferSize)
{
this.stream = stream;
this.encoding = encoding;
+1 -1
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>YesNt.Interpreter</RootNamespace>
<ApplicationIcon />
<OutputType>Exe</OutputType>