Skip to content

Commit

Permalink
Fix escaped numbers parsing (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Jan 7, 2025
1 parent 5b8f910 commit 35d3f36
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
8.0.x
global-json-file: global.json

- name: Test
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
8.0.x
global-json-file: global.json

- name: Test
Expand Down
18 changes: 12 additions & 6 deletions src/Parlot/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ private bool ReadQuotedString(char quoteChar, out ReadOnlySpan<char> result)
// We can read Eof if there is an escaped quote sequence and no actual end quote, e.g. "'abc\'def"
if (Cursor.Eof)
{
Cursor.ResetPosition(start);

result = [];
return false;
}
Expand Down Expand Up @@ -585,11 +587,13 @@ private bool ReadQuotedString(char quoteChar, out ReadOnlySpan<char> result)
// https://stackoverflow.com/a/32175520/142772
// exactly 4 digits
#if NET8_0_OR_GREATER
var lastHexIndex = Cursor.Span.Slice(0, 4).LastIndexOfAny(Character._hexDigits);
var isValidUnicode = lastHexIndex == 3;
var allHexDigits = Cursor.Span.Length > 4 && Cursor.Span.Slice(1, 4).IndexOfAnyExcept(Character._hexDigits) == -1;
var isValidUnicode = allHexDigits;

if (!isValidUnicode)
{
Cursor.ResetPosition(start);

result = [];
return false;
}
Expand Down Expand Up @@ -629,19 +633,21 @@ private bool ReadQuotedString(char quoteChar, out ReadOnlySpan<char> result)
break;
case 'x':

// At least two digits
// At least one digits
#if NET8_0_OR_GREATER
lastHexIndex = Cursor.Span.Slice(0, 4).LastIndexOfAny(Character._hexDigits);
var isValidHex = lastHexIndex > 0;
var firstNonHexDigit = Cursor.Span.Length > 1 ? Cursor.Span.Slice(1).IndexOfAnyExcept(Character._hexDigits) : -1;
var isValidHex = firstNonHexDigit > 0;

if (!isValidHex)
{
Cursor.ResetPosition(start);

result = [];
return false;
}

// Advance the cursor for the read digits
Cursor.Advance(lastHexIndex + 1);
Cursor.Advance(firstNonHexDigit);
#else
var isValidHex = false;

Expand Down
33 changes: 32 additions & 1 deletion test/Parlot.Tests/ScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,41 @@ public void ShouldReadStringsWithLineBreaks(string text, string expected)
[InlineData("' \\xa0 ' ", "' \\xa0 '")]
[InlineData("' \\xfh ' ", "' \\xfh '")]
[InlineData("' \\u1234 ' ", "' \\u1234 '")]

public void ShouldReadUnicodeSequence(string text, string expected)
{
new Scanner(text).ReadQuotedString(out var result);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("'\\u'")]
[InlineData("'\\u1'")]
[InlineData("'\\u12'")]
[InlineData("'\\u123'")]
[InlineData("'\\ug'")]
[InlineData("'\\u1g'")]
[InlineData("'\\u12g'")]
[InlineData("'\\u123g'")]
[InlineData("'\\x'")]
[InlineData("'\\xg'")]
public void ShouldNotParseInvalidEscapedNumbers(string input)
{
var s = new Scanner(input);
Assert.False(s.ReadQuotedString());
Assert.Equal(0, s.Cursor.Position.Offset);
}

[Theory]
[InlineData("'\\u1234'")]
[InlineData("'\\u12345'")]
[InlineData("'\\x1'")]
[InlineData("'\\x12'")]
[InlineData("'\\x123'")]
[InlineData("'\\x1234'")]
[InlineData("'\\x1234g'")]
public void ShouldParseValidEscapedNumbers(string input)
{
var s = new Scanner(input);
Assert.True(s.ReadQuotedString());
}
}

0 comments on commit 35d3f36

Please sign in to comment.