1#!./perl
2use strict;
3
4# This file is intentionally encoded in latin-1.
5#
6# Test uc(), lc(), fc(), ucfirst(), lcfirst(), quotemeta() etc
7
8BEGIN {
9    chdir 't' if -d 't';
10    require './test.pl';
11    set_up_inc('../lib');
12    require Config; import Config;
13    skip_all_without_unicode_tables();
14    require './charset_tools.pl';
15    require './loc_tools.pl';   # Contains find_utf8_ctype_locale()
16}
17
18use feature qw( fc );
19
20plan tests => 139 + 2 * (5 * 256) + 17;
21
22is(lc(undef),	   "", "lc(undef) is ''");
23is(lcfirst(undef), "", "lcfirst(undef) is ''");
24is(uc(undef),	   "", "uc(undef) is ''");
25is(ucfirst(undef), "", "ucfirst(undef) is ''");
26
27{
28    no feature 'fc';
29    is(CORE::fc(undef), "", "fc(undef) is ''");
30    is(CORE::fc(''),    "", "fc('') is ''");
31
32    local $@;
33    eval { fc("eeyup") };
34    like($@, qr/Undefined subroutine &main::fc/, "fc() throws an exception,");
35
36    {
37        use feature 'fc';
38        local $@;
39        eval { fc("eeyup") };
40        ok(!$@, "...but works after requesting the feature");
41    }
42}
43
44$a = "HELLO.* world";
45$b = "hello.* WORLD";
46
47is("\Q$a\E."      , "HELLO\\.\\*\\ world.", '\Q\E HELLO.* world');
48is("\u$a"         , "HELLO\.\* world",      '\u');
49is("\l$a"         , "hELLO\.\* world",      '\l');
50is("\U$a"         , "HELLO\.\* WORLD",      '\U');
51is("\L$a"         , "hello\.\* world",      '\L');
52is("\F$a"         , "hello\.\* world",      '\F');
53
54is(quotemeta($a)  , "HELLO\\.\\*\\ world",  'quotemeta');
55is(ucfirst($a)    , "HELLO\.\* world",      'ucfirst');
56is(lcfirst($a)    , "hELLO\.\* world",      'lcfirst');
57is(uc($a)         , "HELLO\.\* WORLD",      'uc');
58is(lc($a)         , "hello\.\* world",      'lc');
59is(fc($a)         , "hello\.\* world",      'fc');
60
61is("\Q$b\E."      , "hello\\.\\*\\ WORLD.", '\Q\E hello.* WORLD');
62is("\u$b"         , "Hello\.\* WORLD",      '\u');
63is("\l$b"         , "hello\.\* WORLD",      '\l');
64is("\U$b"         , "HELLO\.\* WORLD",      '\U');
65is("\L$b"         , "hello\.\* world",      '\L');
66is("\F$b"         , "hello\.\* world",      '\F');
67
68is(quotemeta($b)  , "hello\\.\\*\\ WORLD",  'quotemeta');
69is(ucfirst($b)    , "Hello\.\* WORLD",      'ucfirst');
70is(lcfirst($b)    , "hello\.\* WORLD",      'lcfirst');
71is(uc($b)         , "HELLO\.\* WORLD",      'uc');
72is(lc($b)         , "hello\.\* world",      'lc');
73is(fc($b)         , "hello\.\* world",      'fc');
74
75# \x{100} is LATIN CAPITAL LETTER A WITH MACRON; its bijective lowercase is
76# \x{101}, LATIN SMALL LETTER A WITH MACRON.
77# Which is also its foldcase.
78
79$a = "\x{100}\x{101}Aa";
80$b = "\x{101}\x{100}aA";
81
82is("\Q$a\E."      , "\x{100}\x{101}Aa.", '\Q\E \x{100}\x{101}Aa');
83is("\u$a"         , "\x{100}\x{101}Aa",  '\u');
84is("\l$a"         , "\x{101}\x{101}Aa",  '\l');
85is("\U$a"         , "\x{100}\x{100}AA",  '\U');
86is("\L$a"         , "\x{101}\x{101}aa",  '\L');
87is("\F$a"         , "\x{101}\x{101}aa",  '\F');
88
89is(quotemeta($a)  , "\x{100}\x{101}Aa",  'quotemeta');
90is(ucfirst($a)    , "\x{100}\x{101}Aa",  'ucfirst');
91is(lcfirst($a)    , "\x{101}\x{101}Aa",  'lcfirst');
92is(uc($a)         , "\x{100}\x{100}AA",  'uc');
93is(lc($a)         , "\x{101}\x{101}aa",  'lc');
94is(fc($a)         , "\x{101}\x{101}aa",  'fc');
95
96is("\Q$b\E."      , "\x{101}\x{100}aA.", '\Q\E \x{101}\x{100}aA');
97is("\u$b"         , "\x{100}\x{100}aA",  '\u');
98is("\l$b"         , "\x{101}\x{100}aA",  '\l');
99is("\U$b"         , "\x{100}\x{100}AA",  '\U');
100is("\L$b"         , "\x{101}\x{101}aa",  '\L');
101is("\F$b"         , "\x{101}\x{101}aa",  '\F');
102
103is(quotemeta($b)  , "\x{101}\x{100}aA",  'quotemeta');
104is(ucfirst($b)    , "\x{100}\x{100}aA",  'ucfirst');
105is(lcfirst($b)    , "\x{101}\x{100}aA",  'lcfirst');
106is(uc($b)         , "\x{100}\x{100}AA",  'uc');
107is(lc($b)         , "\x{101}\x{101}aa",  'lc');
108is(fc($b)         , "\x{101}\x{101}aa",  'fc');
109
110my $sharp_s = uni_to_native("\x{DF}");
111# \x{DF} is LATIN SMALL LETTER SHARP S, its uppercase is SS or \x{53}\x{53};
112# \x{149} is LATIN SMALL LETTER N PRECEDED BY APOSTROPHE, its uppercase is
113# \x{2BC}\x{E4} or MODIFIER LETTER APOSTROPHE and N.
114
115is("\U${sharp_s}aB\x{149}cD", "SSAB\x{2BC}NCD", "multicharacter uppercase");
116
117# The \x{DF} is its own lowercase, ditto for \x{149}.
118# There are no single character -> multiple characters lowercase mappings.
119
120is("\L${sharp_s}aB\x{149}cD", "${sharp_s}ab\x{149}cd",
121       "multicharacter lowercase");
122
123# \x{DF} is LATIN SMALL LETTER SHARP S, its foldcase is ss or \x{73}\x{73};
124# \x{149} is LATIN SMALL LETTER N PRECEDED BY APOSTROPHE, its foldcase is
125# \x{2BC}\x{6E} or MODIFIER LETTER APOSTROPHE and n.
126# Note that is this further tested in t/uni/fold.t
127
128is("\F${sharp_s}aB\x{149}cD", "ssab\x{2BC}ncd", "multicharacter foldcase");
129
130
131# titlecase is used for \u / ucfirst.
132
133# \x{587} is ARMENIAN SMALL LIGATURE ECH YIWN and its titlecase is
134# \x{535}\x{582} ARMENIAN CAPITAL LETTER ECH + ARMENIAN SMALL LETTER YIWN
135# while its lowercase is
136# \x{587} itself
137# and its uppercase is
138# \x{535}\x{552} ARMENIAN CAPITAL LETTER ECH + ARMENIAN CAPITAL LETTER YIWN
139# The foldcase is \x{565}\x{582} ARMENIAN SMALL LETTER ECH + ARMENIAN SMALL LETTER YIWN
140
141$a = "\x{587}";
142
143is("\L\x{587}" , "\x{587}",        "ligature lowercase");
144is("\u\x{587}" , "\x{535}\x{582}", "ligature titlecase");
145is("\U\x{587}" , "\x{535}\x{552}", "ligature uppercase");
146is("\F\x{587}" , "\x{565}\x{582}", "ligature foldcase");
147
148# mktables had problems where many-to-one case mappings didn't work right.
149# The lib/uni/fold.t should give the fourth folding, "casefolding", a good
150# workout.
151# \x{01C4} is LATIN CAPITAL LETTER DZ WITH CARON
152# \x{01C5} is LATIN CAPITAL LETTER D WITH SMALL LETTER Z WITH CARON
153# \x{01C6} is LATIN SMALL LETTER DZ WITH CARON
154# \x{03A3} is GREEK CAPITAL LETTER SIGMA
155# \x{03C2} is GREEK SMALL LETTER FINAL SIGMA
156# \x{03C3} is GREEK SMALL LETTER SIGMA
157
158is(lc("\x{1C4}") , "\x{1C6}",      "U+01C4 lc is U+01C6");
159is(lc("\x{1C5}") , "\x{1C6}",      "U+01C5 lc is U+01C6, too");
160
161is(ucfirst("\x{3C2}") , "\x{3A3}", "U+03C2 ucfirst is U+03A3");
162is(ucfirst("\x{3C3}") , "\x{3A3}", "U+03C3 ucfirst is U+03A3, too");
163
164is(uc("\x{1C5}") , "\x{1C4}",      "U+01C5 uc is U+01C4");
165is(uc("\x{1C6}") , "\x{1C4}",      "U+01C6 uc is U+01C4, too");
166
167# #18107: A host of bugs involving [ul]c{,first}. AMS 20021106
168my $a = "\x{3c3}foo.bar"; # \x{3c3} == GREEK SMALL LETTER SIGMA.
169my $b = "\x{3a3}FOO.BAR"; # \x{3a3} == GREEK CAPITAL LETTER SIGMA.
170
171my $c;
172($c = $b) =~ s/(\w+)/lc($1)/ge;
173is($c , $a, "Using s///e to change case.");
174
175($c = $a) =~ s/(\p{IsWord}+)/uc($1)/ge;
176is($c , $b, "Using s///e to change case.");
177
178($c = $a) =~ s/(\p{IsWord}+)/fc($1)/ge;
179is($c , $a, "Using s///e to foldcase.");
180
181($c = $b) =~ s/(\p{IsWord}+)/lcfirst($1)/ge;
182is($c , "\x{3c3}FOO.bAR", "Using s///e to change case.");
183
184($c = $a) =~ s/(\p{IsWord}+)/ucfirst($1)/ge;
185is($c , "\x{3a3}foo.Bar", "Using s///e to change case.");
186
187# #18931: perl5.8.0 bug in \U..\E processing
188# Test case from Nicholas Clark.
189for my $a (0,1) {
190    $_ = 'abcdefgh';
191    $_ .= chr 256;
192    chop;
193    /(.*)/;
194    is(uc($1), "ABCDEFGH", "[perl #18931]");
195}
196
197{
198    foreach (0, 1) {
199	$a = v10.v257;
200	chop $a;
201	$a =~ s/^(\s*)(\w*)/$1\u$2/;
202	is($a, v10, "[perl #18857]");
203    }
204}
205
206
207# [perl #38619] Bug in lc and uc (interaction between UTF-8, substr, and lc/uc)
208
209for ("a\x{100}", "xyz\x{100}") {
210    is(substr(uc($_), 0), uc($_), "[perl #38619] uc");
211}
212for ("A\x{100}", "XYZ\x{100}") {
213    is(substr(lc($_), 0), lc($_), "[perl #38619] lc");
214}
215for ("a\x{100}", "�yz\x{100}") { # � to Ss (different length)
216    is(substr(ucfirst($_), 0), ucfirst($_), "[perl #38619] ucfirst");
217}
218
219#fc() didn't exist back then, but coverage is coverage.
220for ("a\x{100}", "�yz\x{100}", "xyz\x{100}", "XYZ\x{100}") { # � to Ss (different length)
221    is(substr(fc($_), 0), fc($_), "[perl #38619] fc");
222}
223
224# Related to [perl #38619]
225# the original report concerns PERL_MAGIC_utf8.
226# these cases concern PERL_MAGIC_regex_global.
227
228for (map { $_ } "a\x{100}", "abc\x{100}", "\x{100}") {
229    chop; # get ("a", "abc", "") in utf8
230    my $return =  uc($_) =~ /\G(.?)/g;
231    my $result = $return ? $1 : "not";
232    my $expect = (uc($_) =~ /(.?)/g)[0];
233    is($return, 1,       "[perl #38619]");
234    is($result, $expect, "[perl #38619]");
235}
236
237for (map { $_ } "A\x{100}", "ABC\x{100}", "\x{100}") {
238    chop; # get ("A", "ABC", "") in utf8
239    my $return =  lc($_) =~ /\G(.?)/g;
240    my $result = $return ? $1 : "not";
241    my $expect = (lc($_) =~ /(.?)/g)[0];
242    is($return, 1,       "[perl #38619]");
243    is($result, $expect, "[perl #38619]");
244}
245
246for (map { $_ } "A\x{100}", "ABC\x{100}", "\x{100}") {
247    chop; # get ("A", "ABC", "") in utf8
248    my $return =  fc($_) =~ /\G(.?)/g;
249    my $result = $return ? $1 : "not";
250    my $expect = (fc($_) =~ /(.?)/g)[0];
251    is($return, 1,       "[perl #38619]");
252    is($result, $expect, "[perl #38619]");
253}
254
255for (1, 4, 9, 16, 25) {
256    is(uc "\x{03B0}" x $_, "\x{3a5}\x{308}\x{301}" x $_,
257       'uc U+03B0 grows threefold');
258
259    is(lc "\x{0130}" x $_, "i\x{307}" x $_, 'lc U+0130 grows');
260
261    is(fc "\x{03B0}" x $_, "\x{3C5}\x{308}\x{301}" x $_,
262       'fc U+03B0 grows threefold');
263}
264
265# bug #43207
266my $temp = "HellO";
267for ("$temp") {
268    lc $_;
269    is($_, "HellO", '[perl #43207] lc($_) modifying $_');
270}
271for ("$temp") {
272    fc $_;
273    is($_, "HellO", '[perl #43207] fc($_) modifying $_');
274}
275for ("$temp") {
276    uc $_;
277    is($_, "HellO", '[perl #43207] uc($_) modifying $_');
278}
279for ("$temp") {
280    ucfirst $_;
281    is($_, "HellO", '[perl #43207] ucfirst($_) modifying $_');
282}
283for ("$temp") {
284    lcfirst $_;
285    is($_, "HellO", '[perl #43207] lcfirst($_) modifying $_');
286}
287
288# new in Unicode 5.1.0
289is(lc("\x{1E9E}"), uni_to_native("\x{df}"), "lc(LATIN CAPITAL LETTER SHARP S)");
290
291{
292    use feature 'unicode_strings';
293    use bytes;
294    is(lc(uni_to_native("\xc0")), uni_to_native("\xc0"), "lc of above-ASCII Latin1 is itself under use bytes");
295    is(lcfirst(uni_to_native("\xc0")), uni_to_native("\xc0"), "lcfirst of above-ASCII Latin1 is itself under use bytes");
296    is(uc(uni_to_native("\xe0")), uni_to_native("\xe0"), "uc of above-ASCII Latin1 is itself under use bytes");
297    is(ucfirst(uni_to_native("\xe0")), uni_to_native("\xe0"), "ucfirst of above-ASCII Latin1 is itself under use bytes");
298}
299
300# Brought up in ticket #117855: Constant folding applied to uc() should use
301# the right set of hints.
302fresh_perl_like(<<'constantfolding', qr/^(\d+),\1\z/, {},
303    my $function = "uc";
304    my $char = "\xff";
305    {
306        use feature 'unicode_strings';
307        print ord uc($char), ",",
308              ord eval "$function('$char')", "\n";
309    }
310constantfolding
311    'folded uc() in string eval uses the right hints');
312
313# In-place lc/uc should not corrupt string buffers when given a non-utf8-
314# flagged thingy that stringifies to utf8
315my %h;
316$h{k} = bless[], "\x{3b0}\x{3b0}\x{3b0}bcde"; # U+03B0 grows with uc()
317   # using delete marks it as TEMP, so uc-in-place is permitted
318like uc delete $h{k}, qr "^(?:\x{3a5}\x{308}\x{301}){3}BCDE=ARRAY\(.*\)",
319    'uc(TEMP ref) does not produce a corrupt string';
320$h{k} = bless[], "\x{130}bcde"; # U+0130 grows with lc()
321   # using delete marks it as TEMP, so uc-in-place is permitted
322like lc delete $h{k}, qr "^i\x{307}bcde=array\(.*\)",
323    'lc(TEMP ref) does not produce a corrupt string';
324
325# List::Util::first() etc sets $_ to an SvTEMP without raising its
326# refcount.  This was causing lc() etc to unsafely modify in-place.
327# see http://nntp.perl.org/group/perl.perl5.porters/228213
328
329SKIP: {
330    skip "no List::Util on miniperl", 5, if is_miniperl;
331    require List::Util;
332    my %hl = qw(a 1 b 2 c 3);
333    my %hu = qw(A 1 B 2 C 3);
334    my $x;
335    $x = List::Util::first(sub { uc      $_ eq 'A' }, keys %hl);
336    is($x, "a", "first { uc }");
337    $x = List::Util::first(sub { ucfirst $_ eq 'A' }, keys %hl);
338    is($x, "a", "first { ucfirst }");
339    $x = List::Util::first(sub { lc      $_ eq 'a' }, keys %hu);
340    is($x, "A", "first { lc }");
341    $x = List::Util::first(sub { lcfirst $_ eq 'a' }, keys %hu);
342    is($x, "A", "first { lcfirst }");
343    $x = List::Util::first(sub { fc      $_ eq 'a' }, keys %hu);
344    is($x, "A", "first { fc }");
345}
346
347my $non_turkic_locale = find_utf8_ctype_locale();
348my $turkic_locale = find_utf8_turkic_locale();
349
350foreach my $turkic (0 .. 1) {
351    my $type = ($turkic) ? "turkic" : "non-turkic";
352    my $locale = ($turkic) ? $turkic_locale : $non_turkic_locale;
353
354  SKIP: {
355    skip "Can't find a $type UTF-8 locale", 5*256 unless defined $locale;
356
357    use feature qw( unicode_strings );
358
359    no locale;
360
361    my @unicode_lc;
362    my @unicode_fc;
363    my @unicode_uc;
364    my @unicode_lcfirst;
365    my @unicode_ucfirst;
366
367    # Get all the values outside of 'locale'
368    for my $i (0 .. 255) {
369        push @unicode_lc, lc(chr $i);
370        push @unicode_fc, fc(chr $i);
371        push @unicode_uc, uc(chr $i);
372        push @unicode_lcfirst, lcfirst(chr $i);
373        push @unicode_ucfirst, ucfirst(chr $i);
374    }
375
376    if ($turkic) {
377        $unicode_lc[ord 'I'] = chr 0x131;
378        $unicode_fc[ord 'I'] = chr 0x131;
379        $unicode_lcfirst[ord 'I'] = chr 0x131;
380        $unicode_uc[ord 'i'] = chr 0x130;
381        $unicode_ucfirst[ord 'i'] = chr 0x130;
382    }
383
384    use locale;
385    setlocale(&POSIX::LC_CTYPE, $locale);
386
387    for my $i (0 .. 255) {
388        is(lc(chr $i), $unicode_lc[$i], "In a $type UTF-8 locale, lc(chr $i) is the same as official Unicode");
389        is(uc(chr $i), $unicode_uc[$i], "In a $type UTF-8 locale, uc(chr $i) is the same as official Unicode");
390        is(fc(chr $i), $unicode_fc[$i], "In a $type UTF-8 locale, fc(chr $i) is the same as official Unicode");
391        is(lcfirst(chr $i), $unicode_lcfirst[$i], "In a $type UTF-8 locale, lcfirst(chr $i) is the same as official Unicode");
392        is(ucfirst(chr $i), $unicode_ucfirst[$i], "In a $type UTF-8 locale, ucfirst(chr $i) is the same as official Unicode");
393    }
394  }
395}
396
397SKIP: {
398    skip "Can't find a turkic UTF-8 locale", 17 unless defined $turkic_locale;
399
400    # These are designed to stress the calculation of space needed for the
401    # strings.  $filler contains a variety of characters that have special
402    # handling in the casing functions, and some regular chars as well.
403    # (0x49 = 'I')
404    my $filler_length = 10000;
405    my $filler = uni_to_native("\x{df}\x{49}\x{69}\x{b5}\x{e0}\x{c1}\x{b6}\x{ff}") x $filler_length;
406
407    # These are the correct answers to what should happen when the given
408    # casing function is called on $filler;
409    my $filler_lc = uni_to_native("\x{df}\x{131}\x{69}\x{b5}\x{e0}\x{e1}\x{b6}\x{ff}") x $filler_length;
410    my $filler_fc = ("ss" . uni_to_native("\x{131}\x{69}\x{3bc}\x{e0}\x{e1}\x{b6}\x{ff}")) x $filler_length;
411    my $filler_uc = ("SS" . uni_to_native("\x{49}\x{130}\x{39c}\x{c0}\x{c1}\x{b6}\x{178}")) x $filler_length;
412
413    use locale;
414    setlocale(&POSIX::LC_CTYPE, $turkic_locale);
415
416    is (lc "IIIIIII$filler", "\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}$filler_lc",
417        "lc non-UTF-8, in Turkic locale, beginning with a bunch of I's");
418    is (fc "IIIIIII$filler", "\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}$filler_fc",
419        "fc non-UTF-8, in Turkic locale, beginning with a bunch of I's");
420    is (lc "${filler}IIIIIII$filler", "$filler_lc\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}$filler_lc",
421        "lc non-UTF-8, in Turkic locale, a bunch of I's, but not at the beginning");
422    is (fc "${filler}IIIIIII$filler", "$filler_fc\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}$filler_fc",
423        "fc non-UTF-8, in Turkic locale, a bunch of I's, but not at the beginning");
424    is (lc "${filler}I\x{307}$filler", "${filler_lc}i$filler_lc",
425        "lc in Turkic locale with DOT ABOVE immediately following I");
426    is (lc "${filler}I\x{307}IIIIII$filler", "${filler_lc}i\x{131}\x{131}\x{131}\x{131}\x{131}\x{131}$filler_lc",
427        "lc in Turkic locale with DOT ABOVE immediately following I, then other I's ");
428    is (lc "${filler}I\x{316}\x{307}$filler", "${filler_lc}i\x{316}$filler_lc",
429        "lc in Turkic locale with DOT ABOVE after non-ABOVE");
430    is (lc "${filler}I\x{307}\x{300}$filler", "${filler_lc}i\x{300}$filler_lc",
431        "lc in Turkic locale with DOT ABOVE followed by ABOVE");
432    is (lc "${filler}I\x{300}\x{307}$filler", "$filler_lc\x{131}\x{300}\x{307}$filler_lc",
433        "lc in Turkic locale with other ABOVE before DOT ABOVE");
434    is (lcfirst "IIIIIII$filler", "\x{131}IIIIII$filler",
435        "lcfirst in Turkic locale, only first I changed");
436    is (lcfirst "I\x{307}$filler", "i$filler",
437        "lcfirst in Turkic locale with DOT ABOVE immediately following I");
438    is (lcfirst "I\x{307}IIIIII$filler", "iIIIIII$filler",
439        "lcfirst in Turkic locale with DOT ABOVE immediately following I, then"
440      . " other I's ");
441    is (lcfirst "I\x{316}\x{307}IIIIII$filler", "i\x{316}IIIIII$filler",
442        "lcfirst in Turkic locale with DOT ABOVE after non-ABOVE");
443    is (lcfirst "I\x{307}\x{300}IIIIII$filler", "i\x{300}IIIIII$filler",
444        "lcfirst in Turkic locale with DOT ABOVE followed by ABOVE");
445    is (lcfirst "I\x{300}\x{307}IIIIII$filler", "\x{131}\x{300}\x{307}IIIIII$filler",
446        "lcfirst in Turkic locale with other ABOVE before DOT ABOVE");
447    is (uc "${filler}i$filler", "$filler_uc\x{130}$filler_uc", "long string uc in Turkic locale");
448    is (ucfirst "ii$filler", "\x{130}i$filler", "long string ucfirst in Turkic locale; only first char changes");
449}
450