从技术上讲,内部类型不是担保属性。一切都是一根弦。该代码可能会在任何时候让某个类型的字符闪烁起来。而依赖于内部类型的代码通常非常脆弱或彻底损坏。
所以你的C代码应该调用
Tcl_GetByteArrayFromObj()
而不是偷看内部参数。如果对象还没有byteArray表示,则会进行适当的转换。
关于您的问题:
为什么不
append
是否保留字节数组类型?
如果您做得正确并且从不触发字符串rep的创建,至少在8.6中是这样的。
在tkcon中运行此功能
追加
将值转换为字符串:
() 98 % set a [binary format H2 1]
() 99 % set b [binary format H2 1]
() 100 % ::tcl::unsupported::representation $a
value is a bytearray with a refcount of 2, object pointer at 0000000005665420, internal representation 000000000587B280:0000000005665240, string representation ""
() 101 % ::tcl::unsupported::representation $b
value is a bytearray with a refcount of 2, object pointer at 000000000564EEB0, internal representation 000000000587B4A0:00000000056590E0, string representation ""
() 102 % set x $a
() 103 % ::tcl::unsupported::representation $x
value is a bytearray with a refcount of 4, object pointer at 0000000005665420, internal representation 000000000587B280:0000000005665240, string representation ""
() 104 % append x $b
() 105 % ::tcl::unsupported::representation $x
value is a string with a refcount of 3, object pointer at 0000000005663F50, internal representation 0000000005896BA0:000000000564F030, string representation ""
之所以会发生这种情况,是因为bytearray创建了一个字符串rep(由于Tkcon回显了该值)。这个
追加
优化只适用于“pure”bytearray,例如没有字符串代表的bytearray。这类似于对“pure”列表的一些优化。
因此,它是这样工作的,以防止闪烁的结果回波:
() 106 % set b [binary format H2 1]; puts "pure"
pure
() 107 % set a [binary format H2 1]; puts "pure"
pure
() 108 % set x $a; puts "pure"
pure
() 109 % ::tcl::unsupported::representation $a
value is a bytearray with a refcount of 3, object pointer at 0000000005658780, internal representation 000000000587B320:0000000005658CF0, no string representation
() 110 % ::tcl::unsupported::representation $b
value is a bytearray with a refcount of 2, object pointer at 000000000564ED60, internal representation 000000000587B500:0000000005658750, no string representation
() 111 % ::tcl::unsupported::representation $x
value is a bytearray with a refcount of 3, object pointer at 0000000005658780, internal representation 000000000587B320:0000000005658CF0, no string representation
() 112 % append x $b; puts "pure"
pure
() 113 % ::tcl::unsupported::representation $x
value is a bytearray with a refcount of 2, object pointer at 0000000005658690, internal representation 00000000058A5C60:0000000005658960, no string representation
请注意
无字符串表示
部分
如何将字符串转换为字节数组
只需执行二进制格式:
set x [binary format a* $x]