TCL 的一般字符串引用 [英] General string quoting for TCL

查看:40
本文介绍了TCL 的一般字符串引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个实用程序(恰好在 python 中),它以 TCL 脚本的形式生成输出.给定python中的一些任意字符串变量(不是unicode),我想生成一个像

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, I want to produce a TCL line like

set s something

... 它将把 TCL 变量 's' 设置为那个确切的字符串,不管里面有什么奇怪的字符.不要太奇怪,我不想让输出比需要的更混乱.我相信一个体面的方法是

... which will set TCL variable 's' to that exact string, regardless of what strange characters are in it. Without getting too weird, I don't want to make the output messier than needed. I believe a decent approach is

  1. 如果字符串不为空并且只包含字母数字和一些像 .-_ 这样的字符(但绝对不是 $"{}\),那么它可以按原样使用;

  1. if the string is not empty and contains only alphanumerics, and some characters like .-_ (but definitely not $"{}\) then it can be used as-is;

如果它只包含可打印的字符而没有双引号或花括号(并且不以反斜杠结尾),那么只需将 {} 放在它周围;

if it contains only printable characters and no double-quotes or curly braces (and does not end in backslash ) then simply put {} around it;

否则,在对 " 使用 \ 转义后,将 "" 放在它周围 { >} \ $ [ ]\nnn 转义用于非打印字符.

otherwise, put "" around it after using \ escapes for " { } \ $ [ ] , and \nnn escapes for non-printing characters.

问题:这是需要在双引号内转义的完整字符集吗?我在文档中找不到这个.我是否错过了什么(例如,我几乎错过了 (2) 的字符串不能以 \ 结尾).

Question: is that the full set of characters which need escaping inside double quotes? I can't find this in the docs. And did I miss something (I almost missed that strings for (2) can't end in \ for instance).

我知道还有许多其他字符串可以被 {} 引用,但似乎很难轻松识别它们.此外,如果您不介意在 TCL 输出中直接出现非打印字符(特别是换行符),则它们看起来也适用于 (2).

I know there are many other strings which can be quoted by {}, but it seems difficult to identify them easily. Also, it looks like non-printing characters (in particular, newline) are OK with (2) if you don't mind them being literally present in the TCL output.

推荐答案

你真的只需要 2 条规则,

You really only need 2 rules,

  • 转义花括号
  • 将输出用花括号括起来

您无需担心换行符、不可打印字符等.它们在文字字符串中有效,并且 TCL 具有出色的 Unicode 支持.

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:

  • 转义 [] {}$
  • 将整个输出包装在 set s [subst { $output } ]

Tcl 的美妙之处在于它的语法非常简单.除了以上 3 个字符外,没有其他字符需要转义.

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.

编辑 2 最后一次尝试.

如果你传递了 subst 一些选项,你只需要转义 \{}

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.

祝你好运!

这篇关于TCL 的一般字符串引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆