Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stevedonovan/Penlight.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2019-04-29 01:29:15 +0300
committerThijs Schreijer <thijs@thijsschreijer.nl>2019-04-29 02:02:05 +0300
commit66e85997fa536fdff65fc101253be5dc5f709207 (patch)
tree91f55197ed651df78401cc1c4b07d893c78849ad
parentf9f06de79e87f64a839bc01cb2d80201b53e3047 (diff)
fix(stringx) do not append empty string if hit number of matches
-rw-r--r--CHANGELOG.md2
-rw-r--r--lua/pl/stringx.lua4
-rw-r--r--tests/test-stringx.lua1
3 files changed, 6 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1864e02..85d1d90 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 ce67095..91d0ef0 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 8052f28..b8e5bd4 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(''))