Yes.
Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
print(string.format("%s",[[fn"ord]]));
fn"ord
print(string.format("%q",[[fn"ord]]));
"fn"ord"
(I used [[]] quotes instead of "" for the sake of clarity. They allow any charater, including ", to be entered literally into a string.)
Implemented (including field width limitation):
werror("%q\n", (string)(indices(allocate(35)) + ({ 65, 127, 128, 130, 0x9f, 0xa0, 0x100, 0x2000, 0x22000, 0, 48 })));
"\0\1\2\3\4\5\6\a\b\t\n\v\f\r\16\17\20\21\22\23\24\25\26\27\30\31\32\e\34\35\36\37 !"A\177\200\202\237 \u0100\u2000\U00022000\u00000" (1) Result: 135
werror("%.19q\n", (string)(indices(allocate(35)) + ({ 65, 127, 128, 130, 0x9f, 0xa0, 0x100, 0x2000, 0x22000, 0, 48 })));
"\0\1\2\3\4\5\6\a" (2) Result: 19
Ah, now I see that the result was already 18 characters long, I missed the newline which was included in the result returned by werror().
from the lua reference manual:
The q option formats a string in a form suitable to be safely read back by the Lua interpreter: The string is written between double quotes, and all double quotes, newlines, embedded zeros, and backslashes in the string are correctly escaped when written.
greetings, martin.
Is this the best solution then:
%O formats for debug purposes. %q works as %O, but with formatting restrictions
7.6 compat %O works as %q
Presumably %q would also fail on stuff like programs and objects which can not be encoded reliably as ASCII.
Yes. _sprintf() can do anything, including launching nuclear missiles, I'm just talking about the behaviour of %q itself.
Yes, but there is no mechanism which prevents _sprintf() from having side effects. Maybe there is a policy that forbids it, but I have not seen such a policy written down anywhere.
http://pike.ida.liu.se/generated/manual/modref/ex/lfun_3A_3A/_sprintf.html
at the bottom: Note It's assumed that this function is side-effect free.
greetings, martin.
Assumed != required. That side-effect-freeness is assumed only implies that any side-effects might be optimized away as they are assumed not to exist.
hmm, i read that as: if there are sideffects, unexpected things may happen, (being optimized away may just be one) so there better not be any.
greetings, martin.
If the object supports %q in the _sprintf, %q should work. :)
May I suggest that sscanf(%q) will be a synonym to (%O) then?
If the object supports %anything in the _sprintf, %anything works. This is not related to the semantics of %anything, since the object overrides those semantics.
Hm, not entirely true it appears, if "anything" read as "F". Is there any non-hysterical reason why %F is not delegated to _sprintf()?
Not as far as I know. BTW: Shouldn't you know? As far as I remember, it was you who implemented %F?
Yes, I implemented it. But you seem to be the person who noticed that it didn't work with _sprintf, since you documented the fact (instead of fixing it).
Well, I did document it as a at the time of documentation current limitation. The reason I didn't fix it at that time was probably that I was busy with something else, and needed up-to-date _sprintf() documentation (probably for the second arg). The documentation before that was the rather terse "Sprintf callback.".
And the fix is the rather terse macro invocation
CHECK_OBJECT_SPRINTF()
;-)
Also, I implemented %F _before_ _sprintf even existed. Noring added _sprintf support, but didn't add it for %F.
No. %O should read "hi\0""0" as one string while %q as two (if %q outputs it as "hi\u00000" as per my suggestion).
pike-devel@lists.lysator.liu.se