string.Split and string.SafePattern - #35
Conversation
Co-authored-by: Neo <Cheatoid@users.noreply.github.com>
Based on Cheatoid response
|
@gtnardy what do you think ? |
|
what are the use cases for the is_pattern ans SafePattern method? |
|
is_pattern allow you to split strings using a lua pattern : Split newlines : "([^\r\n]+)" |
|
I see, I found it confusing for someone that doesn't know what it does, and the difference for the methods |
| } | ||
|
|
||
| function string.SafePattern(str) | ||
| return (string.gsub(str, ".", escaped_pattern_characters)) |
There was a problem hiding this comment.
Worth benchmarking on Lua 5.4.8:
return (string.gsub(str, "([%(%)%[%]%.%%%+%-%*%?%^%$])", "%%%1"))
There is a trade off:
- This excludes NUL char
\0, which Lua patterns cannot conveniently match inside a character class. But, it could be handled with an additionalstring.gsubcall.
It's annoying to copy the same code over and over to split strings. (Plus some of my versions in my packages had bugs or limitations)