[Feature Request] Allow use of string.split in luacontroller sandbox environment
Referencing minetest forums post: https://forum.minetest.net/viewtopic.php?p=341908#p341908
thetoolman: I'm working with the lua controller and digilines and I want to make the lua controller count something that is sent in the digilines string.
To be a little more clear here, the digilines chest spits out a string like this "default:cobblestone 9" where the first part is the item, and the last part is the number.
I don't have a github account so I wont be making a merge request, but if someone wants to take a look at this and possibly add it or explain why it's not there, that'd be nice.
So the code I added in mesecons_luacontroller/init.lua looks like this
local env = { ... string = { ... split = string.split, ... } .... }
Are there any technical issues with adding string.split ... it seems like it would be a useful addition. I know it also possible to use a table with named members as the message parameter, but that might not always be practical in all cases (especially when the output format is generated by something that the user has no control over).
Lua doesn't have a string.split function, see http://lua-users.org/wiki/SplitJoin
@benrob0329 What about this:
local env = {
...
string = {
...
split = safe_string_split,
...
}
....
}
-- string.split splits a string by a delimiter
-- if delimiter (c) is omitted, the string (s) is returned as the first element in the table
-- if the string (s) is omitted, then an empty table is returned
local function safe_string_split(s,c)
local list = {}
if s then
if c and string.match(s, c) then
-- split s by delimiter c and append to list
for word in string.gmatch(s, '([^' .. c .. ']+)') do
table.insert(list, word)
end
else
-- s does not contain delimieter c so add s as the first element
table.insert(list, s)
end
end
return list
end
If something like string.split is added, it might make sense to think about other helpers, too.
@DS-Minetest Yes, that would probably be a good idea.
However, that raises another question:
What should the scope of helpers be limited to ?
(some helpers might be by-nature unwanted for the purposes of the sandbox)
Here are a few that I can think of which are good candidates:
- string.match (string contains),
- string.gmatch (regx matching),
- ability to call members of vector class builtin/common/vector.lua https://github.com/minetest/minetest/blob/master/builtin/common/vector.lua
I was just looking and mintest already defines a lua function called string.split https://github.com/minetest/minetest/blob/master/builtin/common/misc_helpers.lua#L167
Fixed with #500