lua-users home
lua-l archive

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


On Wed, Mar 11, 2009 at 8:19 PM, Mike Pall wrote:
> 1. There is no easy way to add a meta-mechanism, because strings
> are parsed before any such mechanism could take effect.

One solution would be to define one's own escaping within an
unescaping string: X[[\x05hello\n\x00]] .  Things like \n would need
to be reimplemented though.  This could be useful for representing a
regex (e.g. lrexlib) that may have very different escape sequences.

> 2. Apart from the \x and \u escape sequences, there is little
> demand for other types of escape sequences which would warrant a
> meta-mechanism.

Multiline string break escapes[1] (including sequences that expand to
nothing) and inline interpolation of variables into strings[2] come
into mind.  Some of this (e.g. control structures) leads to template
libraries.

[1] http://lua-users.org/wiki/MetaLuaRecipes
[2] http://lua-users.org/wiki/StringInterpolation

> 3. Lua's strings are multi-purpose because they are just sequences
> of bytes. They are often used to store non-textual content. An
> idiomatic way to enter this content as literals is missing.

However, on occasions I find X"4c75612e6f72" or even X"4c75 612e 6f72"
or "1001100..." (under a certain byte ordering) syntactically
preferable to "\x4c\x75\x61\x2e\x6f\x72", which is in turn can be
quite preferable to the decimal representation.  Also, if I'm doing
protocol work, as mentioned above, it's typical to have not
"\x05hello\x00" but rather "\x05hello\xcd\x00", where "\xcd" is some
type of checksum.  In that case, it's syntactically cleaner to write
X"hello", where X prepends the initiator byte and appends the checksum
and terminator.

That said, I would not be against Lua adding "\x" (which I too have
asked about in the past).