Skip to content
Tcl Source Code Artifact [16094ff866]
Login

Artifact [16094ff866]

Artifact 16094ff8660d522ff1e5e7a28f23aa812766d9cd:

Wiki page [UTF-8 String] by dgp on 2014-09-06 23:13:09.
D 2014-09-06T23:13:09.398
L UTF-8\sString
P dbc79597964dad829c895b1051831978674c69e3
U dgp
W 4190
<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
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 <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 the <b>Tcl_UniChar</b> 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 <b>Tcl_UniChar</b> 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 <a href="http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8">Modified UTF-8</a> 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 (<b>#define TCL_UTF_MAX 4</b>).




Z 781cec6a013a30dd0e853f6ab2b3f9a2