1Encode::Unicode(3pm) Perl Programmers Reference Guide Encode::Unicode(3pm)
2
3
4
6 Encode::Unicode -- Various Unicode Transformation Formats
7
9 use Encode qw/encode decode/;
10 $ucs2 = encode("UCS-2BE", $utf8);
11 $utf8 = decode("UCS-2BE", $ucs2);
12
14 This module implements all Character Encoding Schemes of Unicode that
15 are officially documented by Unicode Consortium (except, of course, for
16 UTF-8, which is a native format in perl).
17
18 <http://www.unicode.org/glossary/> says:
19 Character Encoding Scheme A character encoding form plus byte seri‐
20 alization. There are Seven character encoding schemes in Unicode:
21 UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32 (UCS-4), UTF-32BE
22 (UCS-4BE) and UTF-32LE (UCS-4LE), and UTF-7.
23
24 Since UTF-7 is a 7-bit (re)encoded version of UTF-16BE, It is not
25 part of Unicode's Character Encoding Scheme. It is separately
26 implemented in Encode::Unicode::UTF7. For details see Encode::Uni‐
27 code::UTF7.
28
29 Quick Reference
30 Decodes from ord(N) Encodes chr(N) to...
31 octet/char BOM S.P d800-dfff ord > 0xffff \x{1abcd} ==
32 ---------------+-----------------+------------------------------
33 UCS-2BE 2 N N is bogus Not Available
34 UCS-2LE 2 N N bogus Not Available
35 UTF-16 2/4 Y Y is S.P S.P BE/LE
36 UTF-16BE 2/4 N Y S.P S.P 0xd82a,0xdfcd
37 UTF-16LE 2 N Y S.P S.P 0x2ad8,0xcddf
38 UTF-32 4 Y - is bogus As is BE/LE
39 UTF-32BE 4 N - bogus As is 0x0001abcd
40 UTF-32LE 4 N - bogus As is 0xcdab0100
41 UTF-8 1-4 - - bogus >= 4 octets \xf0\x9a\af\8d
42 ---------------+-----------------+------------------------------
43
45 You can categorize these CES by 3 criteria: size of each character,
46 endianness, and Byte Order Mark.
47
48 by size
49
50 UCS-2 is a fixed-length encoding with each character taking 16 bits.
51 It does not support surrogate pairs. When a surrogate pair is encoun‐
52 tered during decode(), its place is filled with \x{FFFD} if CHECK is 0,
53 or the routine croaks if CHECK is 1. When a character whose ord value
54 is larger than 0xFFFF is encountered, its place is filled with \x{FFFD}
55 if CHECK is 0, or the routine croaks if CHECK is 1.
56
57 UTF-16 is almost the same as UCS-2 but it supports surrogate pairs.
58 When it encounters a high surrogate (0xD800-0xDBFF), it fetches the
59 following low surrogate (0xDC00-0xDFFF) and "desurrogate"s them to form
60 a character. Bogus surrogates result in death. When \x{10000} or
61 above is encountered during encode(), it "ensurrogate"s them and pushes
62 the surrogate pair to the output stream.
63
64 UTF-32 (UCS-4) is a fixed-length encoding with each character taking 32
65 bits. Since it is 32-bit, there is no need for surrogate pairs.
66
67 by endianness
68
69 The first (and now failed) goal of Unicode was to map all character
70 repertoires into a fixed-length integer so that programmers are happy.
71 Since each character is either a short or long in C, you have to pay
72 attention to the endianness of each platform when you pass data to one
73 another.
74
75 Anything marked as BE is Big Endian (or network byte order) and LE is
76 Little Endian (aka VAX byte order). For anything not marked either BE
77 or LE, a character called Byte Order Mark (BOM) indicating the endian‐
78 ness is prepended to the string.
79
80 CAVEAT: Though BOM in utf8 (\xEF\xBB\xBF) is valid, it is meaningless
81 and as of this writing Encode suite just leave it as is (\x{FeFF}).
82
83 BOM as integer when fetched in network byte order
84 16 32 bits/char
85 -------------------------
86 BE 0xFeFF 0x0000FeFF
87 LE 0xFFeF 0xFFFe0000
88 -------------------------
89
90 This modules handles the BOM as follows.
91
92 · When BE or LE is explicitly stated as the name of encoding, BOM is
93 simply treated as a normal character (ZERO WIDTH NO-BREAK SPACE).
94
95 · When BE or LE is omitted during decode(), it checks if BOM is at
96 the beginning of the string; if one is found, the endianness is set
97 to what the BOM says. If no BOM is found, the routine dies.
98
99 · When BE or LE is omitted during encode(), it returns a BE-encoded
100 string with BOM prepended. So when you want to encode a whole text
101 file, make sure you encode() the whole text at once, not line by
102 line or each line, not file, will have a BOM prepended.
103
104 · "UCS-2" is an exception. Unlike others, this is an alias of
105 UCS-2BE. UCS-2 is already registered by IANA and others that way.
106
108 To say the least, surrogate pairs were the biggest mistake of the Uni‐
109 code Consortium. But according to the late Douglas Adams in The Hitch‐
110 hiker's Guide to the Galaxy Trilogy, "In the beginning the Universe was
111 created. This has made a lot of people very angry and been widely
112 regarded as a bad move". Their mistake was not of this magnitude so
113 let's forgive them.
114
115 (I don't dare make any comparison with Unicode Consortium and the
116 Vogons here ;) Or, comparing Encode to Babel Fish is completely appro‐
117 priate -- if you can only stick this into your ear :)
118
119 Surrogate pairs were born when the Unicode Consortium finally admitted
120 that 16 bits were not big enough to hold all the world's character
121 repertoires. But they already made UCS-2 16-bit. What do we do?
122
123 Back then, the range 0xD800-0xDFFF was not allocated. Let's split that
124 range in half and use the first half to represent the "upper half of a
125 character" and the second half to represent the "lower half of a char‐
126 acter". That way, you can represent 1024 * 1024 = 1048576 more charac‐
127 ters. Now we can store character ranges up to \x{10ffff} even with
128 16-bit encodings. This pair of half-character is now called a surro‐
129 gate pair and UTF-16 is the name of the encoding that embraces them.
130
131 Here is a formula to ensurrogate a Unicode character \x{10000} and
132 above;
133
134 $hi = ($uni - 0x10000) / 0x400 + 0xD800;
135 $lo = ($uni - 0x10000) % 0x400 + 0xDC00;
136
137 And to desurrogate;
138
139 $uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);
140
141 Note this move has made \x{D800}-\x{DFFF} into a forbidden zone but
142 perl does not prohibit the use of characters within this range. To
143 perl, every one of \x{0000_0000} up to \x{ffff_ffff} (*) is a charac‐
144 ter.
145
146 (*) or \x{ffff_ffff_ffff_ffff} if your perl is compiled with 64-bit
147 integer support!
148
150 Unlike most encodings which accept various ways to handle errors, Uni‐
151 code encodings simply croaks.
152
153 % perl -MEncode -e '$_ = "\xfe\xff\xd8\xd9\xda\xdb\0\n"' \
154 -e 'Encode::from_to($_, "utf16","shift_jis", 0); print'
155 UTF-16:Malformed LO surrogate d8d9 at /path/to/Encode.pm line 184.
156 % perl -MEncode -e '$a = "BOM missing"' \
157 -e ' Encode::from_to($a, "utf16", "shift_jis", 0); print'
158 UTF-16:Unrecognised BOM 424f at /path/to/Encode.pm line 184.
159
160 Unlike other encodings where mappings are not one-to-one against Uni‐
161 code, UTFs are supposed to map 100% against one another. So Encode is
162 more strict on UTFs.
163
164 Consider that "division by zero" of Encode :)
165
167 Encode, Encode::Unicode::UTF7, <http://www.unicode.org/glossary/>,
168 <http://www.unicode.org/unicode/faq/utf_bom.html>,
169
170 RFC 2781 <http://rfc.net/rfc2781.html>,
171
172 The whole Unicode standard <http://www.unicode.org/uni‐
173 code/uni2book/u2.html>
174
175 Ch. 15, pp. 403 of "Programming Perl (3rd Edition)" by Larry Wall, Tom
176 Christiansen, Jon Orwant; O'Reilly & Associates; ISBN 0-596-00027-8
177
178
179
180perl v5.8.8 2001-09-21 Encode::Unicode(3pm)