NAME
Encode::Unicode -- Various Unicode Transform Format
SYNOPSIS
use Encode qw/encode decode/;
$ucs2 = encode("UCS-2BE", $utf8);
$utf8 = decode("UCS-2BE", $ucs2);
ABSTRACT
This module implements all Character Encoding Schemes of Unicode that are officially documented by Unicode Consortium (except, of course, for UTF-8, which is a native format in perl).
- http://www.unicode.org/glossary/ says:
-
Character Encoding Scheme A character encoding form plus byte serialization. There are seven character encoding schemes in Unicode: UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE and UTF-32LE.
- Quick Reference
-
Decodes from ord(N) Encodes chr(N) to... octet/char BOM S.P d800-dfff ord > 0xffff \x{1abcd} == ---------------+-----------------+------------------------------ UCS-2BE 2 N N is bogus Not Available UCS-2LE 2 N N bogus Not Available UTF-16 2/4 Y Y is S.P S.P BE/LE UTF-16BE 2/4 N Y S.P S.P 0xd82a,0xdfcd UTF-16LE 2 N Y S.P S.P 0x2ad8,0xcddf UTF-32 4 Y - is bogus As is BE/LE UTF-32BE 4 N - bogus As is 0x0010abcd UTF-32LE 4 N - bogus As is 0xcdab1000 UTF-8 1-4 - - bogus >= 4 octets \xf0\x9a\af\8d ---------------+-----------------+------------------------------
Size, Endianness, and BOM
You can categorize these CES by 3 criteria; Size of each character, Endianness, and Byte Order Mark.
by Size
UCS-2 is a fixed-length encoding with each character taking 16 bits. It does not support Surrogate Pair. When surrogate pair is encountered during decode(), it fills its place with \xFFFD without CHECK or croaks if CHECK. When a character which ord value is larger than 0xFFFF, it uses 0xFFFD without CHECK or croaks if <CHECK>.
UTF-16 is almost the same as UCS-2 but it supports Surrogate Pair. When it encounters a high surrogate (0xD800-0xDBFF), it fetches the following low surrogate (0xDC00-0xDFFF), desurrogate
them to form a character. Bogus surrogates result in death. When \x{10000} or above is encountered during encode(), it ensurrogate
s them and push the surrogate pair to the output stream.
UTF-32 is a fixed-length encoding with each character taking 32 bits. Since it is 32-bit there is no need for Surrogate Pair.
by Endianness
First (and now failed) goal of Unicode was to map all character repartories into a fixed-length integer so programmers are happy. Since each character is either short or long in C, you have to put endianness of each platform when you pass data to one another.
Anything marked as BE is Big Endian (or network byte order) and LE is Little Endian (aka VAX byte order). For anything without, a character called Byte Order Mark (BOM) is prepended to the head of string.
- BOM as integer
-
16 32 bits/char ------------------------- BE 0xFeFF 0x0000FeFF LE 0xFFeF 0xFFFe0000 -------------------------
This modules handles BOM as follows.
When BE or LE is explicitly stated as the name of encoding, BOM is simply treated as one of characters (ZERO WIDTH NO-BREAK SPACE).
When BE or LE is omitted during decode(), it checks if BOM is in the beginning of the string and if found endianness is set to what BOM says. if not found, dies.
When BE or LE is omitted during encode(), it returns a BE-encoded string with BOM prepended. So when you want to encode a whole text file, make sure you encode() by whole text, not line by line or each line, not file, is prepended with BOMs.
UCS-2
is an exception. Unlike others this is an alias of UCS-2BE. UCS-2 is already registered by IANA and others that way.
The Surrogate Pair
To say the least, surrogate pair was the biggest mistake by Unicode Consortium. I don't give a darn if they admit it or not. But according to late Douglas Adams in The Hitchhiker's Guide to the Galaxy Triology, First the Universe was created and it was a bad move
. Their mistake was not this magnitude so let's forgive them.
(I don't dare make any comparison with Unicode Consortium and the Vogons here ;) Or, comparing Encode to Babel Fish is completely appropriate -- if you can only stick this into your ear :)
A surrogate pair was born when Unicode Consortium had finally admitted that 16 bit was not big enough to hold all the world's character repartorie. But they have already made UCS-2 16-bit. What do we do?
Back then 0xD800-0xDFFF was not allocated. Let's split them half and use the first half to represent upper half of a character
and the latter lower half of a character
. That way you can represent 1024 * 1024 = 1048576 more characters. Now we can store character ranges up to \x{10ffff} even with 16-bit encodings. This pair of half-character is now called a Surrogate Pair and UTF-16 is the name of encoding that embraces them.
Here is a fomula to ensurrogate a Unicode character \x{10000} and above;
$hi = ($uni - 0x10000) / 0x400 + 0xD800;
$lo = ($uni - 0x10000) % 0x400 + 0xDC00;
And to desurrogate;
$uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);
Note this move has made \x{D800}-\x{DFFF} forbidden zone but perl does not prohibit them for uses.
SEE ALSO
Encode, http://www.unicode.org/glossary/,
RFC 2781 http://rfc.net/rfc2781.html,