From 66e85997fa536fdff65fc101253be5dc5f709207 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Mon, 29 Apr 2019 00:29:15 +0200 Subject: [PATCH] fix(stringx) do not append empty string if hit number of matches --- CHANGELOG.md | 2 ++ lua/pl/stringx.lua | 4 +++- tests/test-stringx.lua | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1864e027..85d1d903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ - `types.is_empty` would return true on spaces always, indepedent of the parameter - `types.to_bool` will now compare case-insensitive for the extra passed strings - `app.require_here` will now properly handle an absolute base path + - `stringx.split` will no longer append an empty match if the number of requested + elements has already been reached. ## 1.6.0 (2018-11-23) diff --git a/lua/pl/stringx.lua b/lua/pl/stringx.lua index ce670953..91d0ef01 100644 --- a/lua/pl/stringx.lua +++ b/lua/pl/stringx.lua @@ -190,7 +190,9 @@ function stringx.split(s,re,n) plain = false end local res = usplit(s,re,plain,n) - if re and re ~= '' and find(s,re,-#re,true) then + if re and re ~= '' and + find(s,re,-#re,true) and + (n or math.huge) > #res then res[#res+1] = "" end return makelist(res) diff --git a/tests/test-stringx.lua b/tests/test-stringx.lua index 8052f28e..b8e5bd47 100644 --- a/tests/test-stringx.lua +++ b/tests/test-stringx.lua @@ -150,6 +150,7 @@ asserteq(split('a', 'a'), {''}) asserteq(split(' 1 2 3 '),{'1','2','3'}) asserteq(split('a*bb*c*ddd','*'),{'a','bb','c','ddd'}) asserteq(split('dog:fred:bonzo:alice',':',3), {'dog','fred','bonzo:alice'}) +asserteq(split('dog:fred:bonzo:alice:',':',3), {'dog','fred','bonzo:alice:'}) asserteq(split('///','/'),{'','','',''}) -- capitalize asserteq(T(stringx.capitalize('')), T(''))