Skip to content

Commit f38b39f

Browse files
authored
Merge pull request #1 from irihitech/compat
Support both net6 and 8, optimize Regex for 8
2 parents faca336 + a670353 commit f38b39f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Irihi.Rough.NET/Dependencies/PathDataParser/PathDataParserFunctions.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@
33

44
namespace Irihi.Rough.NET.Dependencies.PathDataParser;
55

6-
internal static class RegexHelper
6+
internal partial class RegexHelper
77
{
8+
#if NET6_0
89
internal static readonly Regex WhiteSpaceRegex = new(@"^([ \t\r\n,]+)", RegexOptions.Compiled);
910
internal static readonly Regex CommandRegex = new(@"^([aAcChHlLmMqQsStTvVzZ])", RegexOptions.Compiled);
1011
internal static readonly Regex NumberRegex = new(@"^(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)", RegexOptions.Compiled);
12+
#endif
13+
14+
#if NET8_0_OR_GREATER
15+
[GeneratedRegex(@"^([ \t\r\n,]+)")]
16+
internal static partial Regex WhiteSpaceRegex();
17+
18+
[GeneratedRegex(@"^([aAcChHlLmMqQsStTvVzZ])")]
19+
internal static partial Regex CommandRegex();
20+
21+
[GeneratedRegex(@"^(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)")]
22+
internal static partial Regex NumberRegex();
23+
#endif
1124
}
1225

1326
public static class PathDataParserFunctions
@@ -25,6 +38,7 @@ internal static List<PathToken> Tokenize(string d)
2538

2639
while (span.Length > 0)
2740
{
41+
#if NET6_0
2842
var spanStr = span.ToString();
2943
if (RegexHelper.WhiteSpaceRegex.IsMatch(spanStr))
3044
{
@@ -56,6 +70,43 @@ internal static List<PathToken> Tokenize(string d)
5670
{
5771
return [];
5872
}
73+
#endif
74+
75+
#if NET8_0
76+
if (RegexHelper.WhiteSpaceRegex().IsMatch(span))
77+
{
78+
foreach (var match in RegexHelper.WhiteSpaceRegex().EnumerateMatches(span))
79+
{
80+
if (match.Length == 0) continue;
81+
span = span.Slice(match.Length);
82+
break;
83+
}
84+
}
85+
else if (RegexHelper.CommandRegex().IsMatch(span))
86+
{
87+
foreach (var match in RegexHelper.CommandRegex().EnumerateMatches(span))
88+
{
89+
if (match.Length == 0) continue;
90+
tokens.Add(new PathToken(TokenType.Command, span.Slice(match.Index, match.Length).ToString()));
91+
span = span.Slice(match.Length);
92+
break;
93+
}
94+
}
95+
else if (RegexHelper.NumberRegex().IsMatch(span))
96+
{
97+
foreach (var match in RegexHelper.NumberRegex().EnumerateMatches(span))
98+
{
99+
if (match.Length == 0) continue;
100+
tokens.Add(new PathToken(TokenType.Number, span.Slice(match.Index, match.Length).ToString()));
101+
span = span.Slice(match.Length);
102+
break;
103+
}
104+
}
105+
else
106+
{
107+
return new List<PathToken>();
108+
}
109+
#endif
59110
}
60111

61112
tokens.Add(PathToken.End);

src/Irihi.Rough.NET/Irihi.Rough.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
55
<Nullable>enable</Nullable>
66
<LangVersion>latest</LangVersion>
77
<Version>1.0.0-beta1</Version>

0 commit comments

Comments
 (0)