Unicode::String - String of Unicode characters (UCS2/UTF16)
use Unicode::String qw(utf8 latin1 utf16); $u = utf8("The Unicode Standard is a fixed-width, uniform "); $u .= utf8("encoding scheme for written characters and text");
# convert to various external formats print $u->ucs4; # 4 byte characters print $u->utf16; # 2 byte characters + surrogates print $u->utf8; # 1-4 byte characters print $u->utf7; # 7-bit clean format print $u->latin1; # lossy print $u->hex; # a hexadecimal string
# all these can be used to set string value or as constructor $u->latin1("Å være eller å ikke være"); $u = utf16("\0Å\0 \0v\0æ\0r\0e");
# string operations $u2 = $u->copy; $u->append($u2); $u->repeat(2); $u->chop;
$u->length; $u->index($other); $u->index($other, $pos);
$u->substr($offset); $u->substr($offset, $length); $u->substr($offset, $length, $substitute);
# overloading $u .= "more"; $u = $u x 100; print "$u\n";
# string <--> array of numbers @array = $u->unpack; $u->pack(@array);
# misc $u->ord; $u = uchr($num);
A Unicode::String object represents a sequence of Unicode characters. The Unicode Standard is a fixed-width, uniform encoding scheme for written characters and text. This encoding treats alphabetic characters, ideographic characters, and symbols identically, which means that they can be used in any mixture and with equal facility. Unicode is modeled on the ASCII character set, but uses a 16-bit encoding to support full multilingual text.
Internally a Unicode::String object is a string of 2 byte values in network byte order (big-endian). The class provide various methods to convert from and to various external formats, and all string manipulations are made on strings in this the internal 16-bit format.
The functions utf16(), utf8(), utf7(), ucs2(), ucs4(), latin1(), uchr() can be imported from the Unicode::String module and will work as constructors initializing strings of the corresponding encoding. The ucs2() and utf16() are really aliases for the same function.
The Unicode::String objects overload various operators, so they will normally work like plain 8-bit strings in Perl. This includes conversions to strings, numbers and booleans as well as assignment, concatenation and repetition.
The following methods are available:
This is the customary object constructor. Without argument, it creates an empty Unicode::String object. If an $initial_value argument is given, it is decoded according to the specified stringify_as() encoding and used to initialize the newly created object.
Normally you create Unicode::String objects by importing some of the encoding methods below as functions into your namespace and calling them with an appropriate encoded argument.
The UCS-4 encoding use 32 bits per character. The main benefit of this encoding is that you don't have to deal with surrogate pairs. Encoded as a Perl string we use 4-bytes in network byte order for each character.
The ucs4() method always return the old value of $us and if given an argument decodes the UCS-4 string and set this as the new value of $us. The characters in $newval must be in the range 0x0 .. 0x10FFFF. Characters outside this range is ignored.
The ucs2() and utf16() are really just different names for the same method. The UCS-2 encoding use 16 bits per character. The UTF-16 encoding is identical to UCS-2, but includes the use of surrogate pairs. Surrogates make it possible to encode characters in the range 0x010000 .. 0x10FFFF with the use of two consecutive 16-bit chars. Encoded as a Perl string we use 2-bytes in network byte order for each character (or surrogate code).
The ucs2() method always return the old value of $us and if given an argument set this as the new value of $us.
The UTF-8 encoding use 8-bit for the encoding of characters in the range 0x0 .. 0x7F, 16-bit for the encoding of characters in the range 0x80 .. 0x7FF, 24-bit for the encoding of characters in the range 0x800 .. 0xFFFF and 32-bit for characters in the range 0x01000 .. 0x10FFFF. Americans like this encoding, because plain US-ASCII characters are still US-ASCII. Another benefit is that the character '\0' only occurs as the encoding of 0x0, thus the normal NUL-terminated strings (popular in the C programming language) can still be used.
The utf8() method always return the old value of $us encoded using UTF-8 and if given an argument decodes the UTF-8 string and set this as the new value of $us.
The UTF-7 encoding only use plain US-ASCII characters for the encoding. This makes it safe for transport through 8-bit stripping protocols. Characters outside the US-ASCII range are base64-encoded and '+' is used as an escape character. The UTF-7 encoding is described in RFC1642.
The utf7() method always return the old value of $us encoded using UTF-7 and if given an argument decodes the UTF-7 string and set this as the new value of $us.
If the (global) variable $Unicode::String::UTF7_OPTIONAL_DIRECT_CHARS is TRUE, then a wider range of characters are encoded as themselves. It is even TRUE by default. The characters affected by this are:
! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
Returns a new Unicode::String where the content of $us is repeated $count times. This operation is also overloaded as:
$us x $count
Concatenates the string $us and the string $other_string. If $other_string is not an Unicode::String object, then it is first passed to the Unicode::String->new constructor function. This operation is also overloaded as:
$us . $other_string
Appends the string $other_string to the value of $us. If $other_string is not an Unicode::String object, then it is first passed to the Unicode::String->new constructor function. This operation is also overloaded as:
$us .= $other_string
This method will swap the bytes in the internal representation of the Unicode::String object.
Unicode reserve the character U+FEFF character as a byte order mark. This works because the swapped character, U+FFFE, is reserved to not be valid. For strings that have the byte order mark as the first character, we can guaranty to get the byte order right with the following code:
$ustr->byteswap if $ustr->ord == 0xFFFE;
The following utility functions are provided. They will be exported on request.
Unicode::CharName, Unicode::Map8, http://www.unicode.org/
Copyright 1997-2000 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.