spir wrote:I don't get what you mean here: I'm producing an escaped string (thus backslahes are all escaped in the replacement patterns).
You had
elseif c == '\"'. I changed it to
elseif c == '"'. That's all.
spir wrote:(other things)
Do you know Python? It has two functions for converting things to strings:
str, which is like Lua's tostring, and
repr, which is like your tostring. To only have
str is good, to have both is better, but to only have
repr is madness, because now you have a problem when you want to display things to the user.
To prevent "\"\\\"nested\\\"\"" quoting stacking up, you need to write a function real_tostring:
Code: Select all
function real_tostring(t)
if type(t) == "string" then
return t
end
return tostring(t)
end
and then you need to change all functions that look like this:
Code: Select all
function someFunc(someArg)
someArg = tostring(someArg)
-- ...
to
Code: Select all
function someFunc(someArg)
someArg = real_tostring(someArg)
-- ...
The real problem comes when someone else has to maintain your code, and they find al these calls to
real_tostring in your code. They'll assume
tostring(foo) == foo holds if
foo is a string (because it usually does), and reason:
"real_tostring is the same as"
Code: Select all
function real_tostring(t)
if type(t) == "string" then
return tostring(t)
end
return tostring(t)
end
"which is the same as"
Code: Select all
function real_tostring(t)
return tostring(t)
end
"which means real_tostring is just a waste of stack space. I'll remove the functions and change all the calls to
real_tostring to
tostring."
And that's a problem.
So: I think your function string.quoted is really useful, and if you write a function that writes out tables, be sure to use string.quoted on string keys and values, but you shouldn't change the behaviour of standard library functions (especially not one as basic as tostring).