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
8 changes: 7 additions & 1 deletion src/Lua/Standard/Internal/MatchState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,13 @@ static bool MatchClass(char c, char cl)
res = char.IsLower(c);
break;
case 'p':
res = char.IsPunctuation(c);
// Emulate C ispunct; .NET's char.IsPunctuation does not include symbols like '='.
res =
c
is (>= '!' and <= '/')
or (>= ':' and <= '@')
or (>= '[' and <= '`')
or (>= '{' and <= '~');
break;
case 's':
res = char.IsWhiteSpace(c);
Expand Down
44 changes: 44 additions & 0 deletions tests/Lua.Tests/PatternMatchingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,50 @@ public async Task Test_StringGSub_PatternReplacements()
Assert.That(result[1].Read<double>(), Is.EqualTo(1));
}

[Test]
public async Task Test_StringGSub_PunctuationClassCapturesAsciiSymbolCharacters()
{
var state = LuaState.Create();
state.OpenStringLibrary();

var result = await state.DoStringAsync(
"""
local punctuation = [=[!"#$%&'()*+,-./
:;<=>?@
[\]^_`
{|}~]=]
local matched = ''
local count = 0
string.gsub(punctuation, '%p', function(c)
matched = matched .. c
count = count + 1
end)
return matched, count
"""
);

Assert.That(result[0].Read<string>(), Is.EqualTo("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
Assert.That(result[1].Read<double>(), Is.EqualTo(32));

// Lua %p does not capture non-ASCII punctuation characters
result = await state.DoStringAsync(
"""
local text = '’،。、!?'
return string.gsub(text, '%p', 'X')
"""
);

Assert.That(result[0].Read<string>(), Is.EqualTo("’،。、!?"));
Assert.That(result[1].Read<double>(), Is.EqualTo(0));

result = await state.DoStringAsync(
"return string.gsub('abc=xyz', '(%w*)(%p)(%w+)', '%3%2%1-%0')"
);

Assert.That(result[0].Read<string>(), Is.EqualTo("xyz=abc-abc=xyz"));
Assert.That(result[1].Read<double>(), Is.EqualTo(1));
}

[Test]
public async Task Test_StringGSub_FunctionReplacements()
{
Expand Down