Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "Encode::Unicode 3" | |
132 | .TH Encode::Unicode 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Encode::Unicode \-\- Various Unicode Transformation Formats | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 3 | |
138 | \& use Encode qw/encode decode/; | |
139 | \& $ucs2 = encode("UCS-2BE", $utf8); | |
140 | \& $utf8 = decode("UCS-2BE", $ucs2); | |
141 | .Ve | |
142 | .SH "ABSTRACT" | |
143 | .IX Header "ABSTRACT" | |
144 | This module implements all Character Encoding Schemes of Unicode that | |
145 | are officially documented by Unicode Consortium (except, of course, | |
146 | for \s-1UTF\-8\s0, which is a native format in perl). | |
147 | .IP "<http://www.unicode.org/glossary/> says:" 4 | |
148 | .IX Item "<http://www.unicode.org/glossary/> says:" | |
149 | \&\fICharacter Encoding Scheme\fR A character encoding form plus byte | |
150 | serialization. There are Seven character encoding schemes in Unicode: | |
151 | \&\s-1UTF\-8\s0, \s-1UTF\-16\s0, \s-1UTF\-16BE\s0, \s-1UTF\-16LE\s0, \s-1UTF\-32\s0 (\s-1UCS\-4\s0), \s-1UTF\-32BE\s0 (\s-1UCS\-4BE\s0) and | |
152 | \&\s-1UTF\-32LE\s0 (\s-1UCS\-4LE\s0), and \s-1UTF\-7\s0. | |
153 | .Sp | |
154 | Since \s-1UTF\-7\s0 is a 7\-bit (re)encoded version of \s-1UTF\-16BE\s0, It is not part of | |
155 | Unicode's Character Encoding Scheme. It is separately implemented in | |
156 | Encode::Unicode::UTF7. For details see Encode::Unicode::UTF7. | |
157 | .IP "Quick Reference" 4 | |
158 | .IX Item "Quick Reference" | |
159 | .Vb 13 | |
160 | \& Decodes from ord(N) Encodes chr(N) to... | |
161 | \& octet/char BOM S.P d800-dfff ord > 0xffff \ex{1abcd} == | |
162 | \& ---------------+-----------------+------------------------------ | |
163 | \& UCS-2BE 2 N N is bogus Not Available | |
164 | \& UCS-2LE 2 N N bogus Not Available | |
165 | \& UTF-16 2/4 Y Y is S.P S.P BE/LE | |
166 | \& UTF-16BE 2/4 N Y S.P S.P 0xd82a,0xdfcd | |
167 | \& UTF-16LE 2 N Y S.P S.P 0x2ad8,0xcddf | |
168 | \& UTF-32 4 Y - is bogus As is BE/LE | |
169 | \& UTF-32BE 4 N - bogus As is 0x0001abcd | |
170 | \& UTF-32LE 4 N - bogus As is 0xcdab0100 | |
171 | \& UTF-8 1-4 - - bogus >= 4 octets \exf0\ex9a\eaf\e8d | |
172 | \& ---------------+-----------------+------------------------------ | |
173 | .Ve | |
174 | .SH "Size, Endianness, and BOM" | |
175 | .IX Header "Size, Endianness, and BOM" | |
176 | You can categorize these \s-1CES\s0 by 3 criteria: size of each character, | |
177 | endianness, and Byte Order Mark. | |
178 | .Sh "by size" | |
179 | .IX Subsection "by size" | |
180 | \&\s-1UCS\-2\s0 is a fixed-length encoding with each character taking 16 bits. | |
181 | It \fBdoes not\fR support \fIsurrogate pairs\fR. When a surrogate pair | |
182 | is encountered during \fIdecode()\fR, its place is filled with \ex{\s-1FFFD\s0} | |
183 | if \fI\s-1CHECK\s0\fR is 0, or the routine croaks if \fI\s-1CHECK\s0\fR is 1. When a | |
184 | character whose ord value is larger than 0xFFFF is encountered, | |
185 | its place is filled with \ex{\s-1FFFD\s0} if \fI\s-1CHECK\s0\fR is 0, or the routine | |
186 | croaks if \fI\s-1CHECK\s0\fR is 1. | |
187 | .PP | |
188 | \&\s-1UTF\-16\s0 is almost the same as \s-1UCS\-2\s0 but it supports \fIsurrogate pairs\fR. | |
189 | When it encounters a high surrogate (0xD800\-0xDBFF), it fetches the | |
190 | following low surrogate (0xDC00\-0xDFFF) and \f(CW\*(C`desurrogate\*(C'\fRs them to | |
191 | form a character. Bogus surrogates result in death. When \ex{10000} | |
192 | or above is encountered during \fIencode()\fR, it \f(CW\*(C`ensurrogate\*(C'\fRs them and | |
193 | pushes the surrogate pair to the output stream. | |
194 | .PP | |
195 | \&\s-1UTF\-32\s0 (\s-1UCS\-4\s0) is a fixed-length encoding with each character taking 32 bits. | |
196 | Since it is 32\-bit, there is no need for \fIsurrogate pairs\fR. | |
197 | .Sh "by endianness" | |
198 | .IX Subsection "by endianness" | |
199 | The first (and now failed) goal of Unicode was to map all character | |
200 | repertoires into a fixed-length integer so that programmers are happy. | |
201 | Since each character is either a \fIshort\fR or \fIlong\fR in C, you have to | |
202 | pay attention to the endianness of each platform when you pass data | |
203 | to one another. | |
204 | .PP | |
205 | Anything marked as \s-1BE\s0 is Big Endian (or network byte order) and \s-1LE\s0 is | |
206 | Little Endian (aka \s-1VAX\s0 byte order). For anything not marked either | |
207 | \&\s-1BE\s0 or \s-1LE\s0, a character called Byte Order Mark (\s-1BOM\s0) indicating the | |
208 | endianness is prepended to the string. | |
209 | .PP | |
210 | \&\s-1CAVEAT:\s0 Though \s-1BOM\s0 in utf8 (\exEF\exBB\exBF) is valid, it is meaningless | |
211 | and as of this writing Encode suite just leave it as is (\ex{FeFF}). | |
212 | .IP "\s-1BOM\s0 as integer when fetched in network byte order" 4 | |
213 | .IX Item "BOM as integer when fetched in network byte order" | |
214 | .Vb 5 | |
215 | \& 16 32 bits/char | |
216 | \& ------------------------- | |
217 | \& BE 0xFeFF 0x0000FeFF | |
218 | \& LE 0xFFeF 0xFFFe0000 | |
219 | \& ------------------------- | |
220 | .Ve | |
221 | .PP | |
222 | This modules handles the \s-1BOM\s0 as follows. | |
223 | .IP "\(bu" 4 | |
224 | When \s-1BE\s0 or \s-1LE\s0 is explicitly stated as the name of encoding, \s-1BOM\s0 is | |
225 | simply treated as a normal character (\s-1ZERO\s0 \s-1WIDTH\s0 NO-BREAK \s-1SPACE\s0). | |
226 | .IP "\(bu" 4 | |
227 | When \s-1BE\s0 or \s-1LE\s0 is omitted during \fIdecode()\fR, it checks if \s-1BOM\s0 is at the | |
228 | beginning of the string; if one is found, the endianness is set to | |
229 | what the \s-1BOM\s0 says. If no \s-1BOM\s0 is found, the routine dies. | |
230 | .IP "\(bu" 4 | |
231 | When \s-1BE\s0 or \s-1LE\s0 is omitted during \fIencode()\fR, it returns a BE-encoded | |
232 | string with \s-1BOM\s0 prepended. So when you want to encode a whole text | |
233 | file, make sure you \fIencode()\fR the whole text at once, not line by line | |
234 | or each line, not file, will have a \s-1BOM\s0 prepended. | |
235 | .IP "\(bu" 4 | |
236 | \&\f(CW\*(C`UCS\-2\*(C'\fR is an exception. Unlike others, this is an alias of \s-1UCS\-2BE\s0. | |
237 | \&\s-1UCS\-2\s0 is already registered by \s-1IANA\s0 and others that way. | |
238 | .SH "Surrogate Pairs" | |
239 | .IX Header "Surrogate Pairs" | |
240 | To say the least, surrogate pairs were the biggest mistake of the | |
241 | Unicode Consortium. But according to the late Douglas Adams in \fIThe | |
242 | Hitchhiker's Guide to the Galaxy\fR Trilogy, \f(CW\*(C`In the beginning the | |
243 | Universe was created. This has made a lot of people very angry and | |
244 | been widely regarded as a bad move\*(C'\fR. Their mistake was not of this | |
245 | magnitude so let's forgive them. | |
246 | .PP | |
247 | (I don't dare make any comparison with Unicode Consortium and the | |
248 | Vogons here ;) Or, comparing Encode to Babel Fish is completely | |
249 | appropriate \*(-- if you can only stick this into your ear :) | |
250 | .PP | |
251 | Surrogate pairs were born when the Unicode Consortium finally | |
252 | admitted that 16 bits were not big enough to hold all the world's | |
253 | character repertoires. But they already made \s-1UCS\-2\s0 16\-bit. What | |
254 | do we do? | |
255 | .PP | |
256 | Back then, the range 0xD800\-0xDFFF was not allocated. Let's split | |
257 | that range in half and use the first half to represent the \f(CW\*(C`upper | |
258 | half of a character\*(C'\fR and the second half to represent the \f(CW\*(C`lower | |
259 | half of a character\*(C'\fR. That way, you can represent 1024 * 1024 = | |
260 | 1048576 more characters. Now we can store character ranges up to | |
261 | \&\ex{10ffff} even with 16\-bit encodings. This pair of half-character is | |
262 | now called a \fIsurrogate pair\fR and \s-1UTF\-16\s0 is the name of the encoding | |
263 | that embraces them. | |
264 | .PP | |
265 | Here is a formula to ensurrogate a Unicode character \ex{10000} and | |
266 | above; | |
267 | .PP | |
268 | .Vb 2 | |
269 | \& $hi = ($uni - 0x10000) / 0x400 + 0xD800; | |
270 | \& $lo = ($uni - 0x10000) % 0x400 + 0xDC00; | |
271 | .Ve | |
272 | .PP | |
273 | And to desurrogate; | |
274 | .PP | |
275 | .Vb 1 | |
276 | \& $uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00); | |
277 | .Ve | |
278 | .PP | |
279 | Note this move has made \ex{D800}\-\ex{\s-1DFFF\s0} into a forbidden zone but | |
280 | perl does not prohibit the use of characters within this range. To perl, | |
281 | every one of \ex{0000_0000} up to \ex{ffff_ffff} (*) is \fIa character\fR. | |
282 | .PP | |
283 | .Vb 2 | |
284 | \& (*) or \ex{ffff_ffff_ffff_ffff} if your perl is compiled with 64-bit | |
285 | \& integer support! | |
286 | .Ve | |
287 | .SH "Error Checking" | |
288 | .IX Header "Error Checking" | |
289 | Unlike most encodings which accept various ways to handle errors, | |
290 | Unicode encodings simply croaks. | |
291 | .PP | |
292 | .Vb 6 | |
293 | \& % perl -MEncode -e '$_ = "\exfe\exff\exd8\exd9\exda\exdb\e0\en"' \e | |
294 | \& -e 'Encode::from_to($_, "utf16","shift_jis", 0); print' | |
295 | \& UTF-16:Malformed LO surrogate d8d9 at /path/to/Encode.pm line 184. | |
296 | \& % perl -MEncode -e '$a = "BOM missing"' \e | |
297 | \& -e ' Encode::from_to($a, "utf16", "shift_jis", 0); print' | |
298 | \& UTF-16:Unrecognised BOM 424f at /path/to/Encode.pm line 184. | |
299 | .Ve | |
300 | .PP | |
301 | Unlike other encodings where mappings are not one-to-one against | |
302 | Unicode, UTFs are supposed to map 100% against one another. So Encode | |
303 | is more strict on UTFs. | |
304 | .PP | |
305 | Consider that \*(L"division by zero\*(R" of Encode :) | |
306 | .SH "SEE ALSO" | |
307 | .IX Header "SEE ALSO" | |
308 | Encode, Encode::Unicode::UTF7, <http://www.unicode.org/glossary/>, | |
309 | <http://www.unicode.org/unicode/faq/utf_bom.html>, | |
310 | .PP | |
311 | \&\s-1RFC\s0 2781 <http://rfc.net/rfc2781.html>, | |
312 | .PP | |
313 | The whole Unicode standard <http://www.unicode.org/unicode/uni2book/u2.html> | |
314 | .PP | |
315 | Ch. 15, pp. 403 of \f(CW\*(C`Programming Perl (3rd Edition)\*(C'\fR | |
316 | by Larry Wall, Tom Christiansen, Jon Orwant; | |
317 | O'Reilly & Associates; \s-1ISBN\s0 0\-596\-00027\-8 |