添加utf-8支持到tcl

时间:2011-04-14 18:36:12

标签: tcl eggdrop

set botlisten(port) "3333"
set botlisten(password) "123456"
set botlisten(channel) "#chan"
listen $botlisten(port) script botlisten
proc botlisten {idx} {
    control $idx botlisten2
}
proc botlisten2 {idx args} {
global botlisten newTorrentChannel
set args [join $args]
set botlisten(pass) [lindex [split $args] 0]
set botlisten(message) [join [lrange [split $args] 1 end]]
if {[string match $botlisten(pass) $botlisten(password)]} then {
   putquick "PRIVMSG $botlisten(channel) :$botlisten(message)"
 } else {
  putlog "Unauthorized person tried to connect to the bot"
  }
}  

让我们说消息有这些字符:ąčęėįšųūž所以机器人输出奇怪的字符。因此,在我看来,解决方案是添加utf-8支持。

1 个答案:

答案 0 :(得分:6)

Tcl已经有十多年的完全集成的UTF-8支持(自Tcl 8.1以来,虽然没有人理智使用该版本,因为有单调更好的版本)。

但是,一般情况下,有必要告诉Tcl在外部世界的特定通信通道上使用了什么编码(使用fconfigure的{​​{1}}选项)。 Tcl使用与系统相关的默认猜测;在我的系统上,它实际上是UTF-8,但在其他系统上它是ISO 8859-1或-15或相应的Windows代码页。 (Tcl擅长默认猜测BTW。)在套接字上它更尴尬,因为编码实际上是协议级决策(某些协议指定特定编码 - SMTP,IIRC - 协议操作期间的一些开关编码) - HTTP是一个很好的例子 - 有些根本没有指定 - IRC就是典型的例子。在某些情况下,-encoding命令是必需的,因此脚本可以手动控制字节序列和字符之间的转换。但这种情况相当罕见。

当然,如果正在使用代码只是使用Tcl的字符串并使用低级网络(hellooo,eggdrop!)盲目地推送它们,那么普通的Tcl级别可以做的并不是那么多。在这种情况下的解决方法是要么构建eggdrop以使用不同的编码(如Zero's link from his comment所述),要么使用encoding来进行修改,如下所示:

将UTF-8转换为编码形式:

encoding

将编码的UTF-8转换回普通字符串:

set encoded [encoding convertto utf-8 $normalString]