Skip to content
Tcl Source Code Update of ”UTF-8 String”
Login

Update of ”UTF-8 String”

Overview

Artifact ID: 16094ff8660d522ff1e5e7a28f23aa812766d9cd
Page Name:UTF-8 String
Date: 2014-09-06 23:13:09
Original User: dgp
Parent: dbc79597964dad829c895b1051831978674c69e3 (diff)
Next 28632b4241fd4f04146a966b0d8d17270ea55b0b
Content

Source Files

Public Interface

Private Interface

Directly Depends On Public Interface

Directly Depends On Private Interface of

Discussion

This module provides a set of utility functions to operate on Tcl_UniChar arrays and char arrays with content in the internal encoding of Tcl 8. This encoding is misleadingly referred to as "Utf". Likewise the documentation refers to Tcl_UniChar arrays as "Unicode strings" though that use of the term isn't conventional anywhere else. Many of the functions expose the categorizations of case and character classes defined by Unicode.

The tclWin32Dll.c file contains other routines not mentioned here, but they do not appear to belong in this function area. The routines Tcl_Win*To* that are mentioned here appear to be legacy routines only for Windows platforms we no longer support. They are nothing more than typecasts applied to other public routines.

The dependence on Parsing and Evaluation internals is only in Tcl_UtfBackslash which arguably belongs in parsing instead, and the use of TclIsSpaceProc which could easily be avoided. The dependence on Object System and Fundamental Object Types is only in the exporting interfaces to Tcl_DString, also easily remedied. This module is well within reach of being entirely self contained.

The three private routines are likely good candidates for exporting. They have callers. In some cases perhaps they are preferred replacements for something already public.

The key flaw here is that the Tcl_UniChar type is too small to hold all the codepoint values in the Unicode range (causing Tcl to be restricted to characters in the "Basic Multilingual Plane" (BMP)). This can likely be remedied by just enlarging the type while still retaining source compatibility. Another idea that's been mentioned is to just do away with the Tcl_UniChar representation and address the problems it solves in other ways.

---

Re "Other ways": One idea is to make the 'string rep' not a copy of the bytes in a different format, but some type of index over them, allowing (amortized) O(1) access to all characters despite the vari-length of UTF-8 encoding. While the idea of "ropes" has been thrown about it is not clear if that can be combined with the above or not.

A different idea is to go with "surrogate pairs", where two Tcl_UniChar would represent the characters outside of the BMP.

Neither idea has been fully thought through yet AFAIK (aku).

Re "misleading naming of the internal encoding as utf" ... Our encoding is the same Modified UTF-8 used by Java.

Note further that Tcl 8 restricts UTF-8 to a maximum length of 3 bytes. This is in line with our restriction to the BMP. Covering the entire set of Unicode codepoints requires us to support UTF-8 to up to 4 characters (#define TCL_UTF_MAX 4).