Skip to content

Commit

Permalink
Making string.split with empty string as a delimiter return a table c…
Browse files Browse the repository at this point in the history
…ontaining the characters which make the string up. Also test for same (Mudlet#3482)
  • Loading branch information
demonnic authored Mar 19, 2020
1 parent 2890aa5 commit eed17bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/mudlet-lua/lua/StringUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ end
--- Documentation: https://wiki.mudlet.org/w/Manual:String_Functions#string.split
function string:split(delimiter)
delimiter = delimiter or " "
if delimiter == "" then return {self:match( (self:gsub(".", "(.)")) )} end
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
Expand Down
14 changes: 14 additions & 0 deletions src/mudlet-lua/tests/StringUtils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ describe("Tests StringUtils.lua functions", function()
local actual = str:split(":")
assert.same(expected, actual)
end)

it("should default to splitting on a space", function()
local str = "This is a test"
local expected = { "This", "is", "a", "test" }
local actual = str:split()
assert.same(expected, actual)
end)

it("should return a table with the characters that make up the string if empty string is used for the delimiter", function()
local str = "This is a test"
local expected = {"T", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t"}
local actual = str:split("")
assert.same(expected, actual)
end)
end)

describe("string.starts(str, prefix)", function()
Expand Down

0 comments on commit eed17bb

Please sign in to comment.