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