diff options
| author | ruki <[email protected]> | 2019-06-05 00:56:33 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-06-04 23:39:23 +0800 |
| commit | bd551e18b434c3f8eb018680002e122377e73887 (patch) | |
| tree | 3bec04d4b3ae4947406204969d5516ff9901e646 /xmake/core/base/string.lua | |
| parent | 18663d52e5ff484eea423f88cbf52cc10c5721f3 (diff) | |
improve string.split to support limit
Diffstat (limited to 'xmake/core/base/string.lua')
| -rw-r--r-- | xmake/core/base/string.lua | 7 |
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) |
