Skip to content

Commit e2f4f9e

Browse files
committed
fix string.match/gsub character lower byte shenanigans
Merged the lovely changes from moonsharp-devs/moonsharp#184 into CYF * Fixed a bug where some `string` library functions would incorrectly match characters that only have the same lower byte as the ones you're trying to match. Fix by masaedw on GitHub
1 parent cc9cfed commit e2f4f9e

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Assets/Plugins/MoonSharp/Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ 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
}
209209
else if ((byte)(p[0]) == c) return sig;
@@ -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)