Jump to content




undocumented Lua patterns?


  • You cannot reply to this topic
6 replies to this topic

#1 HDeffo

  • Members
  • 214 posts

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 by HDeffo, 23 February 2016 - 04:01 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 23 February 2016 - 04:03 PM

It's documented here

Edited by KingofGamesYami, 23 February 2016 - 04:03 PM.


#3 HDeffo

  • Members
  • 214 posts

Posted 23 February 2016 - 04:07 PM

so basically %z is "\0"?

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 23 February 2016 - 04:09 PM

That's what I understand, yes.

#5 HDeffo

  • Members
  • 214 posts

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

#6 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 23 February 2016 - 07:12 PM

View PostHDeffo, on 23 February 2016 - 04:13 PM, said:

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

#7 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 23 February 2016 - 09:01 PM

View PostPixelToast, on 23 February 2016 - 07:12 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users