Encode
view release on metacpan or search on metacpan
A UCM file looks like this.
#
# Comments
#
<code_set_name> "US-ascii" # Required
<code_set_alias> "ascii" # Optional
<mb_cur_min> 1 # Required; usually 1
<mb_cur_max> 1 # Max. # of bytes/char
<subchar> \x3F # Substitution char
#
CHARMAP
<U0000> \x00 |0 # <control>
<U0001> \x01 |0 # <control>
<U0002> \x02 |0 # <control>
....
<U007C> \x7C |0 # VERTICAL LINE
<U007D> \x7D |0 # RIGHT CURLY BRACKET
<U007E> \x7E |0 # TILDE
<U007F> \x7F |0 # <control>
END CHARMAP
=over 4
=item *
Anything that follows C<#> is treated as a comment.
=item *
The header section continues until a line containing the word
CHARMAP. This section has a form of I<E<lt>keywordE<gt> value>, one
pair per line. Strings used as values must be quoted. Barewords are
treated as numbers. I<\xXX> represents a byte.
Most of the keywords are self-explanatory. I<subchar> means
substitution character, not subcharacter. When you decode a Unicode
sequence to this encoding but no matching character is found, the byte
sequence defined here will be used. For most cases, the value here is
\x3F; in ASCII, this is a question mark.
=item *
CHARMAP starts the character map section. Each line has a form as
follows:
<UXXXX> \xXX.. |0 # comment
^ ^ ^
| | +- Fallback flag
| +-------- Encoded byte sequence
+-------------- Unicode Character ID in hex
The format is roughly the same as a header section except for the
fallback flag: | followed by 0..3. The meaning of the possible
values is as follows:
=over 4
=item |0
Round trip safe. A character decoded to Unicode encodes back to the
same byte sequence. Most characters have this flag.
=item |1
Fallback for unicode -> encoding. When seen, enc2xs adds this
character for the encode map only.
=item |2
Skip sub-char mapping should there be no code point.
=item |3
Fallback for encoding -> unicode. When seen, enc2xs adds this
character for the decode map only.
=back
=item *
And finally, END OF CHARMAP ends the section.
=back
When you are manually creating a UCM file, you should copy ascii.ucm
or an existing encoding which is close to yours, rather than write
your own from scratch.
When you do so, make sure you leave at least B<U0000> to B<U0020> as
is, unless your environment is EBCDIC.
B<CAVEAT>: not all features in UCM are implemented. For example,
icu:state is not used. Because of that, you need to write a perl
module if you want to support algorithmical encodings, notably
the ISO-2022 series. Such modules include L<Encode::JP::2022_JP>,
L<Encode::KR::2022_KR>, and L<Encode::TW::HZ>.
=head2 Coping with duplicate mappings
When you create a map, you SHOULD make your mappings round-trip safe.
That is, C<encode('your-encoding', decode('your-encoding', $data)) eq
$data> stands for all characters that are marked as C<|0>. Here is
how to make sure:
=over 4
=item *
Sort your map in Unicode order.
=item *
When you have a duplicate entry, mark either one with '|1' or '|3'.
=item *
And make sure the '|1' or '|3' entry FOLLOWS the '|0' entry.
=back
( run in 0.772 second using v1.01-cache-2.11-cpan-3c2a17b8caa )