xref: /openbsd/gnu/usr.bin/perl/t/op/quotemeta.t (revision 8529ddd3)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = qw(../lib .);
6    require Config; import Config;
7    require "test.pl";
8}
9
10plan tests => 60;
11
12if ($Config{ebcdic} eq 'define') {
13    $_ = join "", map chr($_), 129..233;
14
15    # 105 characters - 52 letters = 53 backslashes
16    # 105 characters + 53 backslashes = 158 characters
17    $_ = quotemeta $_;
18    is(length($_), 158, "quotemeta string");
19    # 104 non-backslash characters
20    is(tr/\\//cd, 104, "tr count non-backslashed");
21} else { # some ASCII descendant, then.
22    $_ = join "", map chr($_), 32..127;
23
24    # 96 characters - 52 letters - 10 digits - 1 underscore = 33 backslashes
25    # 96 characters + 33 backslashes = 129 characters
26    $_ = quotemeta $_;
27    is(length($_), 129, "quotemeta string");
28    # 95 non-backslash characters
29    is(tr/\\//cd, 95, "tr count non-backslashed");
30}
31
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
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");
78
79  SKIP: {
80    skip 'No locale testing without d_setlocale', 8 if(!$Config{d_setlocale});
81    require locale; import locale;
82
83    my $char = ":";
84    is(quotemeta($char), "\\$char", "quotemeta '$char' locale");
85    is(length(quotemeta($char)), 2, "quotemeta '$char' locale");
86
87    $char = "M";
88    utf8::upgrade($char);
89    is(quotemeta($char), "$char", "quotemeta '$char' locale");
90    is(length(quotemeta($char)), 1, "quotemeta '$char' locale");
91
92    my $char = "\x{D7}";
93    is(quotemeta($char), "\\$char", "quotemeta '\\x{D7}' locale");
94    is(length(quotemeta($char)), 2, "quotemeta '\\x{D7}' locale length");
95
96    $char = "\x{D8}";  # Every non-ASCII Latin1 is quoted in locale.
97    is(quotemeta($char), "\\$char", "quotemeta '\\x{D8}' locale");
98    is(length(quotemeta($char)), 2, "quotemeta '\\x{D8}' locale length");
99    }
100}
101{
102    use feature 'unicode_strings';
103    is(quotemeta("\x{d7}"), "\\\x{d7}", "quotemeta Latin1 unicode_strings quoted");
104    is(length(quotemeta("\x{d7}")), 2, "quotemeta Latin1 unicode_strings quoted length");
105    is(quotemeta("\x{d8}"), "\x{d8}", "quotemeta Latin1 unicode_strings nonquoted");
106    is(length(quotemeta("\x{d8}")), 1, "quotemeta Latin1 unicode_strings nonquoted length");
107
108  SKIP: {
109    skip 'No locale testing without d_setlocale', 12 if(!$Config{d_setlocale});
110    BEGIN {
111        if($Config{d_setlocale}) {
112            require locale; import locale;
113        }
114    }
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    SKIP: {
132    if (
133        !$Config::Config{d_setlocale}
134    || $Config::Config{ccflags} =~ /\bD?NO_LOCALE(_|\b)/
135    ) {
136        skip "no locale support", 2
137    }
138        $char = "\N{U+D8}";  # Every non-ASCII Latin1 is quoted in locale.
139        utf8::upgrade($char);
140        is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D8}' locale in UTF-8");
141        is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D8}' locale in UTF-8 length");
142    }
143
144    is(quotemeta("\x{263a}"), "\\\x{263a}", "quotemeta locale Unicode quoted");
145    is(length(quotemeta("\x{263a}")), 2, "quotemeta locale Unicode quoted length");
146    is(quotemeta("\x{100}"), "\x{100}", "quotemeta locale Unicode nonquoted");
147    is(length(quotemeta("\x{100}")), 1, "quotemeta locale Unicode nonquoted length");
148  }
149}
150
151$a = "foo|bar";
152is("a\Q\Ec$a", "acfoo|bar", '\Q\E');
153is("a\L\Ec$a", "acfoo|bar", '\L\E');
154is("a\l\Ec$a", "acfoo|bar", '\l\E');
155is("a\U\Ec$a", "acfoo|bar", '\U\E');
156is("a\u\Ec$a", "acfoo|bar", '\u\E');
157