Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Allow use of string.split in luacontroller sandbox environment #470

Closed
neoh4x0r opened this issue Jul 22, 2019 · 5 comments

Comments

@neoh4x0r
Copy link

neoh4x0r commented Jul 22, 2019

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).

@benrob0329
Copy link

benrob0329 commented Jul 23, 2019

Lua doesn't have a string.split function, see http://lua-users.org/wiki/SplitJoin

@neoh4x0r
Copy link
Author

neoh4x0r commented Jul 23, 2019

@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

@Desour
Copy link
Contributor

Desour commented Jul 23, 2019

If something like string.split is added, it might make sense to think about other helpers, too.

@neoh4x0r
Copy link
Author

neoh4x0r commented Jul 23, 2019

@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:

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

@numberZero
Copy link
Contributor

Fixed with #500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants