diff options
| author | Opportunity <[email protected]> | 2020-01-20 20:26:08 +0800 |
|---|---|---|
| committer | Opportunity <[email protected]> | 2020-01-20 22:21:47 +0800 |
| commit | 929d2d1e48456e45118754ded06dd0dd420415f1 (patch) | |
| tree | 52599db7bfe57b76e02802253d88cd7177b88985 | |
| parent | ab317d02fb8442204813b5b66457406498e48c1c (diff) | |
fix string.split
| -rw-r--r-- | tests/modules/string/test.lua | 9 | ||||
| -rw-r--r-- | xmake/core/base/string.lua | 6 |
2 files changed, 14 insertions, 1 deletions
diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua index 538dfd893..57e00dd7e 100644 --- a/tests/modules/string/test.lua +++ b/tests/modules/string/test.lua @@ -66,14 +66,23 @@ function test_split(t) t:are_equal(("1\n\n2\n3"):split('\n', {strict = true}), {"1", "", "2", "3"}) t:are_equal(("abc123123xyz123abc"):split('123', {strict = true}), {"abc", "", "xyz", "abc"}) t:are_equal(("abc123123xyz123abc"):split('[123]+', {strict = true}), {"abc", "xyz", "abc"}) + t:are_equal(("123abc123123xyz123abc123"):split('[123]+', {strict = true}), {"", "abc", "xyz", "abc", ""}) + t:are_equal(("123123"):split('[123]+', {strict = true}), {"", ""}) + t:are_equal((""):split('[123]+', {strict = true}), {""}) -- plain match and contains empty string t:are_equal(("1\n\n2\n3"):split('\n', {plain = true, strict = true}), {"1", "", "2", "3"}) t:are_equal(("abc123123xyz123abc"):split('123', {plain = true, strict = true}), {"abc", "", "xyz", "abc"}) + t:are_equal(("123abc123123xyz123abc123"):split('123', {plain = true, strict = true}), {"", "abc", "", "xyz", "abc", ""}) + t:are_equal(("123"):split('123', {plain = true, strict = true}), {"", ""}) + t:are_equal((""):split('123', {plain = true, strict = true}), {""}) -- limit split count t:are_equal(("1\n\n2\n3"):split('\n', {limit = 2}), {"1", "2\n3"}) t:are_equal(("1\n\n2\n3"):split('\n', {limit = 2, strict = true}), {"1", "\n2\n3"}) + t:are_equal(("\n1\n\n2\n3"):split('\n', {limit = 2, strict = true}), {"", "1\n\n2\n3"}) t:are_equal(("1.2.3.4.5"):split('%.', {limit = 3}), {"1", "2", "3.4.5"}) t:are_equal(("123.45"):split('%.', {limit = 3}), {"123", "45"}) + t:are_equal((""):split('123', {plain = true, limit = 2, strict = true}), {""}) + t:are_equal(("123123"):split('123', {plain = true, limit = 2, strict = true}), {"", "123"}) end diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua index e1b96fb75..8cdca335f 100644 --- a/xmake/core/base/string.lua +++ b/xmake/core/base/string.lua @@ -74,7 +74,7 @@ end function string:split(delimiter, opt) local result = {} local start = 1 - local pos, epos = self:find(delimiter, start, opt and opt.plain) + local pos, epos = self:find(delimiter, start, opt and opt.plain) while pos do local substr = self:sub(start, pos - 1) if (#substr > 0) or (opt and opt.strict) then @@ -88,6 +88,10 @@ function string:split(delimiter, opt) 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 + if start == #self + 1 then + table.insert(result, "") + end end return result end |
