This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deprecate above \xFF in bitwise string ops
[perl5.git] / t / op / quotemeta.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require "./test.pl";
6     set_up_inc(  qw(../lib .) );
7     require Config; import Config;
8     require "./loc_tools.pl";
9 }
10
11 plan tests => 60;
12
13 if ($Config{ebcdic} eq 'define') {
14     $_ = join "", map chr($_), 129..233;
15
16     # 105 characters - 52 letters = 53 backslashes
17     # 105 characters + 53 backslashes = 158 characters
18     $_ = quotemeta $_;
19     is(length($_), 158, "quotemeta string");
20     # 104 non-backslash characters
21     is(tr/\\//cd, 104, "tr count non-backslashed");
22 } else { # some ASCII descendant, then.
23     $_ = join "", map chr(utf8::unicode_to_native($_)), 32..127;
24
25     # 96 characters - 52 letters - 10 digits - 1 underscore = 33 backslashes
26     # 96 characters + 33 backslashes = 129 characters
27     $_ = quotemeta $_;
28     is(length($_), 129, "quotemeta string");
29     # 95 non-backslash characters
30     is(tr/\\//cd, 95, "tr count non-backslashed");
31 }
32
33 is(length(quotemeta ""), 0, "quotemeta empty string");
34
35 is("aA\UbB\LcC\EdD", "aABBccdD", 'aA\UbB\LcC\EdD');
36 is("aA\LbB\UcC\EdD", "aAbbCCdD", 'aA\LbB\UcC\EdD');
37 is("\L\upERL", "Perl", '\L\upERL');
38 is("\u\LpERL", "Perl", '\u\LpERL');
39 is("\U\lPerl", "pERL", '\U\lPerl');
40 is("\l\UPerl", "pERL", '\l\UPerl');
41 is("\u\LpE\Q#X#\ER\EL", "Pe\\#x\\#rL", '\u\LpE\Q#X#\ER\EL');
42 is("\l\UPe\Q!x!\Er\El", "pE\\!X\\!Rl", '\l\UPe\Q!x!\Er\El');
43 is("\Q\u\LpE.X.R\EL\E.", "Pe\\.x\\.rL.", '\Q\u\LpE.X.R\EL\E.');
44 is("\Q\l\UPe*x*r\El\E*", "pE\\*X\\*Rl*", '\Q\l\UPe*x*r\El\E*');
45 is("\U\lPerl\E\E\E\E", "pERL", '\U\lPerl\E\E\E\E');
46 is("\l\UPerl\E\E\E\E", "pERL", '\l\UPerl\E\E\E\E');
47
48 is(quotemeta("\x{263a}"), "\\\x{263a}", "quotemeta Unicode quoted");
49 is(length(quotemeta("\x{263a}")), 2, "quotemeta Unicode quoted length");
50 is(quotemeta("\x{100}"), "\x{100}", "quotemeta Unicode nonquoted");
51 is(length(quotemeta("\x{100}")), 1, "quotemeta Unicode nonquoted length");
52
53 my $char = ":";
54 utf8::upgrade($char);
55 is(quotemeta($char), "\\$char", "quotemeta '$char' in UTF-8");
56 is(length(quotemeta($char)), 2, "quotemeta '$char'  in UTF-8 length");
57
58 $char = "M";
59 utf8::upgrade($char);
60 is(quotemeta($char), "$char", "quotemeta '$char' in UTF-8");
61 is(length(quotemeta($char)), 1, "quotemeta '$char'  in UTF-8 length");
62
63 my $char = "\N{U+D7}";
64 utf8::upgrade($char);
65 is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D7}' in UTF-8");
66 is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D7}'  in UTF-8 length");
67
68 $char = "\N{U+DF}";
69 utf8::upgrade($char);
70 is(quotemeta($char), "$char", "quotemeta '\\N{U+DF}' in UTF-8");
71 is(length(quotemeta($char)), 1, "quotemeta '\\N{U+DF}'  in UTF-8 length");
72
73 {
74     no feature 'unicode_strings';
75
76     # BF is chosen because it is NOt alphanumeric in both Latin1 and EBCDIC
77     # DF is chosen because it IS alphanumeric in both Latin1 and EBCDIC
78     is(quotemeta("\x{bf}"), "\\\x{bf}", "quotemeta Latin1 no unicode_strings quoted");
79     is(length(quotemeta("\x{bf}")), 2, "quotemeta Latin1 no unicode_strings quoted length");
80     is(quotemeta("\x{df}"), "\\\x{df}", "quotemeta Latin1 no unicode_strings quoted");
81     is(length(quotemeta("\x{df}")), 2, "quotemeta Latin1 no unicode_strings quoted length");
82
83   SKIP: {
84     skip 'Locales not available', 8 unless locales_enabled('LC_CTYPE');
85     use locale;
86
87     my $char = ":";
88     is(quotemeta($char), "\\$char", "quotemeta '$char' locale");
89     is(length(quotemeta($char)), 2, "quotemeta '$char' locale");
90
91     $char = "M";
92     utf8::upgrade($char);
93     is(quotemeta($char), "$char", "quotemeta '$char' locale");
94     is(length(quotemeta($char)), 1, "quotemeta '$char' locale");
95
96     my $char = "\x{BF}";
97     is(quotemeta($char), "\\$char", "quotemeta '\\x{BF}' locale");
98     is(length(quotemeta($char)), 2, "quotemeta '\\x{BF}' locale length");
99
100     $char = "\x{DF}";  # Every non-ASCII Latin1 is quoted in locale.
101     is(quotemeta($char), "\\$char", "quotemeta '\\x{DF}' locale");
102     is(length(quotemeta($char)), 2, "quotemeta '\\x{DF}' locale length");
103     }
104 }
105 {
106     use feature 'unicode_strings';
107     is(quotemeta("\x{bf}"), "\\\x{bf}", "quotemeta Latin1 unicode_strings quoted");
108     is(length(quotemeta("\x{bf}")), 2, "quotemeta Latin1 unicode_strings quoted length");
109     is(quotemeta("\x{df}"), "\x{df}", "quotemeta Latin1 unicode_strings nonquoted");
110     is(length(quotemeta("\x{df}")), 1, "quotemeta Latin1 unicode_strings nonquoted length");
111
112   SKIP: {
113     skip 'Locales not available', 12 unless locales_enabled('LC_CTYPE');
114     use locale;
115
116     my $char = ":";
117     utf8::upgrade($char);
118     is(quotemeta($char), "\\$char", "quotemeta '$char' locale in UTF-8");
119     is(length(quotemeta($char)), 2, "quotemeta '$char' locale  in UTF-8 length");
120
121     $char = "M";
122     utf8::upgrade($char);
123     is(quotemeta($char), "$char", "quotemeta '$char' locale in UTF-8");
124     is(length(quotemeta($char)), 1, "quotemeta '$char' locale in UTF-8 length");
125
126     my $char = "\N{U+D7}";
127     utf8::upgrade($char);
128     is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D7}' locale in UTF-8");
129     is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D7}' locale in UTF-8 length");
130
131     $char = "\N{U+DF}";  # Every non-ASCII Latin1 is quoted in locale.
132     utf8::upgrade($char);
133     is(quotemeta($char), "\\$char", "quotemeta '\\N{U+DF}' locale in UTF-8");
134     is(length(quotemeta($char)), 2, "quotemeta '\\N{U+DF}' locale in UTF-8 length");
135
136     is(quotemeta("\x{263a}"), "\\\x{263a}", "quotemeta locale Unicode quoted");
137     is(length(quotemeta("\x{263a}")), 2, "quotemeta locale Unicode quoted length");
138     is(quotemeta("\x{100}"), "\x{100}", "quotemeta locale Unicode nonquoted");
139     is(length(quotemeta("\x{100}")), 1, "quotemeta locale Unicode nonquoted length");
140   }
141 }
142
143 $a = "foo|bar";
144 is("a\Q\Ec$a", "acfoo|bar", '\Q\E');
145 is("a\L\Ec$a", "acfoo|bar", '\L\E');
146 is("a\l\Ec$a", "acfoo|bar", '\l\E');
147 is("a\U\Ec$a", "acfoo|bar", '\U\E');
148 is("a\u\Ec$a", "acfoo|bar", '\u\E');