lua-users home
lua-l archive

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

I'm writing a function to quote a string, that is, it takes a string and returns a string that when printed is acceptable as a lua literal (and the literal is equal to the passed string).

Has anyone already written such a code? (And is it freely available) It seems like such a commonly needed function.

There's a bit in this function where it expands unprintable codes to the \nnn representation. It currently looks a bit like this:

string.gsub("\7ab","[^ %p%w]", function(x)return '\\'..string.byte(x)end)

What I want to do is match all non-printable characters. C provides this concept via isprint and isgraph, but in Lua I have to use the union of " ", "%p" (punctuation), and "%w" (alphanumerics). Which relies on isprint being a union of space, isprint, and isalnum (which the C standard guarantees, but it seems tenuous).

Would it be possible to have another % code for the isprint character class? %t perhaps (for typeable or text)? From a completeness aspect I suppose we should also have one for isgraph. Or do The Implementors think that the current situation, synthesis via %w and %p, is ok?

David Jones