📄 locale.t
字号:
#!./perl -wTBEGIN { chdir 't' if -d 't'; @INC = '../lib'; unshift @INC, '.'; require Config; import Config; if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) { print "1..0\n"; exit; }}use strict;my $debug = 1;use Dumpvalue;my $dumper = Dumpvalue->new( tick => qq{"}, quoteHighBit => 0, unctrl => "quote" );sub debug { return unless $debug; my($mess) = join "", @_; chop $mess; print $dumper->stringify($mess,1), "\n";}sub debugf { printf @_ if $debug;}my $have_setlocale = 0;eval { require POSIX; import POSIX ':locale_h'; $have_setlocale++;};# Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"# and mingw32 uses said silly CRT$have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;my $last = $have_setlocale ? 116 : 98;print "1..$last\n";use vars qw(&LC_ALL);my $a = 'abc %';sub ok { my ($n, $result) = @_; print 'not ' unless ($result); print "ok $n\n";}# First we'll do a lot of taint checking for locales.# This is the easiest to test, actually, as any locale,# even the default locale will taint under 'use locale'.sub is_tainted { # hello, camel two. no warnings 'uninitialized' ; my $dummy; not eval { $dummy = join("", @_), kill 0; 1 }}sub check_taint ($$) { ok $_[0], is_tainted($_[1]);}sub check_taint_not ($$) { ok $_[0], not is_tainted($_[1]);}use locale; # engage locale and therefore locale taint.check_taint_not 1, $a;check_taint 2, uc($a);check_taint 3, "\U$a";check_taint 4, ucfirst($a);check_taint 5, "\u$a";check_taint 6, lc($a);check_taint 7, "\L$a";check_taint 8, lcfirst($a);check_taint 9, "\l$a";check_taint_not 10, sprintf('%e', 123.456);check_taint_not 11, sprintf('%f', 123.456);check_taint_not 12, sprintf('%g', 123.456);check_taint_not 13, sprintf('%d', 123.456);check_taint_not 14, sprintf('%x', 123.456);$_ = $a; # untaint $_$_ = uc($a); # taint $_check_taint 15, $_;/(\w)/; # taint $&, $`, $', $+, $1.check_taint 16, $&;check_taint 17, $`;check_taint 18, $';check_taint 19, $+;check_taint 20, $1;check_taint_not 21, $2;/(.)/; # untaint $&, $`, $', $+, $1.check_taint_not 22, $&;check_taint_not 23, $`;check_taint_not 24, $';check_taint_not 25, $+;check_taint_not 26, $1;check_taint_not 27, $2;/(\W)/; # taint $&, $`, $', $+, $1.check_taint 28, $&;check_taint 29, $`;check_taint 30, $';check_taint 31, $+;check_taint 32, $1;check_taint_not 33, $2;/(\s)/; # taint $&, $`, $', $+, $1.check_taint 34, $&;check_taint 35, $`;check_taint 36, $';check_taint 37, $+;check_taint 38, $1;check_taint_not 39, $2;/(\S)/; # taint $&, $`, $', $+, $1.check_taint 40, $&;check_taint 41, $`;check_taint 42, $';check_taint 43, $+;check_taint 44, $1;check_taint_not 45, $2;$_ = $a; # untaint $_check_taint_not 46, $_;/(b)/; # this must not taintcheck_taint_not 47, $&;check_taint_not 48, $`;check_taint_not 49, $';check_taint_not 50, $+;check_taint_not 51, $1;check_taint_not 52, $2;$_ = $a; # untaint $_check_taint_not 53, $_;$b = uc($a); # taint $bs/(.+)/$b/; # this must taint only the $_check_taint 54, $_;check_taint_not 55, $&;check_taint_not 56, $`;check_taint_not 57, $';check_taint_not 58, $+;check_taint_not 59, $1;check_taint_not 60, $2;$_ = $a; # untaint $_s/(.+)/b/; # this must not taintcheck_taint_not 61, $_;check_taint_not 62, $&;check_taint_not 63, $`;check_taint_not 64, $';check_taint_not 65, $+;check_taint_not 66, $1;check_taint_not 67, $2;$b = $a; # untaint $b($b = $a) =~ s/\w/$&/;check_taint 68, $b; # $b should be tainted.check_taint_not 69, $a; # $a should be not.$_ = $a; # untaint $_s/(\w)/\l$1/; # this must taintcheck_taint 70, $_;check_taint 71, $&;check_taint 72, $`;check_taint 73, $';check_taint 74, $+;check_taint 75, $1;check_taint_not 76, $2;$_ = $a; # untaint $_s/(\w)/\L$1/; # this must taintcheck_taint 77, $_;check_taint 78, $&;check_taint 79, $`;check_taint 80, $';check_taint 81, $+;check_taint 82, $1;check_taint_not 83, $2;$_ = $a; # untaint $_s/(\w)/\u$1/; # this must taintcheck_taint 84, $_;check_taint 85, $&;check_taint 86, $`;check_taint 87, $';check_taint 88, $+;check_taint 89, $1;check_taint_not 90, $2;$_ = $a; # untaint $_s/(\w)/\U$1/; # this must taintcheck_taint 91, $_;check_taint 92, $&;check_taint 93, $`;check_taint 94, $';check_taint 95, $+;check_taint 96, $1;check_taint_not 97, $2;# After all this tainting $a should be cool.check_taint_not 98, $a;# I think we've seen quite enough of taint.# Let us do some *real* locale work now,# unless setlocale() is missing (i.e. minitest).exit unless $have_setlocale;# Find locales.debug "# Scanning for locales...\n";# Note that it's okay that some languages have their native names# capitalized here even though that's not "right". They are lowercased# anyway later during the scanning process (and besides, some clueless# vendor might have them capitalized errorneously anyway).my $locales = <<EOF;Afrikaans:af:za:1 15Arabic:ar:dz eg sa:6 arabic8Brezhoneg Breton:br:fr:1 15Bulgarski Bulgarian:bg:bg:5Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUCHrvatski Croatian:hr:hr:2Cymraeg Welsh:cy:cy:1 14 15Czech:cs:cz:2Dansk Danish:dk:da:1 15Nederlands Dutch:nl:be nl:1 15English American British:en:au ca gb ie nz us uk zw:1 15 cp850Esperanto:eo:eo:3Eesti Estonian:et:ee:4 6 13Suomi Finnish:fi:fi:1 15Flamish::fl:1 15Deutsch German:de:at be ch de lu:1 15Euskaraz Basque:eu:es fr:1 15Galego Galician:gl:es:1 15Ellada Greek:el:gr:7 g8Frysk:fy:nl:1 15Greenlandic:kl:gl:4 6Hebrew:iw:il:8 hebrew8Hungarian:hu:hu:2Indonesian:in:id:1 15Gaeilge Irish:ga:IE:1 14 15Italiano Italian:it:ch it:1 15Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjisKorean:ko:kr:Latine Latin:la:va:1 15Latvian:lv:lv:4 6 13Lithuanian:lt:lt:4 6 13Macedonian:mk:mk:1 15Maltese:mt:mt:3Moldovan:mo:mo:2Norsk Norwegian:no no\@nynorsk:no:1 15Occitan:oc:es:1 15Polski Polish:pl:pl:2Rumanian:ro:ro:2Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866Serbski Serbian:sr:yu:5Slovak:sk:sk:2Slovene Slovenian:sl:si:2Sqhip Albanian:sq:sq:1 15Svenska Swedish:sv:fi se:1 15Thai:th:th:11 tis620Turkish:tr:tr:9 turkish8Yiddish:yi::1 15EOFif ($^O eq 'os390') { # These cause heartburn. Broken locales? $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//; $locales =~ s/Thai:th:th:11 tis620\n//;}sub in_utf8 () { $^H & 0x08 }if (in_utf8) { require "pragma/locale/utf8";} else { require "pragma/locale/latin1";}my @Locale;my $Locale;my @Alnum_;sub getalnum_ { sort grep /\w/, map { chr } 0..255}sub trylocale { my $locale = shift; if (setlocale(LC_ALL, $locale)) { push @Locale, $locale; }}sub decode_encodings { my @enc; foreach (split(/ /, shift)) { if (/^(\d+)$/) { push @enc, "ISO8859-$1"; push @enc, "iso8859$1"; # HP if ($1 eq '1') { push @enc, "roman8"; # HP } } else { push @enc, $_; push @enc, "$_.UTF-8"; } } if ($^O eq 'os390') { push @enc, qw(IBM-037 IBM-819 IBM-1047); } return @enc;}trylocale("C");trylocale("POSIX");foreach (0..15) { trylocale("ISO8859-$_"); trylocale("iso8859$_"); trylocale("iso8859-$_"); trylocale("iso_8859_$_"); trylocale("isolatin$_"); trylocale("isolatin-$_"); trylocale("iso_latin_$_");}# Sanitize the environment so that we can run the external 'locale'# program without the taint mode getting grumpy.# $ENV{PATH} is special in VMS.delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};# Other subversive stuff.delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) { while (<LOCALES>) { chomp; trylocale($_); } close(LOCALES);} elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {# The SYS$I18N_LOCALE logical name search list was not present on # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions. opendir(LOCALES, "SYS\$I18N_LOCALE:"); while ($_ = readdir(LOCALES)) { chomp; trylocale($_); } close(LOCALES);} else { # This is going to be slow. foreach my $locale (split(/\n/, $locales)) { my ($locale_name, $language_codes, $country_codes, $encodings) = split(/:/, $locale); my @enc = decode_encodings($encodings); foreach my $loc (split(/ /, $locale_name)) { trylocale($loc); foreach my $enc (@enc) { trylocale("$loc.$enc"); } $loc = lc $loc; foreach my $enc (@enc) { trylocale("$loc.$enc");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -