if the L should not
be produced by composition normalization. The final two forms
additionally require that this fact not otherwise be determinable from
the Unicode data base for them to return B
.
This routine behaves identically to the final two forms. That is,
it does not return B
if the code point has a decomposition
consisting of another single code point, nor if its decomposition starts
with a code point whose combining class is non-zero. Code points that meet
either of these conditions should also not be produced by composition
normalization, which is probably why you should use the
C
property instead, as shown above.
The routine returns B otherwise.
=cut
sub compexcl {
my $arg = shift;
my $code = _getcode($arg);
croak __PACKAGE__, "::compexcl: unknown code '$arg'"
unless defined $code;
no warnings "non_unicode"; # So works on non-Unicode code points
return chr($code) =~ /\p{Composition_Exclusion}/;
}
=head2 B
use Unicode::UCD 'casefold';
my $casefold = casefold(0xDF);
if (defined $casefold) {
my @full_fold_hex = split / /, $casefold->{'full'};
my $full_fold_string =
join "", map {chr(hex($_))} @full_fold_hex;
my @turkic_fold_hex =
split / /, ($casefold->{'turkic'} ne "")
? $casefold->{'turkic'}
: $casefold->{'full'};
my $turkic_fold_string =
join "", map {chr(hex($_))} @turkic_fold_hex;
}
if (defined $casefold && $casefold->{'simple'} ne "") {
my $simple_fold_hex = $casefold->{'simple'};
my $simple_fold_string = chr(hex($simple_fold_hex));
}
This returns the (almost) locale-independent case folding of the
character specified by the L.
If there is no case folding for that code point, B is returned.
If there is a case folding for that code point, a reference to a hash
with the following fields is returned:
=over
=item B
the input L expressed in hexadecimal, with leading zeros
added if necessary to make it contain at least four hexdigits
=item B
one or more codes (separated by spaces) that taken in order give the
code points for the case folding for I.
Each has at least four hexdigits.
=item B
is empty, or is exactly one code with at least four hexdigits which can be used
as an alternative case folding when the calling program cannot cope with the
fold being a sequence of multiple code points. If I is just one code
point, then I equals I. If there is no single code point folding
defined for I, then I is the empty string. Otherwise, it is an
inferior, but still better-than-nothing alternative folding to I.
=item B
is the same as I if I is not empty, and it is the same as I
otherwise. It can be considered to be the simplest possible folding for
I. It is defined primarily for backwards compatibility.
=item B
is C (for C) if the best possible fold is a single code point
(I equals I equals I). It is C if there are distinct
folds, I and I (I equals I). And it is C if
there only a I fold (I equals I; I is empty). Note
that this
describes the contents of I. It is defined primarily for backwards
compatibility.
On versions 3.1 and earlier of Unicode, I can also be
C which is the same as C but is a special case for dotted uppercase I and
dotless lowercase i:
=over
=item B<*>
If you use this C mapping, the result is case-insensitive,
but dotless and dotted I's are not distinguished
=item B<*>
If you exclude this C mapping, the result is not fully case-insensitive, but
dotless and dotted I's are distinguished
=back
=item B
contains any special folding for Turkic languages. For versions of Unicode
starting with 3.2, this field is empty unless I has a different folding
in Turkic languages, in which case it is one or more codes (separated by
spaces) that taken in order give the code points for the case folding for
I in those languages.
Each code has at least four hexdigits.
Note that this folding does not maintain canonical equivalence without
additional processing.
For versions of Unicode 3.1 and earlier, this field is empty unless there is a
special folding for Turkic languages, in which case I is C, and
I, I, I, and I are all equal.
=back
Programs that want complete generality and the best folding results should use
the folding contained in the I field. But note that the fold for some
code points will be a sequence of multiple code points.
Programs that can't cope with the fold mapping being multiple code points can
use the folding contained in the I field, with the loss of some
generality. In Unicode 5.1, about 7% of the defined foldings have no single
code point folding.
The I and I fields are provided for backwards compatibility for
existing programs. They contain the same values as in previous versions of
this function.
Locale is not completely independent. The I field contains results to
use when the locale is a Turkic language.
For more information about case mappings see
L
=cut
my %CASEFOLD;
sub _casefold {
unless (%CASEFOLD) {
if (openunicode(\$CASEFOLDFH, "CaseFolding.txt")) {
local $_;
while (<$casefoldfh>) {
if (/^([0-9A-F]+); ([CFIST]); ([0-9A-F]+(?: [0-9A-F]+)*);/) {
my $code = hex(1ドル);
$CASEFOLD{$code}{'code'} = 1ドル;
$CASEFOLD{$code}{'turkic'} = "" unless
defined $CASEFOLD{$code}{'turkic'};
if (2ドル eq 'C' || 2ドル eq 'I') { # 'I' is only on 3.1 and
# earlier Unicodes
# Both entries there (I
# only checked 3.1) are
# the same as C, and
# there are no other
# entries for those
# codepoints, so treat
# as if C, but override
# the turkic one for
# 'I'.
$CASEFOLD{$code}{'status'} = 2ドル;
$CASEFOLD{$code}{'full'} = $CASEFOLD{$code}{'simple'} =
$CASEFOLD{$code}{'mapping'} = 3ドル;
$CASEFOLD{$code}{'turkic'} = 3ドル if 2ドル eq 'I';
} elsif (2ドル eq 'F') {
$CASEFOLD{$code}{'full'} = 3ドル;
unless (defined $CASEFOLD{$code}{'simple'}) {
$CASEFOLD{$code}{'simple'} = "";
$CASEFOLD{$code}{'mapping'} = 3ドル;
$CASEFOLD{$code}{'status'} = 2ドル;
}
} elsif (2ドル eq 'S') {
# There can't be a simple without a full, and simple
# overrides all but full
$CASEFOLD{$code}{'simple'} = 3ドル;
$CASEFOLD{$code}{'mapping'} = 3ドル;
$CASEFOLD{$code}{'status'} = 2ドル;
} elsif (2ドル eq 'T') {
$CASEFOLD{$code}{'turkic'} = 3ドル;
} # else can't happen because only [CIFST] are possible
}
}
close($CASEFOLDFH);
}
}
}
sub casefold {
my $arg = shift;
my $code = _getcode($arg);
croak __PACKAGE__, "::casefold: unknown code '$arg'"
unless defined $code;
_casefold() unless %CASEFOLD;
return $CASEFOLD{$code};
}
=head2 B
use Unicode::UCD 'casespec';
my $casespec = casespec(0xFB00);
This returns the potentially locale-dependent case mappings of the L
. The mappings may be longer than a single code point (which the basic
Unicode case mappings as returned by L never are).
If there are no case mappings for the L, or if all three
possible mappings (I, I and I) result in single code
points and are locale independent and unconditional, B is returned
(which means that the case mappings, if any, for the code point are those
returned by L).
Otherwise, a reference to a hash giving the mappings (or a reference to a hash
of such hashes, explained below) is returned with the following keys and their
meanings:
The keys in the bottom layer hash with the meanings of their values are:
=over
=item B
the input L expressed in hexadecimal, with leading zeros
added if necessary to make it contain at least four hexdigits
=item B
one or more codes (separated by spaces) that taken in order give the
code points for the lower case of I.
Each has at least four hexdigits.
=item B
one or more codes (separated by spaces) that taken in order give the
code points for the title case of I.
Each has at least four hexdigits.
=item B
one or more codes (separated by spaces) that taken in order give the
code points for the upper case of I.
Each has at least four hexdigits.
=item B
the conditions for the mappings to be valid.
If B, the mappings are always valid.
When defined, this field is a list of conditions,
all of which must be true for the mappings to be valid.
The list consists of one or more
I (see below)
and/or I (explained in the next paragraph),
separated by spaces.
(Other than as used to separate elements, spaces are to be ignored.)
Case distinctions in the condition list are not significant.
Conditions preceded by "NON_" represent the negation of the condition.
A I is one of those defined in the Unicode standard.
For Unicode 5.1, they are defined in Section 3.13 C
available at
L.
These are for context-sensitive casing.
=back
The hash described above is returned for locale-independent casing, where
at least one of the mappings has length longer than one. If B is
returned, the code point may have mappings, but if so, all are length one,
and are returned by L.
Note that when this function does return a value, it will be for the complete
set of mappings for a code point, even those whose length is one.
If there are additional casing rules that apply only in certain locales,
an additional key for each will be defined in the returned hash. Each such key
will be its locale name, defined as a 2-letter ISO 3166 country code, possibly
followed by a "_" and a 2-letter ISO language code (possibly followed by a "_"
and a variant code). You can find the lists of all possible locales, see
L and L.
(In Unicode 6.0, the only locales returned by this function
are C, C, and C
.)
Each locale key is a reference to a hash that has the form above, and gives
the casing rules for that particular locale, which take precedence over the
locale-independent ones when in that locale.
If the only casing for a code point is locale-dependent, then the returned
hash will not have any of the base keys, like C, C, etc., but
will contain only locale keys.
For more information about case mappings see
L
=cut
my %CASESPEC;
sub _casespec {
unless (%CASESPEC) {
if (openunicode(\$CASESPECFH, "SpecialCasing.txt")) {
local $_;
while (<$casespecfh>) {
if (/^([0-9A-F]+); ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; (\w+(?: \w+)*)?/) {
my ($hexcode, $lower, $title, $upper, $condition) =
(1,ドル 2,ドル 3,ドル 4,ドル 5ドル);
my $code = hex($hexcode);
if (exists $CASESPEC{$code}) {
if (exists $CASESPEC{$code}->{code}) {
my ($oldlower,
$oldtitle,
$oldupper,
$oldcondition) =
@{$CASESPEC{$code}}{qw(lower
title
upper
condition)};
if (defined $oldcondition) {
my ($oldlocale) =
($oldcondition =~ /^([a-z][a-z](?:_\S+)?)/);
delete $CASESPEC{$code};
$CASESPEC{$code}->{$oldlocale} =
{ code => $hexcode,
lower => $oldlower,
title => $oldtitle,
upper => $oldupper,
condition => $oldcondition };
}
}
my ($locale) =
($condition =~ /^([a-z][a-z](?:_\S+)?)/);
$CASESPEC{$code}->{$locale} =
{ code => $hexcode,
lower => $lower,
title => $title,
upper => $upper,
condition => $condition };
} else {
$CASESPEC{$code} =
{ code => $hexcode,
lower => $lower,
title => $title,
upper => $upper,
condition => $condition };
}
}
}
close($CASESPECFH);
}
}
}
sub casespec {
my $arg = shift;
my $code = _getcode($arg);
croak __PACKAGE__, "::casespec: unknown code '$arg'"
unless defined $code;
_casespec() unless %CASESPEC;
return ref $CASESPEC{$code} ? dclone $CASESPEC{$code} : $CASESPEC{$code};
}
=head2 B
use Unicode::UCD 'namedseq';
my $namedseq = namedseq("KATAKANA LETTER AINU P");
my @namedseq = namedseq("KATAKANA LETTER AINU P");
my %namedseq = namedseq();
If used with a single argument in a scalar context, returns the string
consisting of the code points of the named sequence, or B if no
named sequence by that name exists. If used with a single argument in
a list context, it returns the list of the ordinals of the code points. If used
with no
arguments in a list context, returns a hash with the names of the
named sequences as the keys and the named sequences as strings as
the values. Otherwise, it returns B or an empty list depending
on the context.
This function only operates on officially approved (not provisional) named
sequences.
Note that as of Perl 5.14, C<\n{katakana LETTER AINU P}> will insert the named
sequence into double-quoted strings, and C will return the same string this function does, but will also
operate on character names that aren't named sequences, without you having to
know which are which. See L.
=cut
my %NAMEDSEQ;
sub _namedseq {
unless (%NAMEDSEQ) {
if (openunicode(\$NAMEDSEQFH, "Name.pl")) {
local $_;
while (<$namedseqfh>) {
if (/^ [0-9A-F]+ \ /x) {
chomp;
my ($sequence, $name) = split /\t/;
my @s = map { chr(hex($_)) } split(' ', $sequence);
$NAMEDSEQ{$name} = join("", @s);
}
}
close($NAMEDSEQFH);
}
}
}
sub namedseq {
# Use charnames::string_vianame() which now returns this information,
# unless the caller wants the hash returned, in which case we read it in,
# and thereafter use it instead of calling charnames, as it is faster.
my $wantarray = wantarray();
if (defined $wantarray) {
if ($wantarray) {
if (@_ == 0) {
_namedseq() unless %NAMEDSEQ;
return %NAMEDSEQ;
} elsif (@_ == 1) {
my $s;
if (%NAMEDSEQ) {
$s = $NAMEDSEQ{ $_[0] };
}
else {
$s = charnames::string_vianame($_[0]);
}
return defined $s ? map { ord($_) } split('', $s) : ();
}
} elsif (@_ == 1) {
return $NAMEDSEQ{ $_[0] } if %NAMEDSEQ;
return charnames::string_vianame($_[0]);
}
}
return;
}
my %NUMERIC;
sub _numeric {
# Unicode 6.0 instituted the rule that only digits in a consecutive
# block of 10 would be considered decimal digits. Before that, the only
# problematic code point that I'm (khw) aware of is U+019DA, NEW TAI LUE
# THAM DIGIT ONE, which is an alternate form of U+019D1, NEW TAI LUE DIGIT
# ONE. The code could be modified to handle that, but not bothering, as
# in TUS 6.0, U+19DA was changed to Nt=Di.
if ((pack "C*", split /\./, UnicodeVersion()) lt 6.0.0) {
croak __PACKAGE__, "::num requires Unicode 6.0 or greater"
}
my @numbers = _read_table("unicore/To/Nv.pl");
foreach my $entry (@numbers) {
my ($start, $end, $value) = @$entry;
# If value contains a slash, convert to decimal, add a reverse hash
# used by charinfo.
if ((my @rational = split /\//, $value) == 2) {
my $real = $rational[0] / $rational[1];
$real_to_rational{$real} = $value;
$value = $real;
}
for my $i ($start .. $end) {
$NUMERIC{$i} = $value;
}
}
# Decided unsafe to use these that aren't officially part of the Unicode
# standard.
#use Math::Trig;
#my $pi = acos(-1.0);
#$NUMERIC{0x03C0} = $pi;
# Euler's constant, not to be confused with Euler's number
#$NUMERIC{0x2107} = 0.57721566490153286060651209008240243104215933593992;
# Euler's number
#$NUMERIC{0x212F} = 2.7182818284590452353602874713526624977572;
return;
}
=pod
=head2 num
C returns the numeric value of the input Unicode string; or C if it
doesn't think the entire string has a completely valid, safe numeric value.
If the string is just one character in length, the Unicode numeric value
is returned if it has one, or C otherwise. Note that this need
not be a whole number. C, for
example returns -0.5.
=cut
#A few characters to which Unicode doesn't officially
#assign a numeric value are considered numeric by C.
#These are:
# EULER CONSTANT 0.5772... (this is NOT Euler's number)
# SCRIPT SMALL E 2.71828... (this IS Euler's number)
# GREEK SMALL LETTER PI 3.14159...
=pod
If the string is more than one character, C is returned unless
all its characters are decimal digits (that is they would match C<\d+>),
from the same script. For example if you have an ASCII '0' and a Bengali
'3', mixed together, they aren't considered a valid number, and C
is returned. A further restriction is that the digits all have to be of
the same form. A half-width digit mixed with a full-width one will
return C. The Arabic script has two sets of digits; C will
return C unless all the digits in the string come from the same
set.
C errs on the side of safety, and there may be valid strings of
decimal digits that it doesn't recognize. Note that Unicode defines
a number of "digit" characters that aren't "decimal digit" characters.
"Decimal digits" have the property that they have a positional value, i.e.,
there is a units position, a 10's position, a 100's, etc, AND they are
arranged in Unicode in blocks of 10 contiguous code points. The Chinese
digits, for example, are not in such a contiguous block, and so Unicode
doesn't view them as decimal digits, but merely digits, and so C<\d> will not
match them. A single-character string containing one of these digits will
have its decimal value returned by C, but any longer string containing
only these digits will return C.
Strings of multiple sub- and superscripts are not recognized as numbers. You
can use either of the compatibility decompositions in Unicode::Normalize to
change these into digits, and then call C on the result.
=cut
# To handle sub, superscripts, this could if called in list context,
# consider those, and return the type in the second
# array element.
sub num {
my $string = $_[0];
_numeric unless %NUMERIC;
my $length = length($string);
return $NUMERIC{ord($string)} if $length == 1;
return if $string =~ /\D/;
my $first_ord = ord(substr($string, 0, 1));
my $value = $NUMERIC{$first_ord};
my $zero_ord = $first_ord - $value;
for my $i (1 .. $length -1) {
my $ord = ord(substr($string, $i, 1));
my $digit = $ord - $zero_ord;
return unless $digit>= 0 && $digit <= 9; $value = $value * 10 + $digit; } return $value; } =head2 Unicode::UCD::UnicodeVersion This returns the version of the Unicode Character Database, in other words, the version of the Unicode standard the database implements. The version is a string of numbers delimited by dots (C<'.'>).
=cut
my $UNICODEVERSION;
sub UnicodeVersion {
unless (defined $UNICODEVERSION) {
openunicode(\$VERSIONFH, "version");
chomp($UNICODEVERSION = <$versionfh>);
close($VERSIONFH);
croak __PACKAGE__, "::VERSION: strange version '$UNICODEVERSION'"
unless $UNICODEVERSION =~ /^\d+(?:\.\d+)+$/;
}
return $UNICODEVERSION;
}
=head2 B
The difference between a block and a script is that scripts are closer
to the linguistic notion of a set of code points required to present
languages, while block is more of an artifact of the Unicode code point
numbering and separation into blocks of (mostly) 256 code points.
For example the Latin B> is spread over several B, such
as C, C, C, and
C. On the other hand, the Latin script does not
contain all the characters of the C block (also known as
ASCII): it includes only the letters, and not, for example, the digits
or the punctuation.
For blocks see L
For scripts see UTR #24: L
=head2 B
Scripts are matched with the regular-expression construct
C<\p{...}> (e.g. C<\p{tibetan}> matches characters of the Tibetan script),
while C<\p{blk=...}> is used for blocks (e.g. C<\p{blk=tibetan}> matches
any of the 256 code points in the Tibetan block).
=head2 Implementation Note
The first use of charinfo() opens a read-only filehandle to the Unicode
Character Database (the database is included in the Perl distribution).
The filehandle is then kept open for further queries. In other words,
if you are wondering where one of your filehandles went, that's where.
=head1 BUGS
Does not yet support EBCDIC platforms.
=head1 AUTHOR
Jarkko Hietaniemi
=cut
1;