Upgrade to Encode 2.00.
[p5sagit/p5-mst-13.2.git] / ext / Encode / Unicode / Unicode.pm
CommitLineData
f2a2953c 1package Encode::Unicode;
2
df1df145 3use strict;
f2a2953c 4use warnings;
a0d8a30e 5no warnings 'redefine';
f2a2953c 6
7237418a 7our $VERSION = do { my @r = (q$Revision: 2.0 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
f2a2953c 8
85982a32 9use XSLoader;
10XSLoader::load(__PACKAGE__,$VERSION);
df1df145 11
f2a2953c 12#
13# Object Generator 8 transcoders all at once!
14#
df1df145 15
f2a2953c 16require Encode;
10c5ecbb 17
a0d8a30e 18our %BOM_Unknown = map {$_ => 1} qw(UTF-16 UTF-32);
19
f2a2953c 20for my $name (qw(UTF-16 UTF-16BE UTF-16LE
21 UTF-32 UTF-32BE UTF-32LE
22 UCS-2BE UCS-2LE))
df1df145 23{
f2a2953c 24 my ($size, $endian, $ucs2, $mask);
25 $name =~ /^(\w+)-(\d+)(\w*)$/o;
26 if ($ucs2 = ($1 eq 'UCS')){
27 $size = 2;
28 }else{
29 $size = $2/8;
df1df145 30 }
f2a2953c 31 $endian = ($3 eq 'BE') ? 'n' : ($3 eq 'LE') ? 'v' : '' ;
32 $size == 4 and $endian = uc($endian);
33
34 $Encode::Encoding{$name} =
35 bless {
36 Name => $name,
37 size => $size,
38 endian => $endian,
39 ucs2 => $ucs2,
c731e18e 40 } => __PACKAGE__;
df1df145 41}
42
10c5ecbb 43use base qw(Encode::Encoding);
f2a2953c 44
a0d8a30e 45sub renew {
46 my $self = shift;
47 $BOM_Unknown{$self->name} or return $self;
48 my $clone = bless { %$self } => ref($self);
49 $clone->{clone} = 1; # so the caller knows it is renewed.
50 return $clone;
f2a2953c 51}
52
a0d8a30e 53# There used to be a perl implemntation of (en|de)code but with
54# XS version is ripe, perl version is zapped for optimal speed
f2a2953c 55
a0d8a30e 56*decode = \&decode_xs;
57*encode = \&encode_xs;
df1df145 58
591;
60__END__
67d7b5ef 61
62=head1 NAME
63
0ab8f81e 64Encode::Unicode -- Various Unicode Transformation Formats
67d7b5ef 65
66=cut
f2a2953c 67
68=head1 SYNOPSIS
69
fcb875d4 70 use Encode qw/encode decode/;
f2a2953c 71 $ucs2 = encode("UCS-2BE", $utf8);
72 $utf8 = decode("UCS-2BE", $ucs2);
73
74=head1 ABSTRACT
75
76This module implements all Character Encoding Schemes of Unicode that
77are officially documented by Unicode Consortium (except, of course,
78for UTF-8, which is a native format in perl).
79
80=over 4
81
82=item L<http://www.unicode.org/glossary/> says:
83
84I<Character Encoding Scheme> A character encoding form plus byte
1485817e 85serialization. There are Seven character encoding schemes in Unicode:
11067275 86UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32 (UCS-4), UTF-32BE (UCS-4BE) and
1485817e 87UTF-32LE (UCS-4LE), and UTF-7.
88
89Since UTF-7 is a 7-bit (re)encoded version of UTF-16BE, It is not part of
90Unicode's Character Encoding Scheme. It is separately implemented in
91Encode::Unicode::UTF7. For details see L<Encode::Unicode::UTF7>.
f2a2953c 92
93=item Quick Reference
94
95 Decodes from ord(N) Encodes chr(N) to...
96 octet/char BOM S.P d800-dfff ord > 0xffff \x{1abcd} ==
97 ---------------+-----------------+------------------------------
98 UCS-2BE 2 N N is bogus Not Available
99 UCS-2LE 2 N N bogus Not Available
100 UTF-16 2/4 Y Y is S.P S.P BE/LE
101 UTF-16BE 2/4 N Y S.P S.P 0xd82a,0xdfcd
102 UTF-16LE 2 N Y S.P S.P 0x2ad8,0xcddf
103 UTF-32 4 Y - is bogus As is BE/LE
0ab8f81e 104 UTF-32BE 4 N - bogus As is 0x0001abcd
105 UTF-32LE 4 N - bogus As is 0xcdab0100
f2a2953c 106 UTF-8 1-4 - - bogus >= 4 octets \xf0\x9a\af\8d
107 ---------------+-----------------+------------------------------
108
109=back
110
111=head1 Size, Endianness, and BOM
112
0ab8f81e 113You can categorize these CES by 3 criteria: size of each character,
114endianness, and Byte Order Mark.
f2a2953c 115
0ab8f81e 116=head2 by size
f2a2953c 117
118UCS-2 is a fixed-length encoding with each character taking 16 bits.
0ab8f81e 119It B<does not> support I<surrogate pairs>. When a surrogate pair
120is encountered during decode(), its place is filled with \x{FFFD}
121if I<CHECK> is 0, or the routine croaks if I<CHECK> is 1. When a
122character whose ord value is larger than 0xFFFF is encountered,
123its place is filled with \x{FFFD} if I<CHECK> is 0, or the routine
124croaks if I<CHECK> is 1.
125
126UTF-16 is almost the same as UCS-2 but it supports I<surrogate pairs>.
f2a2953c 127When it encounters a high surrogate (0xD800-0xDBFF), it fetches the
0ab8f81e 128following low surrogate (0xDC00-0xDFFF) and C<desurrogate>s them to
129form a character. Bogus surrogates result in death. When \x{10000}
130or above is encountered during encode(), it C<ensurrogate>s them and
131pushes the surrogate pair to the output stream.
f2a2953c 132
11067275 133UTF-32 (UCS-4) is a fixed-length encoding with each character taking 32 bits.
0ab8f81e 134Since it is 32-bit, there is no need for I<surrogate pairs>.
f2a2953c 135
0ab8f81e 136=head2 by endianness
f2a2953c 137
0ab8f81e 138The first (and now failed) goal of Unicode was to map all character
139repertoires into a fixed-length integer so that programmers are happy.
140Since each character is either a I<short> or I<long> in C, you have to
141pay attention to the endianness of each platform when you pass data
142to one another.
f2a2953c 143
144Anything marked as BE is Big Endian (or network byte order) and LE is
0ab8f81e 145Little Endian (aka VAX byte order). For anything not marked either
146BE or LE, a character called Byte Order Mark (BOM) indicating the
147endianness is prepended to the string.
f2a2953c 148
7237418a 149CAVEAT: Though BOM in utf8 (\xEF\xBB\xBF) is valid, it is meaningless
150and as of this writing Encode suite just leave it as is (\x{FeFF}).
151
f2a2953c 152=over 4
153
fcb875d4 154=item BOM as integer when fetched in network byte order
f2a2953c 155
fcb875d4 156 16 32 bits/char
157 -------------------------
158 BE 0xFeFF 0x0000FeFF
159 LE 0xFFeF 0xFFFe0000
160 -------------------------
f2a2953c 161
162=back
151b5d36 163
0ab8f81e 164This modules handles the BOM as follows.
f2a2953c 165
166=over 4
167
168=item *
169
170When BE or LE is explicitly stated as the name of encoding, BOM is
0ab8f81e 171simply treated as a normal character (ZERO WIDTH NO-BREAK SPACE).
f2a2953c 172
173=item *
174
0ab8f81e 175When BE or LE is omitted during decode(), it checks if BOM is at the
176beginning of the string; if one is found, the endianness is set to
177what the BOM says. If no BOM is found, the routine dies.
f2a2953c 178
179=item *
180
181When BE or LE is omitted during encode(), it returns a BE-encoded
182string with BOM prepended. So when you want to encode a whole text
0ab8f81e 183file, make sure you encode() the whole text at once, not line by line
184or each line, not file, will have a BOM prepended.
f2a2953c 185
186=item *
187
0ab8f81e 188C<UCS-2> is an exception. Unlike others, this is an alias of UCS-2BE.
f2a2953c 189UCS-2 is already registered by IANA and others that way.
190
fdd579e2 191=back
f2a2953c 192
fcb875d4 193=head1 Surrogate Pairs
f2a2953c 194
fcb875d4 195To say the least, surrogate pairs were the biggest mistake of the
196Unicode Consortium. But according to the late Douglas Adams in I<The
197Hitchhiker's Guide to the Galaxy> Trilogy, C<In the beginning the
198Universe was created. This has made a lot of people very angry and
199been widely regarded as a bad move>. Their mistake was not of this
200magnitude so let's forgive them.
f2a2953c 201
202(I don't dare make any comparison with Unicode Consortium and the
c731e18e 203Vogons here ;) Or, comparing Encode to Babel Fish is completely
204appropriate -- if you can only stick this into your ear :)
f2a2953c 205
0ab8f81e 206Surrogate pairs were born when the Unicode Consortium finally
fcb875d4 207admitted that 16 bits were not big enough to hold all the world's
0ab8f81e 208character repertoires. But they already made UCS-2 16-bit. What
f2a2953c 209do we do?
210
0ab8f81e 211Back then, the range 0xD800-0xDFFF was not allocated. Let's split
212that range in half and use the first half to represent the C<upper
213half of a character> and the second half to represent the C<lower
214half of a character>. That way, you can represent 1024 * 1024 =
2151048576 more characters. Now we can store character ranges up to
216\x{10ffff} even with 16-bit encodings. This pair of half-character is
217now called a I<surrogate pair> and UTF-16 is the name of the encoding
218that embraces them.
f2a2953c 219
448e90bb 220Here is a formula to ensurrogate a Unicode character \x{10000} and
f2a2953c 221above;
222
223 $hi = ($uni - 0x10000) / 0x400 + 0xD800;
224 $lo = ($uni - 0x10000) % 0x400 + 0xDC00;
225
226And to desurrogate;
227
228 $uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);
229
fcb875d4 230Note this move has made \x{D800}-\x{DFFF} into a forbidden zone but
231perl does not prohibit the use of characters within this range. To perl,
232every one of \x{0000_0000} up to \x{ffff_ffff} (*) is I<a character>.
233
234 (*) or \x{ffff_ffff_ffff_ffff} if your perl is compiled with 64-bit
0ab8f81e 235 integer support!
f2a2953c 236
237=head1 SEE ALSO
238
1485817e 239L<Encode>, L<Encode::Unicode::UTF7>, L<http://www.unicode.org/glossary/>,
11067275 240L<http://www.unicode.org/unicode/faq/utf_bom.html>,
f2a2953c 241
fdd579e2 242RFC 2781 L<http://rfc.net/rfc2781.html>,
243
11067275 244The whole Unicode standard L<http://www.unicode.org/unicode/uni2book/u2.html>
fdd579e2 245
fcb875d4 246Ch. 15, pp. 403 of C<Programming Perl (3rd Edition)>
247by Larry Wall, Tom Christiansen, Jon Orwant;
248O'Reilly & Associates; ISBN 0-596-00027-8
249
fdd579e2 250=cut