Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Encode::Unicode.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Encode::Unicode 3pm"
61 .TH Encode::Unicode 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Encode::Unicode \-\- Various Unicode Transformation Formats
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 3
71 \& use Encode qw/encode decode/;
72 \& $ucs2 = encode("UCS\-2BE", $utf8);
73 \& $utf8 = decode("UCS\-2BE", $ucs2);
74 .Ve
75 .SH ABSTRACT
76 .IX Header "ABSTRACT"
77 This module implements all Character Encoding Schemes of Unicode that
78 are officially documented by Unicode Consortium (except, of course,
79 for UTF\-8, which is a native format in perl).
80 .IP "<http://www.unicode.org/glossary/> says:" 4
81 .IX Item "<http://www.unicode.org/glossary/> says:"
82 \&\fICharacter Encoding Scheme\fR A character encoding form plus byte
83 serialization. There are Seven character encoding schemes in Unicode:
84 UTF\-8, UTF\-16, UTF\-16BE, UTF\-16LE, UTF\-32 (UCS\-4), UTF\-32BE (UCS\-4BE) and
85 UTF\-32LE (UCS\-4LE), and UTF\-7.
86 .Sp
87 Since UTF\-7 is a 7\-bit (re)encoded version of UTF\-16BE, It is not part of
88 Unicode\*(Aqs Character Encoding Scheme. It is separately implemented in
89 Encode::Unicode::UTF7. For details see Encode::Unicode::UTF7.
90 .IP "Quick Reference" 4
91 .IX Item "Quick Reference"
92 .Vb 10
93 \& Decodes from ord(N) Encodes chr(N) to...
94 \& octet/char BOM S.P d800\-dfff ord > 0xffff \ex{1abcd} ==
95 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
96 \& UCS\-2BE 2 N N is bogus Not Available
97 \& UCS\-2LE 2 N N bogus Not Available
98 \& UTF\-16 2/4 Y Y is S.P S.P BE/LE
99 \& UTF\-16BE 2/4 N Y S.P S.P 0xd82a,0xdfcd
100 \& UTF\-16LE 2/4 N Y S.P S.P 0x2ad8,0xcddf
101 \& UTF\-32 4 Y \- is bogus As is BE/LE
102 \& UTF\-32BE 4 N \- bogus As is 0x0001abcd
103 \& UTF\-32LE 4 N \- bogus As is 0xcdab0100
104 \& UTF\-8 1\-4 \- \- bogus >= 4 octets \exf0\ex9a\eaf\e8d
105 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
106 .Ve
107 .SH "Size, Endianness, and BOM"
108 .IX Header "Size, Endianness, and BOM"
109 You can categorize these CES by 3 criteria: size of each character,
110 endianness, and Byte Order Mark.
111 .SS "by size"
112 .IX Subsection "by size"
113 UCS\-2 is a fixed\-length encoding with each character taking 16 bits.
114 It \fBdoes not\fR support \fIsurrogate pairs\fR. When a surrogate pair
115 is encountered during \fBdecode()\fR, its place is filled with \ex{FFFD}
116 if \fICHECK\fR is 0, or the routine croaks if \fICHECK\fR is 1. When a
117 character whose ord value is larger than 0xFFFF is encountered,
118 its place is filled with \ex{FFFD} if \fICHECK\fR is 0, or the routine
119 croaks if \fICHECK\fR is 1.
120 .PP
121 UTF\-16 is almost the same as UCS\-2 but it supports \fIsurrogate pairs\fR.
122 When it encounters a high surrogate (0xD800\-0xDBFF), it fetches the
123 following low surrogate (0xDC00\-0xDFFF) and \f(CW\*(C`desurrogate\*(C'\fRs them to
124 form a character. Bogus surrogates result in death. When \ex{10000}
125 or above is encountered during \fBencode()\fR, it \f(CW\*(C`ensurrogate\*(C'\fRs them and
126 pushes the surrogate pair to the output stream.
127 .PP
128 UTF\-32 (UCS\-4) is a fixed\-length encoding with each character taking 32 bits.
129 Since it is 32\-bit, there is no need for \fIsurrogate pairs\fR.
130 .SS "by endianness"
131 .IX Subsection "by endianness"
132 The first (and now failed) goal of Unicode was to map all character
133 repertoires into a fixed\-length integer so that programmers are happy.
134 Since each character is either a \fIshort\fR or \fIlong\fR in C, you have to
135 pay attention to the endianness of each platform when you pass data
136 to one another.
137 .PP
138 Anything marked as BE is Big Endian (or network byte order) and LE is
139 Little Endian (aka VAX byte order). For anything not marked either
140 BE or LE, a character called Byte Order Mark (BOM) indicating the
141 endianness is prepended to the string.
142 .PP
143 CAVEAT: Though BOM in utf8 (\exEF\exBB\exBF) is valid, it is meaningless
144 and as of this writing Encode suite just leave it as is (\ex{FeFF}).
145 .IP "BOM as integer when fetched in network byte order" 4
146 .IX Item "BOM as integer when fetched in network byte order"
147 .Vb 5
148 \& 16 32 bits/char
149 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
150 \& BE 0xFeFF 0x0000FeFF
151 \& LE 0xFFFe 0xFFFe0000
152 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
153 .Ve
154 .PP
155 This modules handles the BOM as follows.
156 .IP \(bu 4
157 When BE or LE is explicitly stated as the name of encoding, BOM is
158 simply treated as a normal character (ZERO WIDTH NO\-BREAK SPACE).
159 .IP \(bu 4
160 When BE or LE is omitted during \fBdecode()\fR, it checks if BOM is at the
161 beginning of the string; if one is found, the endianness is set to
162 what the BOM says.
163 .IP \(bu 4
164 Default Byte Order
165 .Sp
166 When no BOM is found, Encode 2.76 and blow croaked. Since Encode
167 2.77, it falls back to BE accordingly to RFC2781 and the Unicode
168 Standard version 8.0
169 .IP \(bu 4
170 When BE or LE is omitted during \fBencode()\fR, it returns a BE\-encoded
171 string with BOM prepended. So when you want to encode a whole text
172 file, make sure you \fBencode()\fR the whole text at once, not line by line
173 or each line, not file, will have a BOM prepended.
174 .IP \(bu 4
175 \&\f(CW\*(C`UCS\-2\*(C'\fR is an exception. Unlike others, this is an alias of UCS\-2BE.
176 UCS\-2 is already registered by IANA and others that way.
177 .SH "Surrogate Pairs"
178 .IX Header "Surrogate Pairs"
179 To say the least, surrogate pairs were the biggest mistake of the
180 Unicode Consortium. But according to the late Douglas Adams in \fIThe
181 Hitchhiker\*(Aqs Guide to the Galaxy\fR Trilogy, \f(CW\*(C`In the beginning the
182 Universe was created. This has made a lot of people very angry and
183 been widely regarded as a bad move\*(C'\fR. Their mistake was not of this
184 magnitude so let\*(Aqs forgive them.
185 .PP
186 (I don\*(Aqt dare make any comparison with Unicode Consortium and the
187 Vogons here ;) Or, comparing Encode to Babel Fish is completely
188 appropriate \-\- if you can only stick this into your ear :)
189 .PP
190 Surrogate pairs were born when the Unicode Consortium finally
191 admitted that 16 bits were not big enough to hold all the world\*(Aqs
192 character repertoires. But they already made UCS\-2 16\-bit. What
193 do we do?
194 .PP
195 Back then, the range 0xD800\-0xDFFF was not allocated. Let\*(Aqs split
196 that range in half and use the first half to represent the \f(CW\*(C`upper
197 half of a character\*(C'\fR and the second half to represent the \f(CW\*(C`lower
198 half of a character\*(C'\fR. That way, you can represent 1024 * 1024 =
199 1048576 more characters. Now we can store character ranges up to
200 \&\ex{10ffff} even with 16\-bit encodings. This pair of half\-character is
201 now called a \fIsurrogate pair\fR and UTF\-16 is the name of the encoding
202 that embraces them.
203 .PP
204 Here is a formula to ensurrogate a Unicode character \ex{10000} and
205 above;
206 .PP
207 .Vb 2
208 \& $hi = ($uni \- 0x10000) / 0x400 + 0xD800;
209 \& $lo = ($uni \- 0x10000) % 0x400 + 0xDC00;
210 .Ve
211 .PP
212 And to desurrogate;
213 .PP
214 .Vb 1
215 \& $uni = 0x10000 + ($hi \- 0xD800) * 0x400 + ($lo \- 0xDC00);
216 .Ve
217 .PP
218 Note this move has made \ex{D800}\-\ex{DFFF} into a forbidden zone but
219 perl does not prohibit the use of characters within this range. To perl,
220 every one of \ex{0000_0000} up to \ex{ffff_ffff} (*) is \fIa character\fR.
221 .PP
222 .Vb 2
223 \& (*) or \ex{ffff_ffff_ffff_ffff} if your perl is compiled with 64\-bit
224 \& integer support!
225 .Ve
226 .SH "Error Checking"
227 .IX Header "Error Checking"
228 Unlike most encodings which accept various ways to handle errors,
229 Unicode encodings simply croaks.
230 .PP
231 .Vb 6
232 \& % perl \-MEncode \-e\*(Aq$_ = "\exfe\exff\exd8\exd9\exda\exdb\e0\en"\*(Aq \e
233 \& \-e\*(AqEncode::from_to($_, "utf16","shift_jis", 0); print\*(Aq
234 \& UTF\-16:Malformed LO surrogate d8d9 at /path/to/Encode.pm line 184.
235 \& % perl \-MEncode \-e\*(Aq$a = "BOM missing"\*(Aq \e
236 \& \-e\*(Aq Encode::from_to($a, "utf16", "shift_jis", 0); print\*(Aq
237 \& UTF\-16:Unrecognised BOM 424f at /path/to/Encode.pm line 184.
238 .Ve
239 .PP
240 Unlike other encodings where mappings are not one\-to\-one against
241 Unicode, UTFs are supposed to map 100% against one another. So Encode
242 is more strict on UTFs.
243 .PP
244 Consider that "division by zero" of Encode :)
245 .SH "SEE ALSO"
246 .IX Header "SEE ALSO"
247 Encode, Encode::Unicode::UTF7, <https://www.unicode.org/glossary/>,
248 <https://www.unicode.org/faq/utf_bom.html>,
249 .PP
250 RFC 2781 <http://www.ietf.org/rfc/rfc2781.txt>,
251 .PP
252 The whole Unicode standard <https://www.unicode.org/standard/standard.html>
253 .PP
254 Ch. 6 pp. 275 of \f(CW\*(C`Programming Perl (3rd Edition)\*(C'\fR
255 by Tom Christiansen, brian d foy & Larry Wall;
256 O\*(AqReilly & Associates; ISBN 978\-0\-596\-00492\-7