Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ef39a0294139e0406e32e3cfa399a3383c7b5717fe774c128a83a8f29348834b
Ticket: 141c6f5621e9c9e881f63fbf28c473955b6adae0
[string length] reports a length of 2 for a single character
User & Date: jan.nijtmans on 2022-03-07 10:14:24
Changes

  1. closedate changed to: "2459645.92666744"
  2. icomment:
    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 ....
    
  3. login: "jan.nijtmans"
  4. mimetype: "text/x-fossil-wiki"