I'm reading the Lua reference manual, and it talks about "embedded zeros", symbolized by "\0".
When I try to see it in the Lua console, it prints nothing meaningful:
> print "a \0 b"
a
So, what's this "embedded zero"?
I'm reading the Lua reference manual, and it talks about "embedded zeros", symbolized by "\0".
When I try to see it in the Lua console, it prints nothing meaningful:
> print "a \0 b"
a
So, what's this "embedded zero"?
Every character has an internal numeric representation, such as \97 for 'a'. A character with code \0 does not represent any visible character but is used as a terminator in C and other programming languages.
The manual wants to make it clear that a '\0' is not a terminator in Lua. It also means that you can load arbitrary bytes into a string (image, audio, video, native code, etc.) and you do not risk having it truncated at the first '\0' by some library function (which could happen in C if you use string-related functions).
\0 is just a byte with the value zero, it doesn't need any fancy name. Lua strings are just byte strings that keep track of their length, so they may contain any byte values, zero included. Some functions treat these byte strings as if they were C strings that terminate with \0, apparently print does this.
This means that in lua, #s (string length) is O(1) vs. O(n) for C strings. And the application may use lua strings for any byte streams, for example UTF-16 encoded text or binary file contents.
It is going to be similar to putting a NULL character in a C string. Although your print output does not show the b character, other Lua functions should work with the full length of the string (unlike C string handling functions that work with NULL terminated strings).
One use of this would be to use one string for holding multiple values separated by \0.
\0. Like try print(s:byte(1,-1)) or for b in s:gmatch'.'do print(b)end. I guess that's why print is only for quick debugging: "print is not intended for formatted output, but only as a quick way to show a value, typically for debugging"
Lua 5.2 works well when Lua 5.1 has several problems with nul bytes in the string. I've found that the function print dischards all characters after the nul byte. Also the function string.format returns string up to the first nul character.
The null character is often represented as the escape sequence \0 in source code string literals or character constants.
NUL, that is, the zero byte.