This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string.split implementation (#190)
* init * remove .history files * fix bad test and add Roblox's internal tests * change formatting to please linter
- Loading branch information
Showing
3 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
local rbxString = {} | ||
|
||
for key, value in pairs(string) do | ||
rbxString[key] = value | ||
end | ||
|
||
rbxString.split = function(str, sep) | ||
local result = {} | ||
|
||
if sep == "" then | ||
for i = 1, #str do | ||
result[i] = str:sub(i, i) | ||
end | ||
else | ||
if sep == nil then | ||
sep = "," | ||
end | ||
|
||
local count = 1 | ||
local pos = 1 | ||
local a, b = str:find(sep, pos, true) | ||
|
||
while a do | ||
result[count] = str:sub(pos, a - 1) | ||
count = count + 1 | ||
pos = b + 1 | ||
a, b = str:find(sep, pos, true) | ||
end | ||
|
||
result[count] = str:sub(pos) | ||
end | ||
|
||
return result | ||
end | ||
|
||
return rbxString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
local string = import("./string") | ||
|
||
describe("libs.string", function() | ||
describe("split", function() | ||
it("should be a function", function() | ||
assert.is_function(string.split) | ||
end) | ||
|
||
it("should return an array of comma separated strings if sep is nil", function() | ||
assert.are.same({"Hello", "world", "and", "lemur"}, string.split("Hello,world,and,lemur")) | ||
end) | ||
|
||
it("should return an array of all characters in a string if sep is the empty string", function() | ||
assert.are.same({ | ||
"H", | ||
"e", | ||
"l", | ||
"l", | ||
"o", | ||
",", | ||
"w", | ||
"o", | ||
"r", | ||
"l", | ||
"d", | ||
",", | ||
"a", | ||
"n", | ||
"d", | ||
",", | ||
"l", | ||
"e", | ||
"m", | ||
"u", | ||
"r", | ||
}, string.split("Hello,world,and,lemur", "")) | ||
end) | ||
|
||
it("should return an empty table if the string and sep is the empty string", function() | ||
assert.are.same({}, string.split("", "")) | ||
end) | ||
|
||
it("should return the original string in a table if no sep is matched", function() | ||
assert.are.same({"Hello, world"}, string.split("Hello, world", "K")) | ||
assert.are.same({""}, string.split("", " ")) | ||
end) | ||
|
||
it("should return empty strings at the front and back when seps are present there", function() | ||
assert.are.same({"", "Validark", "Osyris", "Vorlias", ""}, string.split("/Validark/Osyris/Vorlias/", "/")) | ||
assert.are.same({"", "Validark", "Osyris", "Vorlias"}, string.split("/Validark/Osyris/Vorlias", "/")) | ||
assert.are.same({"Validark", "Osyris", "Vorlias", ""}, string.split("Validark/Osyris/Vorlias/", "/")) | ||
assert.are.same({"Validark", "Osyris", "Vorlias"}, string.split("Validark/Osyris/Vorlias", "/")) | ||
end) | ||
|
||
it("should allow multi-character separators", function() | ||
assert.are.same({"Hello", "world"}, string.split("Hello, world", ", ")) | ||
end) | ||
|
||
it("should literally interpret Lua character classes", function() | ||
assert.are.same({"Hello, world"}, string.split("Hello, world", "%l")) | ||
assert.are.same({"Hel", "o, world"}, string.split("Hel%lo, world", "%l")) | ||
end) | ||
|
||
it("should match Roblox's internal tests", function() | ||
-- Provided by tiffany352 at https://github.com/LPGhatguy/lemur/pull/190 | ||
local char = string.char | ||
local ZWJ = char(0xe2, 0x80, 0x8d) | ||
assert.are.same({ "" }, string.split("", ",")) | ||
assert.are.same({ "foo", "", "bar" }, string.split("foo,,bar", ",")) | ||
assert.are.same({ "", "foo" }, string.split(",foo", ",")) | ||
assert.are.same({ "foo", "" }, string.split("foo,", ",")) | ||
assert.are.same({ "", "" }, string.split(",", ",")) | ||
assert.are.same({ "", "", "" }, string.split(",,", ",")) | ||
assert.are.same({ "" }, string.split("", "~~~")) | ||
assert.are.same({ "~~" }, string.split("~~", "~~~")) | ||
assert.are.same({ "~~ ~~" }, string.split("~~ ~~", "~~~")) | ||
assert.are.same({ "foo", "bar" }, string.split("foo~~~bar", "~~~")) | ||
assert.are.same({ "foo", "", "bar" }, string.split("foo~~~~~~bar", "~~~")) | ||
assert.are.same({ "", "foo" }, string.split("~~~foo", "~~~")) | ||
assert.are.same({ "foo", "" }, string.split("foo~~~", "~~~")) | ||
assert.are.same({ "", "" }, string.split("~~~", "~~~")) | ||
assert.are.same({ "", "", "" }, string.split("~~~~~~", "~~~")) | ||
assert.are.same({ "", "", "O" }, string.split("OOOOO", "OO")) | ||
assert.are.same({ " ws " }, string.split(" ws ", ",")) | ||
assert.are.same({ "foo ", " bar" }, string.split("foo , bar", ",")) | ||
assert.are.same({ "我很高兴", "你呢?" }, string.split("我很高兴,你呢?", ",")) | ||
assert.are.same({ "👩", "👩", "👧", "👧" }, string.split("👩👩👧👧", ZWJ)) | ||
assert.are.same({ "foo", "bar" }, string.split("foo\0bar", "\0")) | ||
assert.are.same({ "foo", "bar", "" }, string.split("foo\0bar\0", "\0")) | ||
assert.are.same({ "foo", "bar" }, string.split("foo\0\0bar", "\0\0")) | ||
assert.are.same({ "foo\0" }, string.split("foo\0", "\0\0")) | ||
assert.are.same({ "foo", "\0" }, string.split("foo\0\0\0", "\0\0")) | ||
assert.are.same({ }, string.split("", "")) | ||
assert.are.same({ "a", "b", "c" }, string.split("abc", "")) | ||
assert.are.same({ char(0xef), char(0xbc), char(0x9f) }, string.split("?", "")) | ||
end) | ||
end) | ||
end) |