diff options
| author | ruki <[email protected]> | 2020-03-01 21:48:50 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-03-01 21:48:50 +0800 |
| commit | fc36d8022fed422d34498bc111cd69bff5b08c60 (patch) | |
| tree | c47cbaa506566e1e6c22cf06c383f802a5d7ef18 /xmake/core/base/string.lua | |
| parent | c68fa66a89dcce3bf589703435c01e7aa8b2ef2b (diff) | |
improve split
Diffstat (limited to 'xmake/core/base/string.lua')
| -rw-r--r-- | xmake/core/base/string.lua | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua index 8cdca335f..97a950bb8 100644 --- a/xmake/core/base/string.lua +++ b/xmake/core/base/string.lua @@ -72,23 +72,29 @@ end -- ("1.2.3.4.5"):split('%.', {limit = 3}) => 1, 2, 3.4.5 -- function string:split(delimiter, opt) - local result = {} + local limit, plain, strict + if opt then + limit = opt.limit + plain = opt.plain + strict = opt.strict + end local start = 1 - local pos, epos = self:find(delimiter, start, opt and opt.plain) + local result = {} + local pos, epos = self:find(delimiter, start, plain) while pos do local substr = self:sub(start, pos - 1) - if (#substr > 0) or (opt and opt.strict) then - if opt and opt.limit and opt.limit > 0 and #result + 1 >= opt.limit then + if (#substr > 0) or strict then + if limit and limit > 0 and #result + 1 >= imit then break end table.insert(result, substr) end start = epos + 1 - pos, epos = self:find(delimiter, start, opt and opt.plain) + pos, epos = self:find(delimiter, start, plain) end if start <= #self then table.insert(result, self:sub(start)) - elseif opt and opt.strict and (not opt.limit or #result < opt.limit) then + elseif strict and (not limit or #result < limit) then if start == #self + 1 then table.insert(result, "") end |
