diff options
| author | ruki <[email protected]> | 2019-06-04 22:41:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-06-04 10:08:31 +0800 |
| commit | 18663d52e5ff484eea423f88cbf52cc10c5721f3 (patch) | |
| tree | 3e1e147713b4ca50276b676b9b346f52f893708b /xmake/core/base/string.lua | |
| parent | 255d51be7e7ed10a13a4cd55b5aae55079ab4bb9 (diff) | |
improve string.split
Diffstat (limited to 'xmake/core/base/string.lua')
| -rw-r--r-- | xmake/core/base/string.lua | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua index b27d076d5..878a2f683 100644 --- a/xmake/core/base/string.lua +++ b/xmake/core/base/string.lua @@ -143,19 +143,42 @@ function string:find_last(pattern, plain) end end --- split string with the given characters +-- split string with the given substring/characters -- +-- pattern match and ignore empty string -- ("1\n\n2\n3"):split('\n') => 1, 2, 3 --- ("1\n\n2\n3"):split('\n', true) => 1, , 2, 3 +-- ("abc123123xyz123abc"):split('123') => abc, xyz, abc +-- ("abc123123xyz123abc"):split('[123]+') => abc, xyz, abc -- -function string:split(delimiter, strict) +-- plain match and ignore empty string +-- ("1\n\n2\n3"):split('\n', {plain = true}) => 1, 2, 3 +-- ("abc123123xyz123abc"):split('123', {plain = true}) => abc, xyz, abc +-- +-- pattern match and contains empty string +-- ("1\n\n2\n3"):split('\n', {strict = true}) => 1, , 2, 3 +-- ("abc123123xyz123abc"):split('123', {strict = true}) => abc, , xyz, abc +-- ("abc123123xyz123abc"):split('[123]+', {strict = true}) => abc, xyz, abc +-- +-- plain match and contains empty string +-- ("1\n\n2\n3"):split('\n', {plain = true, strict = true}) => 1, , 2, 3 +-- ("abc123123xyz123abc"):split('123', {plain = true, strict = true}) => abc, , xyz, abc +-- +function string:split(delimiter, opt) local result = {} - if strict then - for match in (self .. delimiter):gmatch("(.-)" .. delimiter) do - table.insert(result, match) + local start = 1 + local pos, epos = self:find(delimiter, start, opt and opt.plain) + while pos do + local substr = self:sub(start, pos - 1) + if #substr > 0 then + table.insert(result, substr) + elseif opt and opt.strict then + table.insert(result, substr) end - else - self:gsub("[^" .. delimiter .."]+", function(v) table.insert(result, v) end) + start = epos + 1 + pos, epos = self:find(delimiter, start, opt and opt.plain) + end + if start <= #self then + table.insert(result, self:sub(start)) end return result end |
