Skip to content

Commit 7924662

Browse files
committed
Update Parlot
1 parent 9fd86dd commit 7924662

File tree

9 files changed

+62
-46
lines changed

9 files changed

+62
-46
lines changed

Shortcodes.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30107.140
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35111.106
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shortcodes.Tests", "tests\Shortcodes.Tests\Shortcodes.Tests.csproj", "{7ECF5D62-FDDF-4A6E-A6DD-8B4EDFB5CB4E}"
77
EndProject
@@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{46FB729C
1717
EndProject
1818
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shortcodes.Benchmarks", "tests\Shortcodes.Benchmarks\Shortcodes.Benchmarks.csproj", "{D7B1F710-04EC-49F1-BE79-FC4A83749E69}"
1919
EndProject
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Parlot", "..\parlot\src\Parlot\Parlot.csproj", "{6CD0F313-1FE4-48E7-98A0-2CA43DADD51F}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -35,6 +37,10 @@ Global
3537
{D7B1F710-04EC-49F1-BE79-FC4A83749E69}.Debug|Any CPU.Build.0 = Debug|Any CPU
3638
{D7B1F710-04EC-49F1-BE79-FC4A83749E69}.Release|Any CPU.ActiveCfg = Release|Any CPU
3739
{D7B1F710-04EC-49F1-BE79-FC4A83749E69}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{6CD0F313-1FE4-48E7-98A0-2CA43DADD51F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{6CD0F313-1FE4-48E7-98A0-2CA43DADD51F}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{6CD0F313-1FE4-48E7-98A0-2CA43DADD51F}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{6CD0F313-1FE4-48E7-98A0-2CA43DADD51F}.Release|Any CPU.Build.0 = Release|Any CPU
3844
EndGlobalSection
3945
GlobalSection(SolutionProperties) = preSolution
4046
HideSolutionNode = FALSE

src/Shortcodes/RawText.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
namespace Shortcodes
1+
using Parlot;
2+
using System;
3+
4+
namespace Shortcodes
25
{
36
public class RawText : Node
47
{
5-
public RawText(string buffer, int offset, int count)
8+
private readonly TextSpan _textSpan;
9+
10+
public RawText(TextSpan textSpan)
611
{
7-
Buffer = buffer;
8-
Offset = offset;
9-
Count = count;
12+
_textSpan = textSpan;
1013
}
1114

12-
public string Buffer { get; }
13-
public int Offset { get; }
14-
public int Count { get; }
15+
public ReadOnlySpan<char> Span => _textSpan.Span;
1516
}
1617
}

src/Shortcodes/Shortcodes.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
14-
<PackageReference Include="Parlot" Version="0.0.24" />
13+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
14+
<PackageReference Include="Parlot" Version="1.0.2" />
1515
</ItemGroup>
1616

1717
</Project>

src/Shortcodes/ShortcodesParser.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ private List<Node> ParseNodes()
5151

5252
private RawText ParseRawText()
5353
{
54-
if (_scanner.ReadRawText(out var _result))
54+
if (_scanner.ReadRawText(out var result))
5555
{
56-
return new RawText(_scanner.Buffer, _result.Start, _result.Length);
56+
return new RawText(result);
5757
}
5858

5959
return null;
@@ -110,7 +110,7 @@ private Shortcode ParseShortcode()
110110
return null;
111111
}
112112

113-
var identifier = _result.GetText();
113+
var identifier = _result.ToString();
114114

115115
_scanner.SkipWhiteSpace();
116116

@@ -128,16 +128,17 @@ private Shortcode ParseShortcode()
128128
{
129129
arguments ??= CreateArgumentsDictionary();
130130

131-
arguments[argumentIndex.ToString()] = Character.DecodeString(new TextSpan(_scanner.Buffer, _result.Start + 1, _result.Length - 2)).ToString();
131+
arguments[argumentIndex.ToString()] = Character.DecodeString(_result)[1..^1].ToString();
132132

133133
argumentIndex += 1;
134134
}
135135
else if (_scanner.ReadIdentifier(out _result))
136136
{
137137
_scanner.SkipWhiteSpace();
138138

139-
var argumentName = _result.GetText();
140-
139+
var argumentName = _result.ToString();
140+
var valueStart = _scanner.Cursor.Offset;
141+
141142
// It might just be a value
142143
if (_scanner.ReadChar('='))
143144
{
@@ -147,13 +148,13 @@ private Shortcode ParseShortcode()
147148
{
148149
arguments ??= CreateArgumentsDictionary();
149150

150-
arguments[argumentName] = Character.DecodeString(new TextSpan(_scanner.Buffer, _result.Start + 1, _result.Length - 2)).ToString();
151+
arguments[argumentName] = Character.DecodeString(_result)[1..^1].ToString();
151152
}
152-
else if (_scanner.ReadValue(out _result))
153+
else if (_scanner.ReadValue(out var textSpan))
153154
{
154155
arguments ??= CreateArgumentsDictionary();
155156

156-
arguments[argumentName] = _result.GetText();
157+
arguments[argumentName] = textSpan.ToString();
157158
}
158159
else
159160
{
@@ -168,11 +169,11 @@ private Shortcode ParseShortcode()
168169

169170
_scanner.Cursor.ResetPosition(argumentStart);
170171

171-
if (_scanner.ReadValue(out _result))
172+
if (_scanner.ReadValue(out var textSpan))
172173
{
173174
arguments ??= CreateArgumentsDictionary();
174175

175-
arguments[argumentIndex.ToString()] = _result.GetText();
176+
arguments[argumentIndex.ToString()] = textSpan.ToString();
176177

177178
argumentIndex += 1;
178179
}
@@ -184,11 +185,11 @@ private Shortcode ParseShortcode()
184185
}
185186
}
186187
}
187-
else if (_scanner.ReadValue(out _result))
188+
else if (_scanner.ReadValue(out var textSpan))
188189
{
189190
arguments ??= CreateArgumentsDictionary();
190191

191-
arguments[argumentIndex.ToString()] = _result.GetText();
192+
arguments[argumentIndex.ToString()] = textSpan.ToString();
192193

193194
argumentIndex += 1;
194195
}

src/Shortcodes/ShortcodesProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private async ValueTask<string> FoldClosingTagsAsync(string input, List<Node> no
104104
{
105105
var text = node as RawText;
106106

107-
sb.Builder.Append(text.Buffer, text.Offset, text.Count);
107+
sb.Builder.Append(text.Span);
108108
}
109109

110110
cursor += 1;
@@ -271,7 +271,7 @@ private async Task AppendAsync(StringBuilder builder, string source, Node start,
271271
switch (start)
272272
{
273273
case RawText raw:
274-
builder.Append(raw.Buffer, raw.Offset, raw.Count);
274+
builder.Append(raw.Span);
275275
return;
276276

277277
case Shortcode code:

src/Shortcodes/ShortcodesScanner.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Parlot;
2+
using System;
23

34
namespace Shortcodes
45
{
@@ -9,7 +10,7 @@ public ShortcodesScanner(string buffer) : base(buffer)
910

1011
}
1112

12-
public bool ReadRawText(out TokenResult result)
13+
public bool ReadRawText(out TextSpan result)
1314
{
1415
var start = Cursor.Offset;
1516

@@ -27,26 +28,26 @@ public bool ReadRawText(out TokenResult result)
2728

2829
if (length == 0)
2930
{
30-
result = TokenResult.Fail();
31+
result = null;
3132
return false;
3233
}
3334

34-
result = TokenResult.Succeed(Buffer, start, Cursor.Offset);
35+
result = new TextSpan(Buffer, start, length);
3536

3637
return true;
3738
}
3839

39-
public bool ReadValue(out TokenResult result)
40+
public bool ReadValue(out TextSpan result)
4041
{
4142
if (Cursor.Match(']') || Cursor.Match('\'') || Cursor.Match('"') || Character.IsWhiteSpaceOrNewLine(Cursor.Current))
4243
{
43-
result = TokenResult.Fail();
44+
result = null;
4445
return false;
4546
}
4647

4748
if (Cursor.Match("/]"))
4849
{
49-
result = TokenResult.Fail();
50+
result = null;
5051
return false;
5152
}
5253

@@ -56,14 +57,14 @@ public bool ReadValue(out TokenResult result)
5657
{
5758
if (Cursor.Eof)
5859
{
59-
result = TokenResult.Fail();
60+
result = null;
6061
return false;
6162
}
6263

6364
Cursor.Advance();
6465
}
6566

66-
result = TokenResult.Succeed(Buffer, start, Cursor.Offset);
67+
result = new TextSpan(Buffer, start, Cursor.Offset - start);
6768

6869
return true;
6970
}

tests/Shortcodes.Benchmarks/Shortcodes.Benchmarks.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<IsPackable>false</IsPackable>
77

88
<DebugSymbols>true</DebugSymbols>
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
17+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
1818
</ItemGroup>
1919

2020
</Project>

tests/Shortcodes.Tests/Shortcodes.Tests.csproj

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net5.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66

77
<DebugSymbols>true</DebugSymbols>
88
<DebugType>pdbonly</DebugType>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
13-
<PackageReference Include="xunit" Version="2.4.0" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
15-
<PackageReference Include="coverlet.collector" Version="1.2.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0-release-24352-06" />
13+
<PackageReference Include="xunit" Version="2.9.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.collector" Version="6.0.2">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
1622
</ItemGroup>
1723

1824
<ItemGroup>

tests/Shortcodes.Tests/ShortcodesParserTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private string EncodeNodes(IEnumerable<Node> nodes)
4343
break;
4444

4545
case RawText raw:
46-
_builder.Append($"R({raw.Count})");
46+
_builder.Append($"R({raw.Span.Length})");
4747
break;
4848
}
4949
}
@@ -118,7 +118,8 @@ public void ShouldScanArguments(string input, string encoded)
118118

119119
[Theory]
120120
[InlineData("[hello a='b]", "R(12)")]
121-
[InlineData("[hello '\\a']", "R(12)")]
121+
// \z is not a valid escape sequence
122+
[InlineData("[hello '\\z']", "R(12)")]
122123
public void ShouldIgnoreMalformedArguments(string input, string encoded)
123124
{
124125
var nodes = new ShortcodesParser().Parse(input);
@@ -131,8 +132,8 @@ public void ShouldIgnoreMalformedArguments(string input, string encoded)
131132
[InlineData("[h a='\\u03A9']", "[h a=Ω]")]
132133
[InlineData("[h a='\\xe9']", "[h a=é]")]
133134
[InlineData("[h a='\\xE9']", "[h a=é]")]
134-
// This is not a valid string (invalid escape sequence), and not a valid value as it start with '
135-
[InlineData("[h a='\\a']", "R(10)")]
135+
// This is not a valid string (invalid escape sequence), and not a valid value as it starts with '
136+
[InlineData("[h a='\\z']", "R(10)")]
136137
[InlineData("[h a='\\\\']", "[h a=\\]")]
137138
[InlineData("[h a='\\\"']", "[h a=\"]")]
138139
[InlineData("[h a='\\\'']", "[h a=']")]

0 commit comments

Comments
 (0)