summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-06-05 00:56:33 +0800
committerruki <[email protected]>2019-06-04 23:39:23 +0800
commitbd551e18b434c3f8eb018680002e122377e73887 (patch)
tree3bec04d4b3ae4947406204969d5516ff9901e646
parent18663d52e5ff484eea423f88cbf52cc10c5721f3 (diff)
improve string.split to support limit
-rw-r--r--tests/test.lua2
-rw-r--r--xmake/core/base/string.lua7
2 files changed, 9 insertions, 0 deletions
diff --git a/tests/test.lua b/tests/test.lua
index faa7755e0..064045db9 100644
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -22,6 +22,8 @@ end
-- main entry
function main(name)
+ assert(false, "dadasd%s", "3")
+
-- disable statistics
os.setenv("XMAKE_STATS", "false")
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua
index 878a2f683..d031a228f 100644
--- a/xmake/core/base/string.lua
+++ b/xmake/core/base/string.lua
@@ -163,11 +163,18 @@ end
-- ("1\n\n2\n3"):split('\n', {plain = true, strict = true}) => 1, , 2, 3
-- ("abc123123xyz123abc"):split('123', {plain = true, strict = true}) => abc, , xyz, abc
--
+-- limit split count
+-- ("1\n\n2\n3"):split('\n', {limit = 2}) => 1, 2\n3
+-- ("1.2.3.4.5"):split('%.', {limit = 3}) => 1, 2, 3.4.5
+--
function string:split(delimiter, opt)
local result = {}
local start = 1
local pos, epos = self:find(delimiter, start, opt and opt.plain)
while pos do
+ if opt and opt.limit and opt.limit > 0 and #result + 1 >= opt.limit then
+ break
+ end
local substr = self:sub(start, pos - 1)
if #substr > 0 then
table.insert(result, substr)