From 264659bd53e92969a1e17d65c0266597cde24b5d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Jan 2019 14:22:32 -0200 Subject: Optional 'init' argument to 'string.gmatch' The function 'string.gmatch' now has an optional 'init' argument, similar to 'string.find' and 'string.match'. Moreover, there was some reorganization in the manipulation of indices in the string library. This commit also includes small janitorial work in the manual and in comments in the interpreter loop. --- testes/pm.lua | 29 +++++++++++++++++++++++++++++ testes/strings.lua | 5 +++++ testes/tpack.lua | 2 -- 3 files changed, 34 insertions(+), 2 deletions(-) (limited to 'testes') diff --git a/testes/pm.lua b/testes/pm.lua index 1afaccf6..8cc8772e 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -297,6 +297,35 @@ for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end assert(a == 3) +do -- init parameter in gmatch + local s = 0 + for k in string.gmatch("10 20 30", "%d+", 3) do + s = s + tonumber(k) + end + assert(s == 50) + + s = 0 + for k in string.gmatch("11 21 31", "%d+", -4) do + s = s + tonumber(k) + end + assert(s == 32) + + -- there is an empty string at the end of the subject + s = 0 + for k in string.gmatch("11 21 31", "%w*", 9) do + s = s + 1 + end + assert(s == 1) + + -- there are no empty strings after the end of the subject + s = 0 + for k in string.gmatch("11 21 31", "%w*", 10) do + s = s + 1 + end + assert(s == 0) +end + + -- tests for `%f' (`frontiers') assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x") diff --git a/testes/strings.lua b/testes/strings.lua index 587a0e06..88480924 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -94,6 +94,11 @@ assert(string.char(string.byte("\xe4l\0 assert(string.char(string.byte("\xe4l\0ķu", 1, 0)) == "") assert(string.char(string.byte("\xe4l\0ķu", -10, 100)) == "\xe4l\0ķu") +checkerror("out of range", string.char, 256) +checkerror("out of range", string.char, -1) +checkerror("out of range", string.char, math.maxinteger) +checkerror("out of range", string.char, math.mininteger) + assert(string.upper("ab\0c") == "AB\0C") assert(string.lower("\0ABCc%$") == "\0abcc%$") assert(string.rep('teste', 0) == '') diff --git a/testes/tpack.lua b/testes/tpack.lua index 4c5fc7f7..2b9953f8 100644 --- a/testes/tpack.lua +++ b/testes/tpack.lua @@ -314,9 +314,7 @@ do -- testing initial position for i = 1, #x + 1 do assert(unpack("c0", x, i) == "") end - checkerror("out of string", unpack, "c0", x, 0) checkerror("out of string", unpack, "c0", x, #x + 2) - checkerror("out of string", unpack, "c0", x, -(#x + 1)) end -- cgit v1.2.3-55-g6feb