Artifacts Associated With Ticket 81242a48c8
Ticket change [28e0c3a0d2] (rid 183234) by jan.nijtmans on 2020-05-10 20:50:44:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- 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.
- foundin initialized to: "all"
- is_private initialized to: "0"
- login: "jan.nijtmans"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "jan.nijtmans"
- subsystem initialized to: "44. UTF-8 Strings"
- title initialized to: "Documentation for Tcl_UtfAtIndex() is incorrect"
- type initialized to: "Bug"
Ticket change [b3d088ecab] (rid 183304) by jan.nijtmans on 2020-05-12 13:20:04:
- assignee changed to: "jan.nijtmans"
- 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.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [4bbb001337] (rid 183305) by jan.nijtmans on 2020-05-12 13:22:04:
- icomment:
Build failures are here: [https://travis-ci.org/github/tcltk/tcl/jobs/685533037] (URL was pasted twice by accident in previous comment)
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [9f08cfd775] (rid 183308) by jan.nijtmans on 2020-05-12 13:56:12:
- 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. - login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [82a0d025c2] (rid 183309) by jan.nijtmans on 2020-05-12 14:11:21:
- 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.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [fb1e792d1c] (rid 183355) by jan.nijtmans on 2020-05-13 09:25:28:
- closedate changed to: "2458982.89269024"
- closer changed to: "jan.nijtmans"
- 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.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"
Ticket change [7ef4754910] (rid 183356) by jan.nijtmans on 2020-05-13 09:26:14:
- closedate changed to: "2458982.89322165"
- 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.
- login: "jan.nijtmans"
- mimetype: "text/plain"
Ticket change [2ef3e670b1] (rid 183357) by jan.nijtmans on 2020-05-13 09:26:44:
- closedate changed to: "2458982.89357436"
- 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.
- login: "jan.nijtmans"
- mimetype: "text/plain"