lua-users home
lua-l archive

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


On 07/07/2011 21.20, tsuui wrote:
Let's see the documented example:

         string.format('%q', 'a string with "quotes" and \n new line')


Using %q will translate every char with special meaning in the context of string literals into an escape sequence.

Since \n in the above string literal is a newline char, it will be translated as \<newline>, where <newline> is a real newline. Note that the interned representation of \n is a newline char, NOT the sequence <backslash>n. In fact:

print 'a string with "quotes" and \n new line'

gives:

a string with "quotes" and
 new line

So what Lua returns is perfectly in line with the specs.



But probably in the above literal you meant:

'a string with "quotes" and \\n new line'

i.e. something that, when printed produces (1):

a string with "quotes" and \n new line

In this case:

print(string.format('%q', 'a string with "quotes" and \\n new line'))

produces:

"a string with \"quotes\" and \\n new line"

which in turn gives exactly (1) when printed:

print "a string with \"quotes\" and \\n new line" --> (1)

So the round trip is complete.

-- Lorenzo