From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 5241823A27 for ; Thu, 20 Dec 2018 13:03:00 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HUA8MGFUB2cT for ; Thu, 20 Dec 2018 13:03:00 -0500 (EST) Received: from mail-40130.protonmail.ch (mail-40130.protonmail.ch [185.70.40.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 92F2423A1C for ; Thu, 20 Dec 2018 13:02:59 -0500 (EST) Date: Thu, 20 Dec 2018 18:02:51 +0000 From: "=?UTF-8?Q?Micha=C5=82_Durak?=" (Redacted sender "gdrbyko1" for DMARC) Subject: [tarantool-patches] [PATCH] lua: add optional 'chars' param to string.strip functions Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: "tarantool-patches@freelists.org" Add optional 'chars' parameter to string.strip, string.lstrip and string.rstrip for specifying the unwanted characters. Behavior modeled after the equivalent Python built-ins. Closes: #2977 --- branch: https://github.com/gdrbyKo1/tarantool/tree/gh-2977 issue: https://github.com/tarantool/tarantool/issues/2977 src/lua/string.lua | 51 ++++++++++++++++++++++++++++++++++++++--= ---- test/app-tap/string.test.lua | 44 +++++++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/lua/string.lua b/src/lua/string.lua index cbce26b35..9eab33423 100644 --- a/src/lua/string.lua +++ b/src/lua/string.lua @@ -339,25 +339,64 @@ local function string_fromhex(inp) return ffi.string(res, len) end -local function string_strip(inp) +local function string_strip(inp, chars) if type(inp) ~=3D 'string' then error(err_string_arg:format(1, "string.strip", 'string', type(inp)= ), 2) end - return (string.gsub(inp, "^%s*(.-)%s*$", "%1")) + if chars =3D=3D nil then + return (string.gsub(inp, "^%s*(.-)%s*$", "%1")) + end + + if type(chars) ~=3D 'string' then + error(err_string_arg:format(2, "string.strip", 'string', type(char= s)), 2) + end + if chars =3D=3D '' then + return inp + end + + chars =3D chars:gsub('[%^%$%(%)%%%.%[%]%*%+%-%?]', "%%%0"):gsub('%z+',= '%%z') + local pattern =3D string.format("^[%s]*(.-)[%s]*$", chars, chars) + return (string.gsub(inp, pattern, "%1")) end -local function string_lstrip(inp) +local function string_lstrip(inp, chars) if type(inp) ~=3D 'string' then error(err_string_arg:format(1, "string.lstrip", 'string', type(inp= )), 2) end - return (string.gsub(inp, "^%s*(.-)", "%1")) + if chars =3D=3D nil then + return (string.gsub(inp, "^%s*(.-)", "%1")) + end + + if type(chars) ~=3D 'string' then + error(err_string_arg:format(2, "string.lstrip", 'string', type(cha= rs)), 2) + end + if chars =3D=3D '' then + return inp + end + + chars =3D chars:gsub('[%^%$%(%)%%%.%[%]%*%+%-%?]', "%%%0"):gsub('%z+',= '%%z') + local pattern =3D string.format("^[%s]*(.-)", chars) + return (string.gsub(inp, pattern, "%1")) end -local function string_rstrip(inp) +local function string_rstrip(inp, chars) if type(inp) ~=3D 'string' then error(err_string_arg:format(1, "string.rstrip", 'string', type(inp= )), 2) end - return (string.gsub(inp, "(.-)%s*$", "%1")) + if chars =3D=3D nil then + return (string.gsub(inp, "(.-)%s*$", "%1")) + end + + if type(chars) ~=3D 'string' then + error(err_string_arg:format(2, "string.rstrip", 'string', type(cha= rs)), 2) + end + if chars =3D=3D '' then + return inp + end + + chars =3D chars:gsub('[%^%$%(%)%%%.%[%]%*%+%-%?]', "%%%0"):gsub('%z+',= '%%z') + local pattern =3D string.format("(.-)[%s]*$", chars) + return (string.gsub(inp, pattern, "%1")) end diff --git a/test/app-tap/string.test.lua b/test/app-tap/string.test.lua index 7203fcd36..6e87d3285 100755 --- a/test/app-tap/string.test.lua +++ b/test/app-tap/string.test.lua @@ -134,18 +134,46 @@ test:test("fromhex", function(test) end) test:test("strip", function(test) - test:plan(6) + test:plan(21) local str =3D " hello hello " - test:is(string.len(string.strip(str)), 11, "strip") - test:is(string.len(string.lstrip(str)), 12, "lstrip") - test:is(string.len(string.rstrip(str)), 13, "rstrip") + test:is(string.len(string.strip(str)), 11, "strip (no chars)") + test:is(string.len(string.lstrip(str)), 12, "lstrip (no chars)") + test:is(string.len(string.rstrip(str)), 13, "rstrip (no chars)") local _, err =3D pcall(string.strip, 12) - test:ok(err and err:match("%(string expected, got number%)")) + test:ok(err and err:match("#1 to '.-%.strip' %(string expected, got nu= mber%)")) _, err =3D pcall(string.lstrip, 12) - test:ok(err and err:match("%(string expected, got number%)")) + test:ok(err and err:match("#1 to '.-%.lstrip' %(string expected, got n= umber%)")) _, err =3D pcall(string.rstrip, 12) - test:ok(err and err:match("%(string expected, got number%)")) -end ) + test:ok(err and err:match("#1 to '.-%.rstrip' %(string expected, got n= umber%)")) + + str =3D "www.example.com/foo" + local chars =3D "w./fomc" + test:is(string.len(string.strip(str, chars)), 7, "strip (chars)") + test:is(string.len(string.lstrip(str, chars)), 15, "lstrip (chars)") + test:is(string.len(string.rstrip(str, chars)), 11, "rstrip (chars)") + + str, chars =3D "^$()%.[]*+-?BEEP^$()%.[]*+-?%", "^$()%.[]*+-?" + test:is(string.len(string.strip(str, chars)), 4, "strip (magic chars)"= ) + test:is(string.len(string.lstrip(str, chars)), 17, "lstrip (magic char= s)") + test:is(string.len(string.rstrip(str, chars)), 16, "rstrip (magic char= s)") + + str, chars =3D "\0\00\000HELLO\000\00\0\0", "\0" + test:is(string.len(string.strip(str, chars)), 5, "strip (chars with em= bedded 0s)") + test:is(string.len(string.lstrip(str, chars)), 9, "lstrip (chars with = embedded 0s)") + test:is(string.len(string.rstrip(str, chars)), 8, "rstrip (chars with = embedded 0s)") + + str, chars =3D " test ", "" + test:is(string.strip(str, chars), str, "strip (0-length chars)") + test:is(string.lstrip(str, chars), str, "lstrip (0-length chars)") + test:is(string.rstrip(str, chars), str, "rstrip (0-length chars)") + + _, err =3D pcall(string.strip, 'foo', 12) + test:ok(err and err:match("#2 to '.-%.strip' %(string expected, got nu= mber%)")) + _, err =3D pcall(string.lstrip, 'bar', 12) + test:ok(err and err:match("#2 to '.-%.lstrip' %(string expected, got n= umber%)")) + _, err =3D pcall(string.rstrip, 'baz', 12) + test:ok(err and err:match("#2 to '.-%.rstrip' %(string expected, got n= umber%)")) +end) test:test("unicode", function(test) test:plan(104) -- 2.11.0