diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua index c483b705a..811d6b87e 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua @@ -942,28 +942,33 @@ end --- Splits a string in a table. -- --- @tparam string string_to_split The string to split. --- @tparam string pattern At what pattern to split the string. +-- @tparam string inputstr The string to split. +-- @tparam string sep The separator to split the string at +-- @tparam nu -- @treturn table A table containing the splitted strings. -- @usage args = textutils.splitString("arg1 arg2", " ") -- @since edit this if it goes live (I hope it does) -local function split(string_to_split, pattern) - local Table = {} - local fpat = "(.-)" .. pattern - local last_end = 1 - local s, e, cap = string_to_split:find(fpat, 1) - while s do - if s ~= 1 or cap ~= "" then - table.insert(Table, cap) +function split(inputstr, sep, split_count) + expect(1, inputstr, "string") + expect(2, sep, "string", "nil") + expect(3, split_count, "number", "nil") + if sep == nil then + sep = "%s" + end + if split_count == nil then + split_count = -1 + end + local splitted_table = {} + local splitted_amount = 0 + for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do + if splitted_amount == split_count then + break + else + table.insert(splitted_table, str) + splitted_amount = splitted_amount + 1 end - last_end = e + 1 - s, e, cap = string_to_split:find(fpat, last_end) end - if last_end <= #string_to_split then - cap = string_to_split:sub(last_end) - table.insert(Table, cap) - end - return Table + return splitted_table end local tEmpty = {}