9 use Storable qw(dclone);
13 our @ISA = qw(Exporter);
15 our @EXPORT_OK = qw(charinfo
17 charblocks charscripts
19 general_categories bidi_types
28 Unicode::UCD - Unicode character database
32 use Unicode::UCD 'charinfo';
33 my $charinfo = charinfo($codepoint);
35 use Unicode::UCD 'casefold';
36 my $casefold = casefold(0xFB00);
38 use Unicode::UCD 'casespec';
39 my $casespec = casespec(0xFB00);
41 use Unicode::UCD 'charblock';
42 my $charblock = charblock($codepoint);
44 use Unicode::UCD 'charscript';
45 my $charscript = charscript($codepoint);
47 use Unicode::UCD 'charblocks';
48 my $charblocks = charblocks();
50 use Unicode::UCD 'charscripts';
51 my $charscripts = charscripts();
53 use Unicode::UCD qw(charscript charinrange);
54 my $range = charscript($script);
55 print "looks like $script\n" if charinrange($range, $codepoint);
57 use Unicode::UCD qw(general_categories bidi_types);
58 my $categories = general_categories();
59 my $types = bidi_types();
61 use Unicode::UCD 'compexcl';
62 my $compexcl = compexcl($codepoint);
64 use Unicode::UCD 'namedseq';
65 my $namedseq = namedseq($named_sequence_name);
67 my $unicode_version = Unicode::UCD::UnicodeVersion();
71 The Unicode::UCD module offers a series of functions that
72 provide a simple interface to the Unicode
75 =head2 code point argument
77 Some of the functions are called with a I<code point argument>, which is either
78 a decimal or a hexadecimal scalar designating a Unicode code point, or C<U+>
79 followed by hexadecimals designating a Unicode code point. In other words, if
80 you want a code point to be interpreted as a hexadecimal number, you must
81 prefix it with either C<0x> or C<U+>, because a string like e.g. C<123> will be
82 interpreted as a decimal code point. Also note that Unicode is B<not> limited
83 to 16 bits (the number of Unicode code points is open-ended, in theory
84 unlimited): you may have more than 4 hexdigits.
97 my ($rfh, @path) = @_;
99 unless (defined $$rfh) {
102 $f = File::Spec->catfile($d, "unicore", @path);
103 last if open($$rfh, $f);
106 croak __PACKAGE__, ": failed to find ",
107 File::Spec->catfile(@path), " in @INC"
115 use Unicode::UCD 'charinfo';
117 my $charinfo = charinfo(0x41);
119 This returns information about the input L</code point argument>
120 as a reference to a hash of fields as defined by the Unicode
121 standard. If the L</code point argument> is not assigned in the standard
122 (i.e., has the general category C<Cn> meaning C<Unassigned>)
123 or is a non-character (meaning it is guaranteed to never be assigned in
125 B<undef> is returned.
127 Fields that aren't applicable to the particular code point argument exist in the
128 returned hash, and are empty.
130 The keys in the hash with the meanings of their values are:
136 the input L</code point argument> expressed in hexadecimal, with leading zeros
137 added if necessary to make it contain at least four hexdigits
141 name of I<code>, all IN UPPER CASE.
142 Some control-type code points do not have names.
143 This field will be empty for C<Surrogate> and C<Private Use> code points,
144 and for the others without a name,
145 it will contain a description enclosed in angle brackets, like
146 C<E<lt>controlE<gt>>.
151 The short name of the general category of I<code>.
152 This will match one of the keys in the hash returned by L</general_categories()>.
156 the combining class number for I<code> used in the Canonical Ordering Algorithm.
157 For Unicode 5.1, this is described in Section 3.11 C<Canonical Ordering Behavior>
159 L<http://www.unicode.org/versions/Unicode5.1.0/>
163 bidirectional type of I<code>.
164 This will match one of the keys in the hash returned by L</bidi_types()>.
166 =item B<decomposition>
168 is empty if I<code> has no decomposition; or is one or more codes
169 (separated by spaces) that taken in order represent a decomposition for
170 I<code>. Each has at least four hexdigits.
171 The codes may be preceded by a word enclosed in angle brackets then a space,
172 like C<E<lt>compatE<gt> >, giving the type of decomposition
176 if I<code> is a decimal digit this is its integer numeric value
180 if I<code> represents a whole number, this is its integer numeric value
184 if I<code> represents a whole or rational number, this is its numeric value.
185 Rational values are expressed as a string like C<1/4>.
189 C<Y> or C<N> designating if I<code> is mirrored in bidirectional text
193 name of I<code> in the Unicode 1.0 standard if one
194 existed for this code point and is different from the current name
198 ISO 10646 comment field.
199 It appears in parentheses in the ISO 10646 names list,
200 or contains an asterisk to indicate there is
201 a note for this code point in Annex P of that standard.
205 is empty if there is no single code point uppercase mapping for I<code>;
206 otherwise it is that mapping expressed as at least four hexdigits.
207 (L</casespec()> should be used in addition to B<charinfo()>
208 for case mappings when the calling program can cope with multiple code point
213 is empty if there is no single code point lowercase mapping for I<code>;
214 otherwise it is that mapping expressed as at least four hexdigits.
215 (L</casespec()> should be used in addition to B<charinfo()>
216 for case mappings when the calling program can cope with multiple code point
221 is empty if there is no single code point titlecase mapping for I<code>;
222 otherwise it is that mapping expressed as at least four hexdigits.
223 (L</casespec()> should be used in addition to B<charinfo()>
224 for case mappings when the calling program can cope with multiple code point
229 block I<code> belongs to (used in \p{In...}).
230 See L</Blocks versus Scripts>.
235 script I<code> belongs to.
236 See L</Blocks versus Scripts>.
240 Note that you cannot do (de)composition and casing based solely on the
241 I<decomposition>, I<combining>, I<lower>, I<upper>, and I<title> fields;
242 you will need also the L</compexcl()>, and L</casespec()> functions.
246 # NB: This function is nearly duplicated in charnames.pm
250 if ($arg =~ /^[1-9]\d*$/) {
252 } elsif ($arg =~ /^(?:[Uu]\+|0[xX])?([[:xdigit:]]+)$/) {
259 # Lingua::KO::Hangul::Util not part of the standard distribution
260 # but it will be used if available.
262 eval { require Lingua::KO::Hangul::Util };
263 my $hasHangulUtil = ! $@;
264 if ($hasHangulUtil) {
265 Lingua::KO::Hangul::Util->import();
268 sub hangul_decomp { # internal: called from charinfo
269 if ($hasHangulUtil) {
270 my @tmp = decomposeHangul(shift);
271 return sprintf("%04X %04X", @tmp) if @tmp == 2;
272 return sprintf("%04X %04X %04X", @tmp) if @tmp == 3;
277 sub hangul_charname { # internal: called from charinfo
278 return sprintf("HANGUL SYLLABLE-%04X", shift);
281 sub han_charname { # internal: called from charinfo
282 return sprintf("CJK UNIFIED IDEOGRAPH-%04X", shift);
285 # Overwritten by data in file
287 'CJK Ideograph Extension A' => [ 0x3400, 0x4DB5 ],
288 'CJK Ideograph' => [ 0x4E00, 0x9FA5 ],
289 'CJK Ideograph Extension B' => [ 0x20000, 0x2A6D6 ],
292 get_charinfo_ranges();
294 sub get_charinfo_ranges {
295 my @blocks = keys %first_last;
298 openunicode( \$fh, 'UnicodeData.txt' );
300 while( my $line = <$fh> ){
301 next unless $line =~ /(?:First|Last)/;
302 if( grep{ $line =~ /[^;]+;<$_\s*,\s*(?:First|Last)>/ }@blocks ){
303 my ($number,$block,$type);
304 ($number,$block) = split /;/, $line;
306 ($block,$type) = split /, /, $block;
307 my $index = $type eq 'First' ? 0 : 1;
308 $first_last{ $block }->[$index] = hex $number;
314 my @CharinfoRanges = (
316 # [ first, last, coderef to name, coderef to decompose ],
317 # CJK Ideographs Extension A
318 [ @{ $first_last{'CJK Ideograph Extension A'} }, \&han_charname, undef ],
320 [ @{ $first_last{'CJK Ideograph'} }, \&han_charname, undef ],
322 [ 0xAC00, 0xD7A3, $hasHangulUtil ? \&getHangulName : \&hangul_charname, \&hangul_decomp ],
323 # Non-Private Use High Surrogates
324 [ 0xD800, 0xDB7F, undef, undef ],
325 # Private Use High Surrogates
326 [ 0xDB80, 0xDBFF, undef, undef ],
328 [ 0xDC00, 0xDFFF, undef, undef ],
329 # The Private Use Area
330 [ 0xE000, 0xF8FF, undef, undef ],
331 # CJK Ideographs Extension B
332 [ @{ $first_last{'CJK Ideograph Extension B'} }, \&han_charname, undef ],
333 # Plane 15 Private Use Area
334 [ 0xF0000, 0xFFFFD, undef, undef ],
335 # Plane 16 Private Use Area
336 [ 0x100000, 0x10FFFD, undef, undef ],
341 my $code = _getcode($arg);
342 croak __PACKAGE__, "::charinfo: unknown code '$arg'"
343 unless defined $code;
344 my $hexk = sprintf("%06X", $code);
345 my($rcode,$rname,$rdec);
346 foreach my $range (@CharinfoRanges){
347 if ($range->[0] <= $code && $code <= $range->[1]) {
350 $rcode = sprintf("%04X", hex($rcode));
351 $rname = $range->[2] ? $range->[2]->($code) : '';
352 $rdec = $range->[3] ? $range->[3]->($code) : '';
353 $hexk = sprintf("%06X", $range->[0]); # replace by the first
357 openunicode(\$UNICODEFH, "UnicodeData.txt");
358 if (defined $UNICODEFH) {
359 use Search::Dict 1.02;
360 if (look($UNICODEFH, "$hexk;", { xfrm => sub { $_[0] =~ /^([^;]+);(.+)/; sprintf "%06X;$2", hex($1) } } ) >= 0) {
361 my $line = <$UNICODEFH>;
362 return unless defined $line;
367 combining bidi decomposition
368 decimal digit numeric
369 mirrored unicode10 comment
371 )} = split(/;/, $line, -1);
373 $hexk = sprintf("%04X", hex($hexk));
374 if ($prop{code} eq $hexk) {
375 $prop{block} = charblock($code);
376 $prop{script} = charscript($code);
378 $prop{code} = $rcode;
379 $prop{name} = $rname;
380 $prop{decomposition} = $rdec;
389 sub _search { # Binary search in a [[lo,hi,prop],[...],...] table.
390 my ($table, $lo, $hi, $code) = @_;
394 my $mid = int(($lo+$hi) / 2);
396 if ($table->[$mid]->[0] < $code) {
397 if ($table->[$mid]->[1] >= $code) {
398 return $table->[$mid]->[2];
400 _search($table, $mid + 1, $hi, $code);
402 } elsif ($table->[$mid]->[0] > $code) {
403 _search($table, $lo, $mid - 1, $code);
405 return $table->[$mid]->[2];
410 my ($range, $arg) = @_;
411 my $code = _getcode($arg);
412 croak __PACKAGE__, "::charinrange: unknown code '$arg'"
413 unless defined $code;
414 _search($range, 0, $#$range, $code);
417 =head2 B<charblock()>
419 use Unicode::UCD 'charblock';
421 my $charblock = charblock(0x41);
422 my $charblock = charblock(1234);
423 my $charblock = charblock(0x263a);
424 my $charblock = charblock("U+263a");
426 my $range = charblock('Armenian');
428 With a L</code point argument> charblock() returns the I<block> the code point
429 belongs to, e.g. C<Basic Latin>.
430 If the code point is unassigned, this returns the block it would belong to if
431 it were assigned (which it may in future versions of the Unicode Standard).
433 See also L</Blocks versus Scripts>.
435 If supplied with an argument that can't be a code point, charblock() tries
436 to do the opposite and interpret the argument as a code point block. The
437 return value is a I<range>: an anonymous list of lists that contain
438 I<start-of-range>, I<end-of-range> code point pairs. You can test whether
439 a code point is in a range using the L</charinrange()> function. If the
440 argument is not a known code point block, B<undef> is returned.
449 if (openunicode(\$BLOCKSFH, "Blocks.txt")) {
451 while (<$BLOCKSFH>) {
452 if (/^([0-9A-F]+)\.\.([0-9A-F]+);\s+(.+)/) {
453 my ($lo, $hi) = (hex($1), hex($2));
454 my $subrange = [ $lo, $hi, $3 ];
455 push @BLOCKS, $subrange;
456 push @{$BLOCKS{$3}}, $subrange;
467 _charblocks() unless @BLOCKS;
469 my $code = _getcode($arg);
472 _search(\@BLOCKS, 0, $#BLOCKS, $code);
474 if (exists $BLOCKS{$arg}) {
475 return dclone $BLOCKS{$arg};
482 =head2 B<charscript()>
484 use Unicode::UCD 'charscript';
486 my $charscript = charscript(0x41);
487 my $charscript = charscript(1234);
488 my $charscript = charscript("U+263a");
490 my $range = charscript('Thai');
492 With a L</code point argument> charscript() returns the I<script> the
493 code point belongs to, e.g. C<Latin>, C<Greek>, C<Han>.
494 If the code point is unassigned, it returns B<undef>
496 If supplied with an argument that can't be a code point, charscript() tries
497 to do the opposite and interpret the argument as a code point script. The
498 return value is a I<range>: an anonymous list of lists that contain
499 I<start-of-range>, I<end-of-range> code point pairs. You can test whether a
500 code point is in a range using the L</charinrange()> function. If the
501 argument is not a known code point script, B<undef> is returned.
503 See also L</Blocks versus Scripts>.
512 if (openunicode(\$SCRIPTSFH, "Scripts.txt")) {
514 while (<$SCRIPTSFH>) {
515 if (/^([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+)/) {
516 my ($lo, $hi) = (hex($1), $2 ? hex($2) : hex($1));
518 $script =~ s/\b(\w)/uc($1)/ge;
519 my $subrange = [ $lo, $hi, $script ];
520 push @SCRIPTS, $subrange;
521 push @{$SCRIPTS{$script}}, $subrange;
525 @SCRIPTS = sort { $a->[0] <=> $b->[0] } @SCRIPTS;
533 _charscripts() unless @SCRIPTS;
535 my $code = _getcode($arg);
538 _search(\@SCRIPTS, 0, $#SCRIPTS, $code);
540 if (exists $SCRIPTS{$arg}) {
541 return dclone $SCRIPTS{$arg};
548 =head2 B<charblocks()>
550 use Unicode::UCD 'charblocks';
552 my $charblocks = charblocks();
554 charblocks() returns a reference to a hash with the known block names
555 as the keys, and the code point ranges (see L</charblock()>) as the values.
557 See also L</Blocks versus Scripts>.
562 _charblocks() unless %BLOCKS;
563 return dclone \%BLOCKS;
566 =head2 B<charscripts()>
568 use Unicode::UCD 'charscripts';
570 my $charscripts = charscripts();
572 charscripts() returns a reference to a hash with the known script
573 names as the keys, and the code point ranges (see L</charscript()>) as
576 See also L</Blocks versus Scripts>.
581 _charscripts() unless %SCRIPTS;
582 return dclone \%SCRIPTS;
585 =head2 B<charinrange()>
587 In addition to using the C<\p{In...}> and C<\P{In...}> constructs, you
588 can also test whether a code point is in the I<range> as returned by
589 L</charblock()> and L</charscript()> or as the values of the hash returned
590 by L</charblocks()> and L</charscripts()> by using charinrange():
592 use Unicode::UCD qw(charscript charinrange);
594 $range = charscript('Hiragana');
595 print "looks like hiragana\n" if charinrange($range, $codepoint);
599 my %GENERAL_CATEGORIES =
602 'LC' => 'CasedLetter',
603 'Lu' => 'UppercaseLetter',
604 'Ll' => 'LowercaseLetter',
605 'Lt' => 'TitlecaseLetter',
606 'Lm' => 'ModifierLetter',
607 'Lo' => 'OtherLetter',
609 'Mn' => 'NonspacingMark',
610 'Mc' => 'SpacingMark',
611 'Me' => 'EnclosingMark',
613 'Nd' => 'DecimalNumber',
614 'Nl' => 'LetterNumber',
615 'No' => 'OtherNumber',
616 'P' => 'Punctuation',
617 'Pc' => 'ConnectorPunctuation',
618 'Pd' => 'DashPunctuation',
619 'Ps' => 'OpenPunctuation',
620 'Pe' => 'ClosePunctuation',
621 'Pi' => 'InitialPunctuation',
622 'Pf' => 'FinalPunctuation',
623 'Po' => 'OtherPunctuation',
625 'Sm' => 'MathSymbol',
626 'Sc' => 'CurrencySymbol',
627 'Sk' => 'ModifierSymbol',
628 'So' => 'OtherSymbol',
630 'Zs' => 'SpaceSeparator',
631 'Zl' => 'LineSeparator',
632 'Zp' => 'ParagraphSeparator',
637 'Co' => 'PrivateUse',
638 'Cn' => 'Unassigned',
641 sub general_categories {
642 return dclone \%GENERAL_CATEGORIES;
645 =head2 B<general_categories()>
647 use Unicode::UCD 'general_categories';
649 my $categories = general_categories();
651 This returns a reference to a hash which has short
652 general category names (such as C<Lu>, C<Nd>, C<Zs>, C<S>) as keys and long
653 names (such as C<UppercaseLetter>, C<DecimalNumber>, C<SpaceSeparator>,
654 C<Symbol>) as values. The hash is reversible in case you need to go
655 from the long names to the short names. The general category is the
657 L</charinfo()> under the C<category> key.
663 'L' => 'Left-to-Right',
664 'LRE' => 'Left-to-Right Embedding',
665 'LRO' => 'Left-to-Right Override',
666 'R' => 'Right-to-Left',
667 'AL' => 'Right-to-Left Arabic',
668 'RLE' => 'Right-to-Left Embedding',
669 'RLO' => 'Right-to-Left Override',
670 'PDF' => 'Pop Directional Format',
671 'EN' => 'European Number',
672 'ES' => 'European Number Separator',
673 'ET' => 'European Number Terminator',
674 'AN' => 'Arabic Number',
675 'CS' => 'Common Number Separator',
676 'NSM' => 'Non-Spacing Mark',
677 'BN' => 'Boundary Neutral',
678 'B' => 'Paragraph Separator',
679 'S' => 'Segment Separator',
680 'WS' => 'Whitespace',
681 'ON' => 'Other Neutrals',
684 =head2 B<bidi_types()>
686 use Unicode::UCD 'bidi_types';
688 my $categories = bidi_types();
690 This returns a reference to a hash which has the short
691 bidi (bidirectional) type names (such as C<L>, C<R>) as keys and long
692 names (such as C<Left-to-Right>, C<Right-to-Left>) as values. The
693 hash is reversible in case you need to go from the long names to the
694 short names. The bidi type is the one returned from
696 under the C<bidi> key. For the exact meaning of the various bidi classes
697 the Unicode TR9 is recommended reading:
698 L<http://www.unicode.org/reports/tr9/>
699 (as of Unicode 5.0.0)
704 return dclone \%BIDI_TYPES;
709 use Unicode::UCD 'compexcl';
711 my $compexcl = compexcl(0x09dc);
713 This returns B<true> if the
714 L</code point argument> should not be produced by composition normalization,
715 B<AND> if that fact is not otherwise determinable from the Unicode data base.
716 It currently does not return B<true> if the code point has a decomposition
717 consisting of another single code point, nor if its decomposition starts
718 with a code point whose combining class is non-zero. Code points that meet
719 either of these conditions should also not be produced by composition
722 It returns B<false> otherwise.
730 if (openunicode(\$COMPEXCLFH, "CompositionExclusions.txt")) {
732 while (<$COMPEXCLFH>) {
733 if (/^([0-9A-F]+)\s+\#\s+/) {
735 $COMPEXCL{$code} = undef;
745 my $code = _getcode($arg);
746 croak __PACKAGE__, "::compexcl: unknown code '$arg'"
747 unless defined $code;
749 _compexcl() unless %COMPEXCL;
751 return exists $COMPEXCL{$code};
756 use Unicode::UCD 'casefold';
758 my $casefold = casefold(0xDF);
759 if (defined $casefold) {
760 my @full_fold_hex = split / /, $casefold->{'full'};
761 my $full_fold_string =
762 join "", map {chr(hex($_))} @full_fold_hex;
763 my @turkic_fold_hex =
764 split / /, ($casefold->{'turkic'} ne "")
765 ? $casefold->{'turkic'}
766 : $casefold->{'full'};
767 my $turkic_fold_string =
768 join "", map {chr(hex($_))} @turkic_fold_hex;
770 if (defined $casefold && $casefold->{'simple'} ne "") {
771 my $simple_fold_hex = $casefold->{'simple'};
772 my $simple_fold_string = chr(hex($simple_fold_hex));
775 This returns the (almost) locale-independent case folding of the
776 character specified by the L</code point argument>.
778 If there is no case folding for that code point, B<undef> is returned.
780 If there is a case folding for that code point, a reference to a hash
781 with the following fields is returned:
787 the input L</code point argument> expressed in hexadecimal, with leading zeros
788 added if necessary to make it contain at least four hexdigits
792 one or more codes (separated by spaces) that taken in order give the
793 code points for the case folding for I<code>.
794 Each has at least four hexdigits.
798 is empty, or is exactly one code with at least four hexdigits which can be used
799 as an alternative case folding when the calling program cannot cope with the
800 fold being a sequence of multiple code points. If I<full> is just one code
801 point, then I<simple> equals I<full>. If there is no single code point folding
802 defined for I<code>, then I<simple> is the empty string. Otherwise, it is an
803 inferior, but still better-than-nothing alternative folding to I<full>.
807 is the same as I<simple> if I<simple> is not empty, and it is the same as I<full>
808 otherwise. It can be considered to be the simplest possible folding for
809 I<code>. It is defined primarily for backwards compatibility.
813 is C<C> (for C<common>) if the best possible fold is a single code point
814 (I<simple> equals I<full> equals I<mapping>). It is C<S> if there are distinct
815 folds, I<simple> and I<full> (I<mapping> equals I<simple>). And it is C<F> if
816 there only a I<full> fold (I<mapping> equals I<full>; I<simple> is empty). Note
818 describes the contents of I<mapping>. It is defined primarily for backwards
821 On versions 3.1 and earlier of Unicode, I<status> can also be
822 C<I> which is the same as C<C> but is a special case for dotted uppercase I and
829 If you use this C<I> mapping, the result is case-insensitive,
830 but dotless and dotted I's are not distinguished
834 If you exclude this C<I> mapping, the result is not fully case-insensitive, but
835 dotless and dotted I's are distinguished
841 contains any special folding for Turkic languages. For versions of Unicode
842 starting with 3.2, this field is empty unless I<code> has a different folding
843 in Turkic languages, in which case it is one or more codes (separated by
844 spaces) that taken in order give the code points for the case folding for
845 I<code> in those languages.
846 Each code has at least four hexdigits.
847 Note that this folding does not maintain canonical equivalence without
848 additional processing.
850 For versions of Unicode 3.1 and earlier, this field is empty unless there is a
851 special folding for Turkic languages, in which case I<status> is C<I>, and
852 I<mapping>, I<full>, I<simple>, and I<turkic> are all equal.
856 Programs that want complete generality and the best folding results should use
857 the folding contained in the I<full> field. But note that the fold for some
858 code points will be a sequence of multiple code points.
860 Programs that can't cope with the fold mapping being multiple code points can
861 use the folding contained in the I<simple> field, with the loss of some
862 generality. In Unicode 5.1, about 7% of the defined foldings have no single
865 The I<mapping> and I<status> fields are provided for backwards compatibility for
866 existing programs. They contain the same values as in previous versions of
869 Locale is not completely independent. The I<turkic> field contains results to
870 use when the locale is a Turkic language.
872 For more information about case mappings see
873 L<http://www.unicode.org/unicode/reports/tr21>
881 if (openunicode(\$CASEFOLDFH, "CaseFolding.txt")) {
883 while (<$CASEFOLDFH>) {
884 if (/^([0-9A-F]+); ([CFIST]); ([0-9A-F]+(?: [0-9A-F]+)*);/) {
886 $CASEFOLD{$code}{'code'} = $1;
887 $CASEFOLD{$code}{'turkic'} = "" unless
888 defined $CASEFOLD{$code}{'turkic'};
889 if ($2 eq 'C' || $2 eq 'I') { # 'I' is only on 3.1 and
891 # Both entries there (I
892 # only checked 3.1) are
896 # codepoints, so treat
897 # as if C, but override
900 $CASEFOLD{$code}{'status'} = $2;
901 $CASEFOLD{$code}{'full'} = $CASEFOLD{$code}{'simple'} =
902 $CASEFOLD{$code}{'mapping'} = $3;
903 $CASEFOLD{$code}{'turkic'} = $3 if $2 eq 'I';
904 } elsif ($2 eq 'F') {
905 $CASEFOLD{$code}{'full'} = $3;
906 unless (defined $CASEFOLD{$code}{'simple'}) {
907 $CASEFOLD{$code}{'simple'} = "";
908 $CASEFOLD{$code}{'mapping'} = $3;
909 $CASEFOLD{$code}{'status'} = $2;
911 } elsif ($2 eq 'S') {
914 # There can't be a simple without a full, and simple
915 # overrides all but full
917 $CASEFOLD{$code}{'simple'} = $3;
918 $CASEFOLD{$code}{'mapping'} = $3;
919 $CASEFOLD{$code}{'status'} = $2;
920 } elsif ($2 eq 'T') {
921 $CASEFOLD{$code}{'turkic'} = $3;
922 } # else can't happen because only [CIFST] are possible
932 my $code = _getcode($arg);
933 croak __PACKAGE__, "::casefold: unknown code '$arg'"
934 unless defined $code;
936 _casefold() unless %CASEFOLD;
938 return $CASEFOLD{$code};
943 use Unicode::UCD 'casespec';
945 my $casespec = casespec(0xFB00);
947 This returns the potentially locale-dependent case mappings of the L</code point
948 argument>. The mappings may be longer than a single code point (which the basic
949 Unicode case mappings as returned by L</charinfo()> never are).
951 If there are no case mappings for the L</code point argument>, or if all three
952 possible mappings (I<lower>, I<title> and I<upper>) result in single code
953 points and are locale independent and unconditional, B<undef> is returned
954 (which means that the case mappings, if any, for the code point are those
955 returned by L</charinfo()>).
957 Otherwise, a reference to a hash giving the mappings (or a reference to a hash
958 of such hashes, explained below) is returned with the following keys and their
961 The keys in the bottom layer hash with the meanings of their values are:
967 the input L</code point argument> expressed in hexadecimal, with leading zeros
968 added if necessary to make it contain at least four hexdigits
972 one or more codes (separated by spaces) that taken in order give the
973 code points for the lower case of I<code>.
974 Each has at least four hexdigits.
978 one or more codes (separated by spaces) that taken in order give the
979 code points for the title case of I<code>.
980 Each has at least four hexdigits.
984 one or more codes (separated by spaces) that taken in order give the
985 code points for the upper case of I<code>.
986 Each has at least four hexdigits.
990 the conditions for the mappings to be valid.
991 If B<undef>, the mappings are always valid.
992 When defined, this field is a list of conditions,
993 all of which must be true for the mappings to be valid.
994 The list consists of one or more
995 I<locales> (see below)
996 and/or I<contexts> (explained in the next paragraph),
998 (Other than as used to separate elements, spaces are to be ignored.)
999 Case distinctions in the condition list are not significant.
1000 Conditions preceded by "NON_" represent the negation of the condition.
1002 A I<context> is one of those defined in the Unicode standard.
1003 For Unicode 5.1, they are defined in Section 3.13 C<Default Case Operations>
1005 L<http://www.unicode.org/versions/Unicode5.1.0/>.
1006 These are for context-sensitive casing.
1010 The hash described above is returned for locale-independent casing, where
1011 at least one of the mappings has length longer than one. If B<undef> is
1012 returned, the code point may have mappings, but if so, all are length one,
1013 and are returned by L</charinfo()>.
1014 Note that when this function does return a value, it will be for the complete
1015 set of mappings for a code point, even those whose length is one.
1017 If there are additional casing rules that apply only in certain locales,
1018 an additional key for each will be defined in the returned hash. Each such key
1019 will be its locale name, defined as a 2-letter ISO 3166 country code, possibly
1020 followed by a "_" and a 2-letter ISO language code (possibly followed by a "_"
1021 and a variant code). You can find the lists of all possible locales, see
1022 L<Locale::Country> and L<Locale::Language>.
1023 (In Unicode 5.1, the only locales returned by this function
1024 are C<lt>, C<tr>, and C<az>.)
1026 Each locale key is a reference to a hash that has the form above, and gives
1027 the casing rules for that particular locale, which take precedence over the
1028 locale-independent ones when in that locale.
1030 If the only casing for a code point is locale-dependent, then the returned
1031 hash will not have any of the base keys, like C<code>, C<upper>, etc., but
1032 will contain only locale keys.
1034 For more information about case mappings see
1035 L<http://www.unicode.org/unicode/reports/tr21/>
1042 unless (%CASESPEC) {
1043 if (openunicode(\$CASESPECFH, "SpecialCasing.txt")) {
1045 while (<$CASESPECFH>) {
1046 if (/^([0-9A-F]+); ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; (\w+(?: \w+)*)?/) {
1047 my ($hexcode, $lower, $title, $upper, $condition) =
1048 ($1, $2, $3, $4, $5);
1049 my $code = hex($hexcode);
1050 if (exists $CASESPEC{$code}) {
1051 if (exists $CASESPEC{$code}->{code}) {
1056 @{$CASESPEC{$code}}{qw(lower
1060 if (defined $oldcondition) {
1062 ($oldcondition =~ /^([a-z][a-z](?:_\S+)?)/);
1063 delete $CASESPEC{$code};
1064 $CASESPEC{$code}->{$oldlocale} =
1069 condition => $oldcondition };
1073 ($condition =~ /^([a-z][a-z](?:_\S+)?)/);
1074 $CASESPEC{$code}->{$locale} =
1079 condition => $condition };
1086 condition => $condition };
1097 my $code = _getcode($arg);
1098 croak __PACKAGE__, "::casespec: unknown code '$arg'"
1099 unless defined $code;
1101 _casespec() unless %CASESPEC;
1103 return ref $CASESPEC{$code} ? dclone $CASESPEC{$code} : $CASESPEC{$code};
1106 =head2 B<namedseq()>
1108 use Unicode::UCD 'namedseq';
1110 my $namedseq = namedseq("KATAKANA LETTER AINU P");
1111 my @namedseq = namedseq("KATAKANA LETTER AINU P");
1112 my %namedseq = namedseq();
1114 If used with a single argument in a scalar context, returns the string
1115 consisting of the code points of the named sequence, or B<undef> if no
1116 named sequence by that name exists. If used with a single argument in
1117 a list context, it returns the list of the ordinals of the code points. If used
1119 arguments in a list context, returns a hash with the names of the
1120 named sequences as the keys and the named sequences as strings as
1121 the values. Otherwise, it returns B<undef> or an empty list depending
1124 This function only operates on officially approved (not provisional) named
1132 unless (%NAMEDSEQ) {
1133 if (openunicode(\$NAMEDSEQFH, "Name.pl")) {
1135 while (<$NAMEDSEQFH>) {
1136 if (/^ [0-9A-F]+ \ /x) {
1138 my ($sequence, $name) = split /\t/;
1139 my @s = map { chr(hex($_)) } split(' ', $sequence);
1140 $NAMEDSEQ{$name} = join("", @s);
1150 # Use charnames::string_vianame() which now returns this information,
1151 # unless the caller wants the hash returned, in which case we read it in,
1152 # and thereafter use it instead of calling charnames, as it is faster.
1154 my $wantarray = wantarray();
1155 if (defined $wantarray) {
1158 _namedseq() unless %NAMEDSEQ;
1163 $s = $NAMEDSEQ{ $_[0] };
1166 $s = charnames::string_vianame($_[0]);
1168 return defined $s ? map { ord($_) } split('', $s) : ();
1171 return $NAMEDSEQ{ $_[0] } if %NAMEDSEQ;
1172 return charnames::string_vianame($_[0]);
1178 =head2 Unicode::UCD::UnicodeVersion
1180 This returns the version of the Unicode Character Database, in other words, the
1181 version of the Unicode standard the database implements. The version is a
1182 string of numbers delimited by dots (C<'.'>).
1188 sub UnicodeVersion {
1189 unless (defined $UNICODEVERSION) {
1190 openunicode(\$VERSIONFH, "version");
1191 chomp($UNICODEVERSION = <$VERSIONFH>);
1193 croak __PACKAGE__, "::VERSION: strange version '$UNICODEVERSION'"
1194 unless $UNICODEVERSION =~ /^\d+(?:\.\d+)+$/;
1196 return $UNICODEVERSION;
1199 =head2 B<Blocks versus Scripts>
1201 The difference between a block and a script is that scripts are closer
1202 to the linguistic notion of a set of code points required to present
1203 languages, while block is more of an artifact of the Unicode code point
1204 numbering and separation into blocks of (mostly) 256 code points.
1206 For example the Latin B<script> is spread over several B<blocks>, such
1207 as C<Basic Latin>, C<Latin 1 Supplement>, C<Latin Extended-A>, and
1208 C<Latin Extended-B>. On the other hand, the Latin script does not
1209 contain all the characters of the C<Basic Latin> block (also known as
1210 ASCII): it includes only the letters, and not, for example, the digits
1213 For blocks see L<http://www.unicode.org/Public/UNIDATA/Blocks.txt>
1215 For scripts see UTR #24: L<http://www.unicode.org/unicode/reports/tr24/>
1217 =head2 B<Matching Scripts and Blocks>
1219 Scripts are matched with the regular-expression construct
1220 C<\p{...}> (e.g. C<\p{Tibetan}> matches characters of the Tibetan script),
1221 while C<\p{In...}> is used for blocks (e.g. C<\p{InTibetan}> matches
1222 any of the 256 code points in the Tibetan block).
1225 =head2 Implementation Note
1227 The first use of charinfo() opens a read-only filehandle to the Unicode
1228 Character Database (the database is included in the Perl distribution).
1229 The filehandle is then kept open for further queries. In other words,
1230 if you are wondering where one of your filehandles went, that's where.
1234 Does not yet support EBCDIC platforms.
1236 L</compexcl()> should give a complete list of excluded code points.