From 446d3b0ce87671bae4070e818ff829554edc209e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BB=D1=8C=D1=86=D0=B5=D0=B2=20=D0=98=D0=BB?= =?UTF-8?q?=D1=8C=D1=8F=20=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 3 Jul 2020 11:43:46 +0300 Subject: [PATCH 1/2] Fix bug with gsub and unicode symbol --- .gitignore | 1 + .../EndToEnd/StringLibTests.cs | 9 +++++++++ .../CoreLib/StringLib/KopiLua_StrLib.cs | 8 ++++---- .../src/CoreLib/StringLib/KopiLua_StrLib.cs | 8 ++++---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c2fc1cd9..2dd738d4 100644 --- a/.gitignore +++ b/.gitignore @@ -170,3 +170,4 @@ $RECYCLE.BIN/ # Mac desktop service store files .DS_Store +/.vs diff --git a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs index e20c93a1..cd23007e 100644 --- a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs +++ b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs @@ -238,6 +238,15 @@ public void String_GSub_3() "; DynValue res = S.DoString(script); } + [Test] + public void String_GSub_4() + { + string script = @" + return string.gsub('Ррррр Нннннн Сссссс','%S+', 'Z') + "; + DynValue res = Script.RunString(script); + Assert.AreEqual("Z Z Z", res.Tuple[0].String); + } [Test] public void String_Match_1() diff --git a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs index f9b90cb3..f305169a 100644 --- a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs +++ b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs @@ -252,7 +252,7 @@ private static CharPtr max_expand(MatchState ms, CharPtr s, CharPtr p, CharPtr ep) { ptrdiff_t i = 0; /* counts maximum expand for item */ - while ((s + i < ms.src_end) && (singlematch((byte)(s[i]), p, ep) != 0)) + while ((s + i < ms.src_end) && (singlematch(s[i], p, ep) != 0)) i++; /* keeps trying to match with the maximum repetitions */ while (i >= 0) @@ -273,7 +273,7 @@ private static CharPtr min_expand(MatchState ms, CharPtr s, CharPtr res = match(ms, s, ep + 1); if (res != null) return res; - else if ((s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0)) + else if ((s < ms.src_end) && (singlematch(s[0], p, ep) != 0)) s = s.next(); /* try with one more repetition */ else return null; } @@ -374,7 +374,7 @@ private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) //ismeretlen hiba miatt lett ide átmásolva { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ - int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0; + int m = (s < ms.src_end) && (singlematch(s[0], p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': @@ -421,7 +421,7 @@ private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) dflt: { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ - int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0; + int m = (s < ms.src_end) && (singlematch(s[0], p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': diff --git a/src/MoonSharp.Interpreter/_Projects/MoonSharp.Interpreter.netcore/src/CoreLib/StringLib/KopiLua_StrLib.cs b/src/MoonSharp.Interpreter/_Projects/MoonSharp.Interpreter.netcore/src/CoreLib/StringLib/KopiLua_StrLib.cs index f9b90cb3..f305169a 100644 --- a/src/MoonSharp.Interpreter/_Projects/MoonSharp.Interpreter.netcore/src/CoreLib/StringLib/KopiLua_StrLib.cs +++ b/src/MoonSharp.Interpreter/_Projects/MoonSharp.Interpreter.netcore/src/CoreLib/StringLib/KopiLua_StrLib.cs @@ -252,7 +252,7 @@ private static CharPtr max_expand(MatchState ms, CharPtr s, CharPtr p, CharPtr ep) { ptrdiff_t i = 0; /* counts maximum expand for item */ - while ((s + i < ms.src_end) && (singlematch((byte)(s[i]), p, ep) != 0)) + while ((s + i < ms.src_end) && (singlematch(s[i], p, ep) != 0)) i++; /* keeps trying to match with the maximum repetitions */ while (i >= 0) @@ -273,7 +273,7 @@ private static CharPtr min_expand(MatchState ms, CharPtr s, CharPtr res = match(ms, s, ep + 1); if (res != null) return res; - else if ((s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0)) + else if ((s < ms.src_end) && (singlematch(s[0], p, ep) != 0)) s = s.next(); /* try with one more repetition */ else return null; } @@ -374,7 +374,7 @@ private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) //ismeretlen hiba miatt lett ide átmásolva { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ - int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0; + int m = (s < ms.src_end) && (singlematch(s[0], p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': @@ -421,7 +421,7 @@ private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) dflt: { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ - int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0; + int m = (s < ms.src_end) && (singlematch(s[0], p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': From d7af213330f083b270e2a16c56fd3323f996445c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BB=D1=8C=D1=86=D0=B5=D0=B2=20=D0=98=D0=BB?= =?UTF-8?q?=D1=8C=D1=8F=20=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 3 Jul 2020 13:41:41 +0300 Subject: [PATCH 2/2] Add test to gmatch --- .../EndToEnd/StringLibTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs index cd23007e..31852fa1 100644 --- a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs +++ b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs @@ -27,6 +27,23 @@ public void String_GMatch_1() Assert.AreEqual(DataType.String, res.Type); Assert.AreEqual("HelloLuauser", res.String); } + [Test] + public void String_GMatch_2() + { + string script = @" + s = 'Ррррр Нннннн Сссссс' + words = {} + for word in s:gmatch('%w+') do table.insert(words, word) end + return words + "; + + DynValue res = Script.RunString(script); + Assert.AreEqual(3, res.Table.Length); + Assert.AreEqual("Ррррр", res.Table.Get(1).String); + Assert.AreEqual("Нннннн", res.Table.Get(2).String); + Assert.AreEqual("Сссссс", res.Table.Get(3).String); + + } [Test] public void String_Find_1()