Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2023-02-27
20:54 • Closed ticket [821620d417]: Different handling of surrogates in utf-8 between Tc... artifact: d22a7e3f05 user: jan.nijtmans
09:49 • Ticket [821620d417]: 3 changes artifact: 6b34fa074d user: apnadkarni
2023-02-26
22:04 • Pending ticket [821620d417]. artifact: 2f2280c288 user: jan.nijtmans
2023-02-25
04:56 • New ticket [821620d417]. artifact: b29857c701 user: apnadkarni

Different handling of surrogates in utf-8 between Tcl8 and Tcl9

Closed / Invalid821620d417f604c40619b610348af78d4c32b855Created 4 years agoUpdated 4 years ago
Descriptionmarkdown2023-02-25 04:56:26

In Tcl 8.7,

% package require Tcl
8.7a6
% codepoints [encoding convertfrom utf-8 [binary decode hex EDAFBFEDBFBF]]
U+10FFFF

In Tcl 9,

% package require Tcl
9.0a4
% codepoints [encoding convertfrom utf-8 [binary decode hex EDAFBFEDBFBF]]
U+00DBFF U+00DFFF

Surrogate pairs are not allowed in strict utf-8 but the above is not strict. In my opinion, the Tcl 8.7 behavior is preferable but since this is non-conformant anyways, either behavior might be ok. But they should be consistent between Tcl 8 and 9.

jan.nijtmanswiki2023-02-26 22:04:44

This change is explained in TIP #619:

https://core.tcl-lang.org/tips/doc/trunk/tip/619.md

Tcl 8.6/8.7 always combines surrogates when it encounters a pair. This is wrong, since it makes the "string length" inconsistant: Glueing two strings together with length 1 (an Upper and a Lower surrogate) results in a new string with length 1. Because Tcl 8.7 uses 3 different internal representations (UTF-8, UTF-16 and UTF-32), and can freely switch between them, there's no way to distinguish a surrogate-pair and a code-point > U+FFFF.

In Tcl 9.0, Surrogates are never joined, so a round-trip between utf-8 -> utf-32 ->utf-8 is losless. That's the intention of TIP #619.

See also https://core.tcl-lang.org/tcl/tktview?name=0d61d3a2bb, which contains the discussion which lead to exactly this change.

apnadkarniplain2023-02-27 09:49:15
Ok, thanks. I'll adapt the test cases accordingly.
jan.nijtmans Closed ticket [821620d417]. [History diff]2023-02-27 20:54:43
Log in (anonymous is fine) to add a comment.