[ACCEPTED]-General string quoting for TCL-quoting

Accepted answer
Score: 17

You really only need 2 rules,

  • Escape curly braces
  • Wrap the output in curly braces

You don't need 12 to worry about newlines, non printable characters 11 etc. They are valid in a literal string, and 10 TCL has excellent Unicode support.

set s { 
this is
string. I have $10 [10,000 cents] only curly braces \{ need \} to be escaped.
\t is not  a real tab, but '    ' is. "quoting somthing" :
{matchin` curly braces are okay, list = string in tcl}

Edit In light 9 of your comment, you can do the following:

  • escape [] {} and $
  • wrap the whole output in set s [subst { $output } ]

The 8 beauty of Tcl is it a has a very simple 7 grammar. There are no other characters besides 6 the 3 above needed to be escaped.

Edit 2 One last 5 try.

If you pass subst some options, you will 4 only need to escape \ and {}

set s [subst -nocommands -novariables { $output } ]

You would need 3 to come up with a regex to convert non printable 2 characters to their escaped codes however.

Good 1 luck!

Score: 6

Tcl has very few metacharacters once you're 38 inside a double-quoted string, and all of 37 them can be quoted by putting a backslash 36 in front of them. The characters you must 35 quote are \ itself, $ and [, but it's considered 34 good practice to also quote ], { and } so that 33 the script itself is embeddable. (Tcl's 32 own list command does this, except that it doesn't 31 actually wrap the double quotes so it also 30 handles backslashes and it will also try 29 to use other techniques on “nice” strings. There's 28 an algorithm for doing this, but I advise 27 not bothering with that much complexity 26 in your code; simple universal rules are 25 much better for correct coding.)

The second 24 step is to get the data into Tcl. If you 23 are generating a file, your best option 22 is to write it as UTF-8 and use the -encoding option 21 to tclsh/wish or to the source command to explicitly 20 state what the encoding is. (If you're inside 19 the same process, write UTF-8 data into 18 a string and evaluate that. Job Done.) That 17 option (introduced in Tcl 8.5) is specifically 16 for dealing with this sort of problem:

source -encoding "utf-8" theScriptYouWrote.tcl

If 15 that's not possible, you're going to have 14 to fall back to adding additional quoting. The 13 best thing is to then assume you've only 12 got ASCII support available (a good lowest 11 common denominator) and quote everything 10 else as a separate step to the quoting described in the first paragraph. To quote, convert every Unicode character 9 from U+00080 up to an escape sequence of 8 the form \uXXXX where XXXX are exactly four hex 7 digits[1] and the other two are literal characters. Don't 6 use the \xXX form, as that has some “surprising” misfeatures 5 (alas).

[1] There's an open bug in Tcl about 4 handling characters outside the Basic Multilingual 3 Pane, part of which is that the \u form isn't 2 able to cope. Fortunately, non-BMP characters 1 are still reasonably rare in practice.

Score: 2

To do it right you should also specify the 14 encoding your python string is in, typically 13 sys.getdefaultencoding(). Otherwise you 12 might garble encodings when translating 11 it to Tcl.

If you have binary data in your 10 string and want Tcl binary strings as a 9 result this will always work:

data = "".join("\\u00%02x" % ord(c) for c in mystring)
tcltxt = "set x %s" % data

Will look like 8 a hex dump though, but well, it is a hex 7 dump...

If you use any special encoding like 6 UTF-8 you can enhance that a bit by using 5 encoding convertfrom/convertto and the appropriate 4 Python idiom.

data = "".join("\\u00%02x" % ord(c) for c in myutf8string)
tcltext = "set x [encoding convertfrom utf-8 %s]" % data

You can of course refine this 3 a bit, avoiding the \u encoding of all the 2 non special chars, but the above is safe 1 in any case.

Score: 1

As far as I can tell you must:

  1. Enclose your string in double quotes ""
  2. Backslash-escape the following characters: [, $, " and \.

The other 6 answers are wrong in various ways:

  • You do need to escape ".
  • You do not need to escape ], {, or } in double-quoted strings.
  • You cannot use {this style of string} because there's no way to put unbalanced curly brackets in it. { \} } does not work because it keeps the backslash in the string.

The spec 5 is very unclear about this, and even wrong 4 in some places (e.g. it says \n will be replaced 3 with a newline in {curley bracket strings} but that doesn't actually 2 happen. So I figured this out mostly from 1 experimentation on repl.it.

More Related questions