package Char::Elatin5;
######################################################################
#
# Char::Elatin5 - Run-time routines for Char/Latin5.pm
#
# Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 INABA Hitoshi <ina@cpan.org>
#
######################################################################
use 5.00503;
BEGIN {
if ($^X =~ / jperl /oxmsi) {
die __FILE__, ": needs perl(not jperl) 5.00503 or later. (\$^X==$^X)";
}
if (CORE::ord('A') == 193) {
die __FILE__, ": is not US-ASCII script (may be EBCDIC or EBCDIK script).";
}
if (CORE::ord('A') != 0x41) {
die __FILE__, ": is not US-ASCII script (must be US-ASCII script).";
}
}
# 12.3. Delaying use Until Runtime
# in Chapter 12. Packages, Libraries, and Modules
# of ISBN 0-596-00313-7 Perl Cookbook, 2nd Edition.
# (and so on)
BEGIN { eval q{ use vars qw($VERSION) } }
$VERSION = sprintf '%d.%02d', q$Revision: 0.86 $ =~ /(\d+)/xmsg;
BEGIN {
my $PERL5LIB = __FILE__;
# DOS-like system
if ($^O =~ /\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) {
$PERL5LIB =~ s{[^/]*$}{Char::Latin5};
}
# UNIX-like system
else {
$PERL5LIB =~ s{[^/]*$}{Char::Latin5};
}
my @inc = ();
my %inc = ();
for my $path ($PERL5LIB, @INC) {
if (not exists $inc{$path}) {
push @inc, $path;
$inc{$path} = 1;
}
}
@INC = @inc;
}
BEGIN {
# instead of utf8.pm
eval q{
no warnings qw(redefine);
*utf8::upgrade = sub { CORE::length $_[0] };
*utf8::downgrade = sub { 1 };
*utf8::encode = sub { };
*utf8::decode = sub { 1 };
*utf8::is_utf8 = sub { };
*utf8::valid = sub { 1 };
};
if ($@) {
*utf8::upgrade = sub { CORE::length $_[0] };
*utf8::downgrade = sub { 1 };
*utf8::encode = sub { };
*utf8::decode = sub { 1 };
*utf8::is_utf8 = sub { };
*utf8::valid = sub { 1 };
}
}
# poor Symbol.pm - substitute of real Symbol.pm
BEGIN {
my $genpkg = "Symbol::";
my $genseq = 0;
sub gensym () {
my $name = "GEN" . $genseq++;
# here, no strict qw(refs); if strict.pm exists
my $ref = \*{$genpkg . $name};
delete $$genpkg{$name};
return $ref;
}
sub qualify ($;$) {
my ($name) = @_;
if (!ref($name) && (Char::Elatin5::index($name, '::') == -1) && (Char::Elatin5::index($name, "'") == -1)) {
my $pkg;
my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT DATA);
# Global names: special character, "^xyz", or other.
if ($name =~ /^(([^a-z])|(\^[a-z_]+))\z/i || $global{$name}) {
# RGS 2001-11-05 : translate leading ^X to control-char
$name =~ s/^\^([a-z_])/'qq(\c'.$1.')'/eei;
$pkg = "main";
}
else {
$pkg = (@_ > 1) ? $_[1] : caller;
}
$name = $pkg . "::" . $name;
}
return $name;
}
sub qualify_to_ref ($;$) {
# here, no strict qw(refs); if strict.pm exists
return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
}
}
# Column: local $@
# in Chapter 9. Osaete okitai Perl no kiso
# of ISBN 10: 4798119172 | ISBN 13: 978-4798119175 MODAN Perl NYUMON
# (and so on)
# use strict; if strict.pm exists
BEGIN {
if (eval { local $@; CORE::require strict }) {
strict::->import;
}
}
# P.714 29.2.39. flock
# in Chapter 29: Functions
# of ISBN 0-596-00027-8 Programming Perl Third Edition.
# P.863 flock
# in Chapter 27: Functions
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
sub LOCK_SH() {1}
sub LOCK_EX() {2}
sub LOCK_UN() {8}
sub LOCK_NB() {4}
# instead of Carp.pm
sub carp;
sub croak;
sub cluck;
sub confess;
my $your_char = q{[\x00-\xFF]};
# regexp of character
my $q_char = qr/$your_char/oxms;
#
# Latin-5 character range per length
#
my %range_tr = ();
#
# alias of encoding name
#
BEGIN { eval q{ use vars qw($encoding_alias) } }
#
# Latin-5 case conversion
#
my %lc = ();
@lc{qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)} =
qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
my %uc = ();
@uc{qw(a b c d e f g h i j k l m n o p q r s t u v w x y z)} =
qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
my %fc = ();
@fc{qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)} =
qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
if (0) {
}
elsif (__PACKAGE__ =~ / \b Elatin5 \z/oxms) {
%range_tr = (
1 => [ [0x00..0xFF],
],
);
$encoding_alias = qr/ \b (?: iso[-_ ]?8859-9 | iec[- ]?8859-9 | latin-?5 ) \b /oxmsi;
%lc = (%lc,
"\xC0" => "\xE0", # LATIN LETTER A WITH GRAVE
"\xC1" => "\xE1", # LATIN LETTER A WITH ACUTE
"\xC2" => "\xE2", # LATIN LETTER A WITH CIRCUMFLEX
"\xC3" => "\xE3", # LATIN LETTER A WITH TILDE
"\xC4" => "\xE4", # LATIN LETTER A WITH DIAERESIS
"\xC5" => "\xE5", # LATIN LETTER A WITH RING ABOVE
"\xC6" => "\xE6", # LATIN LETTER AE
"\xC7" => "\xE7", # LATIN LETTER C WITH CEDILLA
"\xC8" => "\xE8", # LATIN LETTER E WITH GRAVE
"\xC9" => "\xE9", # LATIN LETTER E WITH ACUTE
"\xCA" => "\xEA", # LATIN LETTER E WITH CIRCUMFLEX
"\xCB" => "\xEB", # LATIN LETTER E WITH DIAERESIS
"\xCC" => "\xEC", # LATIN LETTER I WITH GRAVE
"\xCD" => "\xED", # LATIN LETTER I WITH ACUTE
"\xCE" => "\xEE", # LATIN LETTER I WITH CIRCUMFLEX
"\xCF" => "\xEF", # LATIN LETTER I WITH DIAERESIS
"\xD0" => "\xF0", # LATIN LETTER G WITH BREVE
"\xD1" => "\xF1", # LATIN LETTER N WITH TILDE
"\xD2" => "\xF2", # LATIN LETTER O WITH GRAVE
"\xD3" => "\xF3", # LATIN LETTER O WITH ACUTE
"\xD4" => "\xF4", # LATIN LETTER O WITH CIRCUMFLEX
"\xD5" => "\xF5", # LATIN LETTER O WITH TILDE
"\xD6" => "\xF6", # LATIN LETTER O WITH DIAERESIS
"\xD8" => "\xF8", # LATIN LETTER O WITH STROKE
"\xD9" => "\xF9", # LATIN LETTER U WITH GRAVE
"\xDA" => "\xFA", # LATIN LETTER U WITH ACUTE
"\xDB" => "\xFB", # LATIN LETTER U WITH CIRCUMFLEX
"\xDC" => "\xFC", # LATIN LETTER U WITH DIAERESIS
"\xDE" => "\xFE", # LATIN LETTER S WITH CEDILLA
);
%uc = (%uc,
"\xE0" => "\xC0", # LATIN LETTER A WITH GRAVE
"\xE1" => "\xC1", # LATIN LETTER A WITH ACUTE
"\xE2" => "\xC2", # LATIN LETTER A WITH CIRCUMFLEX
"\xE3" => "\xC3", # LATIN LETTER A WITH TILDE
"\xE4" => "\xC4", # LATIN LETTER A WITH DIAERESIS
"\xE5" => "\xC5", # LATIN LETTER A WITH RING ABOVE
"\xE6" => "\xC6", # LATIN LETTER AE
"\xE7" => "\xC7", # LATIN LETTER C WITH CEDILLA
"\xE8" => "\xC8", # LATIN LETTER E WITH GRAVE
"\xE9" => "\xC9", # LATIN LETTER E WITH ACUTE
"\xEA" => "\xCA", # LATIN LETTER E WITH CIRCUMFLEX
"\xEB" => "\xCB", # LATIN LETTER E WITH DIAERESIS
"\xEC" => "\xCC", # LATIN LETTER I WITH GRAVE
"\xED" => "\xCD", # LATIN LETTER I WITH ACUTE
"\xEE" => "\xCE", # LATIN LETTER I WITH CIRCUMFLEX
"\xEF" => "\xCF", # LATIN LETTER I WITH DIAERESIS
"\xF0" => "\xD0", # LATIN LETTER G WITH BREVE
"\xF1" => "\xD1", # LATIN LETTER N WITH TILDE
"\xF2" => "\xD2", # LATIN LETTER O WITH GRAVE
"\xF3" => "\xD3", # LATIN LETTER O WITH ACUTE
"\xF4" => "\xD4", # LATIN LETTER O WITH CIRCUMFLEX
"\xF5" => "\xD5", # LATIN LETTER O WITH TILDE
"\xF6" => "\xD6", # LATIN LETTER O WITH DIAERESIS
"\xF8" => "\xD8", # LATIN LETTER O WITH STROKE
"\xF9" => "\xD9", # LATIN LETTER U WITH GRAVE
"\xFA" => "\xDA", # LATIN LETTER U WITH ACUTE
"\xFB" => "\xDB", # LATIN LETTER U WITH CIRCUMFLEX
"\xFC" => "\xDC", # LATIN LETTER U WITH DIAERESIS
"\xFE" => "\xDE", # LATIN LETTER S WITH CEDILLA
);
%fc = (%fc,
"\xC0" => "\xE0", # LATIN CAPITAL LETTER A WITH GRAVE --> LATIN SMALL LETTER A WITH GRAVE
"\xC1" => "\xE1", # LATIN CAPITAL LETTER A WITH ACUTE --> LATIN SMALL LETTER A WITH ACUTE
"\xC2" => "\xE2", # LATIN CAPITAL LETTER A WITH CIRCUMFLEX --> LATIN SMALL LETTER A WITH CIRCUMFLEX
"\xC3" => "\xE3", # LATIN CAPITAL LETTER A WITH TILDE --> LATIN SMALL LETTER A WITH TILDE
"\xC4" => "\xE4", # LATIN CAPITAL LETTER A WITH DIAERESIS --> LATIN SMALL LETTER A WITH DIAERESIS
"\xC5" => "\xE5", # LATIN CAPITAL LETTER A WITH RING ABOVE --> LATIN SMALL LETTER A WITH RING ABOVE
"\xC6" => "\xE6", # LATIN CAPITAL LETTER AE --> LATIN SMALL LETTER AE
"\xC7" => "\xE7", # LATIN CAPITAL LETTER C WITH CEDILLA --> LATIN SMALL LETTER C WITH CEDILLA
"\xC8" => "\xE8", # LATIN CAPITAL LETTER E WITH GRAVE --> LATIN SMALL LETTER E WITH GRAVE
"\xC9" => "\xE9", # LATIN CAPITAL LETTER E WITH ACUTE --> LATIN SMALL LETTER E WITH ACUTE
"\xCA" => "\xEA", # LATIN CAPITAL LETTER E WITH CIRCUMFLEX --> LATIN SMALL LETTER E WITH CIRCUMFLEX
"\xCB" => "\xEB", # LATIN CAPITAL LETTER E WITH DIAERESIS --> LATIN SMALL LETTER E WITH DIAERESIS
"\xCC" => "\xEC", # LATIN CAPITAL LETTER I WITH GRAVE --> LATIN SMALL LETTER I WITH GRAVE
"\xCD" => "\xED", # LATIN CAPITAL LETTER I WITH ACUTE --> LATIN SMALL LETTER I WITH ACUTE
"\xCE" => "\xEE", # LATIN CAPITAL LETTER I WITH CIRCUMFLEX --> LATIN SMALL LETTER I WITH CIRCUMFLEX
"\xCF" => "\xEF", # LATIN CAPITAL LETTER I WITH DIAERESIS --> LATIN SMALL LETTER I WITH DIAERESIS
"\xD0" => "\xF0", # LATIN CAPITAL LETTER G WITH BREVE --> LATIN SMALL LETTER G WITH BREVE
"\xD1" => "\xF1", # LATIN CAPITAL LETTER N WITH TILDE --> LATIN SMALL LETTER N WITH TILDE
"\xD2" => "\xF2", # LATIN CAPITAL LETTER O WITH GRAVE --> LATIN SMALL LETTER O WITH GRAVE
"\xD3" => "\xF3", # LATIN CAPITAL LETTER O WITH ACUTE --> LATIN SMALL LETTER O WITH ACUTE
"\xD4" => "\xF4", # LATIN CAPITAL LETTER O WITH CIRCUMFLEX --> LATIN SMALL LETTER O WITH CIRCUMFLEX
"\xD5" => "\xF5", # LATIN CAPITAL LETTER O WITH TILDE --> LATIN SMALL LETTER O WITH TILDE
"\xD6" => "\xF6", # LATIN CAPITAL LETTER O WITH DIAERESIS --> LATIN SMALL LETTER O WITH DIAERESIS
"\xD8" => "\xF8", # LATIN CAPITAL LETTER O WITH STROKE --> LATIN SMALL LETTER O WITH STROKE
"\xD9" => "\xF9", # LATIN CAPITAL LETTER U WITH GRAVE --> LATIN SMALL LETTER U WITH GRAVE
"\xDA" => "\xFA", # LATIN CAPITAL LETTER U WITH ACUTE --> LATIN SMALL LETTER U WITH ACUTE
"\xDB" => "\xFB", # LATIN CAPITAL LETTER U WITH CIRCUMFLEX --> LATIN SMALL LETTER U WITH CIRCUMFLEX
"\xDC" => "\xFC", # LATIN CAPITAL LETTER U WITH DIAERESIS --> LATIN SMALL LETTER U WITH DIAERESIS
# CaseFolding-6.1.0.txt
# Date: 2011-07-25, 21:21:56 GMT [MD]
#
# T: special case for uppercase I and dotted uppercase I
# - For non-Turkic languages, this mapping is normally not used.
# - For Turkic languages (tr, az), this mapping can be used instead of the normal mapping for these characters.
# Note that the Turkic mappings do not maintain canonical equivalence without additional processing.
# See the discussions of case mapping in the Unicode Standard for more information.
#-------------------------------------------------------------------------------
"\xDD" => "\x69", # LATIN CAPITAL LETTER I WITH DOT ABOVE
# --> LATIN SMALL LETTER I (without COMBINING DOT ABOVE)
#-------------------------------------------------------------------------------
"\xDE" => "\xFE", # LATIN CAPITAL LETTER S WITH CEDILLA --> LATIN SMALL LETTER S WITH CEDILLA
"\xDF" => "\x73\x73", # LATIN SMALL LETTER SHARP S --> LATIN SMALL LETTER S, LATIN SMALL LETTER S
);
}
else {
croak "Don't know my package name '@{[__PACKAGE__]}'";
}
#
# @ARGV wildcard globbing
#
sub import {
if ($^O =~ /\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) {
my @argv = ();
for (@ARGV) {
# has space
if (/\A (?:$q_char)*? [ ] /oxms) {
if (my @glob = Char::Elatin5::glob(qq{"$_"})) {
push @argv, @glob;
}
else {
push @argv, $_;
}
}
# has wildcard metachar
elsif (/\A (?:$q_char)*? [*?] /oxms) {
if (my @glob = Char::Elatin5::glob($_)) {
push @argv, @glob;
}
else {
push @argv, $_;
}
}
# no wildcard globbing
else {
push @argv, $_;
}
}
@ARGV = @argv;
}
}
# P.230 Care with Prototypes
# in Chapter 6: Subroutines
# of ISBN 0-596-00027-8 Programming Perl Third Edition.
#
# If you aren't careful, you can get yourself into trouble with prototypes.
# But if you are careful, you can do a lot of neat things with them. This is
# all very powerful, of course, and should only be used in moderation to make
# the world a better place.
# P.332 Care with Prototypes
# in Chapter 7: Subroutines
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
#
# If you aren't careful, you can get yourself into trouble with prototypes.
# But if you are careful, you can do a lot of neat things with them. This is
# all very powerful, of course, and should only be used in moderation to make
# the world a better place.
#
# Prototypes of subroutines
#
sub unimport {}
sub Char::Elatin5::split(;$$$);
sub Char::Elatin5::tr($$$$;$);
sub Char::Elatin5::chop(@);
sub Char::Elatin5::index($$;$);
sub Char::Elatin5::rindex($$;$);
sub Char::Elatin5::lcfirst(@);
sub Char::Elatin5::lcfirst_();
sub Char::Elatin5::lc(@);
sub Char::Elatin5::lc_();
sub Char::Elatin5::ucfirst(@);
sub Char::Elatin5::ucfirst_();
sub Char::Elatin5::uc(@);
sub Char::Elatin5::uc_();
sub Char::Elatin5::fc(@);
sub Char::Elatin5::fc_();
sub Char::Elatin5::ignorecase;
sub Char::Elatin5::classic_character_class;
sub Char::Elatin5::capture;
sub Char::Elatin5::chr(;$);
sub Char::Elatin5::chr_();
sub Char::Elatin5::glob($);
sub Char::Elatin5::glob_();
sub Char::Latin5::ord(;$);
sub Char::Latin5::ord_();
sub Char::Latin5::reverse(@);
sub Char::Latin5::getc(;*@);
sub Char::Latin5::length(;$);
sub Char::Latin5::substr($$;$$);
sub Char::Latin5::index($$;$);
sub Char::Latin5::rindex($$;$);
#
# Character class
#
BEGIN { eval q{ use vars qw(
$anchor
$dot
$dot_s
$eD
$eS
$eW
$eH
$eV
$eR
$eN
$not_alnum
$not_alpha
$not_ascii
$not_blank
$not_cntrl
$not_digit
$not_graph
$not_lower
$not_lower_i
$not_print
$not_punct
$not_space
$not_upper
$not_upper_i
$not_word
$not_xdigit
$eb
$eB
) } }
${Char::Elatin5::anchor} = qr{\G(?:[\x00-\xFF])*?};
${Char::Elatin5::dot} = qr{(?:[^\x0A])};
${Char::Elatin5::dot_s} = qr{(?:[\x00-\xFF])};
${Char::Elatin5::eD} = qr{(?:[^0-9])};
${Char::Elatin5::eS} = qr{(?:[^\x09\x0A\x0C\x0D\x20])};
# Incompatible Changes
# \s in regular expressions now matches a Vertical Tab (experimental)
# http://search.cpan.org/~zefram/perl-5.17.0/pod/perldelta.pod
# ${Char::Elatin5::eS} = qr{(?:[^\x09\x0A\x0B\x0C\x0D\x20])};
${Char::Elatin5::eW} = qr{(?:[^0-9A-Z_a-z])};
${Char::Elatin5::eH} = qr{(?:[^\x09\x20])};
${Char::Elatin5::eV} = qr{(?:[^\x0A\x0B\x0C\x0D])};
${Char::Elatin5::eR} = qr{(?:\x0D\x0A|[\x0A\x0D])};
${Char::Elatin5::eN} = qr{(?:[^\x0A])};
${Char::Elatin5::not_alnum} = qr{(?:[^\x30-\x39\x41-\x5A\x61-\x7A])};
${Char::Elatin5::not_alpha} = qr{(?:[^\x41-\x5A\x61-\x7A])};
${Char::Elatin5::not_ascii} = qr{(?:[^\x00-\x7F])};
${Char::Elatin5::not_blank} = qr{(?:[^\x09\x20])};
${Char::Elatin5::not_cntrl} = qr{(?:[^\x00-\x1F\x7F])};
${Char::Elatin5::not_digit} = qr{(?:[^\x30-\x39])};
${Char::Elatin5::not_graph} = qr{(?:[^\x21-\x7F])};
${Char::Elatin5::not_lower} = qr{(?:[^\x61-\x7A])};
${Char::Elatin5::not_lower_i} = qr{(?:[\x00-\xFF])};
${Char::Elatin5::not_print} = qr{(?:[^\x20-\x7F])};
${Char::Elatin5::not_punct} = qr{(?:[^\x21-\x2F\x3A-\x3F\x40\x5B-\x5F\x60\x7B-\x7E])};
${Char::Elatin5::not_space} = qr{(?:[^\x09\x0A\x0B\x0C\x0D\x20])};
${Char::Elatin5::not_upper} = qr{(?:[^\x41-\x5A])};
${Char::Elatin5::not_upper_i} = qr{(?:[\x00-\xFF])};
${Char::Elatin5::not_word} = qr{(?:[^\x30-\x39\x41-\x5A\x5F\x61-\x7A])};
${Char::Elatin5::not_xdigit} = qr{(?:[^\x30-\x39\x41-\x46\x61-\x66])};
${Char::Elatin5::eb} = qr{(?:\A(?=[0-9A-Z_a-z])|(?<=[\x00-\x2F\x40\x5B-\x5E\x60\x7B-\xFF])(?=[0-9A-Z_a-z])|(?<=[0-9A-Z_a-z])(?=[\x00-\x2F\x40\x5B-\x5E\x60\x7B-\xFF]|\z))};
${Char::Elatin5::eB} = qr{(?:(?<=[0-9A-Z_a-z])(?=[0-9A-Z_a-z])|(?<=[\x00-\x2F\x40\x5B-\x5E\x60\x7B-\xFF])(?=[\x00-\x2F\x40\x5B-\x5E\x60\x7B-\xFF]))};
#
# Latin-5 split
#
sub Char::Elatin5::split(;$$$) {
# P.794 29.2.161. split
# in Chapter 29: Functions
# of ISBN 0-596-00027-8 Programming Perl Third Edition.
# P.951 split
# in Chapter 27: Functions
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
my $pattern = $_[0];
my $string = $_[1];
my $limit = $_[2];
# if $string is omitted, the function splits the $_ string
if (not defined $string) {
if (defined $_) {
$string = $_;
}
else {
$string = '';
}
}
my @split = ();
# when string is empty
if ($string eq '') {
# resulting list value in list context
if (wantarray) {
return @split;
}
# count of substrings in scalar context
else {
carp "Use of implicit split to \@_ is deprecated" if $^W;
@_ = @split;
return scalar @_;
}
}
# if $limit is negative, it is treated as if an arbitrarily large $limit has been specified
if ((not defined $limit) or ($limit <= 0)) {
# if $pattern is also omitted or is the literal space, " ", the function splits
# on whitespace, /\s+/, after skipping any leading whitespace
# (and so on)
if ((not defined $pattern) or ($pattern eq ' ')) {
$string =~ s/ \A \s+ //oxms;
# P.1024 Appendix W.10 Multibyte Processing
# of ISBN 1-56592-224-7 CJKV Information Processing
# (and so on)
# the //m modifier is assumed when you split on the pattern /^/
# (and so on)
while ($string =~ s/\A((?:$q_char)*?)\s+//m) {
# if the $pattern contains parentheses, then the substring matched by each pair of parentheses
# is included in the resulting list, interspersed with the fields that are ordinarily returned
# (and so on)
local $@;
for (my $digit=1; $digit <= 1; $digit++) {
push @split, eval('$' . $digit);
}
}
}
# a pattern capable of matching either the null string or something longer than the
# null string will split the value of $string into separate characters wherever it
# matches the null string between characters
# (and so on)
elsif ('' =~ / \A $pattern \z /xms) {
my $last_subexpression_offsets = _last_subexpression_offsets($pattern);
while ($string =~ s/\A((?:$q_char)+?)$pattern//m) {
local $@;
for (my $digit=1; $digit <= ($last_subexpression_offsets + 1); $digit++) {
push @split, eval('$' . $digit);
}
}
}
else {
my $last_subexpression_offsets = _last_subexpression_offsets($pattern);
while ($string =~ s/\A((?:$q_char)*?)$pattern//m) {
local $@;
for (my $digit=1; $digit <= ($last_subexpression_offsets + 1); $digit++) {
push @split, eval('$' . $digit);
}
}
}
}
else {
if ((not defined $pattern) or ($pattern eq ' ')) {
$string =~ s/ \A \s+ //oxms;
while ((--$limit > 0) and (CORE::length($string) > 0)) {
if ($string =~ s/\A((?:$q_char)*?)\s+//m) {
local $@;
for (my $digit=1; $digit <= 1; $digit++) {
push @split, eval('$' . $digit);
}
}
}
}
elsif ('' =~ / \A $pattern \z /xms) {
my $last_subexpression_offsets = _last_subexpression_offsets($pattern);
while ((--$limit > 0) and (CORE::length($string) > 0)) {
if ($string =~ s/\A((?:$q_char)+?)$pattern//m) {
local $@;
for (my $digit=1; $digit <= ($last_subexpression_offsets + 1); $digit++) {
push @split, eval('$' . $digit);
}
}
}
}
else {
my $last_subexpression_offsets = _last_subexpression_offsets($pattern);
while ((--$limit > 0) and (CORE::length($string) > 0)) {
if ($string =~ s/\A((?:$q_char)*?)$pattern//m) {
local $@;
for (my $digit=1; $digit <= ($last_subexpression_offsets + 1); $digit++) {
push @split, eval('$' . $digit);
}
}
}
}
}
push @split, $string;
# if $limit is omitted or zero, trailing null fields are stripped from the result
if ((not defined $limit) or ($limit == 0)) {
while ((scalar(@split) >= 1) and ($split[-1] eq '')) {
pop @split;
}
}
# resulting list value in list context
if (wantarray) {
return @split;
}
# count of substrings in scalar context
else {
carp "Use of implicit split to \@_ is deprecated" if $^W;
@_ = @split;
return scalar @_;
}
}
#
# get last subexpression offsets
#
sub _last_subexpression_offsets {
my $pattern = $_[0];
# remove comment
$pattern =~ s/\(\?\# .*? \)//oxmsg;
my $modifier = '';
if ($pattern =~ /\(\?\^? ([\-A-Za-z]+) :/oxms) {
$modifier = $1;
$modifier =~ s/-[A-Za-z]*//;
}
# with /x modifier
my @char = ();
if ($modifier =~ /x/oxms) {
@char = $pattern =~ /\G(
\\ (?:$q_char) |
\# (?:$q_char)*? $ |
\[ (?: \\\] | (?:$q_char))+? \] |
\(\? |
(?:$q_char)
)/oxmsg;
}
# without /x modifier
else {
@char = $pattern =~ /\G(
\\ (?:$q_char) |
\[ (?: \\\] | (?:$q_char))+? \] |
\(\? |
(?:$q_char)
)/oxmsg;
}
return scalar grep { $_ eq '(' } @char;
}
#
# Latin-5 transliteration (tr///)
#
sub Char::Elatin5::tr($$$$;$) {
my $bind_operator = $_[1];
my $searchlist = $_[2];
my $replacementlist = $_[3];
my $modifier = $_[4] || '';
if ($modifier =~ /r/oxms) {
if ($bind_operator =~ / !~ /oxms) {
croak "Using !~ with tr///r doesn't make sense";
}
}
my @char = $_[0] =~ /\G ($q_char) /oxmsg;
my @searchlist = _charlist_tr($searchlist);
my @replacementlist = _charlist_tr($replacementlist);
my %tr = ();
for (my $i=0; $i <= $#searchlist; $i++) {
if (not exists $tr{$searchlist[$i]}) {
if (defined $replacementlist[$i] and ($replacementlist[$i] ne '')) {
$tr{$searchlist[$i]} = $replacementlist[$i];
}
elsif ($modifier =~ /d/oxms) {
$tr{$searchlist[$i]} = '';
}
elsif (defined $replacementlist[-1] and ($replacementlist[-1] ne '')) {
$tr{$searchlist[$i]} = $replacementlist[-1];
}
else {
$tr{$searchlist[$i]} = $searchlist[$i];
}
}
}
my $tr = 0;
my $replaced = '';
if ($modifier =~ /c/oxms) {
while (defined(my $char = shift @char)) {
if (not exists $tr{$char}) {
if (defined $replacementlist[0]) {
$replaced .= $replacementlist[0];
}
$tr++;
if ($modifier =~ /s/oxms) {
while (@char and (not exists $tr{$char[0]})) {
shift @char;
$tr++;
}
}
}
else {
$replaced .= $char;
}
}
}
else {
while (defined(my $char = shift @char)) {
if (exists $tr{$char}) {
$replaced .= $tr{$char};
$tr++;
if ($modifier =~ /s/oxms) {
while (@char and (exists $tr{$char[0]}) and ($tr{$char[0]} eq $tr{$char})) {
shift @char;
$tr++;
}
}
}
else {
$replaced .= $char;
}
}
}
if ($modifier =~ /r/oxms) {
return $replaced;
}
else {
$_[0] = $replaced;
if ($bind_operator =~ / !~ /oxms) {
return not $tr;
}
else {
return $tr;
}
}
}
#
# Latin-5 chop
#
sub Char::Elatin5::chop(@) {
my $chop;
if (@_ == 0) {
my @char = /\G ($q_char) /oxmsg;
$chop = pop @char;
$_ = join '', @char;
}
else {
for (@_) {
my @char = /\G ($q_char) /oxmsg;
$chop = pop @char;
$_ = join '', @char;
}
}
return $chop;
}
#
# Latin-5 index by octet
#
sub Char::Elatin5::index($$;$) {
my($str,$substr,$position) = @_;
$position ||= 0;
my $pos = 0;
while ($pos < CORE::length($str)) {
if (CORE::substr($str,$pos,CORE::length($substr)) eq $substr) {
if ($pos >= $position) {
return $pos;
}
}
if (CORE::substr($str,$pos) =~ /\A ($q_char) /oxms) {
$pos += CORE::length($1);
}
else {
$pos += 1;
}
}
return -1;
}
#
# Latin-5 reverse index
#
sub Char::Elatin5::rindex($$;$) {
my($str,$substr,$position) = @_;
$position ||= CORE::length($str) - 1;
my $pos = 0;
my $rindex = -1;
while (($pos < CORE::length($str)) and ($pos <= $position)) {
if (CORE::substr($str,$pos,CORE::length($substr)) eq $substr) {
$rindex = $pos;
}
if (CORE::substr($str,$pos) =~ /\A ($q_char) /oxms) {
$pos += CORE::length($1);
}
else {
$pos += 1;
}
}
return $rindex;
}
#
# Latin-5 lower case first with parameter
#
sub Char::Elatin5::lcfirst(@) {
if (@_) {
my $s = shift @_;
if (@_ and wantarray) {
return Char::Elatin5::lc(CORE::substr($s,0,1)) . CORE::substr($s,1), @_;
}
else {
return Char::Elatin5::lc(CORE::substr($s,0,1)) . CORE::substr($s,1);
}
}
else {
return Char::Elatin5::lc(CORE::substr($_,0,1)) . CORE::substr($_,1);
}
}
#
# Latin-5 lower case first without parameter
#
sub Char::Elatin5::lcfirst_() {
return Char::Elatin5::lc(CORE::substr($_,0,1)) . CORE::substr($_,1);
}
#
# Latin-5 lower case with parameter
#
sub Char::Elatin5::lc(@) {
if (@_) {
my $s = shift @_;
if (@_ and wantarray) {
return join('', map {defined($lc{$_}) ? $lc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg)), @_;
}
else {
return join('', map {defined($lc{$_}) ? $lc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg));
}
}
else {
return Char::Elatin5::lc_();
}
}
#
# Latin-5 lower case without parameter
#
sub Char::Elatin5::lc_() {
my $s = $_;
return join '', map {defined($lc{$_}) ? $lc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg);
}
#
# Latin-5 upper case first with parameter
#
sub Char::Elatin5::ucfirst(@) {
if (@_) {
my $s = shift @_;
if (@_ and wantarray) {
return Char::Elatin5::uc(CORE::substr($s,0,1)) . CORE::substr($s,1), @_;
}
else {
return Char::Elatin5::uc(CORE::substr($s,0,1)) . CORE::substr($s,1);
}
}
else {
return Char::Elatin5::uc(CORE::substr($_,0,1)) . CORE::substr($_,1);
}
}
#
# Latin-5 upper case first without parameter
#
sub Char::Elatin5::ucfirst_() {
return Char::Elatin5::uc(CORE::substr($_,0,1)) . CORE::substr($_,1);
}
#
# Latin-5 upper case with parameter
#
sub Char::Elatin5::uc(@) {
if (@_) {
my $s = shift @_;
if (@_ and wantarray) {
return join('', map {defined($uc{$_}) ? $uc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg)), @_;
}
else {
return join('', map {defined($uc{$_}) ? $uc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg));
}
}
else {
return Char::Elatin5::uc_();
}
}
#
# Latin-5 upper case without parameter
#
sub Char::Elatin5::uc_() {
my $s = $_;
return join '', map {defined($uc{$_}) ? $uc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg);
}
#
# Latin-5 fold case with parameter
#
sub Char::Elatin5::fc(@) {
if (@_) {
my $s = shift @_;
if (@_ and wantarray) {
return join('', map {defined($fc{$_}) ? $fc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg)), @_;
}
else {
return join('', map {defined($fc{$_}) ? $fc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg));
}
}
else {
return Char::Elatin5::fc_();
}
}
#
# Latin-5 fold case without parameter
#
sub Char::Elatin5::fc_() {
my $s = $_;
return join '', map {defined($fc{$_}) ? $fc{$_} : $_} ($s =~ /\G ($q_char) /oxmsg);
}
#
# Latin-5 regexp capture
#
{
sub Char::Elatin5::capture {
return $_[0];
}
}
#
# Latin-5 regexp ignore case modifier
#
sub Char::Elatin5::ignorecase {
my @string = @_;
my $metachar = qr/[\@\\|[\]{]/oxms;
# ignore case of $scalar or @array
for my $string (@string) {
# split regexp
my @char = $string =~ /\G(
\[\^ |
\\? (?:$q_char)
)/oxmsg;
# unescape character
for (my $i=0; $i <= $#char; $i++) {
next if not defined $char[$i];
# open character class [...]
if ($char[$i] eq '[') {
my $left = $i;
# [] make die "unmatched [] in regexp ..."
if ($char[$i+1] eq ']') {
$i++;
}
while (1) {
if (++$i > $#char) {
croak "Unmatched [] in regexp";
}
if ($char[$i] eq ']') {
my $right = $i;
my @charlist = charlist_qr(@char[$left+1..$right-1], 'i');
# escape character
for my $char (@charlist) {
if (0) {
}
elsif ($char =~ /\A [.|)] \z/oxms) {
$char = $1 . '\\' . $char;
}
}
# [...]
splice @char, $left, $right-$left+1, '(?:' . join('|', @charlist) . ')';
$i = $left;
last;
}
}
}
# open character class [^...]
elsif ($char[$i] eq '[^') {
my $left = $i;
# [^] make die "unmatched [] in regexp ..."
if ($char[$i+1] eq ']') {
$i++;
}
while (1) {
if (++$i > $#char) {
croak "Unmatched [] in regexp";
}
if ($char[$i] eq ']') {
my $right = $i;
my @charlist = charlist_not_qr(@char[$left+1..$right-1], 'i');
# escape character
for my $char (@charlist) {
if (0) {
}
elsif ($char =~ /\A [.|)] \z/oxms) {
$char = '\\' . $char;
}
}
# [^...]
splice @char, $left, $right-$left+1, '(?!' . join('|', @charlist) . ")(?:$your_char)";
$i = $left;
last;
}
}
}
# rewrite classic character class or escape character
elsif (my $char = classic_character_class($char[$i])) {
$char[$i] = $char;
}
# with /i modifier
elsif ($char[$i] =~ /\A [\x00-\xFF] \z/oxms) {
my $uc = Char::Elatin5::uc($char[$i]);
my $fc = Char::Elatin5::fc($char[$i]);
if ($uc ne $fc) {
if (CORE::length($fc) == 1) {
$char[$i] = '[' . $uc . $fc . ']';
}
else {
$char[$i] = '(?:' . $uc . '|' . $fc . ')';
}
}
}
}
# characterize
for (my $i=0; $i <= $#char; $i++) {
next if not defined $char[$i];
if (0) {
}
# quote character before ? + * {
elsif (($i >= 1) and ($char[$i] =~ /\A [\?\+\*\{] \z/oxms)) {
if ($char[$i-1] !~ /\A [\x00-\xFF] \z/oxms) {
$char[$i-1] = '(?:' . $char[$i-1] . ')';
}
}
}
$string = join '', @char;
}
# make regexp string
return @string;
}
#
# classic character class ( \D \S \W \d \s \w \C \X \H \V \h \v \R \N \b \B )
#
sub Char::Elatin5::classic_character_class {
my($char) = @_;
return {
'\D' => '${Char::Elatin5::eD}',
'\S' => '${Char::Elatin5::eS}',
'\W' => '${Char::Elatin5::eW}',
'\d' => '[0-9]',
# Before Perl 5.6, \s only matched the five whitespace characters
# tab, newline, form-feed, carriage return, and the space character
# itself, which, taken together, is the character class [\t\n\f\r ].
# We can still use the ASCII whitespace semantics using this
# software.
# \t \n \f \r space
'\s' => '[\x09\x0A\x0C\x0D\x20]',
# Incompatible Changes
# \s in regular expressions now matches a Vertical Tab (experimental)
# http://search.cpan.org/~zefram/perl-5.17.0/pod/perldelta.pod
# '\s' => '[\x09\x0A\x0B\x0C\x0D\x20]',
'\w' => '[0-9A-Z_a-z]',
'\C' => '[\x00-\xFF]',
'\X' => 'X',
# \h \v \H \V
# P.114 Character Class Shortcuts
# in Chapter 7: In the World of Regular Expressions
# of ISBN 978-0-596-52010-6 Learning Perl, Fifth Edition
# P.357 13.2.3 Whitespace
# in Chapter 13: perlrecharclass: Perl Regular Expression Character Classes
# of ISBN-13: 978-1-906966-02-7 The Perl Language Reference Manual (for Perl version 5.12.1)
#
# 0x00009 CHARACTER TABULATION h s
# 0x0000a LINE FEED (LF) vs
# 0x0000b LINE TABULATION v
# 0x0000c FORM FEED (FF) vs
# 0x0000d CARRIAGE RETURN (CR) vs
# 0x00020 SPACE h s
# P.196 Table 5-9. Alphanumeric regex metasymbols
# in Chapter 5. Pattern Matching
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
# (and so on)
'\H' => '${Char::Elatin5::eH}',
'\V' => '${Char::Elatin5::eV}',
'\h' => '[\x09\x20]',
'\v' => '[\x0A\x0B\x0C\x0D]',
'\R' => '${Char::Elatin5::eR}',
# \N
#
# http://perldoc.perl.org/perlre.html
# Character Classes and other Special Escapes
# Any character but \n (experimental). Not affected by /s modifier
'\N' => '${Char::Elatin5::eN}',
# \b \B
# P.180 Boundaries: The \b and \B Assertions
# in Chapter 5: Pattern Matching
# of ISBN 0-596-00027-8 Programming Perl Third Edition.
# P.219 Boundaries: The \b and \B Assertions
# in Chapter 5: Pattern Matching
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
# '\b' => '(?:(?<=\A|\W)(?=\w)|(?<=\w)(?=\W|\z))',
'\b' => '${Char::Elatin5::eb}',
# '\B' => '(?:(?<=\w)(?=\w)|(?<=\W)(?=\W))',
'\B' => '${Char::Elatin5::eB}',
}->{$char} || '';
}
#
# prepare Latin-5 characters per length
#
# 1 octet characters
my @chars1 = ();
sub chars1 {
if (@chars1) {
return @chars1;
}
if (exists $range_tr{1}) {
my @ranges = @{ $range_tr{1} };
while (my @range = splice(@ranges,0,1)) {
for my $oct0 (@{$range[0]}) {
push @chars1, pack 'C', $oct0;
}
}
}
return @chars1;
}
# 2 octets characters
my @chars2 = ();
sub chars2 {
if (@chars2) {
return @chars2;
}
if (exists $range_tr{2}) {
my @ranges = @{ $range_tr{2} };
while (my @range = splice(@ranges,0,2)) {
for my $oct0 (@{$range[0]}) {
for my $oct1 (@{$range[1]}) {
push @chars2, pack 'CC', $oct0,$oct1;
}
}
}
}
return @chars2;
}
# 3 octets characters
my @chars3 = ();
sub chars3 {
if (@chars3) {
return @chars3;
}
if (exists $range_tr{3}) {
my @ranges = @{ $range_tr{3} };
while (my @range = splice(@ranges,0,3)) {
for my $oct0 (@{$range[0]}) {
for my $oct1 (@{$range[1]}) {
for my $oct2 (@{$range[2]}) {
push @chars3, pack 'CCC', $oct0,$oct1,$oct2;
}
}
}
}
}
return @chars3;
}
# 4 octets characters
my @chars4 = ();
sub chars4 {
if (@chars4) {
return @chars4;
}
if (exists $range_tr{4}) {
my @ranges = @{ $range_tr{4} };
while (my @range = splice(@ranges,0,4)) {
for my $oct0 (@{$range[0]}) {
for my $oct1 (@{$range[1]}) {
for my $oct2 (@{$range[2]}) {
for my $oct3 (@{$range[3]}) {
push @chars4, pack 'CCCC', $oct0,$oct1,$oct2,$oct3;
}
}
}
}
}
}
return @chars4;
}
#
# Latin-5 open character list for tr
#
sub _charlist_tr {
local $_ = shift @_;
# unescape character
my @char = ();
while (not /\G \z/oxmsgc) {
if (/\G (\\0?55|\\x2[Dd]|\\-) /oxmsgc) {
push @char, '\-';
}
elsif (/\G \\ ([0-7]{2,3}) /oxmsgc) {
push @char, CORE::chr(oct $1);
}
elsif (/\G \\x ([0-9A-Fa-f]{1,2}) /oxmsgc) {
push @char, CORE::chr(hex $1);
}
elsif (/\G \\c ([\x40-\x5F]) /oxmsgc) {
push @char, CORE::chr(CORE::ord($1) & 0x1F);
}
elsif (/\G (\\ [0nrtfbae]) /oxmsgc) {
push @char, {
'\0' => "\0",
'\n' => "\n",
'\r' => "\r",
'\t' => "\t",
'\f' => "\f",
'\b' => "\x08", # \b means backspace in character class
'\a' => "\a",
'\e' => "\e",
}->{$1};
}
elsif (/\G \\ ($q_char) /oxmsgc) {
push @char, $1;
}
elsif (/\G ($q_char) /oxmsgc) {
push @char, $1;
}
}
# join separated multiple-octet
@char = join('',@char) =~ /\G (\\-|$q_char) /oxmsg;
# unescape '-'
my @i = ();
for my $i (0 .. $#char) {
if ($char[$i] eq '\-') {
$char[$i] = '-';
}
elsif ($char[$i] eq '-') {
if ((0 < $i) and ($i < $#char)) {
push @i, $i;
}
}
}
# open character list (reverse for splice)
for my $i (CORE::reverse @i) {
my @range = ();
# range error
if ((CORE::length($char[$i-1]) > CORE::length($char[$i+1])) or ($char[$i-1] gt $char[$i+1])) {
croak "Invalid tr/// range \"\\x" . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]) . '"';
}
# range of multiple-octet code
if (CORE::length($char[$i-1]) == 1) {
if (CORE::length($char[$i+1]) == 1) {
push @range, grep {($char[$i-1] le $_) and ($_ le $char[$i+1])} chars1();
}
elsif (CORE::length($char[$i+1]) == 2) {
push @range, grep {$char[$i-1] le $_} chars1();
push @range, grep {$_ le $char[$i+1]} chars2();
}
elsif (CORE::length($char[$i+1]) == 3) {
push @range, grep {$char[$i-1] le $_} chars1();
push @range, chars2();
push @range, grep {$_ le $char[$i+1]} chars3();
}
elsif (CORE::length($char[$i+1]) == 4) {
push @range, grep {$char[$i-1] le $_} chars1();
push @range, chars2();
push @range, chars3();
push @range, grep {$_ le $char[$i+1]} chars4();
}
else {
croak "Invalid tr/// range (over 4octets) \"\\x" . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]) . '"';
}
}
elsif (CORE::length($char[$i-1]) == 2) {
if (CORE::length($char[$i+1]) == 2) {
push @range, grep {($char[$i-1] le $_) and ($_ le $char[$i+1])} chars2();
}
elsif (CORE::length($char[$i+1]) == 3) {
push @range, grep {$char[$i-1] le $_} chars2();
push @range, grep {$_ le $char[$i+1]} chars3();
}
elsif (CORE::length($char[$i+1]) == 4) {
push @range, grep {$char[$i-1] le $_} chars2();
push @range, chars3();
push @range, grep {$_ le $char[$i+1]} chars4();
}
else {
croak "Invalid tr/// range (over 4octets) \"\\x" . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]) . '"';
}
}
elsif (CORE::length($char[$i-1]) == 3) {
if (CORE::length($char[$i+1]) == 3) {
push @range, grep {($char[$i-1] le $_) and ($_ le $char[$i+1])} chars3();
}
elsif (CORE::length($char[$i+1]) == 4) {
push @range, grep {$char[$i-1] le $_} chars3();
push @range, grep {$_ le $char[$i+1]} chars4();
}
else {
croak "Invalid tr/// range (over 4octets) \"\\x" . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]) . '"';
}
}
elsif (CORE::length($char[$i-1]) == 4) {
if (CORE::length($char[$i+1]) == 4) {
push @range, grep {($char[$i-1] le $_) and ($_ le $char[$i+1])} chars4();
}
else {
croak "Invalid tr/// range (over 4octets) \"\\x" . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]) . '"';
}
}
else {
croak "Invalid tr/// range (over 4octets) \"\\x" . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]) . '"';
}
splice @char, $i-1, 3, @range;
}
return @char;
}
#
# Latin-5 open character class
#
sub _cc {
if (scalar(@_) == 0) {
die __FILE__, ": function cc got no parameter.";
}
elsif (scalar(@_) == 1) {
return sprintf('\x%02X',$_[0]);
}
elsif (scalar(@_) == 2) {
if ($_[0] > $_[1]) {
die __FILE__, ": function cc got \$_[0] > \$_[1] parameters).";
}
elsif ($_[0] == $_[1]) {
return sprintf('\x%02X',$_[0]);
}
elsif (($_[0]+1) == $_[1]) {
return sprintf('[\\x%02X\\x%02X]',$_[0],$_[1]);
}
else {
return sprintf('[\\x%02X-\\x%02X]',$_[0],$_[1]);
}
}
else {
die __FILE__, ": function cc got 3 or more parameters (@{[scalar(@_)]} parameters).";
}
}
#
# Latin-5 octet range
#
sub _octets {
my $length = shift @_;
if ($length == 1) {
my($a1) = unpack 'C', $_[0];
my($z1) = unpack 'C', $_[1];
if ($a1 > $z1) {
croak 'Invalid [] range in regexp (CORE::ord(A) > CORE::ord(B)) ' . '\x' . unpack('H*',$a1) . '-\x' . unpack('H*',$z1);
}
if ($a1 == $z1) {
return sprintf('\x%02X',$a1);
}
elsif (($a1+1) == $z1) {
return sprintf('\x%02X\x%02X',$a1,$z1);
}
else {
return sprintf('\x%02X-\x%02X',$a1,$z1);
}
}
else {
die __FILE__, ": function _octets got invalid length ($length).";
}
}
#
# Latin-5 range regexp
#
sub _range_regexp {
my($length,$first,$last) = @_;
my @range_regexp = ();
if (not exists $range_tr{$length}) {
return @range_regexp;
}
my @ranges = @{ $range_tr{$length} };
while (my @range = splice(@ranges,0,$length)) {
my $min = '';
my $max = '';
for (my $i=0; $i < $length; $i++) {
$min .= pack 'C', $range[$i][0];
$max .= pack 'C', $range[$i][-1];
}
# min___max
# FIRST_____________LAST
# (nothing)
if ($max lt $first) {
}
# **********
# min_________max
# FIRST_____________LAST
# **********
elsif (($min le $first) and ($first le $max) and ($max le $last)) {
push @range_regexp, _octets($length,$first,$max,$min,$max);
}
# **********************
# min________________max
# FIRST_____________LAST
# **********************
elsif (($min eq $first) and ($max eq $last)) {
push @range_regexp, _octets($length,$first,$last,$min,$max);
}
# *********
# min___max
# FIRST_____________LAST
# *********
elsif (($first le $min) and ($max le $last)) {
push @range_regexp, _octets($length,$min,$max,$min,$max);
}
# **********************
# min__________________________max
# FIRST_____________LAST
# **********************
elsif (($min le $first) and ($last le $max)) {
push @range_regexp, _octets($length,$first,$last,$min,$max);
}
# *********
# min________max
# FIRST_____________LAST
# *********
elsif (($first le $min) and ($min le $last) and ($last le $max)) {
push @range_regexp, _octets($length,$min,$last,$min,$max);
}
# min___max
# FIRST_____________LAST
# (nothing)
elsif ($last lt $min) {
}
else {
die __FILE__, ": function _range_regexp panic.";
}
}
return @range_regexp;
}
#
# Latin-5 open character list for qr and not qr
#
sub _charlist {
my $modifier = pop @_;
my @char = @_;
my $ignorecase = ($modifier =~ /i/oxms) ? 1 : 0;
# unescape character
for (my $i=0; $i <= $#char; $i++) {
# escape - to ...
if ($char[$i] eq '-') {
if ((0 < $i) and ($i < $#char)) {
$char[$i] = '...';
}
}
# octal escape sequence
elsif ($char[$i] =~ /\A \\o \{ ([0-7]+) \} \z/oxms) {
$char[$i] = octchr($1);
}
# hexadecimal escape sequence
elsif ($char[$i] =~ /\A \\x \{ ([0-9A-Fa-f]+) \} \z/oxms) {
$char[$i] = hexchr($1);
}
# \N{CHARNAME} --> N\{CHARNAME}
elsif ($char[$i] =~ /\A \\ (N) ( \{ ([^0-9\}][^\}]*) \} ) \z/oxms) {
$char[$i] = $1 . '\\' . $2;
}
# \p{PROPERTY} --> p\{PROPERTY}
elsif ($char[$i] =~ /\A \\ (p) ( \{ ([^0-9\}][^\}]*) \} ) \z/oxms) {
$char[$i] = $1 . '\\' . $2;
}
# \P{PROPERTY} --> P\{PROPERTY}
elsif ($char[$i] =~ /\A \\ (P) ( \{ ([^0-9\}][^\}]*) \} ) \z/oxms) {
$char[$i] = $1 . '\\' . $2;
}
# \p, \P, \X --> p, P, X
elsif ($char[$i] =~ /\A \\ ( [pPX] ) \z/oxms) {
$char[$i] = $1;
}
elsif ($char[$i] =~ /\A \\ ([0-7]{2,3}) \z/oxms) {
$char[$i] = CORE::chr oct $1;
}
elsif ($char[$i] =~ /\A \\x ([0-9A-Fa-f]{1,2}) \z/oxms) {
$char[$i] = CORE::chr hex $1;
}
elsif ($char[$i] =~ /\A \\c ([\x40-\x5F]) \z/oxms) {
$char[$i] = CORE::chr(CORE::ord($1) & 0x1F);
}
elsif ($char[$i] =~ /\A (\\ [0nrtfbaedswDSWHVhvR]) \z/oxms) {
$char[$i] = {
'\0' => "\0",
'\n' => "\n",
'\r' => "\r",
'\t' => "\t",
'\f' => "\f",
'\b' => "\x08", # \b means backspace in character class
'\a' => "\a",
'\e' => "\e",
'\d' => '[0-9]',
# \t \n \f \r space
'\s' => '[\x09\x0A\x0C\x0D\x20]',
# Incompatible Changes
# \s in regular expressions now matches a Vertical Tab (experimental)
# http://search.cpan.org/~zefram/perl-5.17.0/pod/perldelta.pod
# '\s' => '[\x09\x0A\x0B\x0C\x0D\x20]',
'\w' => '[0-9A-Z_a-z]',
'\D' => '${Char::Elatin5::eD}',
'\S' => '${Char::Elatin5::eS}',
'\W' => '${Char::Elatin5::eW}',
'\H' => '${Char::Elatin5::eH}',
'\V' => '${Char::Elatin5::eV}',
'\h' => '[\x09\x20]',
'\v' => '[\x0A\x0B\x0C\x0D]',
'\R' => '${Char::Elatin5::eR}',
}->{$1};
}
# POSIX-style character classes
elsif ($ignorecase and ($char[$i] =~ /\A ( \[\: \^? (?:lower|upper) :\] ) \z/oxms)) {
$char[$i] = {
'[:lower:]' => '[\x41-\x5A\x61-\x7A]',
'[:upper:]' => '[\x41-\x5A\x61-\x7A]',
'[:^lower:]' => '${Char::Elatin5::not_lower_i}',
'[:^upper:]' => '${Char::Elatin5::not_upper_i}',
}->{$1};
}
elsif ($char[$i] =~ /\A ( \[\: \^? (?:alnum|alpha|ascii|blank|cntrl|digit|graph|lower|print|punct|space|upper|word|xdigit) :\] ) \z/oxms) {
$char[$i] = {
'[:alnum:]' => '[\x30-\x39\x41-\x5A\x61-\x7A]',
'[:alpha:]' => '[\x41-\x5A\x61-\x7A]',
'[:ascii:]' => '[\x00-\x7F]',
'[:blank:]' => '[\x09\x20]',
'[:cntrl:]' => '[\x00-\x1F\x7F]',
'[:digit:]' => '[\x30-\x39]',
'[:graph:]' => '[\x21-\x7F]',
'[:lower:]' => '[\x61-\x7A]',
'[:print:]' => '[\x20-\x7F]',
'[:punct:]' => '[\x21-\x2F\x3A-\x3F\x40\x5B-\x5F\x60\x7B-\x7E]',
'[:space:]' => '[\x09\x0A\x0B\x0C\x0D\x20]',
'[:upper:]' => '[\x41-\x5A]',
'[:word:]' => '[\x30-\x39\x41-\x5A\x5F\x61-\x7A]',
'[:xdigit:]' => '[\x30-\x39\x41-\x46\x61-\x66]',
'[:^alnum:]' => '${Char::Elatin5::not_alnum}',
'[:^alpha:]' => '${Char::Elatin5::not_alpha}',
'[:^ascii:]' => '${Char::Elatin5::not_ascii}',
'[:^blank:]' => '${Char::Elatin5::not_blank}',
'[:^cntrl:]' => '${Char::Elatin5::not_cntrl}',
'[:^digit:]' => '${Char::Elatin5::not_digit}',
'[:^graph:]' => '${Char::Elatin5::not_graph}',
'[:^lower:]' => '${Char::Elatin5::not_lower}',
'[:^print:]' => '${Char::Elatin5::not_print}',
'[:^punct:]' => '${Char::Elatin5::not_punct}',
'[:^space:]' => '${Char::Elatin5::not_space}',
'[:^upper:]' => '${Char::Elatin5::not_upper}',
'[:^word:]' => '${Char::Elatin5::not_word}',
'[:^xdigit:]' => '${Char::Elatin5::not_xdigit}',
}->{$1};
}
elsif ($char[$i] =~ /\A \\ ($q_char) \z/oxms) {
$char[$i] = $1;
}
}
# open character list
my @singleoctet = ();
my @multipleoctet = ();
for (my $i=0; $i <= $#char; ) {
# escaped -
if (defined($char[$i+1]) and ($char[$i+1] eq '...')) {
$i += 1;
next;
}
# make range regexp
elsif ($char[$i] eq '...') {
# range error
if (CORE::length($char[$i-1]) > CORE::length($char[$i+1])) {
croak 'Invalid [] range in regexp (length(A) > length(B)) ' . '\x' . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]);
}
elsif (CORE::length($char[$i-1]) == CORE::length($char[$i+1])) {
if ($char[$i-1] gt $char[$i+1]) {
croak 'Invalid [] range in regexp (CORE::ord(A) > CORE::ord(B)) ' . '\x' . unpack('H*',$char[$i-1]) . '-\x' . unpack('H*',$char[$i+1]);
}
}
# make range regexp per length
for my $length (CORE::length($char[$i-1]) .. CORE::length($char[$i+1])) {
my @regexp = ();
# is first and last
if (($length == CORE::length($char[$i-1])) and ($length == CORE::length($char[$i+1]))) {
push @regexp, _range_regexp($length, $char[$i-1], $char[$i+1]);
}
# is first
elsif ($length == CORE::length($char[$i-1])) {
push @regexp, _range_regexp($length, $char[$i-1], "\xFF" x $length);
}
# is inside in first and last
elsif ((CORE::length($char[$i-1]) < $length) and ($length < CORE::length($char[$i+1]))) {
push @regexp, _range_regexp($length, "\x00" x $length, "\xFF" x $length);
}
# is last
elsif ($length == CORE::length($char[$i+1])) {
push @regexp, _range_regexp($length, "\x00" x $length, $char[$i+1]);
}
else {
die __FILE__, ": function make_regexp panic.";
}
if ($length == 1) {
push @singleoctet, @regexp;
}
else {
push @multipleoctet, @regexp;
}
}
$i += 2;
}
# with /i modifier
elsif ($char[$i] =~ /\A [\x00-\xFF] \z/oxms) {
if ($modifier =~ /i/oxms) {
my $uc = Char::Elatin5::uc($char[$i]);
my $fc = Char::Elatin5::fc($char[$i]);
if ($uc ne $fc) {
if (CORE::length($fc) == 1) {
push @singleoctet, $uc, $fc;
}
else {
push @singleoctet, $uc;
push @multipleoctet, $fc;
}
}
else {
push @singleoctet, $char[$i];
}
}
else {
push @singleoctet, $char[$i];
}
$i += 1;
}
# single character of single octet code
elsif ($char[$i] =~ /\A (?: \\h ) \z/oxms) {
push @singleoctet, "\t", "\x20";
$i += 1;
}
elsif ($char[$i] =~ /\A (?: \\v ) \z/oxms) {
push @singleoctet, "\x0A", "\x0B", "\x0C", "\x0D";
$i += 1;
}
elsif ($char[$i] =~ /\A (?: \\d | \\s | \\w ) \z/oxms) {
push @singleoctet, $char[$i];
$i += 1;
}
# single character of multiple-octet code
else {
push @multipleoctet, $char[$i];
$i += 1;
}
}
# quote metachar
for (@singleoctet) {
if ($_ eq '...') {
$_ = '-';
}
elsif (/\A \n \z/oxms) {
$_ = '\n';
}
elsif (/\A \r \z/oxms) {
$_ = '\r';
}
elsif (/\A ([\x00-\x20\x7F-\xFF]) \z/oxms) {
$_ = sprintf('\x%02X', CORE::ord $1);
}
elsif (/\A [\x00-\xFF] \z/oxms) {
$_ = quotemeta $_;
}
}
# return character list
return \@singleoctet, \@multipleoctet;
}
#
# Latin-5 octal escape sequence
#
sub octchr {
my($octdigit) = @_;
my @binary = ();
for my $octal (split(//,$octdigit)) {
push @binary, {
'0' => '000',
'1' => '001',
'2' => '010',
'3' => '011',
'4' => '100',
'5' => '101',
'6' => '110',
'7' => '111',
}->{$octal};
}
my $binary = join '', @binary;
my $octchr = {
# 1234567
1 => pack('B*', "0000000$binary"),
2 => pack('B*', "000000$binary"),
3 => pack('B*', "00000$binary"),
4 => pack('B*', "0000$binary"),
5 => pack('B*', "000$binary"),
6 => pack('B*', "00$binary"),
7 => pack('B*', "0$binary"),
0 => pack('B*', "$binary"),
}->{CORE::length($binary) % 8};
return $octchr;
}
#
# Latin-5 hexadecimal escape sequence
#
sub hexchr {
my($hexdigit) = @_;
my $hexchr = {
1 => pack('H*', "0$hexdigit"),
0 => pack('H*', "$hexdigit"),
}->{CORE::length($_[0]) % 2};
return $hexchr;
}
#
# Latin-5 open character list for qr
#
sub charlist_qr {
my $modifier = pop @_;
my @char = @_;
my($singleoctet, $multipleoctet) = _charlist(@char, $modifier);
my @singleoctet = @$singleoctet;
my @multipleoctet = @$multipleoctet;
# return character list
if (scalar(@singleoctet) >= 1) {
# with /i modifier
if ($modifier =~ m/i/oxms) {
my %singleoctet_ignorecase = ();
for (@singleoctet) {
while (s/ \A \\x(..) - \\x(..) //oxms or s/ \A \\x((..)) //oxms) {
for my $ord (hex($1) .. hex($2)) {
my $char = CORE::chr($ord);
my $uc = Char::Elatin5::uc($char);
my $fc = Char::Elatin5::fc($char);
if ($uc eq $fc) {
$singleoctet_ignorecase{unpack 'C*', $char} = 1;
}
else {
if (CORE::length($fc) == 1) {
$singleoctet_ignorecase{unpack 'C*', $uc} = 1;
$singleoctet_ignorecase{unpack 'C*', $fc} = 1;
}
else {
$singleoctet_ignorecase{unpack 'C*', $uc} = 1;
push @multipleoctet, join '', map {sprintf('\x%02X',$_)} unpack 'C*', $fc;
}
}
}
}
}
my $i = 0;
my @singleoctet_ignorecase = ();
for my $ord (0 .. 255) {
if (exists $singleoctet_ignorecase{$ord}) {
push @{$singleoctet_ignorecase[$i]}, $ord;
}
else {
$i++;
}
}
@singleoctet = ();
for my $range (@singleoctet_ignorecase) {
if (ref $range) {
if (scalar(@{$range}) == 1) {
push @singleoctet, sprintf('\x%02X', @{$range}[0]);
}
elsif (scalar(@{$range}) == 2) {
push @singleoctet, sprintf('\x%02X\x%02X', @{$range}[0], @{$range}[-1]);
}
else {
push @singleoctet, sprintf('\x%02X-\x%02X', @{$range}[0], @{$range}[-1]);
}
}
}
}
my $not_anchor = '';
push @multipleoctet, join('', $not_anchor, '[', @singleoctet, ']' );
}
if (scalar(@multipleoctet) >= 2) {
return '(?:' . join('|', @multipleoctet) . ')';
}
else {
return $multipleoctet[0];
}
}
#
# Latin-5 open character list for not qr
#
sub charlist_not_qr {
my $modifier = pop @_;
my @char = @_;
my($singleoctet, $multipleoctet) = _charlist(@char, $modifier);
my @singleoctet = @$singleoctet;
my @multipleoctet = @$multipleoctet;
# with /i modifier
if ($modifier =~ m/i/oxms) {
my %singleoctet_ignorecase = ();
for (@singleoctet) {
while (s/ \A \\x(..) - \\x(..) //oxms or s/ \A \\x((..)) //oxms) {
for my $ord (hex($1) .. hex($2)) {
my $char = CORE::chr($ord);
my $uc = Char::Elatin5::uc($char);
my $fc = Char::Elatin5::fc($char);
if ($uc eq $fc) {
$singleoctet_ignorecase{unpack 'C*', $char} = 1;
}
else {
if (CORE::length($fc) == 1) {
$singleoctet_ignorecase{unpack 'C*', $uc} = 1;
$singleoctet_ignorecase{unpack 'C*', $fc} = 1;
}
else {
$singleoctet_ignorecase{unpack 'C*', $uc} = 1;
push @multipleoctet, join '', map {sprintf('\x%02X',$_)} unpack 'C*', $fc;
}
}
}
}
}
my $i = 0;
my @singleoctet_ignorecase = ();
for my $ord (0 .. 255) {
if (exists $singleoctet_ignorecase{$ord}) {
push @{$singleoctet_ignorecase[$i]}, $ord;
}
else {
$i++;
}
}
@singleoctet = ();
for my $range (@singleoctet_ignorecase) {
if (ref $range) {
if (scalar(@{$range}) == 1) {
push @singleoctet, sprintf('\x%02X', @{$range}[0]);
}
elsif (scalar(@{$range}) == 2) {
push @singleoctet, sprintf('\x%02X\x%02X', @{$range}[0], @{$range}[-1]);
}
else {
push @singleoctet, sprintf('\x%02X-\x%02X', @{$range}[0], @{$range}[-1]);
}
}
}
}
# return character list
if (scalar(@multipleoctet) >= 1) {
if (scalar(@singleoctet) >= 1) {
# any character other than multiple-octet and single octet character class
return '(?!' . join('|', @multipleoctet) . ')(?:[^' . join('', @singleoctet) . '])';
}
else {
# any character other than multiple-octet character class
return '(?!' . join('|', @multipleoctet) . ")(?:$your_char)";
}
}
else {
if (scalar(@singleoctet) >= 1) {
# any character other than single octet character class
return '(?:[^' . join('', @singleoctet) . '])';
}
else {
# any character
return "(?:$your_char)";
}
}
}
#
# open file in read mode
#
sub _open_r {
my(undef,$file) = @_;
$file =~ s#\A ([\x09\x0A\x0C\x0D\x20]) #./$1#oxms;
return eval(q{open($_[0],'<',$_[1])}) ||
open($_[0],"< $file\0");
}
#
# open file in write mode
#
sub _open_w {
my(undef,$file) = @_;
$file =~ s#\A ([\x09\x0A\x0C\x0D\x20]) #./$1#oxms;
return eval(q{open($_[0],'>',$_[1])}) ||
open($_[0],"> $file\0");
}
#
# open file in append mode
#
sub _open_a {
my(undef,$file) = @_;
$file =~ s#\A ([\x09\x0A\x0C\x0D\x20]) #./$1#oxms;
return eval(q{open($_[0],'>>',$_[1])}) ||
open($_[0],">> $file\0");
}
#
# safe system
#
sub _systemx {
# P.707 29.2.33. exec
# in Chapter 29: Functions
# of ISBN 0-596-00027-8 Programming Perl Third Edition.
#
# Be aware that in older releases of Perl, exec (and system) did not flush
# your output buffer, so you needed to enable command buffering by setting $|
# on one or more filehandles to avoid lost output in the case of exec, or
# misordererd output in the case of system. This situation was largely remedied
# in the 5.6 release of Perl. (So, 5.005 release not yet.)
# P.855 exec
# in Chapter 27: Functions
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
#
# In very old release of Perl (before v5.6), exec (and system) did not flush
# your output buffer, so you needed to enable command buffering by setting $|
# on one or more filehandles to avoid lost output with exec or misordered
# output with system.
$| = 1;
# P.565 23.1.2. Cleaning Up Your Environment
# in Chapter 23: Security
# of ISBN 0-596-00027-8 Programming Perl Third Edition.
# P.656 Cleaning Up Your Environment
# in Chapter 20: Security
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
# local $ENV{'PATH'} = '.';
local @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer
# P.707 29.2.33. exec
# in Chapter 29: Functions
# of ISBN 0-596-00027-8 Programming Perl Third Edition.
#
# As we mentioned earlier, exec treats a discrete list of arguments as an
# indication that it should bypass shell processing. However, there is one
# place where you might still get tripped up. The exec call (and system, too)
# will not distinguish between a single scalar argument and an array containing
# only one element.
#
# @args = ("echo surprise"); # just one element in list
# exec @args # still subject to shell escapes
# or die "exec: $!"; # because @args == 1
#
# To avoid this, you can use the PATHNAME syntax, explicitly duplicating the
# first argument as the pathname, which forces the rest of the arguments to be
# interpreted as a list, even if there is only one of them:
#
# exec { $args[0] } @args # safe even with one-argument list
# or die "can't exec @args: $!";
# P.855 exec
# in Chapter 27: Functions
# of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
#
# As we mentioned earlier, exec treats a discrete list of arguments as a
# directive to bypass shell processing. However, there is one place where
# you might still get tripped up. The exec call (and system, too) cannot
# distinguish between a single scalar argument and an array containing
# only one element.
#
# @args = ("echo surprise"); # just one element in list
# exec @args # still subject to shell escapes
# || die "exec: $!"; # because @args == 1
#
# To avoid this, use the PATHNAME syntax, explicitly duplicating the first
# argument as the pathname, which forces the rest of the arguments to be
# interpreted as a list, even if there is only one of them:
#
# exec { $args[0] } @args # safe even with one-argument list
# || die "can't exec @args: $!";
return CORE::system { $_[0] } @_; # safe even with one-argument list
}
#
# Latin-5 order to character (with parameter)
#
sub Char::Elatin5::chr(;$) {
my $c = @_ ? $_[0] : $_;
if ($c == 0x00) {
return "\x00";
}
else {
my @chr = ();
while ($c > 0) {
unshift @chr, ($c % 0x100);
$c = int($c / 0x100);
}
return pack 'C*', @chr;
}
}
#
# Latin-5 order to character (without parameter)
#
sub Char::Elatin5::chr_() {
my $c = $_;
if ($c == 0x00) {
return "\x00";
}
else {
my @chr = ();
while ($c > 0) {
unshift @chr, ($c % 0x100);
$c = int($c / 0x100);
}
return pack 'C*', @chr;
}
}
#
# Latin-5 path globbing (with parameter)
#
sub Char::Elatin5::glob($) {
if (wantarray) {
my @glob = _DOS_like_glob(@_);
for my $glob (@glob) {
$glob =~ s{ \A (?:\./)+ }{}oxms;
}
return @glob;
}
else {
my $glob = _DOS_like_glob(@_);
$glob =~ s{ \A (?:\./)+ }{}oxms;
return $glob;
}
}
#
# Latin-5 path globbing (without parameter)
#
sub Char::Elatin5::glob_() {
if (wantarray) {
my @glob = _DOS_like_glob();
for my $glob (@glob) {
$glob =~ s{ \A (?:\./)+ }{}oxms;
}
return @glob;
}
else {
my $glob = _DOS_like_glob();
$glob =~ s{ \A (?:\./)+ }{}oxms;
return $glob;
}
}
#
# Latin-5 path globbing from File::DosGlob module
#
# Often I confuse "_dosglob" and "_doglob".
# So, I renamed "_dosglob" to "_DOS_like_glob".
#
my %iter;
my %entries;
sub _DOS_like_glob {
# context (keyed by second cxix argument provided by core)
my($expr,$cxix) = @_;
# glob without args defaults to $_
$expr = $_ if not defined $expr;
# represents the current user's home directory
#
# 7.3. Expanding Tildes in Filenames
# in Chapter 7. File Access
# of ISBN 0-596-00313-7 Perl Cookbook, 2nd Edition.
#
# and File::HomeDir, File::HomeDir::Windows module
# DOS-like system
if ($^O =~ /\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) {
$expr =~ s{ \A ~ (?= [^/\\] ) }
{ $ENV{'HOME'} || $ENV{'USERPROFILE'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}" }oxmse;
}
# UNIX-like system
else {
$expr =~ s{ \A ~ ( (?:[^/])* ) }
{ $1 ? (getpwnam($1))[7] : ($ENV{'HOME'} || $ENV{'LOGDIR'} || (getpwuid($<))[7]) }oxmse;
}
# assume global context if not provided one
$cxix = '_G_' if not defined $cxix;
$iter{$cxix} = 0 if not exists $iter{$cxix};
# if we're just beginning, do it all first
if ($iter{$cxix} == 0) {
$entries{$cxix} = [ _do_glob(1, _parse_line($expr)) ];
}
# chuck it all out, quick or slow
if (wantarray) {
delete $iter{$cxix};
return @{delete $entries{$cxix}};
}
else {
if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
return shift @{$entries{$cxix}};
}
else {
# return undef for EOL
delete $iter{$cxix};
delete $entries{$cxix};
return undef;
}
}
}
#
# Latin-5 path globbing subroutine
#
sub _do_glob {
my($cond,@expr) = @_;
my @glob = ();
my $fix_drive_relative_paths = 0;
OUTER:
for my $expr (@expr) {
next OUTER if not defined $expr;
next OUTER if $expr eq '';
my @matched = ();
my @globdir = ();
my $head = '.';
my $pathsep = '/';
my $tail;
# if argument is within quotes strip em and do no globbing
if ($expr =~ /\A " ((?:$q_char)*) " \z/oxms) {
$expr = $1;
if ($cond eq 'd') {
if (-d $expr) {
push @glob, $expr;
}
}
else {
if (-e $expr) {
push @glob, $expr;
}
}
next OUTER;
}
# wildcards with a drive prefix such as h:*.pm must be changed
# to h:./*.pm to expand correctly
if ($^O =~ /\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) {
if ($expr =~ s# \A ((?:[A-Za-z]:)?) ([^/\\]) #$1./$2#oxms) {
$fix_drive_relative_paths = 1;
}
}
if (($head, $tail) = _parse_path($expr,$pathsep)) {
if ($tail eq '') {
push @glob, $expr;
next OUTER;
}
if ($head =~ / \A (?:$q_char)*? [*?] /oxms) {
if (@globdir = _do_glob('d', $head)) {
push @glob, _do_glob($cond, map {"$_$pathsep$tail"} @globdir);
next OUTER;
}
}
if ($head eq '' or $head =~ /\A [A-Za-z]: \z/oxms) {
$head .= $pathsep;
}
$expr = $tail;
}
# If file component has no wildcards, we can avoid opendir
if ($expr !~ / \A (?:$q_char)*? [*?] /oxms) {
if ($head eq '.') {
$head = '';
}
if ($head ne '' and ($head =~ / \G ($q_char) /oxmsg)[-1] ne $pathsep) {
$head .= $pathsep;
}
$head .= $expr;
if ($cond eq 'd') {
if (-d $head) {
push @glob, $head;
}
}
else {
if (-e $head) {
push @glob, $head;
}
}
next OUTER;
}
opendir(*DIR, $head) or next OUTER;
my @leaf = readdir DIR;
closedir DIR;
if ($head eq '.') {
$head = '';
}
if ($head ne '' and ($head =~ / \G ($q_char) /oxmsg)[-1] ne $pathsep) {
$head .= $pathsep;
}
my $pattern = '';
while ($expr =~ / \G ($q_char) /oxgc) {
my $char = $1;
if ($char eq '*') {
$pattern .= "(?:$your_char)*",
}
elsif ($char eq '?') {
$pattern .= "(?:$your_char)?", # DOS style
# $pattern .= "(?:$your_char)", # UNIX style
}
elsif ((my $fc = Char::Elatin5::fc($char)) ne $char) {
$pattern .= $fc;
}
else {
$pattern .= quotemeta $char;
}
}
my $matchsub = sub { Char::Elatin5::fc($_[0]) =~ /\A $pattern \z/xms };
# if ($@) {
# print STDERR "$0: $@\n";
# next OUTER;
# }
INNER:
for my $leaf (@leaf) {
if ($leaf eq '.' or $leaf eq '..') {
next INNER;
}
if ($cond eq 'd' and not -d "$head$leaf") {
next INNER;
}
if (&$matchsub($leaf)) {
push @matched, "$head$leaf";
next INNER;
}
# [DOS compatibility special case]
# Failed, add a trailing dot and try again, but only...
if (Char::Elatin5::index($leaf,'.') == -1 and # if name does not have a dot in it *and*
CORE::length($leaf) <= 8 and # name is shorter than or equal to 8 chars *and*
Char::Elatin5::index($pattern,'\\.') != -1 # pattern has a dot.
) {
if (&$matchsub("$leaf.")) {
push @matched, "$head$leaf";
next INNER;
}
}
}
if (@matched) {
push @glob, @matched;
}
}
if ($fix_drive_relative_paths) {
for my $glob (@glob) {
$glob =~ s# \A ([A-Za-z]:) \./ #$1#oxms;
}
}
return @glob;
}
#
# Latin-5 parse line
#
sub _parse_line {
my($line) = @_;
$line .= ' ';
my @piece = ();
while ($line =~ /
" ( (?: [^"] )* ) " \s+ |
( (?: [^"\s] )* ) \s+
/oxmsg
) {
push @piece, defined($1) ? $1 : $2;
}
return @piece;
}
#
# Latin-5 parse path
#
sub _parse_path {
my($path,$pathsep) = @_;
$path .= '/';
my @subpath = ();
while ($path =~ /
((?: [^\/\\] )+?) [\/\\]
/oxmsg
) {
push @subpath, $1;
}
my $tail = pop @subpath;
my $head = join $pathsep, @subpath;
return $head, $tail;
}
#
# ${^PREMATCH}, $PREMATCH, $` the string preceding what was matched
#
sub Char::Elatin5::PREMATCH {
return $`;
}
#
# ${^MATCH}, $MATCH, $& the string that matched
#
sub Char::Elatin5::MATCH {
return $&;
}
#
# ${^POSTMATCH}, $POSTMATCH, $' the string following what was matched
#
sub Char::Elatin5::POSTMATCH {
return $';
}
#
# Latin-5 character to order (with parameter)
#
sub Char::Latin5::ord(;$) {
local $_ = shift if @_;
if (/\A ($q_char) /oxms) {
my @ord = unpack 'C*', $1;
my $ord = 0;
while (my $o = shift @ord) {
$ord = $ord * 0x100 + $o;
}
return $ord;
}
else {
return CORE::ord $_;
}
}
#
# Latin-5 character to order (without parameter)
#
sub Char::Latin5::ord_() {
if (/\A ($q_char) /oxms) {
my @ord = unpack 'C*', $1;
my $ord = 0;
while (my $o = shift @ord) {
$ord = $ord * 0x100 + $o;
}
return $ord;
}
else {
return CORE::ord $_;
}
}
#
# Latin-5 reverse
#
sub Char::Latin5::reverse(@) {
if (wantarray) {
return CORE::reverse @_;
}
else {
# One of us once cornered Larry in an elevator and asked him what
# problem he was solving with this, but he looked as far off into
# the distance as he could in an elevator and said, "It seemed like
# a good idea at the time."
return join '', CORE::reverse(join('',@_) =~ /\G ($q_char) /oxmsg);
}
}
#
# Latin-5 getc (with parameter, without parameter)
#
sub Char::Latin5::getc(;*@) {
my($package) = caller;
my $fh = @_ ? qualify_to_ref(shift,$package) : \*STDIN;
croak 'Too many arguments for Char::Latin5::getc' if @_ and not wantarray;
my @length = sort { $a <=> $b } keys %range_tr;
my $getc = '';
for my $length ($length[0] .. $length[-1]) {
$getc .= CORE::getc($fh);
if (exists $range_tr{CORE::length($getc)}) {
if ($getc =~ /\A ${Char::Elatin5::dot_s} \z/oxms) {
return wantarray ? ($getc,@_) : $getc;
}
}
}
return wantarray ? ($getc,@_) : $getc;
}
#
# Latin-5 length by character
#
sub Char::Latin5::length(;$) {
local $_ = shift if @_;
local @_ = /\G ($q_char) /oxmsg;
return scalar @_;
}
#
# Latin-5 substr by character
#
sub Char::Latin5::substr($$;$$) {
my @char = $_[0] =~ /\G ($q_char) /oxmsg;
# substr($string,$offset,$length,$replacement)
if (@_ == 4) {
my(undef,$offset,$length,$replacement) = @_;
my $substr = join '', splice(@char, $offset, $length, $replacement);
$_[0] = join '', @char;
return $substr;
}
# substr($string,$offset,$length)
elsif (@_ == 3) {
my(undef,$offset,$length) = @_;
if ($length == 0) {
return '';
}
if ($offset >= 0) {
return join '', (@char[$offset .. $#char])[0 .. $length-1];
}
else {
return join '', (@char[($#char+$offset+1) .. $#char])[0 .. $length-1];
}
}
# substr($string,$offset)
else {
my(undef,$offset) = @_;
if ($offset >= 0) {
return join '', @char[$offset .. $#char];
}
else {
return join '', @char[($#char+$offset+1) .. $#char];
}
}
}
#
# Latin-5 index by character
#
sub Char::Latin5::index($$;$) {
my $index;
if (@_ == 3) {
$index = Char::Elatin5::index($_[0], $_[1], CORE::length(Char::Latin5::substr($_[0], 0, $_[2])));
}
else {
$index = Char::Elatin5::index($_[0], $_[1]);
}
if ($index == -1) {
return -1;
}
else {
return Char::Latin5::length(CORE::substr $_[0], 0, $index);
}
}
#
# Latin-5 rindex by character
#
sub Char::Latin5::rindex($$;$) {
my $rindex;
if (@_ == 3) {
$rindex = Char::Elatin5::rindex($_[0], $_[1], CORE::length(Char::Latin5::substr($_[0], 0, $_[2])));
}
else {
$rindex = Char::Elatin5::rindex($_[0], $_[1]);
}
if ($rindex == -1) {
return -1;
}
else {
return Char::Latin5::length(CORE::substr $_[0], 0, $rindex);
}
}
#
# instead of Carp::carp
#
sub carp {
my($package,$filename,$line) = caller(1);
print STDERR "@_ at $filename line $line.\n";
}
#
# instead of Carp::croak
#
sub croak {
my($package,$filename,$line) = caller(1);
print STDERR "@_ at $filename line $line.\n";
die "\n";
}
#
# instead of Carp::cluck
#
sub cluck {
my $i = 0;
my @cluck = ();
while (my($package,$filename,$line,$subroutine) = caller($i)) {
push @cluck, "[$i] $filename($line) $package::$subroutine\n";
$i++;
}
print STDERR CORE::reverse @cluck;
print STDERR "\n";
carp @_;
}
#
# instead of Carp::confess
#
sub confess {
my $i = 0;
my @confess = ();
while (my($package,$filename,$line,$subroutine) = caller($i)) {
push @confess, "[$i] $filename($line) $package::$subroutine\n";
$i++;
}
print STDERR CORE::reverse @confess;
print STDERR "\n";
croak @_;
}
1;
__END__
=pod
=head1 NAME
Char::Elatin5 - Run-time routines for Char/Latin5.pm
=head1 SYNOPSIS
use Char::Elatin5;
Char::Elatin5::split(...);
Char::Elatin5::tr(...);
Char::Elatin5::chop(...);
Char::Elatin5::index(...);
Char::Elatin5::rindex(...);
Char::Elatin5::lc(...);
Char::Elatin5::lc_;
Char::Elatin5::lcfirst(...);
Char::Elatin5::lcfirst_;
Char::Elatin5::uc(...);
Char::Elatin5::uc_;
Char::Elatin5::ucfirst(...);
Char::Elatin5::ucfirst_;
Char::Elatin5::fc(...);
Char::Elatin5::fc_;
Char::Elatin5::ignorecase(...);
Char::Elatin5::capture(...);
Char::Elatin5::chr(...);
Char::Elatin5::chr_;
Char::Elatin5::glob(...);
Char::Elatin5::glob_;
# "no Char::Elatin5;" not supported
=head1 ABSTRACT
This module is a run-time routines of the Char/Latin5.pm.
Because the Char/Latin5.pm automatically uses this module, you need not use directly.
=head1 BUGS AND LIMITATIONS
I have tested and verified this software using the best of my ability.
However, a software containing much regular expression is bound to contain
some bugs. Thus, if you happen to find a bug that's in Char::Latin5 software and not
your own program, you can try to reduce it to a minimal test case and then
report it to the following author's address. If you have an idea that could
make this a more useful tool, please let everyone share it.
=head1 HISTORY
This Char::Elatin5 module first appeared in ActivePerl Build 522 Built under
MSWin32 Compiled at Nov 2 1999 09:52:28
=head1 AUTHOR
INABA Hitoshi E<lt>ina@cpan.orgE<gt>
This project was originated by INABA Hitoshi.
For any questions, use E<lt>ina@cpan.orgE<gt> so we can share
this file.
=head1 LICENSE AND COPYRIGHT
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=head1 EXAMPLES
=over 2
=item Split string
@split = Char::Elatin5::split(/pattern/,$string,$limit);
@split = Char::Elatin5::split(/pattern/,$string);
@split = Char::Elatin5::split(/pattern/);
@split = Char::Elatin5::split('',$string,$limit);
@split = Char::Elatin5::split('',$string);
@split = Char::Elatin5::split('');
@split = Char::Elatin5::split();
@split = Char::Elatin5::split;
This function scans a string given by $string for separators, and splits the
string into a list of substring, returning the resulting list value in list
context or the count of substring in scalar context. Scalar context also causes
split to write its result to @_, but this usage is deprecated. The separators
are determined by repeated pattern matching, using the regular expression given
in /pattern/, so the separators may be of any size and need not be the same
string on every match. (The separators are not ordinarily returned; exceptions
are discussed later in this section.) If the /pattern/ doesn't match the string
at all, Char::Elatin5::split returns the original string as a single substring, If it
matches once, you get two substrings, and so on. You may supply regular
expression modifiers to the /pattern/, like /pattern/i, /pattern/x, etc. The
//m modifier is assumed when you split on the pattern /^/.
If $limit is specified and positive, the function splits into no more than that
many fields (though it may split into fewer if it runs out of separators). If
$limit is negative, it is treated as if an arbitrarily large $limit has been
specified If $limit is omitted or zero, trailing null fields are stripped from
the result (which potential users of pop would do wel to remember). If $string
is omitted, the function splits the $_ string. If /pattern/ is also omitted or
is the literal space, " ", the function split on whitespace, /\s+/, after
skipping any leading whitespace.
A /pattern/ of /^/ is secretly treated if it it were /^/m, since it isn't much
use otherwise.
String of any length can be split:
@chars = Char::Elatin5::split(//, $word);
@fields = Char::Elatin5::split(/:/, $line);
@words = Char::Elatin5::split(" ", $paragraph);
@lines = Char::Elatin5::split(/^/, $buffer);
A pattern capable of matching either the null string or something longer than
the null string (for instance, a pattern consisting of any single character
modified by a * or ?) will split the value of $string into separate characters
wherever it matches the null string between characters; nonnull matches will
skip over the matched separator characters in the usual fashion. (In other words,
a pattern won't match in one spot more than once, even if it matched with a zero
width.) For example:
print join(":" => Char::Elatin5::split(/ */, "hi there"));
produces the output "h:i:t:h:e:r:e". The space disappers because it matches
as part of the separator. As a trivial case, the null pattern // simply splits
into separate characters, and spaces do not disappear. (For normal pattern
matches, a // pattern would repeat the last successfully matched pattern, but
Char::Elatin5::split's pattern is exempt from that wrinkle.)
The $limit parameter splits only part of a string:
my ($login, $passwd, $remainder) = Char::Elatin5::split(/:/, $_, 3);
We encourage you to split to lists of names like this to make your code
self-documenting. (For purposes of error checking, note that $remainder would
be undefined if there were fewer than three fields.) When assigning to a list,
if $limit is omitted, Perl supplies a $limit one larger than the number of
variables in the list, to avoid unneccessary work. For the split above, $limit
would have been 4 by default, and $remainder would have received only the third
field, not all the rest of the fields. In time-critical applications, it behooves
you not to split into more fields than you really need. (The trouble with
powerful languages it that they let you be powerfully stupid at times.)
We said earlier that the separators are not returned, but if the /pattern/
contains parentheses, then the substring matched by each pair of parentheses is
included in the resulting list, interspersed with the fields that are ordinarily
returned. Here's a simple example:
Char::Elatin5::split(/([-,])/, "1-10,20");
which produces the list value:
(1, "-", 10, ",", 20)
With more parentheses, a field is returned for each pair, even if some pairs
don't match, in which case undefined values are returned in those positions. So
if you say:
Char::Elatin5::split(/(-)|(,)/, "1-10,20");
you get the value:
(1, "-", undef, 10, undef, ",", 20)
The /pattern/ argument may be replaced with an expression to specify patterns
that vary at runtime. As with ordinary patterns, to do run-time compilation only
once, use /$variable/o.
As a special case, if the expression is a single space (" "), the function
splits on whitespace just as Char::Elatin5::split with no arguments does. Thus,
Char::Elatin5::split(" ") can be used to emulate awk's default behavior. In contrast,
Char::Elatin5::split(/ /) will give you as many null initial fields as there are
leading spaces. (Other than this special case, if you supply a string instead
of a regular expression, it'll be interpreted as a regular expression anyway.)
You can use this property to remove leading and trailing whitespace from a
string and to collapse intervaning stretches of whitespace into a single
space:
$string = join(" ", Char::Elatin5::split(" ", $string));
The following example splits an RFC822 message header into a hash containing
$head{'Date'}, $head{'Subject'}, and so on. It uses the trick of assigning a
list of pairs to a hash, because separators altinate with separated fields, It
users parentheses to return part of each separator as part of the returned list
value. Since the split pattern is guaranteed to return things in pairs by virtue
of containing one set of parentheses, the hash assignment is guaranteed to
receive a list consisting of key/value pairs, where each key is the name of a
header field. (Unfortunately, this technique loses information for multiple lines
with the same key field, such as Received-By lines. Ah well)
$header =~ s/\n\s+/ /g; # Merge continuation lines.
%head = ("FRONTSTUFF", Char::Elatin5::split(/^(\S*?):\s*/m, $header));
The following example processes the entries in a Unix passwd(5) file. You could
leave out the chomp, in which case $shell would have a newline on the end of it.
open(PASSWD, "/etc/passwd");
while (<PASSWD>) {
chomp; # remove trailing newline.
($login, $passwd, $uid, $gid, $gcos, $home, $shell) =
Char::Elatin5::split(/:/);
...
}
Here's how process each word of each line of each file of input to create a
word-frequency hash.
while (<>) {
for my $word (Char::Elatin5::split()) {
$count{$word}++;
}
}
The inverse of Char::Elatin5::split is join, except that join can only join with the
same separator between all fields. To break apart a string with fixed-position
fields, use unpack.
=item Transliteration
$tr = Char::Elatin5::tr($variable,$bind_operator,$searchlist,$replacementlist,$modifier);
$tr = Char::Elatin5::tr($variable,$bind_operator,$searchlist,$replacementlist);
This is the transliteration (sometimes erroneously called translation) operator,
which is like the y/// operator in the Unix sed program, only better, in
everybody's humble opinion.
This function scans a Latin-5 string character by character and replaces all
occurrences of the characters found in $searchlist with the corresponding character
in $replacementlist. It returns the number of characters replaced or deleted.
If no Latin-5 string is specified via =~ operator, the $_ variable is translated.
$modifier are:
---------------------------------------------------------------------------
Modifier Meaning
---------------------------------------------------------------------------
c Complement $searchlist.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.
r Return transliteration and leave the original string untouched.
---------------------------------------------------------------------------
To use with a read-only value without raising an exception, use the /r modifier.
print Char::Elatin5::tr('bookkeeper','=~','boep','peob','r'); # prints 'peekkoobor'
=item Chop string
$chop = Char::Elatin5::chop(@list);
$chop = Char::Elatin5::chop();
$chop = Char::Elatin5::chop;
This function chops off the last character of a string variable and returns the
character chopped. The Char::Elatin5::chop function is used primary to remove the newline
from the end of an input recoed, and it is more efficient than using a
substitution. If that's all you're doing, then it would be safer to use chomp,
since Char::Elatin5::chop always shortens the string no matter what's there, and chomp
is more selective. If no argument is given, the function chops the $_ variable.
You cannot Char::Elatin5::chop a literal, only a variable. If you Char::Elatin5::chop a list of
variables, each string in the list is chopped:
@lines = `cat myfile`;
Char::Elatin5::chop(@lines);
You can Char::Elatin5::chop anything that is an lvalue, including an assignment:
Char::Elatin5::chop($cwd = `pwd`);
Char::Elatin5::chop($answer = <STDIN>);
This is different from:
$answer = Char::Elatin5::chop($tmp = <STDIN>); # WRONG
which puts a newline into $answer because Char::Elatin5::chop returns the character
chopped, not the remaining string (which is in $tmp). One way to get the result
intended here is with substr:
$answer = substr <STDIN>, 0, -1;
But this is more commonly written as:
Char::Elatin5::chop($answer = <STDIN>);
In the most general case, Char::Elatin5::chop can be expressed using substr:
$last_code = Char::Elatin5::chop($var);
$last_code = substr($var, -1, 1, ""); # same thing
Once you understand this equivalence, you can use it to do bigger chops. To
Char::Elatin5::chop more than one character, use substr as an lvalue, assigning a null
string. The following removes the last five characters of $caravan:
substr($caravan, -5) = '';
The negative subscript causes substr to count from the end of the string instead
of the beginning. To save the removed characters, you could use the four-argument
form of substr, creating something of a quintuple Char::Elatin5::chop;
$tail = substr($caravan, -5, 5, '');
This is all dangerous business dealing with characters instead of graphemes. Perl
doesn't really have a grapheme mode, so you have to deal with them yourself.
=item Index string
$byte_pos = Char::Elatin5::index($string,$substr,$byte_offset);
$byte_pos = Char::Elatin5::index($string,$substr);
This function searches for one string within another. It returns the byte position
of the first occurrence of $substring in $string. The $byte_offset, if specified,
says how many bytes from the start to skip before beginning to look. Positions are
based at 0. If the substring is not found, the function returns one less than the
base, ordinarily -1. To work your way through a string, you might say:
$byte_pos = -1;
while (($byte_pos = Char::Elatin5::index($string, $lookfor, $byte_pos)) > -1) {
print "Found at $byte_pos\n";
$byte_pos++;
}
=item Reverse index string
$byte_pos = Char::Elatin5::rindex($string,$substr,$byte_offset);
$byte_pos = Char::Elatin5::rindex($string,$substr);
This function works just like Char::Elatin5::index except that it returns the byte
position of the last occurrence of $substring in $string (a reverse Char::Elatin5::index).
The function returns -1 if $substring is not found. $byte_offset, if specified,
is the rightmost byte position that may be returned. To work your way through a
string backward, say:
$byte_pos = length($string);
while (($byte_pos = Char::Latin5::rindex($string, $lookfor, $byte_pos)) >= 0) {
print "Found at $byte_pos\n";
$byte_pos--;
}
=item Lower case string
$lc = Char::Elatin5::lc($string);
$lc = Char::Elatin5::lc_;
This function returns a lowercased version of Latin-5 $string (or $_, if
$string is omitted). This is the internal function implementing the \L escape
in double-quoted strings.
You can use the Char::Elatin5::fc function for case-insensitive comparisons via Char::Latin5
software.
=item Lower case first character of string
$lcfirst = Char::Elatin5::lcfirst($string);
$lcfirst = Char::Elatin5::lcfirst_;
This function returns a version of Latin-5 $string with the first character
lowercased (or $_, if $string is omitted). This is the internal function
implementing the \l escape in double-quoted strings.
=item Upper case string
$uc = Char::Elatin5::uc($string);
$uc = Char::Elatin5::uc_;
This function returns an uppercased version of Latin-5 $string (or $_, if
$string is omitted). This is the internal function implementing the \U escape
in interpolated strings. For titlecase, use Char::Elatin5::ucfirst instead.
You can use the Char::Elatin5::fc function for case-insensitive comparisons via Char::Latin5
software.
=item Upper case first character of string
$ucfirst = Char::Elatin5::ucfirst($string);
$ucfirst = Char::Elatin5::ucfirst_;
This function returns a version of Latin-5 $string with the first character
titlecased and other characters left alone (or $_, if $string is omitted).
Titlecase is "Camel" for an initial capital that has (or expects to have)
lowercase characters following it, not uppercase ones. Exsamples are the first
letter of a sentence, of a person's name, of a newspaper headline, or of most
words in a title. Characters with no titlecase mapping return the uppercase
mapping instead. This is the internal function implementing the \u escape in
double-quoted strings.
To capitalize a string by mapping its first character to titlecase and the rest
to lowercase, use:
$titlecase = Char::Elatin5::ucfirst(substr($word,0,1)) . Char::Elatin5::lc(substr($word,1));
or
$string =~ s/(\w)(\w*)/\u$1\L$2/g;
Do not use:
$do_not_use = Char::Elatin5::ucfirst(Char::Elatin5::lc($word));
or "\u\L$word", because that can produce a different and incorrect answer with
certain characters. The titlecase of something that's been lowercased doesn't
always produce the same thing titlecasing the original produces.
Because titlecasing only makes sense at the start of a string that's followed
by lowercase characters, we can't think of any reason you might want to titlecase
every character in a string.
See also P.287 A Case of Mistaken Identity
in Chapter 6: Unicode
of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
=item Fold case string
P.860 fc
in Chapter 27: Functions
of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
$fc = Char::Elatin5::fc($string);
$fc = Char::Elatin5::fc_;
New to Char::Latin5 software, this function returns the full Unicode-like casefold of
Latin-5 $string (or $_, if omitted). This is the internal function implementing
the \F escape in double-quoted strings.
Just as title-case is based on uppercase but different, foldcase is based on
lowercase but different. In ASCII there is a one-to-one mapping between only
two cases, but in other encoding there is a one-to-many mapping and between three
cases. Because that's too many combinations to check manually each time, a fourth
casemap called foldcase was invented as a common intermediary for the other three.
It is not a case itself, but it is a casemap.
To compare whether two strings are the same without regard to case, do this:
Char::Elatin5::fc($a) eq Char::Elatin5::fc($b)
The reliable way to compare string case-insensitively was with the /i pattern
modifier, because Char::Latin5 software has always used casefolding semantics for
case-insensitive pattern matches. Knowing this, you can emulate equality
comparisons like this:
sub fc_eq ($$) {
my($a,$b) = @_;
return $a =~ /\A\Q$b\E\z/i;
}
=item Make ignore case string
@ignorecase = Char::Elatin5::ignorecase(@string);
This function is internal use to m/ /i, s/ / /i, split / /i and qr/ /i.
=item Make capture number
$capturenumber = Char::Elatin5::capture($string);
This function is internal use to m/ /, s/ / /, split / / and qr/ /.
=item Make character
$chr = Char::Elatin5::chr($code);
$chr = Char::Elatin5::chr_;
This function returns a programmer-visible character, character represented by
that $code in the character set. For example, Char::Elatin5::chr(65) is "A" in either
ASCII or Latin-5, not Unicode. For the reverse of Char::Elatin5::chr, use Char::Latin5::ord.
=item Filename expansion (globbing)
@glob = Char::Elatin5::glob($string);
@glob = Char::Elatin5::glob_;
This function returns the value of $string with filename expansions the way a
DOS-like shell would expand them, returning the next successive name on each
call. If $string is omitted, $_ is globbed instead. This is the internal
function implementing the <*> and glob operator.
This function function when the pathname ends with chr(0x5C) on MSWin32.
For ease of use, the algorithm matches the DOS-like shell's style of expansion,
not the UNIX-like shell's. An asterisk ("*") matches any sequence of any
character (including none). A question mark ("?") matches any one character or
none. A tilde ("~") expands to a home directory, as in "~/.*rc" for all the
current user's "rc" files, or "~jane/Mail/*" for all of Jane's mail files.
Note that all path components are case-insensitive, and that backslashes and
forward slashes are both accepted, and preserved. You may have to double the
backslashes if you are putting them in literally, due to double-quotish parsing
of the pattern by perl.
The Char::Elatin5::glob function grandfathers the use of whitespace to separate multiple
patterns such as <*.c *.h>. If you want to glob filenames that might contain
whitespace, you'll have to use extra quotes around the spacy filename to protect
it. For example, to glob filenames that have an "e" followed by a space followed
by an "f", use either of:
@spacies = <"*e f*">;
@spacies = Char::Elatin5::glob('"*e f*"');
@spacies = Char::Elatin5::glob(q("*e f*"));
If you had to get a variable through, you could do this:
@spacies = Char::Elatin5::glob("'*${var}e f*'");
@spacies = Char::Elatin5::glob(qq("*${var}e f*"));
Another way on MSWin32
# relative path
@relpath_file = split(/\n/,`dir /b wildcard\\here*.txt 2>NUL`);
# absolute path
@abspath_file = split(/\n/,`dir /s /b wildcard\\here*.txt 2>NUL`);
# on COMMAND.COM
@relpath_file = split(/\n/,`dir /b wildcard\\here*.txt`);
@abspath_file = split(/\n/,`dir /s /b wildcard\\here*.txt`);
=cut