5 no warnings 'surrogate'; # surrogates can be inputs to this
12 our @ISA = qw(Exporter);
14 our @EXPORT_OK = qw(charinfo
16 charblocks charscripts
18 general_categories bidi_types
20 casefold all_casefolds casespec
34 sub IS_ASCII_PLATFORM { ord("A") == 65 }
38 Unicode::UCD - Unicode character database
42 use Unicode::UCD 'charinfo';
43 my $charinfo = charinfo($codepoint);
45 use Unicode::UCD 'casefold';
46 my $casefold = casefold($codepoint);
48 use Unicode::UCD 'all_casefolds';
49 my $all_casefolds_ref = all_casefolds();
51 use Unicode::UCD 'casespec';
52 my $casespec = casespec($codepoint);
54 use Unicode::UCD 'charblock';
55 my $charblock = charblock($codepoint);
57 use Unicode::UCD 'charscript';
58 my $charscript = charscript($codepoint);
60 use Unicode::UCD 'charblocks';
61 my $charblocks = charblocks();
63 use Unicode::UCD 'charscripts';
64 my $charscripts = charscripts();
66 use Unicode::UCD qw(charscript charinrange);
67 my $range = charscript($script);
68 print "looks like $script\n" if charinrange($range, $codepoint);
70 use Unicode::UCD qw(general_categories bidi_types);
71 my $categories = general_categories();
72 my $types = bidi_types();
74 use Unicode::UCD 'prop_aliases';
75 my @space_names = prop_aliases("space");
77 use Unicode::UCD 'prop_value_aliases';
78 my @gc_punct_names = prop_value_aliases("Gc", "Punct");
80 use Unicode::UCD 'prop_values';
81 my @all_EA_short_names = prop_values("East_Asian_Width");
83 use Unicode::UCD 'prop_invlist';
84 my @puncts = prop_invlist("gc=punctuation");
86 use Unicode::UCD 'prop_invmap';
87 my ($list_ref, $map_ref, $format, $missing)
88 = prop_invmap("General Category");
90 use Unicode::UCD 'search_invlist';
91 my $index = search_invlist(\@invlist, $code_point);
93 use Unicode::UCD 'compexcl';
94 my $compexcl = compexcl($codepoint);
96 use Unicode::UCD 'namedseq';
97 my $namedseq = namedseq($named_sequence_name);
99 my $unicode_version = Unicode::UCD::UnicodeVersion();
101 my $convert_to_numeric =
102 Unicode::UCD::num("\N{RUMI DIGIT ONE}\N{RUMI DIGIT TWO}");
106 The Unicode::UCD module offers a series of functions that
107 provide a simple interface to the Unicode
110 =head2 code point argument
112 Some of the functions are called with a I<code point argument>, which is either
113 a decimal or a hexadecimal scalar designating a code point in the platform's
114 native character set (extended to Unicode), or a string containing C<U+>
115 followed by hexadecimals
116 designating a Unicode code point. A leading 0 will force a hexadecimal
117 interpretation, as will a hexadecimal digit that isn't a decimal digit.
121 223 # Decimal 223 in native character set
122 0223 # Hexadecimal 223, native (= 547 decimal)
123 0xDF # Hexadecimal DF, native (= 223 decimal
124 'U+DF' # Hexadecimal DF, in Unicode's character set
125 (= LATIN SMALL LETTER SHARP S)
127 Note that the largest code point in Unicode is U+10FFFF.
136 my $v_unicode_version; # v-string.
139 my ($rfh, @path) = @_;
141 unless (defined $$rfh) {
144 $f = File::Spec->catfile($d, "unicore", @path);
145 last if open($$rfh, $f);
148 croak __PACKAGE__, ": failed to find ",
149 File::Spec->catfile(@path), " in @INC"
155 sub _dclone ($) { # Use Storable::dclone if available; otherwise emulate it.
157 use if defined &DynaLoader::boot_DynaLoader, Storable => qw(dclone);
159 return dclone(shift) if defined &dclone;
163 return $arg unless $type; # No deep cloning needed for scalars
165 if ($type eq 'ARRAY') {
167 foreach my $element (@$arg) {
168 push @return, &_dclone($element);
172 elsif ($type eq 'HASH') {
174 foreach my $key (keys %$arg) {
175 $return{$key} = &_dclone($arg->{$key});
180 croak "_dclone can't handle " . $type;
186 use Unicode::UCD 'charinfo';
188 my $charinfo = charinfo(0x41);
190 This returns information about the input L</code point argument>
191 as a reference to a hash of fields as defined by the Unicode
192 standard. If the L</code point argument> is not assigned in the standard
193 (i.e., has the general category C<Cn> meaning C<Unassigned>)
194 or is a non-character (meaning it is guaranteed to never be assigned in
196 C<undef> is returned.
198 Fields that aren't applicable to the particular code point argument exist in the
199 returned hash, and are empty.
201 The keys in the hash with the meanings of their values are:
207 the input native L</code point argument> expressed in hexadecimal, with
209 added if necessary to make it contain at least four hexdigits
213 name of I<code>, all IN UPPER CASE.
214 Some control-type code points do not have names.
215 This field will be empty for C<Surrogate> and C<Private Use> code points,
216 and for the others without a name,
217 it will contain a description enclosed in angle brackets, like
218 C<E<lt>controlE<gt>>.
223 The short name of the general category of I<code>.
224 This will match one of the keys in the hash returned by L</general_categories()>.
226 The L</prop_value_aliases()> function can be used to get all the synonyms
227 of the category name.
231 the combining class number for I<code> used in the Canonical Ordering Algorithm.
232 For Unicode 5.1, this is described in Section 3.11 C<Canonical Ordering Behavior>
234 L<http://www.unicode.org/versions/Unicode5.1.0/>
236 The L</prop_value_aliases()> function can be used to get all the synonyms
237 of the combining class number.
241 bidirectional type of I<code>.
242 This will match one of the keys in the hash returned by L</bidi_types()>.
244 The L</prop_value_aliases()> function can be used to get all the synonyms
245 of the bidi type name.
247 =item B<decomposition>
249 is empty if I<code> has no decomposition; or is one or more codes
250 (separated by spaces) that, taken in order, represent a decomposition for
251 I<code>. Each has at least four hexdigits.
252 The codes may be preceded by a word enclosed in angle brackets, then a space,
253 like C<E<lt>compatE<gt> >, giving the type of decomposition
255 This decomposition may be an intermediate one whose components are also
256 decomposable. Use L<Unicode::Normalize> to get the final decomposition in one
261 if I<code> represents a decimal digit this is its integer numeric value
265 if I<code> represents some other digit-like number, this is its integer
270 if I<code> represents a whole or rational number, this is its numeric value.
271 Rational values are expressed as a string like C<1/4>.
275 C<Y> or C<N> designating if I<code> is mirrored in bidirectional text
279 name of I<code> in the Unicode 1.0 standard if one
280 existed for this code point and is different from the current name
284 As of Unicode 6.0, this is always empty.
288 is, if non-empty, the uppercase mapping for I<code> expressed as at least four
289 hexdigits. This indicates that the full uppercase mapping is a single
290 character, and is identical to the simple (single-character only) mapping.
291 When this field is empty, it means that the simple uppercase mapping is
292 I<code> itself; you'll need some other means, (like
293 L</casespec()> to get the full mapping.
297 is, if non-empty, the lowercase mapping for I<code> expressed as at least four
298 hexdigits. This indicates that the full lowercase mapping is a single
299 character, and is identical to the simple (single-character only) mapping.
300 When this field is empty, it means that the simple lowercase mapping is
301 I<code> itself; you'll need some other means, (like
302 L</casespec()> to get the full mapping.
306 is, if non-empty, the titlecase mapping for I<code> expressed as at least four
307 hexdigits. This indicates that the full titlecase mapping is a single
308 character, and is identical to the simple (single-character only) mapping.
309 When this field is empty, it means that the simple titlecase mapping is
310 I<code> itself; you'll need some other means, (like
311 L</casespec()> to get the full mapping.
315 the block I<code> belongs to (used in C<\p{Blk=...}>).
316 The L</prop_value_aliases()> function can be used to get all the synonyms
319 See L</Blocks versus Scripts>.
323 the script I<code> belongs to.
324 The L</prop_value_aliases()> function can be used to get all the synonyms
327 See L</Blocks versus Scripts>.
331 Note that you cannot do (de)composition and casing based solely on the
332 I<decomposition>, I<combining>, I<lower>, I<upper>, and I<title> fields; you
333 will need also the L</casespec()> function and the C<Composition_Exclusion>
334 property. (Or you could just use the L<lc()|perlfunc/lc>,
335 L<uc()|perlfunc/uc>, and L<ucfirst()|perlfunc/ucfirst> functions, and the
336 L<Unicode::Normalize> module.)
340 # NB: This function is nearly duplicated in charnames.pm
344 if ($arg =~ /^[1-9]\d*$/) {
347 elsif ($arg =~ /^(?:0[xX])?([[:xdigit:]]+)$/) {
348 return CORE::hex($1);
350 elsif ($arg =~ /^[Uu]\+([[:xdigit:]]+)$/) { # Is of form U+0000, means
351 # wants the Unicode code
352 # point, not the native one
353 my $decimal = CORE::hex($1);
354 return $decimal if IS_ASCII_PLATFORM;
355 return utf8::unicode_to_native($decimal);
361 # Populated by _num. Converts real number back to input rational
362 my %real_to_rational;
364 # To store the contents of files found on disk.
377 # This function has traditionally mimicked what is in UnicodeData.txt,
378 # warts and all. This is a re-write that avoids UnicodeData.txt so that
379 # it can be removed to save disk space. Instead, this assembles
380 # information gotten by other methods that get data from various other
381 # files. It uses charnames to get the character name; and various
384 use feature 'unicode_strings';
386 # Will fail if called under minitest
387 use if defined &DynaLoader::boot_DynaLoader, "Unicode::Normalize" => qw(getCombinClass NFD);
390 my $code = _getcode($arg);
391 croak __PACKAGE__, "::charinfo: unknown code '$arg'" unless defined $code;
393 # Non-unicode implies undef.
394 return if $code > 0x10FFFF;
397 my $char = chr($code);
399 @CATEGORIES =_read_table("To/Gc.pl") unless @CATEGORIES;
400 $prop{'category'} = _search(\@CATEGORIES, 0, $#CATEGORIES, $code)
401 // $utf8::SwashInfo{'ToGc'}{'missing'};
402 # Return undef if category value is 'Unassigned' or one of its synonyms
403 return if grep { lc $_ eq 'unassigned' }
404 prop_value_aliases('Gc', $prop{'category'});
406 $prop{'code'} = sprintf "%04X", $code;
407 $prop{'name'} = ($char =~ /\p{Cntrl}/) ? '<control>'
408 : (charnames::viacode($code) // "");
410 $prop{'combining'} = getCombinClass($code);
412 @BIDIS =_read_table("To/Bc.pl") unless @BIDIS;
413 $prop{'bidi'} = _search(\@BIDIS, 0, $#BIDIS, $code)
414 // $utf8::SwashInfo{'ToBc'}{'missing'};
416 # For most code points, we can just read in "unicore/Decomposition.pl", as
417 # its contents are exactly what should be output. But that file doesn't
418 # contain the data for the Hangul syllable decompositions, which can be
419 # algorithmically computed, and NFD() does that, so we call NFD() for
420 # those. We can't use NFD() for everything, as it does a complete
421 # recursive decomposition, and what this function has always done is to
422 # return what's in UnicodeData.txt which doesn't show that recursiveness.
423 # Fortunately, the NFD() of the Hanguls doesn't have any recursion
425 # Having no decomposition implies an empty field; otherwise, all but
426 # "Canonical" imply a compatible decomposition, and the type is prefixed
427 # to that, as it is in UnicodeData.txt
428 UnicodeVersion() unless defined $v_unicode_version;
429 if ($v_unicode_version ge v2.0.0 && $char =~ /\p{Block=Hangul_Syllables}/) {
430 # The code points of the decomposition are output in standard Unicode
431 # hex format, separated by blanks.
432 $prop{'decomposition'} = join " ", map { sprintf("%04X", $_)}
433 unpack "U*", NFD($char);
436 @DECOMPOSITIONS = _read_table("Decomposition.pl")
437 unless @DECOMPOSITIONS;
438 $prop{'decomposition'} = _search(\@DECOMPOSITIONS, 0, $#DECOMPOSITIONS,
442 # Can use num() to get the numeric values, if any.
443 if (! defined (my $value = num($char))) {
444 $prop{'decimal'} = $prop{'digit'} = $prop{'numeric'} = "";
448 $prop{'decimal'} = $prop{'digit'} = $prop{'numeric'} = $value;
452 # For non-decimal-digits, we have to read in the Numeric type
453 # to distinguish them. It is not just a matter of integer vs.
454 # rational, as some whole number values are not considered digits,
455 # e.g., TAMIL NUMBER TEN.
456 $prop{'decimal'} = "";
458 @NUMERIC_TYPES =_read_table("To/Nt.pl") unless @NUMERIC_TYPES;
459 if ((_search(\@NUMERIC_TYPES, 0, $#NUMERIC_TYPES, $code) // "")
462 $prop{'digit'} = $prop{'numeric'} = $value;
466 $prop{'numeric'} = $real_to_rational{$value} // $value;
471 $prop{'mirrored'} = ($char =~ /\p{Bidi_Mirrored}/) ? 'Y' : 'N';
473 %UNICODE_1_NAMES =_read_table("To/Na1.pl", "use_hash") unless %UNICODE_1_NAMES;
474 $prop{'unicode10'} = $UNICODE_1_NAMES{$code} // "";
476 UnicodeVersion() unless defined $v_unicode_version;
477 if ($v_unicode_version ge v6.0.0) {
478 $prop{'comment'} = "";
481 %ISO_COMMENT = _read_table("To/Isc.pl", "use_hash") unless %ISO_COMMENT;
482 $prop{'comment'} = (defined $ISO_COMMENT{$code})
483 ? $ISO_COMMENT{$code}
487 %SIMPLE_UPPER = _read_table("To/Uc.pl", "use_hash") unless %SIMPLE_UPPER;
488 $prop{'upper'} = (defined $SIMPLE_UPPER{$code})
489 ? sprintf("%04X", $SIMPLE_UPPER{$code})
492 %SIMPLE_LOWER = _read_table("To/Lc.pl", "use_hash") unless %SIMPLE_LOWER;
493 $prop{'lower'} = (defined $SIMPLE_LOWER{$code})
494 ? sprintf("%04X", $SIMPLE_LOWER{$code})
497 %SIMPLE_TITLE = _read_table("To/Tc.pl", "use_hash") unless %SIMPLE_TITLE;
498 $prop{'title'} = (defined $SIMPLE_TITLE{$code})
499 ? sprintf("%04X", $SIMPLE_TITLE{$code})
502 $prop{block} = charblock($code);
503 $prop{script} = charscript($code);
507 sub _search { # Binary search in a [[lo,hi,prop],[...],...] table.
508 my ($table, $lo, $hi, $code) = @_;
512 my $mid = int(($lo+$hi) / 2);
514 if ($table->[$mid]->[0] < $code) {
515 if ($table->[$mid]->[1] >= $code) {
516 return $table->[$mid]->[2];
518 _search($table, $mid + 1, $hi, $code);
520 } elsif ($table->[$mid]->[0] > $code) {
521 _search($table, $lo, $mid - 1, $code);
523 return $table->[$mid]->[2];
527 sub _read_table ($;$) {
529 # Returns the contents of the mktables generated table file located at $1
530 # in the form of either an array of arrays or a hash, depending on if the
531 # optional second parameter is true (for hash return) or not. In the case
532 # of a hash return, each key is a code point, and its corresponding value
533 # is what the table gives as the code point's corresponding value. In the
534 # case of an array return, each outer array denotes a range with [0] the
535 # start point of that range; [1] the end point; and [2] the value that
536 # every code point in the range has. The hash return is useful for fast
537 # lookup when the table contains only single code point ranges. The array
538 # return takes much less memory when there are large ranges.
540 # This function has the side effect of setting
541 # $utf8::SwashInfo{$property}{'format'} to be the mktables format of the
543 # $utf8::SwashInfo{$property}{'missing'} to be the value for all entries
544 # not listed in the table.
545 # where $property is the Unicode property name, preceded by 'To' for map
546 # properties., e.g., 'ToSc'.
548 # Table entries look like one of:
549 # 0000 0040 Common # [65]
553 my $return_hash = shift;
554 $return_hash = 0 unless defined $return_hash;
558 my $list = do "unicore/$table";
560 # Look up if this property requires adjustments, which we do below if it
562 require "unicore/Heavy.pl";
563 my $property = $table =~ s/\.pl//r;
564 $property = $utf8::file_to_swash_name{$property};
565 my $to_adjust = defined $property
566 && $utf8::SwashInfo{$property}{'format'} =~ / ^ a /x;
568 for (split /^/m, $list) {
569 my ($start, $end, $value) = / ^ (.+?) \t (.*?) \t (.+?)
570 \s* ( \# .* )? # Optional comment
572 my $decimal_start = hex $start;
573 my $decimal_end = ($end eq "") ? $decimal_start : hex $end;
574 $value = hex $value if $to_adjust
575 && $utf8::SwashInfo{$property}{'format'} eq 'ax';
577 foreach my $i ($decimal_start .. $decimal_end) {
578 $return{$i} = ($to_adjust)
579 ? $value + $i - $decimal_start
585 && $return[-1][1] == $decimal_start - 1
586 && $return[-1][2] eq $value)
588 # If this is merely extending the previous range, do just that.
589 $return[-1]->[1] = $decimal_end;
592 push @return, [ $decimal_start, $decimal_end, $value ];
595 return ($return_hash) ? %return : @return;
599 my ($range, $arg) = @_;
600 my $code = _getcode($arg);
601 croak __PACKAGE__, "::charinrange: unknown code '$arg'"
602 unless defined $code;
603 _search($range, 0, $#$range, $code);
606 =head2 B<charblock()>
608 use Unicode::UCD 'charblock';
610 my $charblock = charblock(0x41);
611 my $charblock = charblock(1234);
612 my $charblock = charblock(0x263a);
613 my $charblock = charblock("U+263a");
615 my $range = charblock('Armenian');
617 With a L</code point argument> C<charblock()> returns the I<block> the code point
618 belongs to, e.g. C<Basic Latin>. The old-style block name is returned (see
619 L</Old-style versus new-style block names>).
620 The L</prop_value_aliases()> function can be used to get all the synonyms
623 If the code point is unassigned, this returns the block it would belong to if
624 it were assigned. (If the Unicode version being used is so early as to not
625 have blocks, all code points are considered to be in C<No_Block>.)
627 See also L</Blocks versus Scripts>.
629 If supplied with an argument that can't be a code point, C<charblock()> tries to
630 do the opposite and interpret the argument as an old-style block name. On an
631 ASCII platform, the return value is a I<range set> with one range: an
632 anonymous array with a single element that consists of another anonymous array
633 whose first element is the first code point in the block, and whose second
634 element is the final code point in the block. On an EBCDIC
635 platform, the first two Unicode blocks are not contiguous. Their range sets
636 are lists containing I<start-of-range>, I<end-of-range> code point pairs. You
637 can test whether a code point is in a range set using the L</charinrange()>
638 function. (To be precise, each I<range set> contains a third array element,
639 after the range boundary ones: the old_style block name.)
641 If the argument to C<charblock()> is not a known block, C<undef> is
651 # Can't read from the mktables table because it loses the hyphens in the
654 UnicodeVersion() unless defined $v_unicode_version;
655 if ($v_unicode_version lt v2.0.0) {
656 my $subrange = [ 0, 0x10FFFF, 'No_Block' ];
657 push @BLOCKS, $subrange;
658 push @{$BLOCKS{'No_Block'}}, $subrange;
660 elsif (openunicode(\$BLOCKSFH, "Blocks.txt")) {
663 while (<$BLOCKSFH>) {
664 if (/^([0-9A-F]+)\.\.([0-9A-F]+);\s+(.+)/) {
665 my ($lo, $hi) = (hex($1), hex($2));
666 my $subrange = [ $lo, $hi, $3 ];
667 push @BLOCKS, $subrange;
668 push @{$BLOCKS{$3}}, $subrange;
672 if (! IS_ASCII_PLATFORM) {
673 # The first two blocks, through 0xFF, are wrong on EBCDIC
676 my @new_blocks = _read_table("To/Blk.pl");
678 # Get rid of the first two ranges in the Unicode version, and
679 # replace them with the ones computed by mktables.
682 delete $BLOCKS{'Basic Latin'};
683 delete $BLOCKS{'Latin-1 Supplement'};
685 # But there are multiple entries in the computed versions, and
686 # we change their names to (which we know) to be the old-style
688 for my $i (0.. @new_blocks - 1) {
689 if ($new_blocks[$i][2] =~ s/Basic_Latin/Basic Latin/
690 or $new_blocks[$i][2] =~
691 s/Latin_1_Supplement/Latin-1 Supplement/)
693 push @{$BLOCKS{$new_blocks[$i][2]}}, $new_blocks[$i];
696 splice @new_blocks, $i;
700 unshift @BLOCKS, @new_blocks;
709 _charblocks() unless @BLOCKS;
711 my $code = _getcode($arg);
714 my $result = _search(\@BLOCKS, 0, $#BLOCKS, $code);
715 return $result if defined $result;
718 elsif (exists $BLOCKS{$arg}) {
719 return _dclone $BLOCKS{$arg};
723 =head2 B<charscript()>
725 use Unicode::UCD 'charscript';
727 my $charscript = charscript(0x41);
728 my $charscript = charscript(1234);
729 my $charscript = charscript("U+263a");
731 my $range = charscript('Thai');
733 With a L</code point argument>, C<charscript()> returns the I<script> the
734 code point belongs to, e.g., C<Latin>, C<Greek>, C<Han>.
735 If the code point is unassigned or the Unicode version being used is so early
736 that it doesn't have scripts, this function returns C<"Unknown">.
737 The L</prop_value_aliases()> function can be used to get all the synonyms
740 If supplied with an argument that can't be a code point, charscript() tries
741 to do the opposite and interpret the argument as a script name. The
742 return value is a I<range set>: an anonymous array of arrays that contain
743 I<start-of-range>, I<end-of-range> code point pairs. You can test whether a
744 code point is in a range set using the L</charinrange()> function.
745 (To be precise, each I<range set> contains a third array element,
746 after the range boundary ones: the script name.)
748 If the C<charscript()> argument is not a known script, C<undef> is returned.
750 See also L</Blocks versus Scripts>.
759 UnicodeVersion() unless defined $v_unicode_version;
760 if ($v_unicode_version lt v3.1.0) {
761 push @SCRIPTS, [ 0, 0x10FFFF, 'Unknown' ];
764 @SCRIPTS =_read_table("To/Sc.pl");
767 foreach my $entry (@SCRIPTS) {
768 $entry->[2] =~ s/(_\w)/\L$1/g; # Preserve old-style casing
769 push @{$SCRIPTS{$entry->[2]}}, $entry;
776 _charscripts() unless @SCRIPTS;
778 my $code = _getcode($arg);
781 my $result = _search(\@SCRIPTS, 0, $#SCRIPTS, $code);
782 return $result if defined $result;
783 return $utf8::SwashInfo{'ToSc'}{'missing'};
784 } elsif (exists $SCRIPTS{$arg}) {
785 return _dclone $SCRIPTS{$arg};
791 =head2 B<charblocks()>
793 use Unicode::UCD 'charblocks';
795 my $charblocks = charblocks();
797 C<charblocks()> returns a reference to a hash with the known block names
798 as the keys, and the code point ranges (see L</charblock()>) as the values.
800 The names are in the old-style (see L</Old-style versus new-style block
803 L<prop_invmap("block")|/prop_invmap()> can be used to get this same data in a
804 different type of data structure.
806 L<prop_values("Block")|/prop_values()> can be used to get all
807 the known new-style block names as a list, without the code point ranges.
809 See also L</Blocks versus Scripts>.
814 _charblocks() unless %BLOCKS;
815 return _dclone \%BLOCKS;
818 =head2 B<charscripts()>
820 use Unicode::UCD 'charscripts';
822 my $charscripts = charscripts();
824 C<charscripts()> returns a reference to a hash with the known script
825 names as the keys, and the code point ranges (see L</charscript()>) as
828 L<prop_invmap("script")|/prop_invmap()> can be used to get this same data in a
829 different type of data structure.
831 L<C<prop_values("Script")>|/prop_values()> can be used to get all
832 the known script names as a list, without the code point ranges.
834 See also L</Blocks versus Scripts>.
839 _charscripts() unless %SCRIPTS;
840 return _dclone \%SCRIPTS;
843 =head2 B<charinrange()>
845 In addition to using the C<\p{Blk=...}> and C<\P{Blk=...}> constructs, you
846 can also test whether a code point is in the I<range> as returned by
847 L</charblock()> and L</charscript()> or as the values of the hash returned
848 by L</charblocks()> and L</charscripts()> by using C<charinrange()>:
850 use Unicode::UCD qw(charscript charinrange);
852 $range = charscript('Hiragana');
853 print "looks like hiragana\n" if charinrange($range, $codepoint);
857 my %GENERAL_CATEGORIES =
860 'LC' => 'CasedLetter',
861 'Lu' => 'UppercaseLetter',
862 'Ll' => 'LowercaseLetter',
863 'Lt' => 'TitlecaseLetter',
864 'Lm' => 'ModifierLetter',
865 'Lo' => 'OtherLetter',
867 'Mn' => 'NonspacingMark',
868 'Mc' => 'SpacingMark',
869 'Me' => 'EnclosingMark',
871 'Nd' => 'DecimalNumber',
872 'Nl' => 'LetterNumber',
873 'No' => 'OtherNumber',
874 'P' => 'Punctuation',
875 'Pc' => 'ConnectorPunctuation',
876 'Pd' => 'DashPunctuation',
877 'Ps' => 'OpenPunctuation',
878 'Pe' => 'ClosePunctuation',
879 'Pi' => 'InitialPunctuation',
880 'Pf' => 'FinalPunctuation',
881 'Po' => 'OtherPunctuation',
883 'Sm' => 'MathSymbol',
884 'Sc' => 'CurrencySymbol',
885 'Sk' => 'ModifierSymbol',
886 'So' => 'OtherSymbol',
888 'Zs' => 'SpaceSeparator',
889 'Zl' => 'LineSeparator',
890 'Zp' => 'ParagraphSeparator',
895 'Co' => 'PrivateUse',
896 'Cn' => 'Unassigned',
899 sub general_categories {
900 return _dclone \%GENERAL_CATEGORIES;
903 =head2 B<general_categories()>
905 use Unicode::UCD 'general_categories';
907 my $categories = general_categories();
909 This returns a reference to a hash which has short
910 general category names (such as C<Lu>, C<Nd>, C<Zs>, C<S>) as keys and long
911 names (such as C<UppercaseLetter>, C<DecimalNumber>, C<SpaceSeparator>,
912 C<Symbol>) as values. The hash is reversible in case you need to go
913 from the long names to the short names. The general category is the
915 L</charinfo()> under the C<category> key.
917 The L</prop_values()> and L</prop_value_aliases()> functions can be used as an
918 alternative to this function; the first returning a simple list of the short
919 category names; and the second gets all the synonyms of a given category name.
925 'L' => 'Left-to-Right',
926 'LRE' => 'Left-to-Right Embedding',
927 'LRO' => 'Left-to-Right Override',
928 'R' => 'Right-to-Left',
929 'AL' => 'Right-to-Left Arabic',
930 'RLE' => 'Right-to-Left Embedding',
931 'RLO' => 'Right-to-Left Override',
932 'PDF' => 'Pop Directional Format',
933 'EN' => 'European Number',
934 'ES' => 'European Number Separator',
935 'ET' => 'European Number Terminator',
936 'AN' => 'Arabic Number',
937 'CS' => 'Common Number Separator',
938 'NSM' => 'Non-Spacing Mark',
939 'BN' => 'Boundary Neutral',
940 'B' => 'Paragraph Separator',
941 'S' => 'Segment Separator',
942 'WS' => 'Whitespace',
943 'ON' => 'Other Neutrals',
946 =head2 B<bidi_types()>
948 use Unicode::UCD 'bidi_types';
950 my $categories = bidi_types();
952 This returns a reference to a hash which has the short
953 bidi (bidirectional) type names (such as C<L>, C<R>) as keys and long
954 names (such as C<Left-to-Right>, C<Right-to-Left>) as values. The
955 hash is reversible in case you need to go from the long names to the
956 short names. The bidi type is the one returned from
958 under the C<bidi> key. For the exact meaning of the various bidi classes
959 the Unicode TR9 is recommended reading:
960 L<http://www.unicode.org/reports/tr9/>
961 (as of Unicode 5.0.0)
963 The L</prop_values()> and L</prop_value_aliases()> functions can be used as an
964 alternative to this function; the first returning a simple list of the short
965 bidi type names; and the second gets all the synonyms of a given bidi type
971 return _dclone \%BIDI_TYPES;
976 use Unicode::UCD 'compexcl';
978 my $compexcl = compexcl(0x09dc);
980 This routine returns C<undef> if the Unicode version being used is so early
981 that it doesn't have this property.
983 C<compexcl()> is included for backwards
984 compatibility, but as of Perl 5.12 and more modern Unicode versions, for
985 most purposes it is probably more convenient to use one of the following
988 my $compexcl = chr(0x09dc) =~ /\p{Comp_Ex};
989 my $compexcl = chr(0x09dc) =~ /\p{Full_Composition_Exclusion};
993 my $compexcl = chr(0x09dc) =~ /\p{CE};
994 my $compexcl = chr(0x09dc) =~ /\p{Composition_Exclusion};
996 The first two forms return B<true> if the L</code point argument> should not
997 be produced by composition normalization. For the final two forms to return
998 B<true>, it is additionally required that this fact not otherwise be
999 determinable from the Unicode data base.
1001 This routine behaves identically to the final two forms. That is,
1002 it does not return B<true> if the code point has a decomposition
1003 consisting of another single code point, nor if its decomposition starts
1004 with a code point whose combining class is non-zero. Code points that meet
1005 either of these conditions should also not be produced by composition
1006 normalization, which is probably why you should use the
1007 C<Full_Composition_Exclusion> property instead, as shown above.
1009 The routine returns B<false> otherwise.
1015 my $code = _getcode($arg);
1016 croak __PACKAGE__, "::compexcl: unknown code '$arg'"
1017 unless defined $code;
1019 UnicodeVersion() unless defined $v_unicode_version;
1020 return if $v_unicode_version lt v3.0.0;
1022 no warnings "non_unicode"; # So works on non-Unicode code points
1023 return chr($code) =~ /\p{Composition_Exclusion}/;
1026 =head2 B<casefold()>
1028 use Unicode::UCD 'casefold';
1030 my $casefold = casefold(0xDF);
1031 if (defined $casefold) {
1032 my @full_fold_hex = split / /, $casefold->{'full'};
1033 my $full_fold_string =
1034 join "", map {chr(hex($_))} @full_fold_hex;
1035 my @turkic_fold_hex =
1036 split / /, ($casefold->{'turkic'} ne "")
1037 ? $casefold->{'turkic'}
1038 : $casefold->{'full'};
1039 my $turkic_fold_string =
1040 join "", map {chr(hex($_))} @turkic_fold_hex;
1042 if (defined $casefold && $casefold->{'simple'} ne "") {
1043 my $simple_fold_hex = $casefold->{'simple'};
1044 my $simple_fold_string = chr(hex($simple_fold_hex));
1047 This returns the (almost) locale-independent case folding of the
1048 character specified by the L</code point argument>. (Starting in Perl v5.16,
1049 the core function C<fc()> returns the C<full> mapping (described below)
1050 faster than this does, and for entire strings.)
1052 If there is no case folding for the input code point, C<undef> is returned.
1054 If there is a case folding for that code point, a reference to a hash
1055 with the following fields is returned:
1061 the input native L</code point argument> expressed in hexadecimal, with
1063 added if necessary to make it contain at least four hexdigits
1067 one or more codes (separated by spaces) that, taken in order, give the
1068 code points for the case folding for I<code>.
1069 Each has at least four hexdigits.
1073 is empty, or is exactly one code with at least four hexdigits which can be used
1074 as an alternative case folding when the calling program cannot cope with the
1075 fold being a sequence of multiple code points. If I<full> is just one code
1076 point, then I<simple> equals I<full>. If there is no single code point folding
1077 defined for I<code>, then I<simple> is the empty string. Otherwise, it is an
1078 inferior, but still better-than-nothing alternative folding to I<full>.
1082 is the same as I<simple> if I<simple> is not empty, and it is the same as I<full>
1083 otherwise. It can be considered to be the simplest possible folding for
1084 I<code>. It is defined primarily for backwards compatibility.
1088 is C<C> (for C<common>) if the best possible fold is a single code point
1089 (I<simple> equals I<full> equals I<mapping>). It is C<S> if there are distinct
1090 folds, I<simple> and I<full> (I<mapping> equals I<simple>). And it is C<F> if
1091 there is only a I<full> fold (I<mapping> equals I<full>; I<simple> is empty).
1093 describes the contents of I<mapping>. It is defined primarily for backwards
1096 For Unicode versions between 3.1 and 3.1.1 inclusive, I<status> can also be
1097 C<I> which is the same as C<C> but is a special case for dotted uppercase I and
1098 dotless lowercase i:
1102 =item Z<>B<*> If you use this C<I> mapping
1104 the result is case-insensitive,
1105 but dotless and dotted I's are not distinguished
1107 =item Z<>B<*> If you exclude this C<I> mapping
1109 the result is not fully case-insensitive, but
1110 dotless and dotted I's are distinguished
1116 contains any special folding for Turkic languages. For versions of Unicode
1117 starting with 3.2, this field is empty unless I<code> has a different folding
1118 in Turkic languages, in which case it is one or more codes (separated by
1119 spaces) that, taken in order, give the code points for the case folding for
1120 I<code> in those languages.
1121 Each code has at least four hexdigits.
1122 Note that this folding does not maintain canonical equivalence without
1123 additional processing.
1125 For Unicode versions between 3.1 and 3.1.1 inclusive, this field is empty unless
1127 special folding for Turkic languages, in which case I<status> is C<I>, and
1128 I<mapping>, I<full>, I<simple>, and I<turkic> are all equal.
1132 Programs that want complete generality and the best folding results should use
1133 the folding contained in the I<full> field. But note that the fold for some
1134 code points will be a sequence of multiple code points.
1136 Programs that can't cope with the fold mapping being multiple code points can
1137 use the folding contained in the I<simple> field, with the loss of some
1138 generality. In Unicode 5.1, about 7% of the defined foldings have no single
1141 The I<mapping> and I<status> fields are provided for backwards compatibility for
1142 existing programs. They contain the same values as in previous versions of
1145 Locale is not completely independent. The I<turkic> field contains results to
1146 use when the locale is a Turkic language.
1148 For more information about case mappings see
1149 L<http://www.unicode.org/unicode/reports/tr21>
1156 unless (%CASEFOLD) { # Populate the hash
1157 my ($full_invlist_ref, $full_invmap_ref, undef, $default)
1158 = prop_invmap('Case_Folding');
1160 # Use the recipe given in the prop_invmap() pod to convert the
1161 # inversion map into the hash.
1162 for my $i (0 .. @$full_invlist_ref - 1 - 1) {
1163 next if $full_invmap_ref->[$i] == $default;
1165 for my $j ($full_invlist_ref->[$i] .. $full_invlist_ref->[$i+1] -1) {
1167 if (! ref $full_invmap_ref->[$i]) {
1169 # This is a single character mapping
1170 $CASEFOLD{$j}{'status'} = 'C';
1171 $CASEFOLD{$j}{'simple'}
1172 = $CASEFOLD{$j}{'full'}
1173 = $CASEFOLD{$j}{'mapping'}
1174 = sprintf("%04X", $full_invmap_ref->[$i] + $adjust);
1175 $CASEFOLD{$j}{'code'} = sprintf("%04X", $j);
1176 $CASEFOLD{$j}{'turkic'} = "";
1178 else { # prop_invmap ensures that $adjust is 0 for a ref
1179 $CASEFOLD{$j}{'status'} = 'F';
1180 $CASEFOLD{$j}{'full'}
1181 = $CASEFOLD{$j}{'mapping'}
1182 = join " ", map { sprintf "%04X", $_ }
1183 @{$full_invmap_ref->[$i]};
1184 $CASEFOLD{$j}{'simple'} = "";
1185 $CASEFOLD{$j}{'code'} = sprintf("%04X", $j);
1186 $CASEFOLD{$j}{'turkic'} = "";
1191 # We have filled in the full mappings above, assuming there were no
1192 # simple ones for the ones with multi-character maps. Now, we find
1193 # and fix the cases where that assumption was false.
1194 (my ($simple_invlist_ref, $simple_invmap_ref, undef), $default)
1195 = prop_invmap('Simple_Case_Folding');
1196 for my $i (0 .. @$simple_invlist_ref - 1 - 1) {
1197 next if $simple_invmap_ref->[$i] == $default;
1199 for my $j ($simple_invlist_ref->[$i]
1200 .. $simple_invlist_ref->[$i+1] -1)
1203 next if $CASEFOLD{$j}{'status'} eq 'C';
1204 $CASEFOLD{$j}{'status'} = 'S';
1205 $CASEFOLD{$j}{'simple'}
1206 = $CASEFOLD{$j}{'mapping'}
1207 = sprintf("%04X", $simple_invmap_ref->[$i] + $adjust);
1208 $CASEFOLD{$j}{'code'} = sprintf("%04X", $j);
1209 $CASEFOLD{$j}{'turkic'} = "";
1213 # We hard-code in the turkish rules
1214 UnicodeVersion() unless defined $v_unicode_version;
1215 if ($v_unicode_version ge v3.2.0) {
1217 # These two code points should already have regular entries, so
1218 # just fill in the turkish fields
1219 $CASEFOLD{ord('I')}{'turkic'} = '0131';
1220 $CASEFOLD{0x130}{'turkic'} = sprintf "%04X", ord('i');
1222 elsif ($v_unicode_version ge v3.1.0) {
1224 # These two code points don't have entries otherwise.
1225 $CASEFOLD{0x130}{'code'} = '0130';
1226 $CASEFOLD{0x131}{'code'} = '0131';
1227 $CASEFOLD{0x130}{'status'} = $CASEFOLD{0x131}{'status'} = 'I';
1228 $CASEFOLD{0x130}{'turkic'}
1229 = $CASEFOLD{0x130}{'mapping'}
1230 = $CASEFOLD{0x130}{'full'}
1231 = $CASEFOLD{0x130}{'simple'}
1232 = $CASEFOLD{0x131}{'turkic'}
1233 = $CASEFOLD{0x131}{'mapping'}
1234 = $CASEFOLD{0x131}{'full'}
1235 = $CASEFOLD{0x131}{'simple'}
1236 = sprintf "%04X", ord('i');
1243 my $code = _getcode($arg);
1244 croak __PACKAGE__, "::casefold: unknown code '$arg'"
1245 unless defined $code;
1247 _casefold() unless %CASEFOLD;
1249 return $CASEFOLD{$code};
1252 =head2 B<all_casefolds()>
1255 use Unicode::UCD 'all_casefolds';
1257 my $all_folds_ref = all_casefolds();
1258 foreach my $char_with_casefold (sort { $a <=> $b }
1259 keys %$all_folds_ref)
1261 printf "%04X:", $char_with_casefold;
1262 my $casefold = $all_folds_ref->{$char_with_casefold};
1264 # Get folds for $char_with_casefold
1266 my @full_fold_hex = split / /, $casefold->{'full'};
1267 my $full_fold_string =
1268 join "", map {chr(hex($_))} @full_fold_hex;
1269 print " full=", join " ", @full_fold_hex;
1270 my @turkic_fold_hex =
1271 split / /, ($casefold->{'turkic'} ne "")
1272 ? $casefold->{'turkic'}
1273 : $casefold->{'full'};
1274 my $turkic_fold_string =
1275 join "", map {chr(hex($_))} @turkic_fold_hex;
1276 print "; turkic=", join " ", @turkic_fold_hex;
1277 if (defined $casefold && $casefold->{'simple'} ne "") {
1278 my $simple_fold_hex = $casefold->{'simple'};
1279 my $simple_fold_string = chr(hex($simple_fold_hex));
1280 print "; simple=$simple_fold_hex";
1285 This returns all the case foldings in the current version of Unicode in the
1286 form of a reference to a hash. Each key to the hash is the decimal
1287 representation of a Unicode character that has a casefold to other than
1288 itself. The casefold of a semi-colon is itself, so it isn't in the hash;
1289 likewise for a lowercase "a", but there is an entry for a capital "A". The
1290 hash value for each key is another hash, identical to what is returned by
1291 L</casefold()> if called with that code point as its argument. So the value
1292 C<< all_casefolds()->{ord("A")}' >> is equivalent to C<casefold(ord("A"))>;
1296 sub all_casefolds () {
1297 _casefold() unless %CASEFOLD;
1298 return _dclone \%CASEFOLD;
1301 =head2 B<casespec()>
1303 use Unicode::UCD 'casespec';
1305 my $casespec = casespec(0xFB00);
1307 This returns the potentially locale-dependent case mappings of the L</code point
1308 argument>. The mappings may be longer than a single code point (which the basic
1309 Unicode case mappings as returned by L</charinfo()> never are).
1311 If there are no case mappings for the L</code point argument>, or if all three
1312 possible mappings (I<lower>, I<title> and I<upper>) result in single code
1313 points and are locale independent and unconditional, C<undef> is returned
1314 (which means that the case mappings, if any, for the code point are those
1315 returned by L</charinfo()>).
1317 Otherwise, a reference to a hash giving the mappings (or a reference to a hash
1318 of such hashes, explained below) is returned with the following keys and their
1321 The keys in the bottom layer hash with the meanings of their values are:
1327 the input native L</code point argument> expressed in hexadecimal, with
1329 added if necessary to make it contain at least four hexdigits
1333 one or more codes (separated by spaces) that, taken in order, give the
1334 code points for the lower case of I<code>.
1335 Each has at least four hexdigits.
1339 one or more codes (separated by spaces) that, taken in order, give the
1340 code points for the title case of I<code>.
1341 Each has at least four hexdigits.
1345 one or more codes (separated by spaces) that, taken in order, give the
1346 code points for the upper case of I<code>.
1347 Each has at least four hexdigits.
1351 the conditions for the mappings to be valid.
1352 If C<undef>, the mappings are always valid.
1353 When defined, this field is a list of conditions,
1354 all of which must be true for the mappings to be valid.
1355 The list consists of one or more
1356 I<locales> (see below)
1357 and/or I<contexts> (explained in the next paragraph),
1358 separated by spaces.
1359 (Other than as used to separate elements, spaces are to be ignored.)
1360 Case distinctions in the condition list are not significant.
1361 Conditions preceded by "NON_" represent the negation of the condition.
1363 A I<context> is one of those defined in the Unicode standard.
1364 For Unicode 5.1, they are defined in Section 3.13 C<Default Case Operations>
1366 L<http://www.unicode.org/versions/Unicode5.1.0/>.
1367 These are for context-sensitive casing.
1371 The hash described above is returned for locale-independent casing, where
1372 at least one of the mappings has length longer than one. If C<undef> is
1373 returned, the code point may have mappings, but if so, all are length one,
1374 and are returned by L</charinfo()>.
1375 Note that when this function does return a value, it will be for the complete
1376 set of mappings for a code point, even those whose length is one.
1378 If there are additional casing rules that apply only in certain locales,
1379 an additional key for each will be defined in the returned hash. Each such key
1380 will be its locale name, defined as a 2-letter ISO 3166 country code, possibly
1381 followed by a "_" and a 2-letter ISO language code (possibly followed by a "_"
1382 and a variant code). You can find the lists of all possible locales, see
1383 L<Locale::Country> and L<Locale::Language>.
1384 (In Unicode 6.0, the only locales returned by this function
1385 are C<lt>, C<tr>, and C<az>.)
1387 Each locale key is a reference to a hash that has the form above, and gives
1388 the casing rules for that particular locale, which take precedence over the
1389 locale-independent ones when in that locale.
1391 If the only casing for a code point is locale-dependent, then the returned
1392 hash will not have any of the base keys, like C<code>, C<upper>, etc., but
1393 will contain only locale keys.
1395 For more information about case mappings see
1396 L<http://www.unicode.org/unicode/reports/tr21/>
1403 unless (%CASESPEC) {
1404 UnicodeVersion() unless defined $v_unicode_version;
1405 if ($v_unicode_version lt v2.1.8) {
1408 elsif (openunicode(\$CASESPECFH, "SpecialCasing.txt")) {
1411 while (<$CASESPECFH>) {
1412 if (/^([0-9A-F]+); ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; (\w+(?: \w+)*)?/) {
1414 my ($hexcode, $lower, $title, $upper, $condition) =
1415 ($1, $2, $3, $4, $5);
1416 if (! IS_ASCII_PLATFORM) { # Remap entry to native
1417 foreach my $var_ref (\$hexcode,
1422 next unless defined $$var_ref;
1423 $$var_ref = join " ",
1424 map { sprintf("%04X",
1425 utf8::unicode_to_native(hex $_)) }
1426 split " ", $$var_ref;
1430 my $code = hex($hexcode);
1432 # In 2.1.8, there were duplicate entries; ignore all but
1433 # the first one -- there were no conditions in the file
1435 if (exists $CASESPEC{$code} && $v_unicode_version ne v2.1.8)
1437 if (exists $CASESPEC{$code}->{code}) {
1442 @{$CASESPEC{$code}}{qw(lower
1446 if (defined $oldcondition) {
1448 ($oldcondition =~ /^([a-z][a-z](?:_\S+)?)/);
1449 delete $CASESPEC{$code};
1450 $CASESPEC{$code}->{$oldlocale} =
1455 condition => $oldcondition };
1459 ($condition =~ /^([a-z][a-z](?:_\S+)?)/);
1460 $CASESPEC{$code}->{$locale} =
1465 condition => $condition };
1472 condition => $condition };
1483 my $code = _getcode($arg);
1484 croak __PACKAGE__, "::casespec: unknown code '$arg'"
1485 unless defined $code;
1487 _casespec() unless %CASESPEC;
1489 return ref $CASESPEC{$code} ? _dclone $CASESPEC{$code} : $CASESPEC{$code};
1492 =head2 B<namedseq()>
1494 use Unicode::UCD 'namedseq';
1496 my $namedseq = namedseq("KATAKANA LETTER AINU P");
1497 my @namedseq = namedseq("KATAKANA LETTER AINU P");
1498 my %namedseq = namedseq();
1500 If used with a single argument in a scalar context, returns the string
1501 consisting of the code points of the named sequence, or C<undef> if no
1502 named sequence by that name exists. If used with a single argument in
1503 a list context, it returns the list of the ordinals of the code points.
1506 arguments in a list context, it returns a hash with the names of all the
1507 named sequences as the keys and their sequences as strings as
1508 the values. Otherwise, it returns C<undef> or an empty list depending
1511 This function only operates on officially approved (not provisional) named
1514 Note that as of Perl 5.14, C<\N{KATAKANA LETTER AINU P}> will insert the named
1515 sequence into double-quoted strings, and C<charnames::string_vianame("KATAKANA
1516 LETTER AINU P")> will return the same string this function does, but will also
1517 operate on character names that aren't named sequences, without you having to
1518 know which are which. See L<charnames>.
1525 unless (%NAMEDSEQ) {
1526 if (openunicode(\$NAMEDSEQFH, "Name.pl")) {
1529 while (<$NAMEDSEQFH>) {
1530 if (/^ [0-9A-F]+ \ /x) {
1532 my ($sequence, $name) = split /\t/;
1533 my @s = map { chr(hex($_)) } split(' ', $sequence);
1534 $NAMEDSEQ{$name} = join("", @s);
1544 # Use charnames::string_vianame() which now returns this information,
1545 # unless the caller wants the hash returned, in which case we read it in,
1546 # and thereafter use it instead of calling charnames, as it is faster.
1548 my $wantarray = wantarray();
1549 if (defined $wantarray) {
1552 _namedseq() unless %NAMEDSEQ;
1557 $s = $NAMEDSEQ{ $_[0] };
1560 $s = charnames::string_vianame($_[0]);
1562 return defined $s ? map { ord($_) } split('', $s) : ();
1565 return $NAMEDSEQ{ $_[0] } if %NAMEDSEQ;
1566 return charnames::string_vianame($_[0]);
1575 my @numbers = _read_table("To/Nv.pl");
1576 foreach my $entry (@numbers) {
1577 my ($start, $end, $value) = @$entry;
1579 # If value contains a slash, convert to decimal, add a reverse hash
1581 if ((my @rational = split /\//, $value) == 2) {
1582 my $real = $rational[0] / $rational[1];
1583 $real_to_rational{$real} = $value;
1586 # Should only be single element, but just in case...
1587 for my $i ($start .. $end) {
1588 $NUMERIC{$i} = $value;
1592 # The values require adjusting, as is in 'a' format
1593 for my $i ($start .. $end) {
1594 $NUMERIC{$i} = $value + $i - $start;
1599 # Decided unsafe to use these that aren't officially part of the Unicode
1602 #my $pi = acos(-1.0);
1603 #$NUMERIC{0x03C0} = $pi;
1605 # Euler's constant, not to be confused with Euler's number
1606 #$NUMERIC{0x2107} = 0.57721566490153286060651209008240243104215933593992;
1609 #$NUMERIC{0x212F} = 2.7182818284590452353602874713526624977572;
1618 use Unicode::UCD 'num';
1620 my $val = num("123");
1621 my $one_quarter = num("\N{VULGAR FRACTION 1/4}");
1623 C<num()> returns the numeric value of the input Unicode string; or C<undef> if it
1624 doesn't think the entire string has a completely valid, safe numeric value.
1626 If the string is just one character in length, the Unicode numeric value
1627 is returned if it has one, or C<undef> otherwise. Note that this need
1628 not be a whole number. C<num("\N{TIBETAN DIGIT HALF ZERO}")>, for
1629 example returns -0.5.
1633 #A few characters to which Unicode doesn't officially
1634 #assign a numeric value are considered numeric by C<num>.
1637 # EULER CONSTANT 0.5772... (this is NOT Euler's number)
1638 # SCRIPT SMALL E 2.71828... (this IS Euler's number)
1639 # GREEK SMALL LETTER PI 3.14159...
1643 If the string is more than one character, C<undef> is returned unless
1644 all its characters are decimal digits (that is, they would match C<\d+>),
1645 from the same script. For example if you have an ASCII '0' and a Bengali
1646 '3', mixed together, they aren't considered a valid number, and C<undef>
1647 is returned. A further restriction is that the digits all have to be of
1648 the same form. A half-width digit mixed with a full-width one will
1649 return C<undef>. The Arabic script has two sets of digits; C<num> will
1650 return C<undef> unless all the digits in the string come from the same
1653 C<num> errs on the side of safety, and there may be valid strings of
1654 decimal digits that it doesn't recognize. Note that Unicode defines
1655 a number of "digit" characters that aren't "decimal digit" characters.
1656 "Decimal digits" have the property that they have a positional value, i.e.,
1657 there is a units position, a 10's position, a 100's, etc, AND they are
1658 arranged in Unicode in blocks of 10 contiguous code points. The Chinese
1659 digits, for example, are not in such a contiguous block, and so Unicode
1660 doesn't view them as decimal digits, but merely digits, and so C<\d> will not
1661 match them. A single-character string containing one of these digits will
1662 have its decimal value returned by C<num>, but any longer string containing
1663 only these digits will return C<undef>.
1665 Strings of multiple sub- and superscripts are not recognized as numbers. You
1666 can use either of the compatibility decompositions in Unicode::Normalize to
1667 change these into digits, and then call C<num> on the result.
1671 # To handle sub, superscripts, this could if called in list context,
1672 # consider those, and return the <decomposition> type in the second
1678 _numeric unless %NUMERIC;
1680 my $length = length($string);
1681 return $NUMERIC{ord($string)} if $length == 1;
1682 return if $string =~ /\D/;
1683 my $first_ord = ord(substr($string, 0, 1));
1684 my $value = $NUMERIC{$first_ord};
1686 # To be a valid decimal number, it should be in a block of 10 consecutive
1687 # characters, whose values are 0, 1, 2, ... 9. Therefore this digit's
1688 # value is its offset in that block from the character that means zero.
1689 my $zero_ord = $first_ord - $value;
1691 # Unicode 6.0 instituted the rule that only digits in a consecutive
1692 # block of 10 would be considered decimal digits. If this is an earlier
1693 # release, we verify that this first character is a member of such a
1694 # block. That is, that the block of characters surrounding this one
1695 # consists of all \d characters whose numeric values are the expected
1697 UnicodeVersion() unless defined $v_unicode_version;
1698 if ($v_unicode_version lt v6.0.0) {
1699 for my $i (0 .. 9) {
1700 my $ord = $zero_ord + $i;
1701 return unless chr($ord) =~ /\d/;
1702 my $numeric = $NUMERIC{$ord};
1703 return unless defined $numeric;
1704 return unless $numeric == $i;
1708 for my $i (1 .. $length -1) {
1710 # Here we know either by verifying, or by fact of the first character
1711 # being a \d in Unicode 6.0 or later, that any character between the
1712 # character that means 0, and 9 positions above it must be \d, and
1713 # must have its value correspond to its offset from the zero. Any
1714 # characters outside these 10 do not form a legal number for this
1716 my $ord = ord(substr($string, $i, 1));
1717 my $digit = $ord - $zero_ord;
1718 return unless $digit >= 0 && $digit <= 9;
1719 $value = $value * 10 + $digit;
1727 =head2 B<prop_aliases()>
1729 use Unicode::UCD 'prop_aliases';
1731 my ($short_name, $full_name, @other_names) = prop_aliases("space");
1732 my $same_full_name = prop_aliases("Space"); # Scalar context
1733 my ($same_short_name) = prop_aliases("Space"); # gets 0th element
1734 print "The full name is $full_name\n";
1735 print "The short name is $short_name\n";
1736 print "The other aliases are: ", join(", ", @other_names), "\n";
1739 The full name is White_Space
1740 The short name is WSpace
1741 The other aliases are: Space
1743 Most Unicode properties have several synonymous names. Typically, there is at
1744 least a short name, convenient to type, and a long name that more fully
1745 describes the property, and hence is more easily understood.
1747 If you know one name for a Unicode property, you can use C<prop_aliases> to find
1748 either the long name (when called in scalar context), or a list of all of the
1749 names, somewhat ordered so that the short name is in the 0th element, the long
1750 name in the next element, and any other synonyms are in the remaining
1751 elements, in no particular order.
1753 The long name is returned in a form nicely capitalized, suitable for printing.
1755 The input parameter name is loosely matched, which means that white space,
1756 hyphens, and underscores are ignored (except for the trailing underscore in
1757 the old_form grandfathered-in C<"L_">, which is better written as C<"LC">, and
1758 both of which mean C<General_Category=Cased Letter>).
1760 If the name is unknown, C<undef> is returned (or an empty list in list
1761 context). Note that Perl typically recognizes property names in regular
1762 expressions with an optional C<"Is_>" (with or without the underscore)
1763 prefixed to them, such as C<\p{isgc=punct}>. This function does not recognize
1764 those in the input, returning C<undef>. Nor are they included in the output
1765 as possible synonyms.
1767 C<prop_aliases> does know about the Perl extensions to Unicode properties,
1768 such as C<Any> and C<XPosixAlpha>, and the single form equivalents to Unicode
1769 properties such as C<XDigit>, C<Greek>, C<In_Greek>, and C<Is_Greek>. The
1770 final example demonstrates that the C<"Is_"> prefix is recognized for these
1771 extensions; it is needed to resolve ambiguities. For example,
1772 C<prop_aliases('lc')> returns the list C<(lc, Lowercase_Mapping)>, but
1773 C<prop_aliases('islc')> returns C<(Is_LC, Cased_Letter)>. This is
1774 because C<islc> is a Perl extension which is short for
1775 C<General_Category=Cased Letter>. The lists returned for the Perl extensions
1776 will not include the C<"Is_"> prefix (whether or not the input had it) unless
1777 needed to resolve ambiguities, as shown in the C<"islc"> example, where the
1778 returned list had one element containing C<"Is_">, and the other without.
1780 It is also possible for the reverse to happen: C<prop_aliases('isc')> returns
1781 the list C<(isc, ISO_Comment)>; whereas C<prop_aliases('c')> returns
1782 C<(C, Other)> (the latter being a Perl extension meaning
1783 C<General_Category=Other>.
1784 L<perluniprops/Properties accessible through Unicode::UCD> lists the available
1785 forms, including which ones are discouraged from use.
1787 Those discouraged forms are accepted as input to C<prop_aliases>, but are not
1788 returned in the lists. C<prop_aliases('isL&')> and C<prop_aliases('isL_')>,
1789 which are old synonyms for C<"Is_LC"> and should not be used in new code, are
1790 examples of this. These both return C<(Is_LC, Cased_Letter)>. Thus this
1791 function allows you to take a discouraged form, and find its acceptable
1792 alternatives. The same goes with single-form Block property equivalences.
1793 Only the forms that begin with C<"In_"> are not discouraged; if you pass
1794 C<prop_aliases> a discouraged form, you will get back the equivalent ones that
1795 begin with C<"In_">. It will otherwise look like a new-style block name (see.
1796 L</Old-style versus new-style block names>).
1798 C<prop_aliases> does not know about any user-defined properties, and will
1799 return C<undef> if called with one of those. Likewise for Perl internal
1800 properties, with the exception of "Perl_Decimal_Digit" which it does know
1801 about (and which is documented below in L</prop_invmap()>).
1805 # It may be that there are use cases where the discouraged forms should be
1806 # returned. If that comes up, an optional boolean second parameter to the
1807 # function could be created, for example.
1809 # These are created by mktables for this routine and stored in unicore/UCD.pl
1810 # where their structures are described.
1811 our %string_property_loose_to_name;
1812 our %ambiguous_names;
1813 our %loose_perlprop_to_name;
1816 sub prop_aliases ($) {
1818 return unless defined $prop;
1820 require "unicore/UCD.pl";
1821 require "unicore/Heavy.pl";
1822 require "utf8_heavy.pl";
1824 # The property name may be loosely or strictly matched; we don't know yet.
1825 # But both types use lower-case.
1828 # It is loosely matched if its lower case isn't known to be strict.
1830 if (! exists $utf8::stricter_to_file_of{$prop}) {
1831 my $loose = utf8::_loose_name($prop);
1833 # There is a hash that converts from any loose name to its standard
1834 # form, mapping all synonyms for a name to one name that can be used
1835 # as a key into another hash. The whole concept is for memory
1836 # savings, as the second hash doesn't have to have all the
1837 # combinations. Actually, there are two hashes that do the
1838 # converstion. One is used in utf8_heavy.pl (stored in Heavy.pl) for
1839 # looking up properties matchable in regexes. This function needs to
1840 # access string properties, which aren't available in regexes, so a
1841 # second conversion hash is made for them (stored in UCD.pl). Look in
1842 # the string one now, as the rest can have an optional 'is' prefix,
1843 # which these don't.
1844 if (exists $string_property_loose_to_name{$loose}) {
1846 # Convert to its standard loose name.
1847 $prop = $string_property_loose_to_name{$loose};
1850 my $retrying = 0; # bool. ? Has an initial 'is' been stripped
1852 if (exists $utf8::loose_property_name_of{$loose}
1854 || ! exists $ambiguous_names{$loose}))
1856 # Found an entry giving the standard form. We don't get here
1857 # (in the test above) when we've stripped off an
1858 # 'is' and the result is an ambiguous name. That is because
1859 # these are official Unicode properties (though Perl can have
1860 # an optional 'is' prefix meaning the official property), and
1861 # all ambiguous cases involve a Perl single-form extension
1862 # for the gc, script, or block properties, and the stripped
1863 # 'is' means that they mean one of those, and not one of
1865 $prop = $utf8::loose_property_name_of{$loose};
1867 elsif (exists $loose_perlprop_to_name{$loose}) {
1869 # This hash is specifically for this function to list Perl
1870 # extensions that aren't in the earlier hashes. If there is
1871 # only one element, the short and long names are identical.
1872 # Otherwise the form is already in the same form as
1873 # %prop_aliases, which is handled at the end of the function.
1874 $list_ref = $loose_perlprop_to_name{$loose};
1875 if (@$list_ref == 1) {
1876 my @list = ($list_ref->[0], $list_ref->[0]);
1880 elsif (! exists $utf8::loose_to_file_of{$loose}) {
1882 # loose_to_file_of is a complete list of loose names. If not
1883 # there, the input is unknown.
1886 elsif ($loose =~ / [:=] /x) {
1888 # Here we found the name but not its aliases, so it has to
1889 # exist. Exclude property-value combinations. (This shows up
1890 # for something like ccc=vr which matches loosely, but is a
1891 # synonym for ccc=9 which matches only strictly.
1896 # Here it has to exist, and isn't a property-value
1897 # combination. This means it must be one of the Perl
1898 # single-form extensions. First see if it is for a
1899 # property-value combination in one of the following
1902 foreach my $property ("gc", "script") {
1903 @list = prop_value_aliases($property, $loose);
1908 # Here, it is one of those property-value combination
1909 # single-form synonyms. There are ambiguities with some
1910 # of these. Check against the list for these, and adjust
1912 for my $i (0 .. @list -1) {
1913 if (exists $ambiguous_names
1914 {utf8::_loose_name(lc $list[$i])})
1916 # The ambiguity is resolved by toggling whether or
1917 # not it has an 'is' prefix
1918 $list[$i] =~ s/^Is_// or $list[$i] =~ s/^/Is_/;
1924 # Here, it wasn't one of the gc or script single-form
1925 # extensions. It could be a block property single-form
1926 # extension. An 'in' prefix definitely means that, and should
1927 # be looked up without the prefix. However, starting in
1928 # Unicode 6.1, we have to special case 'indic...', as there
1929 # is a property that begins with that name. We shouldn't
1930 # strip the 'in' from that. I'm (khw) generalizing this to
1931 # 'indic' instead of the single property, because I suspect
1932 # that others of this class may come along in the future.
1933 # However, this could backfire and a block created whose name
1934 # begins with 'dic...', and we would want to strip the 'in'.
1935 # At which point this would have to be tweaked.
1936 my $began_with_in = $loose =~ s/^in(?!dic)//;
1937 @list = prop_value_aliases("block", $loose);
1939 map { $_ =~ s/^/In_/ } @list;
1943 # Here still haven't found it. The last opportunity for it
1944 # being valid is only if it began with 'is'. We retry without
1945 # the 'is', setting a flag to that effect so that we don't
1946 # accept things that begin with 'isis...'
1947 if (! $retrying && ! $began_with_in && $loose =~ s/^is//) {
1952 # Here, didn't find it. Since it was in %loose_to_file_of, we
1953 # should have been able to find it.
1954 carp __PACKAGE__, "::prop_aliases: Unexpectedly could not find '$prop'. Send bug report to perlbug\@perl.org";
1961 # Here, we have set $prop to a standard form name of the input. Look
1962 # it up in the structure created by mktables for this purpose, which
1963 # contains both strict and loosely matched properties. Avoid
1965 $list_ref = $prop_aliases{$prop} if exists $prop_aliases{$prop};
1966 return unless $list_ref;
1969 # The full name is in element 1.
1970 return $list_ref->[1] unless wantarray;
1972 return @{_dclone $list_ref};
1977 =head2 B<prop_values()>
1979 use Unicode::UCD 'prop_values';
1981 print "AHex values are: ", join(", ", prop_values("AHex")),
1984 AHex values are: N, Y
1986 Some Unicode properties have a restricted set of legal values. For example,
1987 all binary properties are restricted to just C<true> or C<false>; and there
1988 are only a few dozen possible General Categories. Use C<prop_values>
1989 to find out if a given property is one such, and if so, to get a list of the
1992 print join ", ", prop_values("NFC_Quick_Check");
1996 If the property doesn't have such a restricted set, C<undef> is returned.
1998 There are usually several synonyms for each possible value. Use
1999 L</prop_value_aliases()> to access those.
2001 Case, white space, hyphens, and underscores are ignored in the input property
2002 name (except for the trailing underscore in the old-form grandfathered-in
2003 general category property value C<"L_">, which is better written as C<"LC">).
2005 If the property name is unknown, C<undef> is returned. Note that Perl typically
2006 recognizes property names in regular expressions with an optional C<"Is_>"
2007 (with or without the underscore) prefixed to them, such as C<\p{isgc=punct}>.
2008 This function does not recognize those in the property parameter, returning
2011 For the block property, new-style block names are returned (see
2012 L</Old-style versus new-style block names>).
2014 C<prop_values> does not know about any user-defined properties, and
2015 will return C<undef> if called with one of those.
2019 # These are created by mktables for this module and stored in unicore/UCD.pl
2020 # where their structures are described.
2021 our %loose_to_standard_value;
2022 our %prop_value_aliases;
2024 sub prop_values ($) {
2026 return undef unless defined $prop;
2028 require "unicore/UCD.pl";
2029 require "utf8_heavy.pl";
2031 # Find the property name synonym that's used as the key in other hashes,
2032 # which is element 0 in the returned list.
2033 ($prop) = prop_aliases($prop);
2034 return undef if ! $prop;
2035 $prop = utf8::_loose_name(lc $prop);
2037 # Here is a legal property.
2038 return undef unless exists $prop_value_aliases{$prop};
2040 foreach my $value_key (sort { lc $a cmp lc $b }
2041 keys %{$prop_value_aliases{$prop}})
2043 push @return, $prop_value_aliases{$prop}{$value_key}[0];
2050 =head2 B<prop_value_aliases()>
2052 use Unicode::UCD 'prop_value_aliases';
2054 my ($short_name, $full_name, @other_names)
2055 = prop_value_aliases("Gc", "Punct");
2056 my $same_full_name = prop_value_aliases("Gc", "P"); # Scalar cntxt
2057 my ($same_short_name) = prop_value_aliases("Gc", "P"); # gets 0th
2059 print "The full name is $full_name\n";
2060 print "The short name is $short_name\n";
2061 print "The other aliases are: ", join(", ", @other_names), "\n";
2064 The full name is Punctuation
2066 The other aliases are: Punct
2068 Some Unicode properties have a restricted set of legal values. For example,
2069 all binary properties are restricted to just C<true> or C<false>; and there
2070 are only a few dozen possible General Categories.
2072 You can use L</prop_values()> to find out if a given property is one which has
2073 a restricted set of values, and if so, what those values are. But usually
2074 each value actually has several synonyms. For example, in Unicode binary
2075 properties, I<truth> can be represented by any of the strings "Y", "Yes", "T",
2076 or "True"; and the General Category "Punctuation" by that string, or "Punct",
2079 Like property names, there is typically at least a short name for each such
2080 property-value, and a long name. If you know any name of the property-value
2081 (which you can get by L</prop_values()>, you can use C<prop_value_aliases>()
2082 to get the long name (when called in scalar context), or a list of all the
2083 names, with the short name in the 0th element, the long name in the next
2084 element, and any other synonyms in the remaining elements, in no particular
2085 order, except that any all-numeric synonyms will be last.
2087 The long name is returned in a form nicely capitalized, suitable for printing.
2089 Case, white space, hyphens, and underscores are ignored in the input parameters
2090 (except for the trailing underscore in the old-form grandfathered-in general
2091 category property value C<"L_">, which is better written as C<"LC">).
2093 If either name is unknown, C<undef> is returned. Note that Perl typically
2094 recognizes property names in regular expressions with an optional C<"Is_>"
2095 (with or without the underscore) prefixed to them, such as C<\p{isgc=punct}>.
2096 This function does not recognize those in the property parameter, returning
2099 If called with a property that doesn't have synonyms for its values, it
2100 returns the input value, possibly normalized with capitalization and
2101 underscores, but not necessarily checking that the input value is valid.
2103 For the block property, new-style block names are returned (see
2104 L</Old-style versus new-style block names>).
2106 To find the synonyms for single-forms, such as C<\p{Any}>, use
2107 L</prop_aliases()> instead.
2109 C<prop_value_aliases> does not know about any user-defined properties, and
2110 will return C<undef> if called with one of those.
2114 sub prop_value_aliases ($$) {
2115 my ($prop, $value) = @_;
2116 return unless defined $prop && defined $value;
2118 require "unicore/UCD.pl";
2119 require "utf8_heavy.pl";
2121 # Find the property name synonym that's used as the key in other hashes,
2122 # which is element 0 in the returned list.
2123 ($prop) = prop_aliases($prop);
2125 $prop = utf8::_loose_name(lc $prop);
2127 # Here is a legal property, but the hash below (created by mktables for
2128 # this purpose) only knows about the properties that have a very finite
2129 # number of potential values, that is not ones whose value could be
2130 # anything, like most (if not all) string properties. These don't have
2131 # synonyms anyway. Simply return the input. For example, there is no
2132 # synonym for ('Uppercase_Mapping', A').
2133 if (! exists $prop_value_aliases{$prop}) {
2135 # Here, we have a legal property, but an unknown value. Since the
2136 # property is legal, if it isn't in the prop_aliases hash, it must be
2137 # a Perl-extension All perl extensions are binary, hence are
2138 # enumerateds, which means that we know that the input unknown value
2140 return if ! exists $Unicode::UCD::prop_aliases{$prop};
2142 # Otherwise, we assume it's valid, as documented.
2146 # The value name may be loosely or strictly matched; we don't know yet.
2147 # But both types use lower-case.
2150 # If the name isn't found under loose matching, it certainly won't be
2151 # found under strict
2152 my $loose_value = utf8::_loose_name($value);
2153 return unless exists $loose_to_standard_value{"$prop=$loose_value"};
2155 # Similarly if the combination under loose matching doesn't exist, it
2156 # won't exist under strict.
2157 my $standard_value = $loose_to_standard_value{"$prop=$loose_value"};
2158 return unless exists $prop_value_aliases{$prop}{$standard_value};
2160 # Here we did find a combination under loose matching rules. But it could
2161 # be that is a strict property match that shouldn't have matched.
2162 # %prop_value_aliases is set up so that the strict matches will appear as
2163 # if they were in loose form. Thus, if the non-loose version is legal,
2164 # we're ok, can skip the further check.
2165 if (! exists $utf8::stricter_to_file_of{"$prop=$value"}
2167 # We're also ok and skip the further check if value loosely matches.
2168 # mktables has verified that no strict name under loose rules maps to
2169 # an existing loose name. This code relies on the very limited
2170 # circumstances that strict names can be here. Strict name matching
2171 # happens under two conditions:
2172 # 1) when the name begins with an underscore. But this function
2173 # doesn't accept those, and %prop_value_aliases doesn't have
2175 # 2) When the values are numeric, in which case we need to look
2176 # further, but their squeezed-out loose values will be in
2177 # %stricter_to_file_of
2178 && exists $utf8::stricter_to_file_of{"$prop=$loose_value"})
2180 # The only thing that's legal loosely under strict is that can have an
2181 # underscore between digit pairs XXX
2182 while ($value =~ s/(\d)_(\d)/$1$2/g) {}
2183 return unless exists $utf8::stricter_to_file_of{"$prop=$value"};
2186 # Here, we know that the combination exists. Return it.
2187 my $list_ref = $prop_value_aliases{$prop}{$standard_value};
2188 if (@$list_ref > 1) {
2189 # The full name is in element 1.
2190 return $list_ref->[1] unless wantarray;
2192 return @{_dclone $list_ref};
2195 return $list_ref->[0] unless wantarray;
2197 # Only 1 element means that it repeats
2198 return ( $list_ref->[0], $list_ref->[0] );
2201 # All 1 bits is the largest possible UV.
2202 $Unicode::UCD::MAX_CP = ~0;
2206 =head2 B<prop_invlist()>
2208 C<prop_invlist> returns an inversion list (described below) that defines all the
2209 code points for the binary Unicode property (or "property=value" pair) given
2210 by the input parameter string:
2213 use Unicode::UCD 'prop_invlist';
2214 say join ", ", prop_invlist("Any");
2219 If the input is unknown C<undef> is returned in scalar context; an empty-list
2220 in list context. If the input is known, the number of elements in
2221 the list is returned if called in scalar context.
2223 L<perluniprops|perluniprops/Properties accessible through \p{} and \P{}> gives
2224 the list of properties that this function accepts, as well as all the possible
2225 forms for them (including with the optional "Is_" prefixes). (Except this
2226 function doesn't accept any Perl-internal properties, some of which are listed
2227 there.) This function uses the same loose or tighter matching rules for
2228 resolving the input property's name as is done for regular expressions. These
2229 are also specified in L<perluniprops|perluniprops/Properties accessible
2230 through \p{} and \P{}>. Examples of using the "property=value" form are:
2232 say join ", ", prop_invlist("Script=Shavian");
2237 say join ", ", prop_invlist("ASCII_Hex_Digit=No");
2240 0, 48, 58, 65, 71, 97, 103
2242 say join ", ", prop_invlist("ASCII_Hex_Digit=Yes");
2245 48, 58, 65, 71, 97, 103
2247 Inversion lists are a compact way of specifying Unicode property-value
2248 definitions. The 0th item in the list is the lowest code point that has the
2249 property-value. The next item (item [1]) is the lowest code point beyond that
2250 one that does NOT have the property-value. And the next item beyond that
2251 ([2]) is the lowest code point beyond that one that does have the
2252 property-value, and so on. Put another way, each element in the list gives
2253 the beginning of a range that has the property-value (for even numbered
2254 elements), or doesn't have the property-value (for odd numbered elements).
2255 The name for this data structure stems from the fact that each element in the
2256 list toggles (or inverts) whether the corresponding range is or isn't on the
2259 In the final example above, the first ASCII Hex digit is code point 48, the
2260 character "0", and all code points from it through 57 (a "9") are ASCII hex
2261 digits. Code points 58 through 64 aren't, but 65 (an "A") through 70 (an "F")
2262 are, as are 97 ("a") through 102 ("f"). 103 starts a range of code points
2263 that aren't ASCII hex digits. That range extends to infinity, which on your
2264 computer can be found in the variable C<$Unicode::UCD::MAX_CP>. (This
2265 variable is as close to infinity as Perl can get on your platform, and may be
2266 too high for some operations to work; you may wish to use a smaller number for
2269 Note that the inversion lists returned by this function can possibly include
2270 non-Unicode code points, that is anything above 0x10FFFF. Unicode properties
2271 are not defined on such code points. You might wish to change the output to
2272 not include these. Simply add 0x110000 at the end of the non-empty returned
2273 list if it isn't already that value; and pop that value if it is; like:
2275 my @list = prop_invlist("foo");
2277 if ($list[-1] == 0x110000) {
2278 pop @list; # Defeat the turning on for above Unicode
2281 push @list, 0x110000; # Turn off for above Unicode
2285 It is a simple matter to expand out an inversion list to a full list of all
2286 code points that have the property-value:
2288 my @invlist = prop_invlist($property_name);
2289 die "empty" unless @invlist;
2291 for (my $i = 0; $i < @invlist; $i += 2) {
2292 my $upper = ($i + 1) < @invlist
2293 ? $invlist[$i+1] - 1 # In range
2294 : $Unicode::UCD::MAX_CP; # To infinity. You may want
2295 # to stop much much earlier;
2296 # going this high may expose
2297 # perl deficiencies with very
2299 for my $j ($invlist[$i] .. $upper) {
2300 push @full_list, $j;
2304 C<prop_invlist> does not know about any user-defined nor Perl internal-only
2305 properties, and will return C<undef> if called with one of those.
2307 The L</search_invlist()> function is provided for finding a code point within
2312 # User-defined properties could be handled with some changes to utf8_heavy.pl;
2313 # and implementing here of dealing with EXTRAS. If done, consideration should
2314 # be given to the fact that the user subroutine could return different results
2315 # with each call; security issues need to be thought about.
2317 # These are created by mktables for this routine and stored in unicore/UCD.pl
2318 # where their structures are described.
2319 our %loose_defaults;
2320 our $MAX_UNICODE_CODEPOINT;
2322 sub prop_invlist ($;$) {
2325 # Undocumented way to get at Perl internal properties; it may be changed
2326 # or removed without notice at any time.
2327 my $internal_ok = defined $_[1] && $_[1] eq '_perl_core_internal_ok';
2329 return if ! defined $prop;
2331 require "utf8_heavy.pl";
2333 # Warnings for these are only for regexes, so not applicable to us
2334 no warnings 'deprecated';
2336 # Get the swash definition of the property-value.
2337 my $swash = utf8::SWASHNEW(__PACKAGE__, $prop, undef, 1, 0);
2339 # Fail if not found, or isn't a boolean property-value, or is a
2340 # user-defined property, or is internal-only.
2343 || $swash->{'BITS'} != 1
2344 || $swash->{'USER_DEFINED'}
2345 || (! $internal_ok && $prop =~ /^\s*_/);
2347 if ($swash->{'EXTRAS'}) {
2348 carp __PACKAGE__, "::prop_invlist: swash returned for $prop unexpectedly has EXTRAS magic";
2351 if ($swash->{'SPECIALS'}) {
2352 carp __PACKAGE__, "::prop_invlist: swash returned for $prop unexpectedly has SPECIALS magic";
2358 if ($swash->{'LIST'} =~ /^V/) {
2360 # A 'V' as the first character marks the input as already an inversion
2361 # list, in which case, all we need to do is put the remaining lines
2363 @invlist = split "\n", $swash->{'LIST'} =~ s/ \s* (?: \# .* )? $ //xmgr;
2367 # The input lines look like:
2371 # Split into lines, stripped of trailing comments
2372 foreach my $range (split "\n",
2373 $swash->{'LIST'} =~ s/ \s* (?: \# .* )? $ //xmgr)
2375 # And find the beginning and end of the range on the line
2376 my ($hex_begin, $hex_end) = split "\t", $range;
2377 my $begin = hex $hex_begin;
2379 # If the new range merely extends the old, we remove the marker
2380 # created the last time through the loop for the old's end, which
2381 # causes the new one's end to be used instead.
2382 if (@invlist && $begin == $invlist[-1]) {
2386 # Add the beginning of the range
2387 push @invlist, $begin;
2390 if (defined $hex_end) { # The next item starts with the code point 1
2391 # beyond the end of the range.
2392 no warnings 'portable';
2393 my $end = hex $hex_end;
2394 last if $end == $Unicode::UCD::MAX_CP;
2395 push @invlist, $end + 1;
2397 else { # No end of range, is a single code point.
2398 push @invlist, $begin + 1;
2403 # Could need to be inverted: add or subtract a 0 at the beginning of the
2405 if ($swash->{'INVERT_IT'}) {
2406 if (@invlist && $invlist[0] == 0) {
2410 unshift @invlist, 0;
2419 =head2 B<prop_invmap()>
2421 use Unicode::UCD 'prop_invmap';
2422 my ($list_ref, $map_ref, $format, $default)
2423 = prop_invmap("General Category");
2425 C<prop_invmap> is used to get the complete mapping definition for a property,
2426 in the form of an inversion map. An inversion map consists of two parallel
2427 arrays. One is an ordered list of code points that mark range beginnings, and
2428 the other gives the value (or mapping) that all code points in the
2429 corresponding range have.
2431 C<prop_invmap> is called with the name of the desired property. The name is
2432 loosely matched, meaning that differences in case, white-space, hyphens, and
2433 underscores are not meaningful (except for the trailing underscore in the
2434 old-form grandfathered-in property C<"L_">, which is better written as C<"LC">,
2435 or even better, C<"Gc=LC">).
2437 Many Unicode properties have more than one name (or alias). C<prop_invmap>
2438 understands all of these, including Perl extensions to them. Ambiguities are
2439 resolved as described above for L</prop_aliases()>. The Perl internal
2440 property "Perl_Decimal_Digit, described below, is also accepted. An empty
2441 list is returned if the property name is unknown.
2442 See L<perluniprops/Properties accessible through Unicode::UCD> for the
2443 properties acceptable as inputs to this function.
2445 It is a fatal error to call this function except in list context.
2447 In addition to the two arrays that form the inversion map, C<prop_invmap>
2448 returns two other values; one is a scalar that gives some details as to the
2449 format of the entries of the map array; the other is a default value, useful
2450 in maps whose format name begins with the letter C<"a">, as described
2451 L<below in its subsection|/a>; and for specialized purposes, such as
2452 converting to another data structure, described at the end of this main
2455 This means that C<prop_invmap> returns a 4 element list. For example,
2457 my ($blocks_ranges_ref, $blocks_maps_ref, $format, $default)
2458 = prop_invmap("Block");
2460 In this call, the two arrays will be populated as shown below (for Unicode
2463 Index @blocks_ranges @blocks_maps
2464 0 0x0000 Basic Latin
2465 1 0x0080 Latin-1 Supplement
2466 2 0x0100 Latin Extended-A
2467 3 0x0180 Latin Extended-B
2468 4 0x0250 IPA Extensions
2469 5 0x02B0 Spacing Modifier Letters
2470 6 0x0300 Combining Diacritical Marks
2471 7 0x0370 Greek and Coptic
2474 233 0x2B820 No_Block
2475 234 0x2F800 CJK Compatibility Ideographs Supplement
2476 235 0x2FA20 No_Block
2478 237 0xE0080 No_Block
2479 238 0xE0100 Variation Selectors Supplement
2480 239 0xE01F0 No_Block
2481 240 0xF0000 Supplementary Private Use Area-A
2482 241 0x100000 Supplementary Private Use Area-B
2483 242 0x110000 No_Block
2485 The first line (with Index [0]) means that the value for code point 0 is "Basic
2486 Latin". The entry "0x0080" in the @blocks_ranges column in the second line
2487 means that the value from the first line, "Basic Latin", extends to all code
2488 points in the range from 0 up to but not including 0x0080, that is, through
2489 127. In other words, the code points from 0 to 127 are all in the "Basic
2490 Latin" block. Similarly, all code points in the range from 0x0080 up to (but
2491 not including) 0x0100 are in the block named "Latin-1 Supplement", etc.
2492 (Notice that the return is the old-style block names; see L</Old-style versus
2493 new-style block names>).
2495 The final line (with Index [242]) means that the value for all code points above
2496 the legal Unicode maximum code point have the value "No_Block", which is the
2497 term Unicode uses for a non-existing block.
2499 The arrays completely specify the mappings for all possible code points.
2500 The final element in an inversion map returned by this function will always be
2501 for the range that consists of all the code points that aren't legal Unicode,
2502 but that are expressible on the platform. (That is, it starts with code point
2503 0x110000, the first code point above the legal Unicode maximum, and extends to
2504 infinity.) The value for that range will be the same that any typical
2505 unassigned code point has for the specified property. (Certain unassigned
2506 code points are not "typical"; for example the non-character code points, or
2507 those in blocks that are to be written right-to-left. The above-Unicode
2508 range's value is not based on these atypical code points.) It could be argued
2509 that, instead of treating these as unassigned Unicode code points, the value
2510 for this range should be C<undef>. If you wish, you can change the returned
2513 The maps for almost all properties are simple scalars that should be
2515 These values are those given in the Unicode-supplied data files, which may be
2516 inconsistent as to capitalization and as to which synonym for a property-value
2517 is given. The results may be normalized by using the L</prop_value_aliases()>
2520 There are exceptions to the simple scalar maps. Some properties have some
2521 elements in their map list that are themselves lists of scalars; and some
2522 special strings are returned that are not to be interpreted as-is. Element
2523 [2] (placed into C<$format> in the example above) of the returned four element
2524 list tells you if the map has any of these special elements or not, as follows:
2530 means all the elements of the map array are simple scalars, with no special
2531 elements. Almost all properties are like this, like the C<block> example
2536 means that some of the map array elements have the form given by C<"s">, and
2537 the rest are lists of scalars. For example, here is a portion of the output
2538 of calling C<prop_invmap>() with the "Script Extensions" property:
2540 @scripts_ranges @scripts_maps
2543 0x0964 [ Bengali, Devanagari, Gurumukhi, Oriya ]
2547 Here, the code points 0x964 and 0x965 are both used in Bengali,
2548 Devanagari, Gurmukhi, and Oriya, but no other scripts.
2550 The Name_Alias property is also of this form. But each scalar consists of two
2551 components: 1) the name, and 2) the type of alias this is. They are
2552 separated by a colon and a space. In Unicode 6.1, there are several alias types:
2558 indicates that the name is a corrected form for the
2559 original name (which remains valid) for the same code point.
2563 adds a new name for a control character.
2567 is an alternate name for a character
2571 is a name for a character that has been documented but was never in any
2574 =item C<abbreviation>
2576 is a common abbreviation for a character
2580 The lists are ordered (roughly) so the most preferred names come before less
2585 @aliases_ranges @alias_maps
2587 0x009E [ 'PRIVACY MESSAGE: control', 'PM: abbreviation' ]
2588 0x009F [ 'APPLICATION PROGRAM COMMAND: control',
2591 0x00A0 'NBSP: abbreviation'
2593 0x00AD 'SHY: abbreviation'
2595 0x01A2 'LATIN CAPITAL LETTER GHA: correction'
2596 0x01A3 'LATIN SMALL LETTER GHA: correction'
2600 A map to the empty string means that there is no alias defined for the code
2605 is like C<"s"> in that all the map array elements are scalars, but here they are
2606 restricted to all being integers, and some have to be adjusted (hence the name
2607 C<"a">) to get the correct result. For example, in:
2609 my ($uppers_ranges_ref, $uppers_maps_ref, $format, $default)
2610 = prop_invmap("Simple_Uppercase_Mapping");
2612 the returned arrays look like this:
2614 @$uppers_ranges_ref @$uppers_maps_ref Note
2616 97 65 'a' maps to 'A', b => B ...
2618 181 924 MICRO SIGN => Greek Cap MU
2622 and C<$default> is 0.
2624 Let's start with the second line. It says that the uppercase of code point 97
2625 is 65; or C<uc("a")> == "A". But the line is for the entire range of code
2626 points 97 through 122. To get the mapping for any code point in this range,
2627 you take the offset it has from the beginning code point of the range, and add
2628 that to the mapping for that first code point. So, the mapping for 122 ("z")
2629 is derived by taking the offset of 122 from 97 (=25) and adding that to 65,
2630 yielding 90 ("z"). Likewise for everything in between.
2632 Requiring this simple adjustment allows the returned arrays to be
2633 significantly smaller than otherwise, up to a factor of 10, speeding up
2634 searching through them.
2636 Ranges that map to C<$default>, C<"0">, behave somewhat differently. For
2637 these, each code point maps to itself. So, in the first line in the example,
2638 S<C<ord(uc(chr(0)))>> is 0, S<C<ord(uc(chr(1)))>> is 1, ..
2639 S<C<ord(uc(chr(96)))>> is 96.
2643 means that some of the map array elements have the form given by C<"a">, and
2644 the rest are ordered lists of code points.
2647 my ($uppers_ranges_ref, $uppers_maps_ref, $format, $default)
2648 = prop_invmap("Uppercase_Mapping");
2650 the returned arrays look like this:
2652 @$uppers_ranges_ref @$uppers_maps_ref
2659 0x0149 [ 0x02BC 0x004E ]
2664 This is the full Uppercase_Mapping property (as opposed to the
2665 Simple_Uppercase_Mapping given in the example for format C<"a">). The only
2666 difference between the two in the ranges shown is that the code point at
2667 0x0149 (LATIN SMALL LETTER N PRECEDED BY APOSTROPHE) maps to a string of two
2668 characters, 0x02BC (MODIFIER LETTER APOSTROPHE) followed by 0x004E (LATIN
2671 No adjustments are needed to entries that are references to arrays; each such
2672 entry will have exactly one element in its range, so the offset is always 0.
2674 The fourth (index [3]) element (C<$default>) in the list returned for this
2679 This is like C<"a">, but some elements are the empty string, and should not be
2681 The one internal Perl property accessible by C<prop_invmap> is of this type:
2682 "Perl_Decimal_Digit" returns an inversion map which gives the numeric values
2683 that are represented by the Unicode decimal digit characters. Characters that
2684 don't represent decimal digits map to the empty string, like so:
2699 This means that the code points from 0 to 0x2F do not represent decimal digits;
2700 the code point 0x30 (DIGIT ZERO) represents 0; code point 0x31, (DIGIT ONE),
2701 represents 0+1-0 = 1; ... code point 0x39, (DIGIT NINE), represents 0+9-0 = 9;
2702 ... code points 0x3A through 0x65F do not represent decimal digits; 0x660
2703 (ARABIC-INDIC DIGIT ZERO), represents 0; ... 0x07C1 (NKO DIGIT ONE),
2704 represents 0+1-0 = 1 ...
2706 The fourth (index [3]) element (C<$default>) in the list returned for this
2707 format is the empty string.
2711 is a combination of the C<"al"> type and the C<"ae"> type. Some of
2712 the map array elements have the forms given by C<"al">, and
2713 the rest are the empty string. The property C<NFKC_Casefold> has this form.
2714 An example slice is:
2716 @$ranges_ref @$maps_ref Note
2718 0x00AA 97 FEMININE ORDINAL INDICATOR => 'a'
2720 0x00AD SOFT HYPHEN => ""
2722 0x00AF [ 0x0020, 0x0304 ] MACRON => SPACE . COMBINING MACRON
2726 The fourth (index [3]) element (C<$default>) in the list returned for this
2731 means that all the elements of the map array are either rational numbers or
2732 the string C<"NaN">, meaning "Not a Number". A rational number is either an
2733 integer, or two integers separated by a solidus (C<"/">). The second integer
2734 represents the denominator of the division implied by the solidus, and is
2735 actually always positive, so it is guaranteed not to be 0 and to not be
2736 signed. When the element is a plain integer (without the
2737 solidus), it may need to be adjusted to get the correct value by adding the
2738 offset, just as other C<"a"> properties. No adjustment is needed for
2739 fractions, as the range is guaranteed to have just a single element, and so
2740 the offset is always 0.
2742 If you want to convert the returned map to entirely scalar numbers, you
2743 can use something like this:
2745 my ($invlist_ref, $invmap_ref, $format) = prop_invmap($property);
2746 if ($format && $format eq "ar") {
2747 map { $_ = eval $_ if $_ ne 'NaN' } @$map_ref;
2750 Here's some entries from the output of the property "Nv", which has format
2753 @numerics_ranges @numerics_maps Note
2755 0x30 0 DIGIT 0 .. DIGIT 9
2757 0xB2 2 SUPERSCRIPTs 2 and 3
2759 0xB9 1 SUPERSCRIPT 1
2761 0xBC 1/4 VULGAR FRACTION 1/4
2762 0xBD 1/2 VULGAR FRACTION 1/2
2763 0xBE 3/4 VULGAR FRACTION 3/4
2765 0x660 0 ARABIC-INDIC DIGIT ZERO .. NINE
2768 The fourth (index [3]) element (C<$default>) in the list returned for this
2773 means the Name property. All the elements of the map array are simple
2774 scalars, but some of them contain special strings that require more work to
2775 get the actual name.
2779 CJK UNIFIED IDEOGRAPH-<code point>
2781 mean that the name for the code point is "CJK UNIFIED IDEOGRAPH-"
2782 with the code point (expressed in hexadecimal) appended to it, like "CJK
2783 UNIFIED IDEOGRAPH-3403" (similarly for S<C<CJK COMPATIBILITY IDEOGRAPH-E<lt>code
2790 means that the name is algorithmically calculated. This is easily done by
2791 the function L<charnames/charnames::viacode(code)>.
2793 Note that for control characters (C<Gc=cc>), Unicode's data files have the
2794 string "C<E<lt>controlE<gt>>", but the real name of each of these characters is the empty
2795 string. This function returns that real name, the empty string. (There are
2796 names for these characters, but they are considered aliases, not the Name
2797 property name, and are contained in the C<Name_Alias> property.)
2801 means the Decomposition_Mapping property. This property is like C<"al">
2802 properties, except that one of the scalar elements is of the form:
2806 This signifies that this entry should be replaced by the decompositions for
2807 all the code points whose decomposition is algorithmically calculated. (All
2808 of them are currently in one range and no others outside the range are likely
2809 to ever be added to Unicode; the C<"n"> format
2810 has this same entry.) These can be generated via the function
2811 L<Unicode::Normalize::NFD()|Unicode::Normalize>.
2813 Note that the mapping is the one that is specified in the Unicode data files,
2814 and to get the final decomposition, it may need to be applied recursively.
2816 The fourth (index [3]) element (C<$default>) in the list returned for this
2821 Note that a format begins with the letter "a" if and only the property it is
2822 for requires adjustments by adding the offsets in multi-element ranges. For
2823 all these properties, an entry should be adjusted only if the map is a scalar
2824 which is an integer. That is, it must match the regular expression:
2828 Further, the first element in a range never needs adjustment, as the
2829 adjustment would be just adding 0.
2831 A binary search such as that provided by L</search_invlist()>, can be used to
2832 quickly find a code point in the inversion list, and hence its corresponding
2835 The final, fourth element (index [3], assigned to C<$default> in the "block"
2836 example) in the four element list returned by this function is used with the
2837 C<"a"> format types; it may also be useful for applications
2838 that wish to convert the returned inversion map data structure into some
2839 other, such as a hash. It gives the mapping that most code points map to
2840 under the property. If you establish the convention that any code point not
2841 explicitly listed in your data structure maps to this value, you can
2842 potentially make your data structure much smaller. As you construct your data
2843 structure from the one returned by this function, simply ignore those ranges
2844 that map to this value. For example, to
2845 convert to the data structure searchable by L</charinrange()>, you can follow
2846 this recipe for properties that don't require adjustments:
2848 my ($list_ref, $map_ref, $format, $default) = prop_invmap($property);
2851 # Look at each element in the list, but the -2 is needed because we
2852 # look at $i+1 in the loop, and the final element is guaranteed to map
2853 # to $default by prop_invmap(), so we would skip it anyway.
2854 for my $i (0 .. @$list_ref - 2) {
2855 next if $map_ref->[$i] eq $default;
2856 push @range_list, [ $list_ref->[$i],
2862 print charinrange(\@range_list, $code_point), "\n";
2864 With this, C<charinrange()> will return C<undef> if its input code point maps
2865 to C<$default>. You can avoid this by omitting the C<next> statement, and adding
2866 a line after the loop to handle the final element of the inversion map.
2868 Similarly, this recipe can be used for properties that do require adjustments:
2870 for my $i (0 .. @$list_ref - 2) {
2871 next if $map_ref->[$i] eq $default;
2873 # prop_invmap() guarantees that if the mapping is to an array, the
2874 # range has just one element, so no need to worry about adjustments.
2875 if (ref $map_ref->[$i]) {
2877 [ $list_ref->[$i], $list_ref->[$i], $map_ref->[$i] ];
2879 else { # Otherwise each element is actually mapped to a separate
2880 # value, so the range has to be split into single code point
2885 # For each code point that gets mapped to something...
2886 for my $j ($list_ref->[$i] .. $list_ref->[$i+1] -1 ) {
2888 # ... add a range consisting of just it mapping to the
2889 # original plus the adjustment, which is incremented for the
2890 # next time through the loop, as the offset increases by 1
2891 # for each element in the range
2893 [ $j, $j, $map_ref->[$i] + $adjustment++ ];
2898 Note that the inversion maps returned for the C<Case_Folding> and
2899 C<Simple_Case_Folding> properties do not include the Turkic-locale mappings.
2900 Use L</casefold()> for these.
2902 C<prop_invmap> does not know about any user-defined properties, and will
2903 return C<undef> if called with one of those.
2905 The returned values for the Perl extension properties, such as C<Any> and
2906 C<Greek> are somewhat misleading. The values are either C<"Y"> or C<"N>".
2907 All Unicode properties are bipartite, so you can actually use the C<"Y"> or
2908 C<"N>" in a Perl regular rexpression for these, like C<qr/\p{ID_Start=Y/}> or
2909 C<qr/\p{Upper=N/}>. But the Perl extensions aren't specified this way, only
2910 like C</qr/\p{Any}>, I<etc>. You can't actually use the C<"Y"> and C<"N>" in
2915 # User-defined properties could be handled with some changes to utf8_heavy.pl;
2916 # if done, consideration should be given to the fact that the user subroutine
2917 # could return different results with each call, which could lead to some
2920 # One could store things in memory so they don't have to be recalculated, but
2921 # it is unlikely this will be called often, and some properties would take up
2922 # significant memory.
2924 # These are created by mktables for this routine and stored in unicore/UCD.pl
2925 # where their structures are described.
2926 our @algorithmic_named_code_points;
2930 sub prop_invmap ($;$) {
2932 croak __PACKAGE__, "::prop_invmap: must be called in list context" unless wantarray;
2935 return unless defined $prop;
2937 # Undocumented way to get at Perl internal properties; it may be changed
2938 # or removed without notice at any time. It currently also changes the
2939 # output to use the format specified in the file rather than the one we
2940 # normally compute and return
2941 my $internal_ok = defined $_[1] && $_[1] eq '_perl_core_internal_ok';
2943 # Fail internal properties
2944 return if $prop =~ /^_/ && ! $internal_ok;
2946 # The values returned by this function.
2947 my (@invlist, @invmap, $format, $missing);
2949 # The swash has two components we look at, the base list, and a hash,
2950 # named 'SPECIALS', containing any additional members whose mappings don't
2951 # fit into the base list scheme of things. These generally 'override'
2952 # any value in the base list for the same code point.
2955 require "utf8_heavy.pl";
2956 require "unicore/UCD.pl";
2960 # If there are multiple entries for a single code point
2961 my $has_multiples = 0;
2963 # Try to get the map swash for the property. They have 'To' prepended to
2964 # the property name, and 32 means we will accept 32 bit return values.
2965 # The 0 means we aren't calling this from tr///.
2966 my $swash = utf8::SWASHNEW(__PACKAGE__, "To$prop", undef, 32, 0);
2968 # If didn't find it, could be because needs a proxy. And if was the
2969 # 'Block' or 'Name' property, use a proxy even if did find it. Finding it
2970 # in these cases would be the result of the installation changing mktables
2971 # to output the Block or Name tables. The Block table gives block names
2972 # in the new-style, and this routine is supposed to return old-style block
2973 # names. The Name table is valid, but we need to execute the special code
2974 # below to add in the algorithmic-defined name entries.
2975 # And NFKCCF needs conversion, so handle that here too.
2976 if (ref $swash eq ""
2977 || $swash->{'TYPE'} =~ / ^ To (?: Blk | Na | NFKCCF ) $ /x)
2980 # Get the short name of the input property, in standard form
2981 my ($second_try) = prop_aliases($prop);
2982 return unless $second_try;
2983 $second_try = utf8::_loose_name(lc $second_try);
2985 if ($second_try eq "in") {
2987 # This property is identical to age for inversion map purposes
2991 elsif ($second_try =~ / ^ s ( cf | fc | [ltu] c ) $ /x) {
2993 # These properties use just the LIST part of the full mapping,
2994 # which includes the simple maps that are otherwise overridden by
2995 # the SPECIALS. So all we need do is to not look at the SPECIALS;
2996 # set $overrides to indicate that
2999 # The full name is the simple name stripped of its initial 's'
3002 # .. except for this case
3003 $prop = 'cf' if $prop eq 'fc';
3007 elsif ($second_try eq "blk") {
3009 # We use the old block names. Just create a fake swash from its
3013 $blocks{'LIST'} = "";
3014 $blocks{'TYPE'} = "ToBlk";
3015 $utf8::SwashInfo{ToBlk}{'missing'} = "No_Block";
3016 $utf8::SwashInfo{ToBlk}{'format'} = "s";
3018 foreach my $block (@BLOCKS) {
3019 $blocks{'LIST'} .= sprintf "%x\t%x\t%s\n",
3026 elsif ($second_try eq "na") {
3028 # Use the combo file that has all the Name-type properties in it,
3029 # extracting just the ones that are for the actual 'Name'
3030 # property. And create a fake swash from it.
3032 $names{'LIST'} = "";
3033 my $original = do "unicore/Name.pl";
3034 my $algorithm_names = \@algorithmic_named_code_points;
3036 # We need to remove the names from it that are aliases. For that
3037 # we need to also read in that table. Create a hash with the keys
3038 # being the code points, and the values being a list of the
3039 # aliases for the code point key.
3040 my ($aliases_code_points, $aliases_maps, undef, undef) =
3041 &prop_invmap('Name_Alias');
3043 for (my $i = 0; $i < @$aliases_code_points; $i++) {
3044 my $code_point = $aliases_code_points->[$i];
3045 $aliases{$code_point} = $aliases_maps->[$i];
3047 # If not already a list, make it into one, so that later we
3048 # can treat things uniformly
3049 if (! ref $aliases{$code_point}) {
3050 $aliases{$code_point} = [ $aliases{$code_point} ];
3053 # Remove the alias type from the entry, retaining just the
3055 map { s/:.*// } @{$aliases{$code_point}};
3059 foreach my $line (split "\n", $original) {
3060 my ($hex_code_point, $name) = split "\t", $line;
3062 # Weeds out all comments, blank lines, and named sequences
3063 next if $hex_code_point =~ /[^[:xdigit:]]/a;
3065 my $code_point = hex $hex_code_point;
3067 # The name of all controls is the default: the empty string.
3068 # The set of controls is immutable
3069 next if chr($code_point) =~ /[[:cntrl:]]/u;
3071 # If this is a name_alias, it isn't a name
3072 next if grep { $_ eq $name } @{$aliases{$code_point}};
3074 # If we are beyond where one of the special lines needs to
3076 while ($i < @$algorithm_names
3077 && $code_point > $algorithm_names->[$i]->{'low'})
3080 # ... then insert it, ahead of what we were about to
3082 $names{'LIST'} .= sprintf "%x\t%x\t%s\n",
3083 $algorithm_names->[$i]->{'low'},
3084 $algorithm_names->[$i]->{'high'},
3085 $algorithm_names->[$i]->{'name'};
3087 # Done with this range.
3090 # We loop until all special lines that precede the next
3091 # regular one are output.
3094 # Here, is a normal name.
3095 $names{'LIST'} .= sprintf "%x\t\t%s\n", $code_point, $name;
3096 } # End of loop through all the names
3098 $names{'TYPE'} = "ToNa";
3099 $utf8::SwashInfo{ToNa}{'missing'} = "";
3100 $utf8::SwashInfo{ToNa}{'format'} = "n";
3103 elsif ($second_try =~ / ^ ( d [mt] ) $ /x) {
3105 # The file is a combination of dt and dm properties. Create a
3106 # fake swash from the portion that we want.
3107 my $original = do "unicore/Decomposition.pl";
3110 if ($second_try eq 'dt') {
3111 $decomps{'TYPE'} = "ToDt";
3112 $utf8::SwashInfo{'ToDt'}{'missing'} = "None";
3113 $utf8::SwashInfo{'ToDt'}{'format'} = "s";
3114 } # 'dm' is handled below, with 'nfkccf'
3116 $decomps{'LIST'} = "";
3118 # This property has one special range not in the file: for the
3119 # hangul syllables. But not in Unicode version 1.
3120 UnicodeVersion() unless defined $v_unicode_version;
3121 my $done_hangul = ($v_unicode_version lt v2.0.0)
3123 : 0; # Have we done the hangul range ?
3124 foreach my $line (split "\n", $original) {
3125 my ($hex_lower, $hex_upper, $type_and_map) = split "\t", $line;
3126 my $code_point = hex $hex_lower;
3130 # The type, enclosed in <...>, precedes the mapping separated
3132 if ($type_and_map =~ / ^ < ( .* ) > \s+ (.*) $ /x) {
3133 $value = ($second_try eq 'dt') ? $1 : $2
3135 else { # If there is no type specified, it's canonical
3136 $value = ($second_try eq 'dt')
3141 # Insert the hangul range at the appropriate spot.
3142 if (! $done_hangul && $code_point > $HANGUL_BEGIN) {
3145 sprintf "%x\t%x\t%s\n",
3147 $HANGUL_BEGIN + $HANGUL_COUNT - 1,
3148 ($second_try eq 'dt')
3150 : "<hangul syllable>";
3153 if ($value =~ / / && $hex_upper ne "" && $hex_upper ne $hex_lower) {
3154 $line = sprintf("%04X\t%s\t%s", hex($hex_lower) + 1, $hex_upper, $value);
3159 # And append this to our constructed LIST.
3160 $decomps{'LIST'} .= "$hex_lower\t$hex_upper\t$value\n";
3166 elsif ($second_try ne 'nfkccf') { # Don't know this property. Fail.
3170 if ($second_try eq 'nfkccf' || $second_try eq 'dm') {
3172 # The 'nfkccf' property is stored in the old format for backwards
3173 # compatibility for any applications that has read its file
3174 # directly before prop_invmap() existed.
3175 # And the code above has extracted the 'dm' property from its file
3176 # yielding the same format. So here we convert them to adjusted
3177 # format for compatibility with the other properties similar to
3181 # We construct a new converted list.
3184 my @ranges = split "\n", $swash->{'LIST'};
3185 for (my $i = 0; $i < @ranges; $i++) {
3186 my ($hex_begin, $hex_end, $map) = split "\t", $ranges[$i];
3188 # The dm property has maps that are space separated sequences
3189 # of code points, as well as the special entry "<hangul
3190 # syllable>, which also contains a blank.
3191 my @map = split " ", $map;
3194 # If it's just the special entry, append as-is.
3195 if ($map eq '<hangul syllable>') {
3196 $list .= "$ranges[$i]\n";
3200 # These should all be single-element ranges.
3201 croak __PACKAGE__, "::prop_invmap: Not expecting a mapping with multiple code points in a multi-element range, $ranges[$i]" if $hex_end ne "" && $hex_end ne $hex_begin;
3203 # Convert them to decimal, as that's what's expected.
3204 $list .= "$hex_begin\t\t"
3205 . join(" ", map { hex } @map)
3211 # Here, the mapping doesn't have a blank, is for a single code
3213 my $begin = hex $hex_begin;
3214 my $end = (defined $hex_end && $hex_end ne "")
3218 # Again, the output is to be in decimal.
3219 my $decimal_map = hex $map;
3221 # We know that multi-element ranges with the same mapping
3222 # should not be adjusted, as after the adjustment
3223 # multi-element ranges are for consecutive increasing code
3224 # points. Further, the final element in the list won't be
3225 # adjusted, as there is nothing after it to include in the
3227 if ($begin != $end || $i == @ranges -1) {
3229 # So just convert these to single-element ranges
3230 foreach my $code_point ($begin .. $end) {
3231 $list .= sprintf("%04X\t\t%d\n",
3232 $code_point, $decimal_map);
3237 # Here, we have a candidate for adjusting. What we do is
3238 # look through the subsequent adjacent elements in the
3239 # input. If the map to the next one differs by 1 from the
3240 # one before, then we combine into a larger range with the
3241 # initial map. Loop doing this until we find one that
3242 # can't be combined.
3244 my $offset = 0; # How far away are we from the initial
3246 my $squished = 0; # ? Did we squish at least two
3247 # elements together into one range
3248 for ( ; $i < @ranges; $i++) {
3249 my ($next_hex_begin, $next_hex_end, $next_map)
3250 = split "\t", $ranges[$i+1];
3252 # In the case of 'dm', the map may be a sequence of
3253 # multiple code points, which are never combined with
3255 last if $next_map =~ / /;
3258 my $next_decimal_map = hex $next_map;
3260 # If the next map is not next in sequence, it
3261 # shouldn't be combined.
3262 last if $next_decimal_map != $decimal_map + $offset;
3264 my $next_begin = hex $next_hex_begin;
3266 # Likewise, if the next element isn't adjacent to the
3267 # previous one, it shouldn't be combined.
3268 last if $next_begin != $begin + $offset;
3270 my $next_end = (defined $next_hex_end
3271 && $next_hex_end ne "")
3275 # And finally, if the next element is a multi-element
3276 # range, it shouldn't be combined.
3277 last if $next_end != $next_begin;
3279 # Here, we will combine. Loop to see if we should
3280 # combine the next element too.
3286 # Here, 'i' is the element number of the last element to
3287 # be combined, and the range is single-element, or we
3288 # wouldn't be combining. Get it's code point.
3289 my ($hex_end, undef, undef) = split "\t", $ranges[$i];
3290 $list .= "$hex_begin\t$hex_end\t$decimal_map\n";
3293 # Here, no combining done. Just append the initial
3294 # (and current) values.
3295 $list .= "$hex_begin\t\t$decimal_map\n";
3298 } # End of loop constructing the converted list
3300 # Finish up the data structure for our converted swash
3301 my $type = ($second_try eq 'nfkccf') ? 'ToNFKCCF' : 'ToDm';
3302 $revised_swash{'LIST'} = $list;
3303 $revised_swash{'TYPE'} = $type;
3304 $revised_swash{'SPECIALS'} = $swash->{'SPECIALS'};
3305 $swash = \%revised_swash;
3307 $utf8::SwashInfo{$type}{'missing'} = 0;
3308 $utf8::SwashInfo{$type}{'format'} = 'a';
3312 if ($swash->{'EXTRAS'}) {
3313 carp __PACKAGE__, "::prop_invmap: swash returned for $prop unexpectedly has EXTRAS magic";
3317 # Here, have a valid swash return. Examine it.
3318 my $returned_prop = $swash->{'TYPE'};
3320 # All properties but binary ones should have 'missing' and 'format'
3322 $missing = $utf8::SwashInfo{$returned_prop}{'missing'};
3323 $missing = 'N' unless defined $missing;
3325 $format = $utf8::SwashInfo{$returned_prop}{'format'};
3326 $format = 'b' unless defined $format;
3328 my $requires_adjustment = $format =~ /^a/;
3330 if ($swash->{'LIST'} =~ /^V/) {
3331 @invlist = split "\n", $swash->{'LIST'} =~ s/ \s* (?: \# .* )? $ //xmgr;
3333 foreach my $i (0 .. @invlist - 1) {
3334 $invmap[$i] = ($i % 2 == 0) ? 'Y' : 'N'
3337 # The map includes lines for all code points; add one for the range
3338 # from 0 to the first Y.
3339 if ($invlist[0] != 0) {
3340 unshift @invlist, 0;
3341 unshift @invmap, 'N';
3345 # The LIST input lines look like:
3348 # 0375\t0377\tGreek # [3]
3349 # 037A\t037D\tGreek # [4]
3354 # Convert them to like
3363 # For binary properties, the final non-comment column is absent, and
3364 # assumed to be 'Y'.
3366 foreach my $range (split "\n", $swash->{'LIST'}) {
3367 $range =~ s/ \s* (?: \# .* )? $ //xg; # rmv trailing space, comments
3369 # Find the beginning and end of the range on the line
3370 my ($hex_begin, $hex_end, $map) = split "\t", $range;
3371 my $begin = hex $hex_begin;
3372 no warnings 'portable';
3373 my $end = (defined $hex_end && $hex_end ne "")
3377 # Each time through the loop (after the first):
3378 # $invlist[-2] contains the beginning of the previous range processed
3379 # $invlist[-1] contains the end+1 of the previous range processed
3380 # $invmap[-2] contains the value of the previous range processed
3381 # $invmap[-1] contains the default value for missing ranges
3384 # Thus, things are set up for the typical case of a new
3385 # non-adjacent range of non-missings to be added. But, if the new
3386 # range is adjacent, it needs to replace the [-1] element; and if
3387 # the new range is a multiple value of the previous one, it needs
3388 # to be added to the [-2] map element.
3390 # The first time through, everything will be empty. If the
3391 # property doesn't have a range that begins at 0, add one that
3396 push @invmap, $missing;
3399 elsif (@invlist > 1 && $invlist[-2] == $begin) {
3401 # Here we handle the case where the input has multiple entries
3402 # for each code point. mktables should have made sure that
3403 # each such range contains only one code point. At this
3404 # point, $invlist[-1] is the $missing that was added at the
3405 # end of the last loop iteration, and [-2] is the last real
3406 # input code point, and that code point is the same as the one
3407 # we are adding now, making the new one a multiple entry. Add
3408 # it to the existing entry, either by pushing it to the
3409 # existing list of multiple entries, or converting the single
3410 # current entry into a list with both on it. This is all we
3411 # need do for this iteration.
3413 if ($end != $begin) {
3414 croak __PACKAGE__, ":prop_invmap: Multiple maps per code point in '$prop' require single-element ranges: begin=$begin, end=$end, map=$map";
3416 if (! ref $invmap[-2]) {
3417 $invmap[-2] = [ $invmap[-2], $map ];
3420 push @{$invmap[-2]}, $map;
3425 elsif ($invlist[-1] == $begin) {
3427 # If the input isn't in the most compact form, so that there
3428 # are two adjacent ranges that map to the same thing, they
3429 # should be combined (EXCEPT where the arrays require
3430 # adjustments, in which case everything is already set up
3431 # correctly). This happens in our constructed dt mapping, as
3432 # Element [-2] is the map for the latest range so far
3433 # processed. Just set the beginning point of the map to
3434 # $missing (in invlist[-1]) to 1 beyond where this range ends.
3438 # we have set it up so that it looks like
3442 # We now see that it should be
3445 if (! $requires_adjustment && @invlist > 1 && ( (defined $map)
3446 ? $invmap[-2] eq $map
3447 : $invmap[-2] eq 'Y'))
3449 $invlist[-1] = $end + 1;
3453 # Here, the range started in the previous iteration that maps
3454 # to $missing starts at the same code point as this range.
3455 # That means there is no gap to fill that that range was
3456 # intended for, so we just pop it off the parallel arrays.
3461 # Add the range beginning, and the range's map.
3462 push @invlist, $begin;
3463 if ($returned_prop eq 'ToDm') {
3465 # The decomposition maps are either a line like <hangul
3466 # syllable> which are to be taken as is; or a sequence of code
3467 # points in hex and separated by blanks. Convert them to
3468 # decimal, and if there is more than one, use an anonymous
3470 if ($map =~ /^ < /x) {
3474 my @map = split " ", $map;
3476 push @invmap, $map[0];
3479 push @invmap, \@map;
3485 # Otherwise, convert hex formatted list entries to decimal;
3486 # add a 'Y' map for the missing value in binary properties, or
3487 # otherwise, use the input map unchanged.
3488 $map = ($format eq 'x' || $format eq 'ax')
3496 # We just started a range. It ends with $end. The gap between it
3497 # and the next element in the list must be filled with a range
3498 # that maps to the default value. If there is no gap, the next
3499 # iteration will pop this, unless there is no next iteration, and
3500 # we have filled all of the Unicode code space, so check for that
3502 if ($end < $Unicode::UCD::MAX_CP) {
3503 push @invlist, $end + 1;
3504 push @invmap, $missing;
3509 # If the property is empty, make all code points use the value for missing
3513 push @invmap, $missing;
3516 # The final element is always for just the above-Unicode code points. If
3517 # not already there, add it. It merely splits the current final range
3518 # that extends to infinity into two elements, each with the same map.
3519 # (This is to conform with the API that says the final element is for
3520 # $MAX_UNICODE_CODEPOINT + 1 .. INFINITY.)
3521 if ($invlist[-1] != $MAX_UNICODE_CODEPOINT + 1) {
3522 push @invmap, $invmap[-1];
3523 push @invlist, $MAX_UNICODE_CODEPOINT + 1;
3526 # The second component of the map are those values that require
3527 # non-standard specification, stored in SPECIALS. These override any
3528 # duplicate code points in LIST. If we are using a proxy, we may have
3529 # already set $overrides based on the proxy.
3530 $overrides = $swash->{'SPECIALS'} unless defined $overrides;
3533 # A negative $overrides implies that the SPECIALS should be ignored,
3534 # and a simple 'a' list is the value.
3535 if ($overrides < 0) {
3540 # Currently, all overrides are for properties that normally map to
3541 # single code points, but now some will map to lists of code
3542 # points (but there is an exception case handled below).
3545 # Look through the overrides.
3546 foreach my $cp_maybe_utf8 (keys %$overrides) {
3550 # If the overrides came from SPECIALS, the code point keys are
3552 if ($overrides == $swash->{'SPECIALS'}) {
3553 $cp = $cp_maybe_utf8;
3554 if (! utf8::decode($cp)) {
3555 croak __PACKAGE__, "::prop_invmap: Malformed UTF-8: ",
3556 map { sprintf("\\x{%02X}", unpack("C", $_)) }
3560 $cp = unpack("W", $cp);
3561 @map = unpack "W*", $swash->{'SPECIALS'}{$cp_maybe_utf8};
3563 # The empty string will show up unpacked as an empty
3565 $format = 'ale' if @map == 0;
3569 # But if we generated the overrides, we didn't bother to
3570 # pack them, and we, so far, do this only for properties
3571 # that are 'a' ones.
3572 $cp = $cp_maybe_utf8;
3573 @map = hex $overrides->{$cp};
3577 # Find the range that the override applies to.
3578 my $i = search_invlist(\@invlist, $cp);
3579 if ($cp < $invlist[$i] || $cp >= $invlist[$i + 1]) {
3580 croak __PACKAGE__, "::prop_invmap: wrong_range, cp=$cp; i=$i, current=$invlist[$i]; next=$invlist[$i + 1]"
3583 # And what that range currently maps to
3584 my $cur_map = $invmap[$i];
3586 # If there is a gap between the next range and the code point
3587 # we are overriding, we have to add elements to both arrays to
3588 # fill that gap, using the map that applies to it, which is
3589 # $cur_map, since it is part of the current range.
3590 if ($invlist[$i + 1] > $cp + 1) {
3592 #say "Before splice:";
3593 #say 'i-2=[', $i-2, ']', sprintf("%04X maps to %s", $invlist[$i-2], $invmap[$i-2]) if $i >= 2;
3594 #say 'i-1=[', $i-1, ']', sprintf("%04X maps to %s", $invlist[$i-1], $invmap[$i-1]) if $i >= 1;
3595 #say 'i =[', $i, ']', sprintf("%04X maps to %s", $invlist[$i], $invmap[$i]);
3596 #say 'i+1=[', $i+1, ']', sprintf("%04X maps to %s", $invlist[$i+1], $invmap[$i+1]) if $i < @invlist + 1;
3597 #say 'i+2=[', $i+2, ']', sprintf("%04X maps to %s", $invlist[$i+2], $invmap[$i+2]) if $i < @invlist + 2;
3599 splice @invlist, $i + 1, 0, $cp + 1;
3600 splice @invmap, $i + 1, 0, $cur_map;
3602 #say "After splice:";
3603 #say 'i-2=[', $i-2, ']', sprintf("%04X maps to %s", $invlist[$i-2], $invmap[$i-2]) if $i >= 2;
3604 #say 'i-1=[', $i-1, ']', sprintf("%04X maps to %s", $invlist[$i-1], $invmap[$i-1]) if $i >= 1;
3605 #say 'i =[', $i, ']', sprintf("%04X maps to %s", $invlist[$i], $invmap[$i]);
3606 #say 'i+1=[', $i+1, ']', sprintf("%04X maps to %s", $invlist[$i+1], $invmap[$i+1]) if $i < @invlist + 1;
3607 #say 'i+2=[', $i+2, ']', sprintf("%04X maps to %s", $invlist[$i+2], $invmap[$i+2]) if $i < @invlist + 2;
3610 # If the remaining portion of the range is multiple code
3611 # points (ending with the one we are replacing, guaranteed by
3612 # the earlier splice). We must split it into two
3613 if ($invlist[$i] < $cp) {
3614 $i++; # Compensate for the new element
3617 #say "Before splice:";
3618 #say 'i-2=[', $i-2, ']', sprintf("%04X maps to %s", $invlist[$i-2], $invmap[$i-2]) if $i >= 2;
3619 #say 'i-1=[', $i-1, ']', sprintf("%04X maps to %s", $invlist[$i-1], $invmap[$i-1]) if $i >= 1;
3620 #say 'i =[', $i, ']', sprintf("%04X maps to %s", $invlist[$i], $invmap[$i]);
3621 #say 'i+1=[', $i+1, ']', sprintf("%04X maps to %s", $invlist[$i+1], $invmap[$i+1]) if $i < @invlist + 1;
3622 #say 'i+2=[', $i+2, ']', sprintf("%04X maps to %s", $invlist[$i+2], $invmap[$i+2]) if $i < @invlist + 2;
3624 splice @invlist, $i, 0, $cp;
3625 splice @invmap, $i, 0, 'dummy';
3627 #say "After splice:";
3628 #say 'i-2=[', $i-2, ']', sprintf("%04X maps to %s", $invlist[$i-2], $invmap[$i-2]) if $i >= 2;
3629 #say 'i-1=[', $i-1, ']', sprintf("%04X maps to %s", $invlist[$i-1], $invmap[$i-1]) if $i >= 1;
3630 #say 'i =[', $i, ']', sprintf("%04X maps to %s", $invlist[$i], $invmap[$i]);
3631 #say 'i+1=[', $i+1, ']', sprintf("%04X maps to %s", $invlist[$i+1], $invmap[$i+1]) if $i < @invlist + 1;
3632 #say 'i+2=[', $i+2, ']', sprintf("%04X maps to %s", $invlist[$i+2], $invmap[$i+2]) if $i < @invlist + 2;
3635 # Here, the range we are overriding contains a single code
3636 # point. The result could be the empty string, a single
3637 # value, or a list. If the last case, we use an anonymous
3639 $invmap[$i] = (scalar @map == 0)
3647 elsif ($format eq 'x') {
3649 # All hex-valued properties are really to code points, and have been
3650 # converted to decimal.
3653 elsif ($returned_prop eq 'ToDm') {
3656 elsif ($format eq 'sw') { # blank-separated elements to form a list.
3657 map { $_ = [ split " ", $_ ] if $_ =~ / / } @invmap;
3660 elsif ($returned_prop eq 'ToNameAlias') {
3662 # This property currently doesn't have any lists, but theoretically
3666 elsif ($returned_prop eq 'ToPerlDecimalDigit') {
3669 elsif ($returned_prop eq 'ToNv') {
3671 # The one property that has this format is stored as a delta, so needs
3672 # to indicate that need to add code point to it.
3675 elsif ($format ne 'n' && $format ne 'a') {
3677 # All others are simple scalars
3680 if ($has_multiples && $format !~ /l/) {
3681 croak __PACKAGE__, "::prop_invmap: Wrong format '$format' for prop_invmap('$prop'); should indicate has lists";
3684 return (\@invlist, \@invmap, $format, $missing);
3687 sub search_invlist {
3691 =head2 B<search_invlist()>
3693 use Unicode::UCD qw(prop_invmap prop_invlist);
3694 use Unicode::UCD 'search_invlist';
3696 my @invlist = prop_invlist($property_name);
3697 print $code_point, ((search_invlist(\@invlist, $code_point) // -1) % 2)
3700 " in $property_name\n";
3702 my ($blocks_ranges_ref, $blocks_map_ref) = prop_invmap("Block");
3703 my $index = search_invlist($blocks_ranges_ref, $code_point);
3704 print "$code_point is in block ", $blocks_map_ref->[$index], "\n";
3706 C<search_invlist> is used to search an inversion list returned by
3707 C<prop_invlist> or C<prop_invmap> for a particular L</code point argument>.
3708 C<undef> is returned if the code point is not found in the inversion list
3709 (this happens only when it is not a legal L<code point argument>, or is less
3710 than the list's first element). A warning is raised in the first instance.
3712 Otherwise, it returns the index into the list of the range that contains the
3713 code point.; that is, find C<i> such that
3715 list[i]<= code_point < list[i+1].
3717 As explained in L</prop_invlist()>, whether a code point is in the list or not
3718 depends on if the index is even (in) or odd (not in). And as explained in
3719 L</prop_invmap()>, the index is used with the returned parallel array to find
3725 my $list_ref = shift;
3726 my $input_code_point = shift;
3727 my $code_point = _getcode($input_code_point);
3729 if (! defined $code_point) {
3730 carp __PACKAGE__, "::search_invlist: unknown code '$input_code_point'";
3734 my $max_element = @$list_ref - 1;
3736 # Return undef if list is empty or requested item is before the first element.
3737 return if $max_element < 0;
3738 return if $code_point < $list_ref->[0];
3740 # Short cut something at the far-end of the table. This also allows us to
3741 # refer to element [$i+1] without fear of being out-of-bounds in the loop
3743 return $max_element if $code_point >= $list_ref->[$max_element];
3745 use integer; # want integer division
3747 my $i = $max_element / 2;
3750 my $upper = $max_element;
3753 if ($code_point >= $list_ref->[$i]) {
3755 # Here we have met the lower constraint. We can quit if we
3756 # also meet the upper one.
3757 last if $code_point < $list_ref->[$i+1];
3759 $lower = $i; # Still too low.
3764 # Here, $code_point < $list_ref[$i], so look lower down.
3768 # Split search domain in half to try again.
3769 my $temp = ($upper + $lower) / 2;
3771 # No point in continuing unless $i changes for next time
3773 return $i if $temp == $i;
3775 } # End of while loop
3777 # Here we have found the offset
3781 =head2 Unicode::UCD::UnicodeVersion
3783 This returns the version of the Unicode Character Database, in other words, the
3784 version of the Unicode standard the database implements. The version is a
3785 string of numbers delimited by dots (C<'.'>).
3791 sub UnicodeVersion {
3792 unless (defined $UNICODEVERSION) {
3793 openunicode(\$VERSIONFH, "version");
3795 chomp($UNICODEVERSION = <$VERSIONFH>);
3797 croak __PACKAGE__, "::VERSION: strange version '$UNICODEVERSION'"
3798 unless $UNICODEVERSION =~ /^\d+(?:\.\d+)+$/;
3800 $v_unicode_version = pack "C*", split /\./, $UNICODEVERSION;
3801 return $UNICODEVERSION;
3804 =head2 B<Blocks versus Scripts>
3806 The difference between a block and a script is that scripts are closer
3807 to the linguistic notion of a set of code points required to represent
3808 languages, while block is more of an artifact of the Unicode code point
3809 numbering and separation into blocks of consecutive code points (so far the
3810 size of a block is some multiple of 16, like 128 or 256).
3812 For example the Latin B<script> is spread over several B<blocks>, such
3813 as C<Basic Latin>, C<Latin 1 Supplement>, C<Latin Extended-A>, and
3814 C<Latin Extended-B>. On the other hand, the Latin script does not
3815 contain all the characters of the C<Basic Latin> block (also known as
3816 ASCII): it includes only the letters, and not, for example, the digits
3817 nor the punctuation.
3819 For blocks see L<http://www.unicode.org/Public/UNIDATA/Blocks.txt>
3821 For scripts see UTR #24: L<http://www.unicode.org/unicode/reports/tr24/>
3823 =head2 B<Matching Scripts and Blocks>
3825 Scripts are matched with the regular-expression construct
3826 C<\p{...}> (e.g. C<\p{Tibetan}> matches characters of the Tibetan script),
3827 while C<\p{Blk=...}> is used for blocks (e.g. C<\p{Blk=Tibetan}> matches
3828 any of the 256 code points in the Tibetan block).
3830 =head2 Old-style versus new-style block names
3832 Unicode publishes the names of blocks in two different styles, though the two
3833 are equivalent under Unicode's loose matching rules.
3835 The original style uses blanks and hyphens in the block names (except for
3836 C<No_Block>), like so:
3838 Miscellaneous Mathematical Symbols-B
3840 The newer style replaces these with underscores, like this:
3842 Miscellaneous_Mathematical_Symbols_B
3844 This newer style is consistent with the values of other Unicode properties.
3845 To preserve backward compatibility, all the functions in Unicode::UCD that
3846 return block names (except one) return the old-style ones. That one function,
3847 L</prop_value_aliases()> can be used to convert from old-style to new-style:
3849 my $new_style = prop_values_aliases("block", $old_style);
3851 Perl also has single-form extensions that refer to blocks, C<In_Cyrillic>,
3852 meaning C<Block=Cyrillic>. These have always been written in the new style.
3854 To convert from new-style to old-style, follow this recipe:
3856 $old_style = charblock((prop_invlist("block=$new_style"))[0]);
3858 (which finds the range of code points in the block using C<prop_invlist>,
3859 gets the lower end of the range (0th element) and then looks up the old name
3860 for its block using C<charblock>).
3862 Note that starting in Unicode 6.1, many of the block names have shorter
3863 synonyms. These are always given in the new style.
3867 Jarkko Hietaniemi. Now maintained by perl5 porters.