|
2022-03-07
| ||
| 15:33 | • Ticket [141c6f5621] string length reports a length of 2 for a single character... artifact: 74598acdd5 user: dgp | |
| 15:15 | • Ticket [141c6f5621]: 5 changes artifact: ac9f99f443 user: dgp | |
| 15:12 | • Ticket [141c6f5621]: 5 changes artifact: 3dae6863ea user: jan.nijtmans | |
| 14:59 | • Ticket [141c6f5621]: 5 changes artifact: 633f84f451 user: dgp | |
| 14:54 | • Ticket [141c6f5621]: 5 changes artifact: 204e749262 user: jan.nijtmans | |
| 14:47 | • Ticket [141c6f5621]: 5 changes artifact: f637b35211 user: dgp | |
| 13:31 | • Ticket [141c6f5621]: 5 changes artifact: 17b30f43a4 user: pooryorick | |
| 10:14 | • Ticket [141c6f5621]: 4 changes artifact: ef39a02941 user: jan.nijtmans | |
| 10:13 | • Closed ticket [141c6f5621]. artifact: c32fe6722c user: jan.nijtmans | |
| 00:07 | • New ticket [141c6f5621]. artifact: 362267ca82 user: pooryorick | |
[string length] reports a length of 2 for a single character
set string \Uade19
puts [list string length [string length $string]]
puts [string length [string index $string 0]]
puts [string length [string index $string 1]]
Just wondering, what character is \Uade19 ??? It's "" wherever I look ... Said that, let's demonstrate with another reasonable character 💣 In Tcl 8.6: <pre> $ tclsh8.6 % string length 💣 2 % string index 💣 0 ▒ % string index 💣 1 ▒ </pre> In Tcl 8.7: <pre> $ tclsh8.7 % string length 💣 2 % string index 💣 0 💣 % string index 💣 1 </pre> The fact that "string length" returns 2 comes from the internal UTF-16 representation for string in Tcl 8.x, not something that's trivial to change. You can change that compiling Tcl 8.7 with -DTCL_UTF_MAX=4. But that has effect on the C-API using Tcl_UniChar, therefore this is only done for Tcl 9.0. Since the UTF-8 encoding for 💣 is 0xF0 0x9F0 x92 0xA3, which cannot be split, the current 8.7 behavior is the best possible under the given restriction. So, this behavior is as designed. See also the "Caveat" section of [https://core.tcl-lang.org/tips/doc/trunk/tip/389.md|TIP #389], which explains this behavior. Sorry, but closing ....
Just wondering, what character is \Uade19 ??? It's "" wherever I look ...
Said that, let's demonstrate with another reasonable character 💣
In Tcl 8.6:
$ tclsh8.6 % string length 💣 2 % string index 💣 0 ▒ % string index 💣 1 ▒
In Tcl 8.7:
$ tclsh8.7 % string length 💣 2 % string index 💣 0 💣 % string index 💣 1
The fact that "string length" returns 2 comes from the internal UTF-16 representation for string in Tcl 8.x, not something that's trivial to change. You can change that compiling Tcl 8.7 with -DTCL_UTF_MAX=4. But that has effect on the C-API using Tcl_UniChar, therefore this is only done for Tcl 9.0. Since the UTF-8 encoding for 💣 is 0xF0 0x9F0 x92 0xA3, which cannot be split, the current 8.7 behavior is the best possible under the given restriction.
So, this behavior is as designed. See also the "Caveat" section of TIP #389, which explains this behavior. Sorry, but closing ....
In my opinion, this is unacceptable behaviour, even for an "interim" release of Tcl, and no further releases of Tcl should feature it.
Note this is new behavior in Tcl 8.6.10 and later.
> Note this is new behavior in Tcl 8.6.10 and later.
Indeed, before Tcl 8.6.10, "string length 💣" gave the answer "4", which is even more surprising .....
In particular in late Tcl 8.6 patchlevels and in Tcl 8.7 pre-releases, [string length] is no longer counting characters, it is counting UTF-16 code units. This is at least controversial. That said, I think it's been TIP-approved (I'm hazy on the 8.6.* changes). It's not out of bounds to complain, and point out problems, possibly leading to a counter proposal, but "not a bug" is a fair assessment.
The only 'real' solution is compiling Tcl 8.7 with UCS-2-support:
./configure CFLAGS="-DTCL_UTF_MAX=4"
That's possible, it has some implications for extensions. It's up to you whether those implications are acceptable or not. Both TCL_UTF_MAX=3 and TCL_UTF_MAX=4 are supported for Tcl 8.7 (see TIP #542
The troubling disruptions are mostly in the inferences programmers have been able to make that have been invalidated by the change. As an example, whenever (string length [string index $s $i] == 0) && ($i >= 0) a programmer could reliably conclude [string length $s] < $i without making any additional tests. Any program built on inferences like that are at risk through this transition. Tcl 9 then reverses course and transitions back to: % string length 💣 1 % string index 💣 0 💣 % string index 💣 1 % I think this double transition back to where we started is fueling the stated plans of several people to skip Tcl 8.7 altogether.
Another programming inference that might commonly be in use:
[llength [split $s {}]] == [string length $s]
We could use a list of such "Gotchas!" to call attention to
in a migration guide to Tcl 8.7. These loops no longer reliably
implement the same function in Tcl 8.7:
foreach c [split $s {}] {process $c}
for {set i 0} {$i < [string length $s]} {incr i} {process [string index $s $i]}
Here again, the expectations of functional equivalence are restored in Tcl 9.