Overview
| Artifact ID: | 7ad5084b00363efcd90fd57bd8ea800f816f038885b0462881bf8bec907480b1 |
|---|---|
| Ticket: | 0d61d3a2bb905178694d6cde8147039974600274
Tcl interprets two adjacent surrogate code points as a character encoded using UTF-16 |
| User & Date: | jan.nijtmans on 2021-03-10 13:28:32 |
Changes
- closedate changed to: "2459284.06148672"
- closer changed to: "jan.nijtmans"
- comment changed to:
This report was inspired by [debd088e48998a75]. Various Unicode code points do not correspond to any abstract character. Such code points include private-use code points, surrogate code points, noncharacters, specials, tag characters, and unassigned code points. Although none of these code points correspond to any abstract character, the Unicode standard takes care not to forbid these code points in a Unicode string. Rather than saying that such characters and non-characters can not occur in a text, it simply labels some of them as "restricted interchange". Elsewhere, the standard explicitly permits their use for internal purposes. A surrogate pair is a sequence of two code points used in utf-16 to represent a character beyond those that could be represented in ucs-2. When a surrogate pair is encountered during the decoding of text encoded in utf-16, it is translated into another code point. These surrogate code points were added to Unicode to ensure no overlap between their use in utf-16 and other **external** uses. According to the Unicode standard, "Surrogate code points cannot be conformantly interchanged using Unicode encoding forms. They do not correspond to Unicode scalar values and thus do not have well-formed representations in any Unicode encoding form." The Unicode standard also states that "A process shall not interpret a high-surrogate code point or a low-surrogate code point as an abstract character." What it does not say is that a surrogate code point can not be used internally for some arbitrary purpose. We also know that the "abstract character" prohibition doesn't imply a prohibition on internal use because the Unicode standard explicitly says this about noncharacter code points: "The noncharacter code points may be used internally, such as for sentinel values or delimiters, but should not be exchanged publicly." What can be derived from all this is that if a code point does not map to an abstract character, in the absence of any other interpretation it should be treated merely as a code point, but not disallowed. Tcl correctly handles the case of a single surrogate code point by simply including the code point in the string as requested by the script author: <code><verbatim> string length xxx\ud83eyyy #-> 7 </verbatim></code> But when two surrogate code points occur successively Tcl does something bizarre: It translates the two code points into another code point as if it was a utf-16 decoder. <code><verbatim> expr { [encoding convertto utf-8 \ud83e\udd1d] eq [encoding convertto utf-8 \U1f91d] } # -> 1 </verbatim></code> This is a big mistake. Tcl is not a utf-16 decoder. For transcoding utf-16 Tcl provides <code><verbatim> encoding convertfrom/convertto utf-16 </verbatim></code> Tcl is also not a Unicode application. Rather, it is a tool for creating Unicode applications. In fact, it is even further removed than that: It is a tool for creating tools that create Unicode applications. As such, Tcl must be **Unicode-agnostic**, not **Unicode-conformant**. As an all-purpose tool for creating Unicode-conformant applications, Tcl must provide the capability to create arbitrary sequences of Unicode code points so that application authors can create systems that meet their needs. Restricting scripts to working only with Unicode characters rather than Unicode code points deprives those scripts of significant utility. What this means, for example, is that rather than performing normalization, Tcl should provide routines that perform whatever normalization a particular application requires. In the case at hand, though, what it means is that Tcl **should not** try to conform to the Unicode standard by becoming a quasi utf-16 transcoder. The application (script) author should be free to craft whatever Unicode strings they see fit to craft for internal use. If the author wants the HANDSHAKE character, there is an easy way to do that: <code><verbatim> set string \U1f91d </verbatim></code> Likewise, if the author wishes to create a sequence of two code points which both happen to be surrogates, there should be an easy way to do that: <code><verbatim> string length \ud83e\udd1d #-> 2 </verbatim></code> These two code points should subsequently continue to be treated as two code points. This facilitates the creation of any possible sequence of code points. The script level is not the place to expose details of utf-16 encoding. Rather, it should remain a pure and flexible Unicode environment where it is possible to craft any sequence of code points. It is the responsibility of the application author to ultimately produce conformant text. If a script attempts to convert to utf-16 a sequence of code points that can't be represented in utf-16, Tcl should then produce and error, as this is the boundary between internal and external use. Recent tickets hint at the troubles this new behaviour will cause. One example is [22324bcbdf]. The incorrect assumption in this ticket is that <code>\ud83d\udca3</code> should be interpreted as the BOMB character. It should not. Rather, it should be interpreted as two Unicode code points that each correspond to no abstract character. String reversal of these two code points then becomes trivial: The result of reversing <code>\ud83d\udca3</code> would be <code>\udca3\ud83d</code> In short, the only time Tcl should do anything special with surrogate code points is when it is converting to/from utf-16. Treating the <code>\u</code> notation of two consecutive surrogate code points as the character they encode in utf-16 makes as much sense as as treating the occurrence of <code>\xc0\x80</code> in a script as a <code>NULL</code> character, and would have similar consequences. This mode of operation cripples Tcl as a usable Unicode environment. Hopefully it will be backed out before it's too late. - foundin changed to: "8.7"
- icomment:
Thanks, Don, for this clear explanation. It inspired me to write [https://core.tcl-lang.org/tips/doc/trunk/tip/597.md|TIP #597], as a possible way out: No need to reduce the Tcl value set, but still the guarantee that produced "utf-8" conforms to the Unicode standard. I'm closing this ticket now.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Wont Fix"
- status changed to: "Closed"