This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
HDeffo's profile picture

undocumented Lua patterns?

Started by HDeffo, 23 February 2016 - 02:59 PM
HDeffo #1
Posted 23 February 2016 - 03:59 PM
So as far as I was aware this is all of the regular expression type patterns lua accepts in functions like gmatch and gsub

Patterns in Lua Manual

however while just randomly browsing through the Lua mail lists I discovered quite a few official/semi-official programs use %z in gsub… As far as I am aware this shouldn't do anything special should it? or are there some codes that the manual doesn't properly document? Or is this not being used as a pattern in gsub and instead I'm just extremely confused about what it is? Below is one example of it I found in which it is being used to handle bytecode.



string.gsub(foobar,"\20(\1%z%z%z)","\2%1")
Edited on 23 February 2016 - 03:01 PM
KingofGamesYami #2
Posted 23 February 2016 - 04:03 PM
It's documented here
Edited on 23 February 2016 - 03:03 PM
HDeffo #3
Posted 23 February 2016 - 04:07 PM
so basically %z is "\0"?
KingofGamesYami #4
Posted 23 February 2016 - 04:09 PM
That's what I understand, yes.
HDeffo #5
Posted 23 February 2016 - 04:13 PM
Just did a quick test and as far as I can tell that does seem to be the case :D/> thank you very much yami. Still don't understand the point instead of just using \0 but I guess some people just have weird preferences
PixelToast #6
Posted 23 February 2016 - 07:12 PM
Just did a quick test and as far as I can tell that does seem to be the case :D/> thank you very much yami. Still don't understand the point instead of just using \0 but I guess some people just have weird preferences

you cannot have a \0 in a lua pattern (in vanilla lua atleast)

the reason is performance, Lua is written in C where standard strings are zero terminated, ie you cannot have a "\0" in a string without using your own string type that holds its own length
to make lua patterns faster and easier to implement, the creators of lua decided to use the standard C string functions on patterns instead of their own functions that would handle any \0s in patterns
SquidDev #7
Posted 23 February 2016 - 09:01 PM
you cannot have a \0 in a lua pattern (in vanilla lua atleast)

the reason is performance, Lua is written in C where standard strings are zero terminated, ie you cannot have a "\0" in a string without using your own string type that holds its own length
to make lua patterns faster and easier to implement, the creators of lua decided to use the standard C string functions on patterns instead of their own functions that would handle any \0s in patterns

Interestingly this limitation was removed in Lua 5.2. Looks like they manually tracked start and end positions of strings instead.