什么是嵌入零?
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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
每个字符都有一个内部数字表示,例如 \97 代表“a”。代码为 \0 的字符不代表任何可见字符,但在 C 和其他编程语言中用作终止符。
该手册希望明确说明 '\0' 在 Lua 中不是终止符。这也意味着您可以将任意字节加载到字符串中(图像、音频、视频、本机代码等),并且您不会冒被某些库函数在第一个“\0”处截断的风险(这可能发生在 C 语言中)如果您使用字符串相关的函数)。
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
只是一个值为零的字节,它不需要任何花哨的名称。 Lua 字符串只是记录其长度的字节字符串,因此它们可以包含任何字节值,包括零。某些函数将这些字节字符串视为以\0
结尾的 C 字符串,显然print
就是这样做的。这意味着在 lua 中,#s(字符串长度)是 O(1),而 C 字符串则为 O(n)。应用程序可以将 lua 字符串用于任何字节流,例如 UTF-16 编码的文本或二进制文件内容。
\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
, apparentlyprint
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.这与在 C 字符串中放入 NULL 字符类似。尽管打印输出不显示
b
字符,但其他 Lua 函数应该处理字符串的完整长度(与处理 NULL 终止字符串的 C 字符串处理函数不同)。其用途之一是使用一个字符串来保存由
\0
分隔的多个值。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
.当 Lua 5.1 在字符串中存在 nul 字节的几个问题时,Lua 5.2 工作得很好。
我发现函数 print 会丢弃 nul 字节之后的所有字符。
另外,函数 string.format 返回第一个 nul 字符之前的字符串。
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.
维基百科空字符
Wikipedia Null Character