General string quoting for TCL

前端 未结 3 703
暗喜
暗喜 2021-02-05 09:25

I\'m writing a utility (which happens to be in python) which is generating output in the form of a TCL script. Given some arbitrary string variable (not unicode) in the python,

3条回答
  •  你的背包
    2021-02-05 09:51

    You really only need 2 rules,

    • Escape curly braces
    • Wrap the output in curly braces

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

    set s { 
    this is
    a 
    long 
    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 of your comment, you can do the following:

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

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

    Edit 2 One last try.

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

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

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

    Good luck!

提交回复
热议问题