如何在 gperf 中使用空字节?

How to use null bytes in gperf?

gperf info pages 声称如果您指定 -l 那么

The keywords in the input file may contain NUL bytes, written in string syntax as [=16=]0 or \x00, and the code generated by gperf will treat NUL like any other byte

然而当我运行这个输入文件通过gperf -L C++ -l:

foo
[=10=]0bar[=10=]0
\x00baz\x00
bat

我得到:

  <snip>
  static const char * wordlist[] =
    {
      "", "", "",
      "foo",
      "", "", "", "",
      "bat",
      "", "",
      "\x00baz\x00",
      "", "", "", "",
      "\000bar\000"
    };
  <snip>

这看起来像是将 [=14=]0\x00 视为文字值而不是空字节。

如何在我的 gperf 字符串中正确指定空字节?

您可以在 section "Format for Keyword Entries" 中找到更精确的输入语法文档:

It can be given in two ways: as a simple name, i.e., without surrounding string quotation marks, or as a string enclosed in double-quotes, in C syntax, possibly with backslash escapes like \" or 4 or \xa8.

并且在 gperf 的测试套件中,您会发现 an example:

"\x00\x45\x00\x6E\x00\x67\x00\x6C\x00\x69\x00\x73\x00\x68",    "English",    "en_GB.UTF-8"