Update textutils.lua

This commit is contained in:
TKB Studios 2024-03-29 15:24:27 +01:00 committed by GitHub
parent de5ef3f2ea
commit ab7ffa846f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 17 deletions

View File

@ -942,28 +942,33 @@ end
--- Splits a string in a table. --- Splits a string in a table.
-- --
-- @tparam string string_to_split The string to split. -- @tparam string inputstr The string to split.
-- @tparam string pattern At what pattern to split the string. -- @tparam string sep The separator to split the string at
-- @tparam nu
-- @treturn table A table containing the splitted strings. -- @treturn table A table containing the splitted strings.
-- @usage args = textutils.splitString("arg1 arg2", " ") -- @usage args = textutils.splitString("arg1 arg2", " ")
-- @since edit this if it goes live (I hope it does) -- @since edit this if it goes live (I hope it does)
local function split(string_to_split, pattern) function split(inputstr, sep, split_count)
local Table = {} expect(1, inputstr, "string")
local fpat = "(.-)" .. pattern expect(2, sep, "string", "nil")
local last_end = 1 expect(3, split_count, "number", "nil")
local s, e, cap = string_to_split:find(fpat, 1) if sep == nil then
while s do sep = "%s"
if s ~= 1 or cap ~= "" then end
table.insert(Table, cap) 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 end
last_end = e + 1
s, e, cap = string_to_split:find(fpat, last_end)
end end
if last_end <= #string_to_split then return splitted_table
cap = string_to_split:sub(last_end)
table.insert(Table, cap)
end
return Table
end end
local tEmpty = {} local tEmpty = {}