Skip to content
This repository has been archived by the owner on Dec 1, 2017. It is now read-only.

Commit

Permalink
complex string splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Nov 3, 2012
1 parent 8552996 commit 80ccb34
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/underscore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -747,17 +747,39 @@ function _.tap(value, func)
return value
end

function _.split(value)
if _.isString(value) then
local values = {}
string.gsub(value, "[^%s+]", function(c)
table.insert(values, c)
end)

return values
function splitIterator(value, pattern, start)
if pattern then
return string.find(value, pattern, start)
else
return {}
if start > string.len(value) then
return nil
else
return start+1, start
end
end
end


function _.split(value, pattern)
if not _.isString(value) then return {} end
local values = {}
local start = 1
local start_pattern, end_pattern = splitIterator(value, pattern, start)

while start_pattern do
table.insert(
values,
string.sub(value, start, start_pattern - 1)
)
start = end_pattern + 1
start_pattern, end_pattern = splitIterator(value, pattern, start)
end

if start <= string.len(value) then
table.insert(values, string.sub(value, start))
end

return values
end

function _.chain(value)
Expand Down
17 changes: 17 additions & 0 deletions spec/string_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,22 @@ describe("string functions", function()
assert.same(_.split(123),{})
assert.same(_.split(function() end),{})
end)

describe("with a specific split string", function()
it("can split a specific characters", function()
assert.same(_.split("a|b|c|d", "|"), {"a","b","c","d"})
assert.same(_.split("a 1 c 2", " "), {"a","1","c","2"})
assert.same(_.split("a\tb\tc\td", "\t"), {"a","b","c","d"})
end)

it("can split using Lua pattern matchers", function()
assert.same(_.split("a b c d", "%s+"), {"a","b","c","d"})
assert.same(_.split("a1b2c3d4e", "%d"), {"a","b","c","d","e"})
end)

it("can split on whole words", function()
assert.same(_.split("arabbitbrabbitc", "rabbit"), {"a","b","c"})
end)
end)
end)
end)

0 comments on commit 80ccb34

Please sign in to comment.