lua-users home
lua-l archive

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


Am 01.07.2016 um 14:51 schröbte Martin:
On 16-06-30 06:53 PM, Hisham wrote:
On 1 July 2016 at 07:34, Martin <eden_martin_fuhrspam@gmx.de> wrote:
In lua key-value pair in table may be defined as

   "[" <expr> "]" "=" <expr>

How you this is correctly to handle cases like "[[[\]]] = [[\\]]"
(mapping backspace to quoted version)? For me it looks like syntax flaw.

You can avoid this by adding a space:

t = { [ [[\]] ] = [[\\]] }

Surely, when writing by hand. By I'm writing serializer. In recursive
function serialize(node) key-vals serialization is like

io.write("[", serialize(key), "]", " = ", serialize(value), ",", "\n")

Of course I may change "[" to "[ " but spaces will be added for all
indexed keys and result will be really ugly. So I had to change code and
pass additional silly flag "guy, you're inside indexed key. If your data
first char is '[', write space before writing it".

You could check whether key is a string (which when serialized would result in a long string literal) and act accordingly. But I wouldn't use long string literals for serialization anyway because it might eat some whitespace at the beginning and change your newlines (and you have to scan your string contents to make sure it doesn't contain the string terminator). Use `string.format( "%q", key )` instead.

Philipp