Fix string.gsub behaviour with negative n

This commit is contained in:
Enrico Horn
2021-07-09 23:46:34 +02:00
parent 6b9dece367
commit 560a4694e4
5 changed files with 5 additions and 0 deletions

View File

@@ -640,6 +640,8 @@ public class StringLib extends TwoArgFunction {
int lastmatch = -1; /* end of last match */
LuaValue repl = args.arg(3);
int max_s = args.optint(4, srclen+1);
if (max_s < 0)
max_s = srclen+1;
final boolean anchor = p.length() > 0 && p.charAt(0) == '^';
Buffer lbuf = new Buffer(srclen);

View File

@@ -21,6 +21,9 @@ print( string.match( "abbaaababaabaaabaa", "b(a*)()b", 12 ) )
print( string.byte("hi", -3) )
print( string.gsub("ABC ABC ABC", "ABC", "DEF", -1) )
print( string.gsub("ABC ABC ABC", "ABC", "DEF", 0) )
print( string.gsub("ABC ABC ABC", "ABC", "DEF", 2) )
print( string.gsub("ABC", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("@123", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123", "@(%x+)", function(s) return "|abcd" end) )