lua-users home
lua-l archive

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

On Fri, Feb 22, 2013 at 12:30 AM, Choonster TheMage <> wrote:
On Thu, Feb 21, 2013 at 11:54 PM, Kevin Martin <> wrote:

On 21 Feb 2013, at 12:48, Marc Balmer wrote:

> How should "%q" format it's parameter?

From the 5.2 manual:

"The q option formats a string between double quotes, using escape sequences when necessary to ensure that it can safely be read back by the Lua interpreter."

I'm not saying it's really difficult to do, I just thought there might be some code out there already, as if I do it myself, it's the kind of thing where it's really easy to write a buggy implementation.


I've never used PHP, but looking through its manual I found the addcslashes[1] function, which escapes the specified characters with backslashes in the style of C.

You could create a wrapper around it to imitate the %q option of string.format like the code below (which may or may not work).


function escape_lua($str)
$str = addcslashes($str, "\\\"\n\0"); // Escape backslashes, double quotes, newlines and nulls (embedded zeros)
return '"' . $str . '"';


Sorry for the noise, I should have included the documentation link: