摘记—Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha

来源:互联网 发布:java代码的基本格式 编辑:程序博客网 时间:2024/05/22 15:23

What a Unicode string ?

The binaries in RAM have the final word. NOT string literals in Text Editor(VS, Emacs), but the executable binary(in .str section) or binary data file(like cookie cache file in some sort of encoding) and loaded into variables / data structures like std::string.

string literal only tells the compiler to treat string literals as UTF-8 or UTF-16( L / _T() ), and thus come into .str section of a executable file image on disk .

 

code point : index to letters in code pages.

code pages :non-ASCII values (values greater than 127) represent international characters. These code pages are used natively in Windows Me, and are also available on Windows NT and later.


0-31 : ANSI unprintable

32 - 127:ANSI printable

ASCII table

128 + : OEM charsets -> (codified into ANSI)  : ANSI code pages ( IBM,M$)

在 Unicode 使用之前,通过DBCS来操纵编码 single/double byte 混合的char。Joel 称之为, messy system。 尤其突出的,是char分界的问题,比如,s++ and s--  和 Windows' AnsiNext and AnsiPrev 。


Unicode 通过fixed的2个byte,很好地划定界限。但是有如下的特点

(1)通过debate解决的:UTF-16的 non-ANSI 的 字符集合。 并且,因此导致UTF-16其实并不仅是65536种可能字符。

(2)在UTF-16中,128以下的每个char都会被扩展到2bytes,与原本的ANSI不兼容:需要修改之前的代码。

Windows API 在 NT 之后采用了UTF-16,因此,很多API加上了A或者W的后缀。

(The "A" version handles text based on Windows code pages, while the "W" version handles Unicode text. )

对于英语国家的人来讲,其实ANSI已经够用了。

(3) 2个byte自然有先后的问题,于是,需要添加BOM头来识别是little/big endian。


UTF-16 由于浪费空间的问题,被“冷遇”了几年,直到做出改进,得到UTF-8。

UTF-8是一个“变长”的编码系统。ANSI部分(0-127)是1byte的编码,这样,可以seamlessly和ANSI对接,并且不需要修改古老的代码。

之后,有2~6bytes不等的编码。但共同点是:没有一个byte是0x0。这一点,对于 old string-processing code that wants to use a single 0 byte as the null-terminator 就不会盲目截断strings了。



0 0
原创粉丝点击