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

You can use a table as the replacement argument of string.gsub #5

Open
bpj opened this issue Mar 18, 2021 · 0 comments
Open

You can use a table as the replacement argument of string.gsub #5

bpj opened this issue Mar 18, 2021 · 0 comments

Comments

@bpj
Copy link

bpj commented Mar 18, 2021

I saw that here you are using a replacement function to just look up a value in a table. The function is unnecessary since if you pass a table with string keys and values as the replacement argument to string.gsub Lua will automatically use the first capture (or the whole match if there was no capture) as a key to look up in the table and use the value of that key as replacement, so you can replace that with

  text = string.gsub(text, escPattern, escapes)

and it will have the exact same effect, except that since no extra Lua function is created and called every time it is much more efficient. (You will still need a function if you want to look up another capture than the first one of course!)

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

No branches or pull requests

1 participant