Skip to content
Tcl Source Code History Of Ticket 81242a48c8
Login

History Of Ticket 81242a48c8

Artifacts Associated With Ticket 81242a48c8

  1. Ticket change [28e0c3a0d2] (rid 183234) by jan.nijtmans on 2020-05-10 20:50:44:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      According to the documentation, calling Tcl_UtfNext "is  equivalent to calling
      -\fBTcl_UtfNext\fR \fIindex\fR times".    If that's true, the implementation of
      Tcl_UtfNext() as repeatedly calling Tcl_UtfNext() would be correct.
      
      So, as a demonstration, I modified Tcl_UtfAtIndex() accordingly (in a branch, of course) and see what happens. I'll explain later why it's wrong and why Tcl_UtfNext() cannot be corrected to make this documentation right again.
      
    5. foundin initialized to: "all"
    6. is_private initialized to: "0"
    7. login: "jan.nijtmans"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "jan.nijtmans"
    13. subsystem initialized to: "44. UTF-8 Strings"
    14. title initialized to: "Documentation for Tcl_UtfAtIndex() is incorrect"
    15. type initialized to: "Bug"
  2. Ticket change [b3d088ecab] (rid 183304) by jan.nijtmans on 2020-05-12 13:20:04:

    1. assignee changed to: "jan.nijtmans"
    2. icomment:
      OK, build failures can be seen here: [https://travis-ci.org/github/tcltk/tcl/jobs/685533037https://travis-ci.org/github/tcltk/tcl/jobs/685533037].
      
      The reason for the test failures are that sequences of 3 continuation bytes (e.g. \xA0\xA0\xA0) don't work well. If they are preceded by e.g. \xF2, it's a valid 4-byte sequence according to TIP #389. But Tcl_UtfNext() doesn't have access to src[-1]. Tcl_UtfToUniChar() has access to its previous bytes, because it uses chPtr to store the previous result of Tcl_UtfToUniChar(). If the previous result was an upper surrogate, Tcl_UtfToUniChar() can conclude - in this case - that it's handling a valid 4-byte UTF-8 sequence. Tcl_UtfNext() cannot do that.
      
      So, how should we fix this documentation? The easiest fix is saying "is  equivalent to calling
      -\fBTcl_UtfToUniChar\fR \fIindex\fR times". Still that's not 100% correct: In the TIP #389 implementation, Tcl_UtfAtIndex() executes Tcl_UtfToUniChar() one additional time when it detects that index points in the middle of a character >U+FFFF.
      
      So, next experiment: What happens if we don't do the extra call to Tcl_UtfToUniChar() any more?  If that works, the documentation change is easy. If it cannot be done, the description becomes a little bit more complicated.
      
      When this documentation was written, surrogates didn't exist yet in the Unicode standard. Code points > U+FFFF didn't exist yet either. So, it's very understandable no-one noticed this up to now: Tcl never supported code points > U+FFFF either.
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
  3. Ticket change [4bbb001337] (rid 183305) by jan.nijtmans on 2020-05-12 13:22:04:

    1. icomment:
      Build failures are here: [https://travis-ci.org/github/tcltk/tcl/jobs/685533037]   (URL was pasted twice by accident in previous comment)
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  4. Ticket change [9f08cfd775] (rid 183308) by jan.nijtmans on 2020-05-12 13:56:12:

    1. icomment:
      It turns out that the additional call to Tcl_UtfToUniChar() is neccessary. Leaving it out makes it possible to create a pointer in the middle of a surrogate pair. There are two testcases detecting this:
      
      <pre>
      ==== string-17.9.0 string totitle, surrogates, bug [11ae2be95dac9417] FAILED
      ==== Contents of test case:
      
          run {list [string totitle a\U118c0c 1 1] [string totitle a\U118c0c 2 2]  [string totitle a\U118c0c 3 3]}
      
      ---- Result was:
      að‘£€c að‘£€c að‘£€C
      ---- Result should have been (exact matching):
      að‘¢ c að‘£€C að‘£€C
      ==== string-17.9.0 FAILED
      
      
      
      ==== string-17.9.1 string totitle, surrogates, bug [11ae2be95dac9417] FAILED
      ==== Contents of test case:
      
          run {list [string totitle a\U118c0c 1 1] [string totitle a\U118c0c 2 2]  [string totitle a\U118c0c 3 3]}
      
      ---- Result was:
      að‘£€c að‘£€c að‘£€C
      ---- Result should have been (exact matching):
      að‘¢ c að‘£€C að‘£€C
      ==== string-17.9.1 FAILED
      
      </pre>
      
      So, the extra Tcl_UtfToUniChar() call is necessary.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  5. Ticket change [82a0d025c2] (rid 183309) by jan.nijtmans on 2020-05-12 14:11:21:

    1. icomment:
      Another indication, why the documentation of Tcl_UtfAtIndex() is incorrect, can be seen in the very very first version of tclUtf.c, checked in by Scott Stanton ([https://core.tcl-lang.org/tcl/artifact?ln=670-681&name=3f7d7e0b3e21d37f]) in 1999.  It has always been implemented as repeated calls to Tcl_UtfToUniChar(), not Tcl_UtfNext().
      
      B.T.W. This documentation has been wrong since the first commit of doc/Utf.3 as well.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  6. Ticket change [fb1e792d1c] (rid 183355) by jan.nijtmans on 2020-05-13 09:25:28:

    1. closedate changed to: "2458982.89269024"
    2. closer changed to: "jan.nijtmans"
    3. icomment:
      Documentation fixed in all branches. Sorry, Don, but replacing Tcl_UtfToUniChar() with Tcl_UtfNext() in many random places of the source code really was a bad idea. All branches are back to normal now, and the (20-jear old) documentation error which caused this confusion corrected. Tcl_UtfNext() is NOT a cheap replacement for Tcl_UtfToUniChar(), since it doesn't do the surrogate handling from TIP #389.
      
    4. login: "jan.nijtmans"
    5. mimetype: "text/x-fossil-wiki"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
  7. Ticket change [7ef4754910] (rid 183356) by jan.nijtmans on 2020-05-13 09:26:14:

    1. closedate changed to: "2458982.89322165"
    2. comment changed to:
      According to the documentation, calling Tcl_UtfNext "is  equivalent to calling
      -\fBTcl_UtfNext\fR \fIindex\fR times".    If that's true, the implementation of
      Tcl_UtfAtIndex() as repeatedly calling Tcl_UtfNext() would be correct.
      
      So, as a demonstration, I modified Tcl_UtfAtIndex() accordingly (in a branch, of course) and see what happens. I'll explain later why it's wrong and why Tcl_UtfNext() cannot be corrected to make this documentation right again.
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/plain"
  8. Ticket change [2ef3e670b1] (rid 183357) by jan.nijtmans on 2020-05-13 09:26:44:

    1. closedate changed to: "2458982.89357436"
    2. comment changed to:
      According to the documentation, calling Tcl_UtfAtIndex "is  equivalent to calling
      -\fBTcl_UtfNext\fR \fIindex\fR times".    If that's true, the implementation of
      Tcl_UtfAtIndex() as repeatedly calling Tcl_UtfNext() would be correct.
      
      So, as a demonstration, I modified Tcl_UtfAtIndex() accordingly (in a branch, of course) and see what happens. I'll explain later why it's wrong and why Tcl_UtfNext() cannot be corrected to make this documentation right again.
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/plain"