lua-users home
lua-l archive

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


So, I needed a way to quote a string and found addquoted() in lstrlib.c, it does exactly what I require but is a private function which requires using string.format to access. I found it was quite simple to move the contents of addquoted() to a new lauxlib.c function I named luaL_quotelstring() and then call the new function from addquoted().

Everything works as expected, the entire test suite passes (in ltests mode), and with very little extra code the '%q' format specifier may also be used in luaO_pushvfstring(), which is where I wanted it to work.

Now, one thing I noticed, which I thought was a bug, but found is "working as intended" according to the documentation...

if (*s == '"' || *s == '\\' || *s == '\n') {
  luaL_addchar(B, '\\');
  luaL_addchar(B, *s);
}

This little bit of code, from addquoted(), will backslash escape a literal " or \ and it will backslash a literal newline. Meaning the string generated by this code could have a literal newline character preceded by a backslash, not 'n' to make '\n' as I would of thought.

As I would prefer to have my newline escape sequence be '\n' instead of a backslash followed by a literal newline character I tweaked the above code to the following:

if (*s == '"' || *s == '\\' || *s == '\n') {
  luaL_addchar(B, '\\');
  luaL_addchar(B, *s == '\n' ? 'n' : *s);
}

This now produces '\"' or '\\' or '\n' and will not produce an escaped literal newline. This would also require removing a single backslash from the `strings.lua` test suite file on line 139... no other changes would be required for all tests to successfully pass (in ltests mode).

I am then left wondering what the escaped literal newline is used for? When quoting strings why wouldn't we want a newline to show up as '\n' instead of a backslash and a literal newline character? As this is existing functionality I will not change it, though I still wonder what it is used for, or if it something that is actually needed?

This all came about for my Token Storage patch... I wanted to add the ability to preprocess Lua source into source that has had all enumerations, constants, macros and inlines expanded into pure Lua code. This can now be achieved with a new -E parameter to `luac`... more information very soon on my latest patch.

~pmd