From 88be9c4d2278c21410e93cf44f1cfbd3feb9cf39 Mon Sep 17 00:00:00 2001 From: Masayuki Muto Date: Wed, 15 Feb 2017 18:32:41 +0900 Subject: [PATCH 1/3] Fix: string.match ignores upper case of chars --- .../EndToEnd/StringLibTests.cs | 24 +++++++++++++++++++ .../CoreLib/StringLib/KopiLua_StrLib.cs | 12 ++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs index e20c93a1..5b7301c5 100644 --- a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs +++ b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs @@ -248,6 +248,30 @@ public void String_Match_1() TestMatch(s, p, true); } + [Test] + public void String_Match_2() + { + string s = "糸筍"; // U+7CF8, U+7B4D + string p = "書籍"; // U+66F8, U+7C4D + + TestMatch(s, p, false); + } + + [Test] + public void String_Match_3() + { + // maxexpand + string s = "書籍籍筍筍筍"; // U+66F8, U+7C4D, U+7B4D, ... + string p = "書籍+"; // U+66F8, U+7C4D + + Script S = new Script(CoreModules.String); + S.Globals["s"] = s; + S.Globals["p"] = p; + DynValue res = S.DoString("return string.match(s, p)"); + + Utils.DynAssert(res, "書籍籍"); + } + private void TestMatch(string s, string p, bool expected) { Script S = new Script(CoreModules.String); diff --git a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs index f9b90cb3..2bf8d1ea 100644 --- a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs +++ b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs @@ -219,11 +219,10 @@ private static int singlematch(int c, CharPtr p, CharPtr ep) case '.': return 1; /* matches any char */ case L_ESC: return match_class((char)c, (char)(p[1])); case '[': return matchbracketclass(c, p, ep - 1); - default: return ((byte)(p[0]) == c) ? 1 : 0; + default: return (p[0] == c) ? 1 : 0; } } - private static CharPtr matchbalance(MatchState ms, CharPtr s, CharPtr p) { @@ -247,12 +246,11 @@ private static CharPtr matchbalance(MatchState ms, CharPtr s, return null; /* string ends out of balance */ } - 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 +271,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 +372,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 +419,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 19e41ab301e9502b7902745d06acf9d55b614f21 Mon Sep 17 00:00:00 2001 From: Masayuki Muto Date: Wed, 15 Feb 2017 19:29:49 +0900 Subject: [PATCH 2/3] Fix: string.match bracket pattern --- .../EndToEnd/StringLibTests.cs | 9 +++++++++ .../CoreLib/StringLib/KopiLua_StrLib.cs | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs index 5b7301c5..a6741211 100644 --- a/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs +++ b/src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs @@ -272,6 +272,15 @@ public void String_Match_3() Utils.DynAssert(res, "書籍籍"); } + [Test] + public void String_Match_4() + { + string s = "㍝"; // U+335D + string p = "[Aそ]"; // U+005B, U+0041, U+305D, U+005D + + TestMatch(s, p, false); + } + private void TestMatch(string s, string p, bool expected) { Script S = new Script(CoreModules.String); diff --git a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs index 2bf8d1ea..728280a6 100644 --- a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs +++ b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs @@ -203,10 +203,10 @@ private static int matchbracketclass(int c, CharPtr p, CharPtr ec) else if ((p[1] == '-') && (p + 2 < ec)) { p += 2; - if ((byte)((p[-2])) <= c && (c <= (byte)p[0])) + if (p[-2] <= c && (c <= p[0])) return sig; } - else if ((byte)(p[0]) == c) return sig; + else if (p[0] == c) return sig; } return (sig == 0) ? 1 : 0; } From 3ec21918e5985e7090341a95aa089e9394ebdd1b Mon Sep 17 00:00:00 2001 From: Masayuki Muto Date: Wed, 15 Feb 2017 19:42:17 +0900 Subject: [PATCH 3/3] Fix other ignoring upper byte of chars --- .../CoreLib/StringLib/KopiLua_StrLib.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs index 728280a6..e18227f7 100644 --- a/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs +++ b/src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs @@ -357,15 +357,15 @@ private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) LUA_QL("%f") + " in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms.src_init) ? '\0' : s[-1]; - if ((matchbracketclass((byte)(previous), p, ep - 1) != 0) || - (matchbracketclass((byte)(s[0]), p, ep - 1) == 0)) return null; + if ((matchbracketclass(previous, p, ep - 1) != 0) || + (matchbracketclass(s[0], p, ep - 1) == 0)) return null; p = ep; goto init; /* else return match(ms, s, ep); */ } default: { if (isdigit((char)(p[1]))) { /* capture results (%0-%9)? */ - s = match_capture(ms, s, (byte)(p[1])); + s = match_capture(ms, s, p[1]); if (s == null) return null; p += 2; goto init; /* else return match(ms, s, p+2) */ } @@ -857,15 +857,15 @@ private static CharPtr scanformat(LuaState L, CharPtr strfrmt, CharPtr form) while (p[0] != '\0' && strchr(FLAGS, p[0]) != null) p = p.next(); /* skip flags */ if ((uint)(p - strfrmt) >= (FLAGS.Length + 1)) LuaLError(L, "invalid format (repeated flags)"); - if (isdigit((byte)(p[0]))) p = p.next(); /* skip width */ - if (isdigit((byte)(p[0]))) p = p.next(); /* (2 digits at most) */ + if (isdigit(p[0])) p = p.next(); /* skip width */ + if (isdigit(p[0])) p = p.next(); /* (2 digits at most) */ if (p[0] == '.') { p = p.next(); - if (isdigit((byte)(p[0]))) p = p.next(); /* skip precision */ - if (isdigit((byte)(p[0]))) p = p.next(); /* (2 digits at most) */ + if (isdigit(p[0])) p = p.next(); /* skip precision */ + if (isdigit(p[0])) p = p.next(); /* (2 digits at most) */ } - if (isdigit((byte)(p[0]))) + if (isdigit(p[0])) LuaLError(L, "invalid format (width or precision too long)"); form[0] = '%'; form = form.next();