Map(3pm)

โœจ Unicode::Map ๐Ÿ’– (Map(3pm)) โœจ

okay bestie, this is the manual page for Unicode::Map, from the User Contributed Perl Documentation collection ๐Ÿ“š๐Ÿฆ‹ it's the Perl module that converts strings to and from UTF-16 Unicode ๐Ÿ’…

๐ŸŽ€ NAME ๐ŸŽ€

Unicode::Map - maps charsets from and to utf16 unicode ๐ŸŒธ

๐Ÿช„ SYNOPSIS ๐Ÿช„

use Unicode::Map();

$Map = new Unicode::Map("ISO-8859-1");

$utf16 = $Map -> to_unicode ("Hello world!");
  => $utf16 == "\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"

$locale = $Map -> from_unicode ($utf16);
  => $locale == "Hello world!"

so the vibe is: make a Map object for a charset, call to_unicode to go into utf16, and from_unicode to come back out ๐Ÿ’œ there's a more detailed breakdown below, don't worry ๐Ÿซถ

2do: short note about perl's Unicode perspectives. ๐Ÿ™ˆ

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

okay so this module converts strings from and to 2-byte Unicode UCS2 format โœจ every mapping goes through 2-byte UTF16 encodings, NOT through 1-byte UTF8 encoding. if you need to transform stuff into or out of those, use Unicode::String ๐Ÿงš

for historical reasons, this module lives alongside Unicode::Map8 ๐Ÿ•ฐ๏ธ please use Unicode::Map8 unless you actually need to handle two-byte character sets, like chinese GB2312 ๐Ÿ‡จ๐Ÿ‡ณ honestly though, if you stick to the basic functionality (see the documentation) you can use either module and it's the same energy ๐Ÿ’…

ngl this module will probably disappear eventually, because Unicode mapping support needs to make its way into perl's core ๐Ÿ˜ญ if you're into working on this area, please don't hesitate to contact Gisle Aas!

this module can't deal with utf8 directly ๐Ÿ™…โ€โ™€๏ธ use Unicode::String to convert utf8 to utf16 and back again.

character mapping follows the data in the binary mapfiles inside the Unicode::Map hierarchy ๐Ÿ—‚๏ธ you can also create binary mapfiles with this module, which means you can install your own custom character sets ๐ŸŽจ check out mkmapfile or the file REGISTRY in the Unicode::Map hierarchy for more.

๐Ÿ‘‘ CONVERSION METHODS ๐Ÿ‘‘

these are probably the only methods you'll ever need from this module ๐Ÿ’– their usage is compatible with Unicode::Map8, so no stress ๐Ÿฆ‹

๐ŸŒˆ new

$Map = new Unicode::Map("GB2312-80")

makes and returns a new Map object for the GB2312-80 encoding โœจ

๐ŸŒˆ from_unicode

$dest = $Map -> from_unicode ($src)

makes a string in locale charset representation from the utf16 encoded string $src ๐Ÿ“

๐ŸŒˆ to_unicode

$dest   = $Map -> to_unicode ($src)

makes a string in utf16 representation from $src ๐Ÿงš

๐ŸŒˆ to8

an alias for from_unicode. it's here for compatibility with Unicode::Map8 ๐Ÿซถ

๐ŸŒˆ to16

an alias for to_unicode. also here for compatibility with Unicode::Map8 ๐Ÿ’…

โš ๏ธ WARNINGS โš ๏ธ

you can tell Unicode::Map to throw warnings when you use something deprecated or incompatible ๐Ÿ™ˆ the constants are WARN_DEFAULT, WARN_DEPRECATION or WARN_COMPATIBILITY. the latter two can be ored together, so you can combine them ๐Ÿ”ฅ

no special warnings:

$Unicode::Map::WARNINGS = Unicode::Map::WARN_DEFAULT

warnings for deprecated usage:

$Unicode::Map::WARNINGS = Unicode::Map::WARN_DEPRECATION

warnings for incompatible usage:

$Unicode::Map::WARNINGS = Unicode::Map::WARN_COMPATIBILITY

๐Ÿ› ๏ธ MAINTAINANCE METHODS ๐Ÿ› ๏ธ

note: these methods are ONLY for maintaining Unicode::Map itself ๐Ÿ”ง using any of them will lead to programs that are incompatible with Unicode::Map8, so be careful bestie ๐Ÿ’€

๐ŸŒท alias

@list = $Map -> alias ($csid)

returns a list of alias names for the character set $csid ๐ŸŒธ

๐ŸŒท mapping

$path = $Map -> mapping ($csid)

returns the absolute path of the binary character mapping for the character set $csid, according to the REGISTRY file of Unicode::Map ๐Ÿ—บ๏ธ

๐ŸŒท id

$real_id||"" = $Map -> id ($test_id)

returns a valid character set identifier $real_id if $test_id is a valid character set name or alias name, according to the REGISTRY file of Unicode::Map โœจ (if it isn't valid, you get "" back ๐Ÿ™ˆ)

๐ŸŒท ids

@ids = $Map -> ids()

returns a list of all the character set names defined in the REGISTRY file ๐ŸŒˆ

๐ŸŒท read_text_mapping

1||0 = $Map -> read_text_mapping ($csid, $path, $style)

reads a text mapping of style $style, named $csid, from the filename $path ๐Ÿ“– you can then save the mapping to a file with the method write_binary_mapping. $style can be:

  • "unicode" โ€” a text mapping as of ftp://ftp.unicode.org/MAPPINGS/
  • "" โ€” same as "unicode"
  • "reverse" โ€” similar to unicode, but both columns are switched ๐Ÿ”„
  • "keld" โ€” a text mapping as of ftp://dkuug.dk/i18n/charmaps/

๐ŸŒท src

$path = $Map -> src ($csid)

returns the path of the textual character mapping for the character set $csid, according to the REGISTRY file of Unicode::Map ๐Ÿ“„

๐ŸŒท style

$path = $Map -> style ($csid)

returns the style of the textual character mapping for the character set $csid, according to the REGISTRY file of Unicode::Map ๐ŸŽ€

๐ŸŒท write_binary_mapping

1||0 = $Map -> write_binary_mapping ($csid, $path)

stores a mapping that you've loaded with read_text_mapping into the file $path ๐Ÿ’พ

๐Ÿชฆ DEPRECATED METHODS ๐Ÿชฆ

some functionality isn't promoted anymore ๐Ÿ™ƒ

โ˜ ๏ธ noise

deprecated! don't use this any longer.

โ˜ ๏ธ reverse_unicode

deprecated! use Unicode::String::byteswap instead.

๐Ÿงฉ BINARY MAPPINGS ๐Ÿงฉ

this is the structure of binary mapfiles ๐Ÿค“

unicode character mapping tables have sequences of sequential keys and sequential values, and this property is what makes it possible to crunch the maps easily ๐Ÿฌ n (0<n<256) sequential characters are stored as a bytecount n plus the first character code, key_start. for these subsequences, the matching value sequences get crunched together too ๐Ÿ’œ the value 0 is used to start an extended information block (which is only partially implemented, though ๐Ÿ˜ญ).

there are two ways you could make a binary mapfile ๐Ÿง  the first method would be to write a list of all the key codes first, and THEN a list of all the value codes. the second method, which is the one used here, appends the crunched value code lists right after each partial key code list. this keeps value codes a little closer to their key codes ๐Ÿซถ

heads up: the file format is still in a very liquid state ๐Ÿซ  don't rely on it staying like this, don't assume the description is bug-free, and don't assume every feature is implemented.

๐Ÿ—‚๏ธ STRUCTURE:

<main>:
       offset  structure     value

       0x00    word          0x27b8   (magic)
       0x02    @(<extended> || <submapping>)

the mapfile ends with extended mode <end> in the main stream ๐Ÿ

<submapping>:
       0x00    byte != 0     charsize1 (bits)
       0x01    byte          n1 number of chars for one entry
       0x02    byte          charsize2 (bits)
       0x03    byte          n2 number of chars for one entry
       0x04    @(<extended> || <key_seq> || <key_val_seq)

       bs1=int((charsize1+7)/8), bs2=int((charsize2+7)/8)

one submapping ends when a <mapend> entry shows up ๐Ÿ”š

<key_val_seq>:
       0x00    size=0|1|2|4  n, number of sequential characters
       size    bs1           key1
       +bs1    bs2           value1
       +bs2    bs1           key2
       +bs1    bs2           value2
       ...

key_val_seq ends if either the file ends (that's n = infinite mode) or n pairs have been read ๐ŸŒŠ

<key_seq>:
       0x00    byte          n, number of sequential characters
       0x01    bs1           key_start, first character of sequence
       1+bs1   @(<extended> || <val_seq>)

a key sequence starts with a byte count telling you how long the sequence is ๐Ÿ“ it's followed by the key start code, and after that comes a list of value sequences. that list ends once sum(m) equals n โœจ

<val_seq>:
       0x00    byte          m, number of sequential characters
       0x01    bs2           val_start, first character of sequence
<extended>:
       0x00    byte          0
       0x01    byte          ftype
       0x02    byte          fsize, size of following structure
       0x03    fsize bytes   something

for future extensions or private use, you can slot in 1..255 byte long streams here ๐Ÿง ftype can be 30..255, and values 0..29 are reserved ๐Ÿšซ the modi aren't fully defined yet and could change, and they'll be explained later on ๐Ÿ”ฎ

๐Ÿšง TO BE DONE ๐Ÿšง

  • โœจ something clever for when a character has no translation
  • ๐Ÿ” direct charset to charset mapping
  • ๐ŸŽ๏ธ better performance
  • ๐Ÿ“œ support for mappings according to RFC 1345

๐Ÿ”— SEE ALSO ๐Ÿ”—

๐Ÿซถ AUTHOR ๐Ÿซถ

Martin Schwartz <martin@nacho.de> ๐Ÿ’œ


and that's the whole tea, bestie โ˜•๐Ÿ’– this was the Map(3pm) page from perl v5.40.0, dated 2024-10-20, and it's the manual for Unicode::Map ๐Ÿงšโœจ go be iconic ๐Ÿ‘‘๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from