Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Lua/CodeAnalysis/Syntax/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ public bool MoveNext()
if (c is '\\')
{
Advance(1);

if (span.Length <= offset) break;
if (span[offset] == '\r')
{
if (span.Length<=offset +1) continue;
if (span[offset+1] == '\n')Advance(1);
}
}
else if (c == quote)
{
Expand Down Expand Up @@ -530,8 +536,8 @@ void ReadNumber(ref ReadOnlySpan<char> span, ref int offset, out int readCount)
static bool IsIdentifier(char c)
{
return c == '_' ||
('A' <= c && c <= 'Z') ||
('a' <= c && c <= 'z') ||
StringHelper.IsNumber(c);
('A' <= c && c <= 'Z') ||
('a' <= c && c <= 'z') ||
StringHelper.IsNumber(c);
}
}
}
7 changes: 4 additions & 3 deletions src/Lua/Internal/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static bool TryFromStringLiteral(ReadOnlySpan<char> literal, [NotNullWhen
builder.Append('\n');
break;
case '\r':
builder.Append('\r');
builder.Append('\n');
// check CRLF
if (i + 1 < literal.Length && literal[i + 1] is '\n')
{
Expand Down Expand Up @@ -306,6 +306,7 @@ public static Regex ToRegex(ReadOnlySpan<char> pattern)
builder.Append(c);
break;
}

isEscapeSequence = false;
}
}
Expand Down Expand Up @@ -358,7 +359,7 @@ public static bool IsNumber(char c)
public static bool IsDigit(char c)
{
return IsNumber(c) ||
('a' <= c && c <= 'f') ||
('A' <= c && c <= 'F');
('a' <= c && c <= 'f') ||
('A' <= c && c <= 'F');
}
}
17 changes: 17 additions & 0 deletions tests/Lua.Tests/StringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Lua.CodeAnalysis.Syntax;
using Lua.CodeAnalysis.Syntax.Nodes;

namespace Lua.Tests;

public class StringTests
{
[TestCase("\r")]
[TestCase("\n")]
[TestCase("\r\n")]
public async Task Test_ShortString_RealNewLine(string newLine)
{
var result = await LuaState.Create().DoStringAsync($"return \"\\{newLine}\"");
Assert.That(result, Has.Length.EqualTo(1));
Assert.That(result[0], Is.EqualTo(new LuaValue("\n")));
}
}