Skip to content
Tcl Source Code View Ticket
Login

View Ticket

[string length] reports a length of 2 for a single character

Closed / Invalid141c6f5621e9c9e881f63fbf28c473955b6adae0Created 5 years agoUpdated 5 years ago
Descriptionwiki2022-03-07 00:07:06
When the following script is evaluated in a core_8_branch build, the total length of the string containing a single character is reported as 2, the length of the first character is reported as 2, and the length of the next character is reported as 0:

set string \Uade19
puts [list string length [string length $string]]
puts [string length [string index $string 0]]
puts [string length [string index $string 1]]
jan.nijtmansplain2022-03-07 10:13:40
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 ....
jan.nijtmanswiki2022-03-07 10:14:24

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 ....

pooryorickwiki2022-03-07 13:31:09

In my opinion, this is unacceptable behaviour, even for an "interim" release of Tcl, and no further releases of Tcl should feature it.

dgpplain2022-03-07 14:47:20
Note this is new behavior in Tcl 8.6.10 and later.
jan.nijtmanswiki2022-03-07 14:54:51

> 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 .....

dgpplain2022-03-07 14:59:08
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.
jan.nijtmanswiki2022-03-07 15:12:40

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

dgpplain2022-03-07 15:15:02
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.
dgpplain2022-03-07 15:33:41
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.
Log in (anonymous is fine) to add a comment.