Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2020-05-13
09:26 • Ticket [81242a48c8] Documentation for Tcl_UtfAtIndex() is incorrect status s... artifact: 2ef3e670b1 user: jan.nijtmans
09:26 • Ticket [81242a48c8]: 4 changes artifact: 7ef4754910 user: jan.nijtmans
09:25 • Closed ticket [81242a48c8]. artifact: fb1e792d1c user: jan.nijtmans
09:07
Fix documentation of Tcl_UtfAtIndex(), in accordance with implementation. See: [81242a48c8] check-in: 07324a311e user: jan.nijtmans tags: core-8-6-branch
09:06
Fix documentation of Tcl_UtfAtIndex(), in accordance with implementation. See: [81242a48c8] check-in: e44aa30085 user: jan.nijtmans tags: core-8-5-branch
2020-05-12
14:11 • Ticket [81242a48c8] Documentation for Tcl_UtfAtIndex() is incorrect status s... artifact: 82a0d025c2 user: jan.nijtmans
13:56 • Ticket [81242a48c8]: 3 changes artifact: 9f08cfd775 user: jan.nijtmans
13:22 • Ticket [81242a48c8]: 3 changes artifact: 4bbb001337 user: jan.nijtmans
13:20 • Ticket [81242a48c8]: 4 changes artifact: b3d088ecab user: jan.nijtmans
2020-05-10
20:50 • New ticket [81242a48c8]. artifact: 28e0c3a0d2 user: jan.nijtmans

Documentation for Tcl_UtfAtIndex() is incorrect

Closed / Fixed81242a48c80efaecf2c70271cafa2c2a0a3bf126Created 7 years agoUpdated 7 years ago
Descriptionwiki2020-05-10 20:50:44
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.

Referenced by2
  • 07324a311eFix documentation of Tcl_UtfAtIndex(), in accordance with implementation. See: [81242a48c8] check-in2020-05-13 09:07:48
  • e44aa30085Fix documentation of Tcl_UtfAtIndex(), in accordance with implementation. See: [81242a48c8] check-in2020-05-13 09:06:07
jan.nijtmanswiki2020-05-12 13:20:04

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.

jan.nijtmanswiki2020-05-12 13:22:04

Build failures are here: https://travis-ci.org/github/tcltk/tcl/jobs/685533037 (URL was pasted twice by accident in previous comment)

jan.nijtmanswiki2020-05-12 13:56:12

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:

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

So, the extra Tcl_UtfToUniChar() call is necessary.

jan.nijtmanswiki2020-05-12 14:11:21

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.

jan.nijtmanswiki2020-05-13 09:25:28

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.

jan.nijtmans Ticket [81242a48c8]: 4 changes [History diff]2020-05-13 09:26:14
jan.nijtmans Ticket [81242a48c8]: 4 changes [History diff]2020-05-13 09:26:44
Log in (anonymous is fine) to add a comment.