Skip to content
Tcl Source Code Artifact [6913f70b02]
Login

Artifact [6913f70b02]

Artifact 6913f70b02af5271dcfd7d88ab02931c0ab4f160:

Wiki page [UTF-8 String] by dgp on 2014-09-07 02:19:58.
D 2014-09-07T02:19:58.990
L UTF-8\sString
P 28632b4241fd4f04146a966b0d8d17270ea55b0b
U dgp
W 4627
<h1>Source Files</h1>
  *  [/finfo?name=generic/tclUtf.c|tclUtf.c]
  *  [/finfo?name=win/tclWin32Dll.c|tclWin32Dll.c]

<h1>Public Interface</h1>
  *  Tcl_UniCharToUtf
  *  Tcl_UniCharToUtfDString
  *  Tcl_UtfToUniChar
  *  Tcl_UtfToUniCharDString
  *  Tcl_UtfCharComplete
  *  Tcl_NumUtfChars
  *  Tcl_UtfFindFirst
  *  Tcl_UtfFindLast
  *  Tcl_UtfNext
  *  Tcl_UtfPrev
  *  Tcl_UniCharAtIndex
  *  Tcl_UtfAtIndex
  *  Tcl_UtfBackslash
  *  Tcl_UtfToUpper
  *  Tcl_UtfToLower
  *  Tcl_UtfToTitle
  *  Tcl_UtfNcmp
  *  Tcl_UtfNcasecmp
  *  Tcl_UniCharToUpper
  *  Tcl_UniCharToLower
  *  Tcl_UniCharToTitle
  *  Tcl_UniCharLen
  *  Tcl_UniCharNcmp
  *  Tcl_UniCharNcasecmp
  *  Tcl_UniCharIsAlnum
  *  Tcl_UniCharIsAlpha
  *  Tcl_UniCharIsControl
  *  Tcl_UniCharIsDigit
  *  Tcl_UniCharIsGraph
  *  Tcl_UniCharIsLower
  *  Tcl_UniCharIsPrint
  *  Tcl_UniCharIsPunct
  *  Tcl_UniCharIsSpace
  *  Tcl_UniCharIsUpper
  *  Tcl_UniCharIsWordChar
  *  Tcl_UniCharCaseMatch
  *  Tcl_WinUtfToTChar
  *  Tcl_WinTCharToUtf

<h1>Private Interface</h1>
  *  TclUtfCasecmp
  *  TclUniCharMatch
  *  TclpUtfNcmp2

<h1>Directly Depends On Public Interface</h1>
  *  [Object System and Fundamental Object Types]

<h1>Directly Depends On Private Interface of</h1>
  *  [Parsing and Evaluation]
 
<h1>Discussion</h1>

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
referred to as "Utf", but is more precisely <a href="http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8">Modified UTF-8</a> without surrogate pair support.  Likewise the documentation refers to 
Tcl_UniChar arrays as "Unicode strings" though that term lacks precision.  The storage in
these arrays is in UCS-2.  Many of the functions expose the categorizations
of <i>case</i> and character classes defined by Unicode.

The <i>tclWin32Dll.c</i> file contains other routines not mentioned here, but
they do not appear to belong in this function area.  The routines <b>Tcl_Win*To*</b>
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 <b>Tcl_UtfBackslash</b>
which arguably belongs in parsing instead, and the use of <b>TclIsSpaceProc</b> which
could easily be avoided.  The dependence on [Object System and Fundamental Object Types]
is only in the exporting interfaces to <b>Tcl_DString</b>, 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 UCS-2 fails to encode the complete set of Unicode codepoints.
In encodes only the "Basic Multilingual Plane" (BMP).  One binary compatible way to
remedy that flaw is to convert from UCS-2 to UTF-16 in the same storage arrays.
[http://tip.tcl.tk/389|TIP 389] points in that direction.

One problem with that answer is that the key reason UCS-2 is used in the first place is
because it is a fixed-width encoding, and that fact allows for more efficient string 
processing operations in finding a character at an index, extracting and substituting
substrings, and computing lengths.  (It is also used to efficiently and losslessly take in
UTF-16 data from external sources, notably command line data on Windows.  On the
contrary, UTF-16 is a variable length encoding so the advantage for those operations
is at least diminished, if not lost.  If we must manage a variable-length encoding
anyway, we might as well just stick with one encoding for the simplicity that would
imply.  To the extent that might happen, many of the routines here would just become
deprecated legacy support routines.


---

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.

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 (<b>#define TCL_UTF_MAX 4</b>).




Z dd2caa0bb63dc856226b7ae8386e8109