lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I'd like to ask whether this quotation improvement is guaranteed or
> not to work in Lua:
> output:write ((string.gsub (string.format ("bootstrap_image = %q",
> binary), "\\000", "\0")))
> -- Zero bytes are very common for binary formats so it's great to
> reduce it's encoded length.
> 
> The manual says `Strings in Lua may contain any 8-bit value, including
> embedded zeros, which can be specified as '\0'.`
> But can it be specified as zero itself?
> It works fine on my host, but is it guaranteed to work elsewhere?

You can use '\0', but there is a problem if the next element is a digit.
"\0007" is '\0' followed by '7', but "\07" is something different. You
can avoid this problem with a frontier pattern:

  output:write ((string.gsub (string.format ("bootstrap_image = %q",
  binary), "\\000%f[%D]", "\0")))


(Anyway note that, as Rapin pointed out, there may be more efficient
ways to store your data.)

-- Roberto