Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2021-04-07
12:49 • Closed ticket [e617e8a71a]: comparing UTF-16 strings doesn't account for sur... artifact: ecc384092c user: jan.nijtmans
2020-04-30
19:12 • Ticket [e617e8a71a]: 4 changes artifact: 43c29e23e3 user: dgp
18:23 • Ticket [e617e8a71a]: 3 changes artifact: 1517a02e77 user: dgp
2020-04-29
21:20 • Ticket [e617e8a71a]: 4 changes artifact: 8671b9ccdf user: dgp
18:05 • Ticket [e617e8a71a]: 3 changes artifact: eb61088af8 user: dgp
17:55 • Ticket [e617e8a71a]: 3 changes artifact: cf69f7f4c7 user: dgp
17:50 • Ticket [e617e8a71a]: 3 changes artifact: c912054732 user: dgp
2020-04-27
21:14 • New ticket [e617e8a71a]. artifact: 671304cb32 user: dgp

comparing UTF-16 strings doesn't account for surrogates

Closed / Fixede617e8a71a00496e9ec682caa1b4e7b5b98c55a7Created 7 years agoUpdated 6 years ago
Descriptionplain2020-04-27 21:14:27
==== utf-25.5 Tcl_UniCharNcasecmp FAILED
==== Contents of test case:

	teststringobj set 1 $one
	teststringobj set 2 $two
	teststringobj maxchars 1
	teststringobj maxchars 2
	set result [string compare -nocase [teststringobj get 1] [teststringobj get 2]]
	if {$result eq [string map {< -1 = 0 > 1} $order]} {
	    set result ok
	} else {
	    set result "'$one' should be $order '$two' (no case)"
	}
	set result
    
---- Result was:
'￿' should be < '𐀀' (no case)
---- Result should have been (exact matching):
ok
==== utf-25.5 FAILED



==== utf-25.6 Tcl_UniCharNcasecmp FAILED
==== Contents of test case:

	teststringobj set 1 $one
	teststringobj set 2 $two
	teststringobj maxchars 1
	teststringobj maxchars 2
	set result [string compare -nocase [teststringobj get 1] [teststringobj get 2]]
	if {$result eq [string map {< -1 = 0 > 1} $order]} {
	    set result ok
	} else {
	    set result "'$one' should be $order '$two' (no case)"
	}
	set result
    
---- Result was:
'￿' should be < '𐀀' (no case)
---- Result should have been (exact matching):
ok
==== utf-25.6 FAILED
dgpplain2020-04-29 17:50:02
First attempt at a fix committed. It exposes trouble with the interface.

The routine signature is

Tcl_UniCharNcasecmp(const Tcl_UniChar *ucs, const Tcl_UniChar *uct, unsigned long numChars)

The numChars argument name suggests we should count chars, but we've
been counting UCS-2 code units.  As we enter a world where those are not
the same thing, we have to re-specify what the argument should be taken to mean.

If callers think of the argument as the number of UCS-2 code units, the length
of sub-array, then the calls can stay unchanged, but when that number splits
a character, we'll get nonsense results, as seen in this first attempt.

We want to compare the character \uFFFF against the character \uD800\uDC00
but the (numChars = 1) argument prevents us taking the low surrogate into account
as we must do to get the right answer.
dgpplain2020-04-29 17:55:37
Note that TIP 542 has deprecated this function.
dgpplain2020-04-29 18:05:36
So long as we stick to that deprecation, I should not be trying to fix this
routine, but eliminating its use.
dgpplain2020-04-29 21:20:55
Tcl_UniCharNcmp() is also deprecated, and also has similar issues.

% string compare \uFFFF\uFFFF \U10000\U10000
-1
% string compare [string repeat \uFFFF 2] [string repeat \U10000 2]
1
dgpplain2020-04-30 18:23:27
Work continues on this in a branch, but it's worth noting that matters
are confusing and difficult because routines and commands that by
their names and documentation one expects to operate on characters
are actually operating on UTF-16 code units.  For example, if I don't
think very carefully about the implementation details, this result is
surprising to me:

% string compare -length 1 \U10000 \U10001
0

I think it would be surprising to anyone giving the docs for
[string compare] a plain reading.

My knee-jerk reaction is to want to revise 8.7 so indexing and lengths
count characters and not code units, but I know that means a TIP,
and I have to believe there were strong reasons not to do that in
the first place.

If we do not attempt something like that, though, we'll need to 
improve the clarity of a lot of documentation on this point.
dgpplain2020-04-30 19:12:48
According to the revised understanding that [string compare] operates
and is defined on sequences of UTF-16 code units and not on sequences
of characters, this bug is Not a Bug at all.

\UFFFF is the one-unit string \uFFFF
\U10000 is the two-unit string \uD800\uDC00

and indeed the first sorts higher than the second.

Expecting character comparisons is currently something that can only
be done under constraint "ucs4".

I'm not happy about that, but it seems to be the status of things now.

I'm going to keep this ticket open, but it's now a different category of work.
jan.nijtmanswiki2021-04-07 12:49:18

Dup of [4c591fa487], which is fixed now.

Log in (anonymous is fine) to add a comment.