Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an embedded zero?

Tags:

lua

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"?

like image 972
The Student Avatar asked Dec 12 '11 14:12

The Student


2 Answers

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).

like image 133
marcus Avatar answered Oct 28 '22 03:10

marcus


\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.

like image 35
u0b34a0f6ae Avatar answered Oct 28 '22 03:10

u0b34a0f6ae