summaryrefslogtreecommitdiff
path: root/xmake/core/base/string.lua
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 /xmake/core/base/string.lua
parent18663d52e5ff484eea423f88cbf52cc10c5721f3 (diff)
improve string.split to support limit
Diffstat (limited to 'xmake/core/base/string.lua')
-rw-r--r--xmake/core/base/string.lua7
1 files changed, 7 insertions, 0 deletions
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)