Browse Source

Fix regexp splitting in lua.

tags/0.9.0
Vsevolod Stakhov 9 years ago
parent
commit
2e14aa473e
2 changed files with 16 additions and 3 deletions
  1. 11
    0
      src/lua/lua_regexp.c
  2. 5
    3
      test/lua/unit/regxep.lua

+ 11
- 0
src/lua/lua_regexp.c View File

@@ -491,6 +491,17 @@ lua_regexp_split (lua_State *L)
}
old_start = end;
}

if (len > 0 && (end == NULL || end < data + len)) {
if (end == NULL) {
end = data;
}

lua_pushlstring (L, end, (data + len) - end);
lua_rawseti (L, -2, ++i);
matched = TRUE;
}

if (!matched) {
lua_pop (L, 1);
lua_pushnil (L);

+ 5
- 3
test/lua/unit/regxep.lua View File

@@ -34,10 +34,12 @@ context("Regexp unit tests", function()
test("Regexp split", function()
local cases = {
{'\\s', 'one', {'one'}}, -- one arg
{'\\s', 'one two', {'one', 'two'}}, -- trivial
{'/,/i', '1,2', {'1', '2'}}, -- trivial
{'\\s', 'one two', {'one', 'two'}}, -- multiple delimiters
{'\\s', ' one two ', {'one', 'two'}}, -- multiple delimiters
{'\\s', ' one ', {'one', 'two'}}, -- multiple delimiters
{'\\s', ' one ', {'one'}}, -- multiple delimiters
{'[:,]', ',,,:::one,two,,', {'one', 'two'}}, -- multiple delimiters
}
@@ -48,8 +50,8 @@ context("Regexp unit tests", function()
local res = r:split(c[2])
assert_not_nil(res, "cannot split " .. c[2])
for i,r in ipairs(res) do
assert_equal(r, c[3][i])
for i,r in ipairs(c[3]) do
assert_equal(res[i], r)
end
end
end)

Loading…
Cancel
Save