Skip to content

Commit d30310e

Browse files
authored
Merge pull request #3 from masaedw/fix-stringlib
Merge moonsharp-devs#184
2 parents c33ecef + 3ec2191 commit d30310e

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

src/MoonSharp.Interpreter.Tests/EndToEnd/StringLibTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,39 @@ public void String_Match_1()
248248
TestMatch(s, p, true);
249249
}
250250

251+
[Test]
252+
public void String_Match_2()
253+
{
254+
string s = "糸筍"; // U+7CF8, U+7B4D
255+
string p = "書籍"; // U+66F8, U+7C4D
256+
257+
TestMatch(s, p, false);
258+
}
259+
260+
[Test]
261+
public void String_Match_3()
262+
{
263+
// maxexpand
264+
string s = "書籍籍筍筍筍"; // U+66F8, U+7C4D, U+7B4D, ...
265+
string p = "書籍+"; // U+66F8, U+7C4D
266+
267+
Script S = new Script(CoreModules.String);
268+
S.Globals["s"] = s;
269+
S.Globals["p"] = p;
270+
DynValue res = S.DoString("return string.match(s, p)");
271+
272+
Utils.DynAssert(res, "書籍籍");
273+
}
274+
275+
[Test]
276+
public void String_Match_4()
277+
{
278+
string s = "㍝"; // U+335D
279+
string p = "[Aそ]"; // U+005B, U+0041, U+305D, U+005D
280+
281+
TestMatch(s, p, false);
282+
}
283+
251284
private void TestMatch(string s, string p, bool expected)
252285
{
253286
Script S = new Script(CoreModules.String);

src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ private static int matchbracketclass(int c, CharPtr p, CharPtr ec)
203203
else if ((p[1] == '-') && (p + 2 < ec))
204204
{
205205
p += 2;
206-
if ((byte)((p[-2])) <= c && (c <= (byte)p[0]))
206+
if (p[-2] <= c && (c <= p[0]))
207207
return sig;
208208
}
209-
else if ((byte)(p[0]) == c) return sig;
209+
else if (p[0] == c) return sig;
210210
}
211211
return (sig == 0) ? 1 : 0;
212212
}
@@ -219,11 +219,10 @@ private static int singlematch(int c, CharPtr p, CharPtr ep)
219219
case '.': return 1; /* matches any char */
220220
case L_ESC: return match_class((char)c, (char)(p[1]));
221221
case '[': return matchbracketclass(c, p, ep - 1);
222-
default: return ((byte)(p[0]) == c) ? 1 : 0;
222+
default: return (p[0] == c) ? 1 : 0;
223223
}
224224
}
225225

226-
227226
private static CharPtr matchbalance(MatchState ms, CharPtr s,
228227
CharPtr p)
229228
{
@@ -247,12 +246,11 @@ private static CharPtr matchbalance(MatchState ms, CharPtr s,
247246
return null; /* string ends out of balance */
248247
}
249248

250-
251249
private static CharPtr max_expand(MatchState ms, CharPtr s,
252250
CharPtr p, CharPtr ep)
253251
{
254252
ptrdiff_t i = 0; /* counts maximum expand for item */
255-
while ((s + i < ms.src_end) && (singlematch((byte)(s[i]), p, ep) != 0))
253+
while ((s + i < ms.src_end) && (singlematch(s[i], p, ep) != 0))
256254
i++;
257255
/* keeps trying to match with the maximum repetitions */
258256
while (i >= 0)
@@ -273,7 +271,7 @@ private static CharPtr min_expand(MatchState ms, CharPtr s,
273271
CharPtr res = match(ms, s, ep + 1);
274272
if (res != null)
275273
return res;
276-
else if ((s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0))
274+
else if ((s < ms.src_end) && (singlematch(s[0], p, ep) != 0))
277275
s = s.next(); /* try with one more repetition */
278276
else return null;
279277
}
@@ -359,22 +357,22 @@ private static CharPtr match(MatchState ms, CharPtr s, CharPtr p)
359357
LUA_QL("%f") + " in pattern");
360358
ep = classend(ms, p); /* points to what is next */
361359
previous = (s == ms.src_init) ? '\0' : s[-1];
362-
if ((matchbracketclass((byte)(previous), p, ep - 1) != 0) ||
363-
(matchbracketclass((byte)(s[0]), p, ep - 1) == 0)) return null;
360+
if ((matchbracketclass(previous, p, ep - 1) != 0) ||
361+
(matchbracketclass(s[0], p, ep - 1) == 0)) return null;
364362
p = ep; goto init; /* else return match(ms, s, ep); */
365363
}
366364
default:
367365
{
368366
if (isdigit((char)(p[1])))
369367
{ /* capture results (%0-%9)? */
370-
s = match_capture(ms, s, (byte)(p[1]));
368+
s = match_capture(ms, s, p[1]);
371369
if (s == null) return null;
372370
p += 2; goto init; /* else return match(ms, s, p+2) */
373371
}
374372
//ismeretlen hiba miatt lett ide átmásolva
375373
{ /* it is a pattern item */
376374
CharPtr ep = classend(ms, p); /* points to what is next */
377-
int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0;
375+
int m = (s < ms.src_end) && (singlematch(s[0], p, ep) != 0) ? 1 : 0;
378376
switch (ep[0])
379377
{
380378
case '?':
@@ -421,7 +419,7 @@ private static CharPtr match(MatchState ms, CharPtr s, CharPtr p)
421419
dflt:
422420
{ /* it is a pattern item */
423421
CharPtr ep = classend(ms, p); /* points to what is next */
424-
int m = (s < ms.src_end) && (singlematch((byte)(s[0]), p, ep) != 0) ? 1 : 0;
422+
int m = (s < ms.src_end) && (singlematch(s[0], p, ep) != 0) ? 1 : 0;
425423
switch (ep[0])
426424
{
427425
case '?':
@@ -859,15 +857,15 @@ private static CharPtr scanformat(LuaState L, CharPtr strfrmt, CharPtr form)
859857
while (p[0] != '\0' && strchr(FLAGS, p[0]) != null) p = p.next(); /* skip flags */
860858
if ((uint)(p - strfrmt) >= (FLAGS.Length + 1))
861859
LuaLError(L, "invalid format (repeated flags)");
862-
if (isdigit((byte)(p[0]))) p = p.next(); /* skip width */
863-
if (isdigit((byte)(p[0]))) p = p.next(); /* (2 digits at most) */
860+
if (isdigit(p[0])) p = p.next(); /* skip width */
861+
if (isdigit(p[0])) p = p.next(); /* (2 digits at most) */
864862
if (p[0] == '.')
865863
{
866864
p = p.next();
867-
if (isdigit((byte)(p[0]))) p = p.next(); /* skip precision */
868-
if (isdigit((byte)(p[0]))) p = p.next(); /* (2 digits at most) */
865+
if (isdigit(p[0])) p = p.next(); /* skip precision */
866+
if (isdigit(p[0])) p = p.next(); /* (2 digits at most) */
869867
}
870-
if (isdigit((byte)(p[0])))
868+
if (isdigit(p[0]))
871869
LuaLError(L, "invalid format (width or precision too long)");
872870
form[0] = '%';
873871
form = form.next();

0 commit comments

Comments
 (0)