3 # This tests (?[...]). XXX These are just basic tests, as full ones would be
4 # best done with an infrastructure change to allow getting out the inversion
5 # list of the constructed set and then comparing it character by character
6 # with the expected result.
10 @INC = ('../lib','.','../ext/re');
12 require './charset_tools.pl';
13 require './loc_tools.pl';
14 skip_all_without_unicode_tables();
23 no warnings 'experimental::regex_sets';
25 like("a", qr/(?[ [a] # This is a comment
26 ])/, 'Can ignore a comment');
27 like("a", qr/(?[ [a] # [[:notaclass:]]
28 ])/, 'A comment isn\'t parsed');
29 unlike(uni_to_native("\x85"), qr/(?[ \t
\85 ])/, 'NEL is white space');
30 like(uni_to_native("\x85"), qr/(?[ \t + \
\85 ])/, 'can escape NEL to match');
31 like(uni_to_native("\x85"), qr/(?[ [\
\85] ])/, '... including within nested []');
32 like("\t", qr/(?[ \t + \
\85 ])/, 'can do basic union');
33 like("\cK", qr/(?[ \s ])/, '\s matches \cK');
34 unlike("\cK", qr/(?[ \s - \cK ])/, 'can do basic subtraction');
35 like(" ", qr/(?[ \s - \cK ])/, 'can do basic subtraction');
36 like(":", qr/(?[ [:] ])/, '[:] is not a posix class');
37 unlike("\t", qr/(?[ ! \t ])/, 'can do basic complement');
38 like("\t", qr/(?[ ! [ ^ \t ] ])/, 'can do basic complement');
39 unlike("\r", qr/(?[ \t ])/, '\r doesn\'t match \t ');
40 like("\r", qr/(?[ ! \t ])/, 'can do basic complement');
41 like("0", qr/(?[ [:word:] & [:digit:] ])/, 'can do basic intersection');
42 unlike("A", qr/(?[ [:word:] & [:digit:] ])/, 'can do basic intersection');
43 like("0", qr/(?[[:word:]&[:digit:]])/, 'spaces around internal [] aren\'t required');
45 like("a", qr/(?[ [a] | [b] ])/, '| means union');
46 like("b", qr/(?[ [a] | [b] ])/, '| means union');
47 unlike("c", qr/(?[ [a] | [b] ])/, '| means union');
49 like("a", qr/(?[ [ab] ^ [bc] ])/, 'basic symmetric difference works');
50 unlike("b", qr/(?[ [ab] ^ [bc] ])/, 'basic symmetric difference works');
51 like("c", qr/(?[ [ab] ^ [bc] ])/, 'basic symmetric difference works');
53 like("2", qr/(?[ ( ( \pN & ( [a] + [2] ) ) ) ])/, 'Nesting parens and grouping');
54 unlike("a", qr/(?[ ( ( \pN & ( [a] + [2] ) ) ) ])/, 'Nesting parens and grouping');
56 unlike("\x{17f}", qr/(?[ [k] + \p{Blk=ASCII} ])/i, '/i doesn\'t affect \p{}');
57 like("\N{KELVIN SIGN}", qr/(?[ [k] + \p{Blk=ASCII} ])/i, '/i does affect literals');
59 my $thai_or_lao = qr/(?[ \p{Thai} + \p{Lao} ])/;
60 my $thai_or_lao_digit = qr/(?[ \p{Digit} & $thai_or_lao ])/;
61 like("\N{THAI DIGIT ZERO}", $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
62 unlike(chr(ord("\N{THAI DIGIT ZERO}") - 1), $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
63 like("\N{THAI DIGIT NINE}", $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
64 unlike(chr(ord("\N{THAI DIGIT NINE}") + 1), $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
65 like("\N{LAO DIGIT ZERO}", $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
66 unlike(chr(ord("\N{LAO DIGIT ZERO}") - 1), $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
67 like("\N{LAO DIGIT NINE}", $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
68 unlike(chr(ord("\N{LAO DIGIT NINE}") + 1), $thai_or_lao_digit, 'embedded qr/(?[ ])/ works');
70 my $ascii_word = qr/(?[ \w ])/a;
71 my $ascii_digits_plus_all_of_arabic = qr/(?[ \p{Arabic} + \p{Digit} & $ascii_word ])/;
72 like("9", $ascii_digits_plus_all_of_arabic, "/a, then interpolating and intersection works for ASCII in the set");
73 unlike("A", $ascii_digits_plus_all_of_arabic, "/a, then interpolating and intersection works for ASCII not in the set");
74 unlike("\N{BENGALI DIGIT ZERO}", $ascii_digits_plus_all_of_arabic, "/a, then interpolating and intersection works for non-ASCII not in either set");
75 unlike("\N{BENGALI LETTER A}", $ascii_digits_plus_all_of_arabic, "/a, then interpolating and intersection works for non-ASCII in one set");
76 like("\N{ARABIC LETTER HAMZA}", $ascii_digits_plus_all_of_arabic, "intersection has higher precedence than union");
77 like("\N{EXTENDED ARABIC-INDIC DIGIT ZERO}", $ascii_digits_plus_all_of_arabic, "intersection has higher precedence than union");
79 like("\r", qr/(?[ \p{lb=cr} ])/, '\r matches \p{lb=cr}');
80 unlike("\r", qr/(?[ ! \p{lb=cr} ])/, '\r doesnt match ! \p{lb=cr}');
81 like("\r", qr/(?[ ! ! \p{lb=cr} ])/, 'Two ! ! are the original');
82 unlike("\r", qr/(?[ ! ! ! \p{lb=cr} ])/, 'Three ! ! ! are the complement');
85 my $kelvin = qr/(?[ \N{KELVIN SIGN} ])/;
86 my $fold = qr/(?[ $kelvin ])/i;
87 like("\N{KELVIN SIGN}", $kelvin, '"\N{KELVIN SIGN}" matches compiled qr/(?[ \N{KELVIN SIGN} ])/');
88 unlike("K", $fold, "/i on outer (?[ ]) doesn't leak to interpolated one");
89 unlike("k", $fold, "/i on outer (?[ ]) doesn't leak to interpolated one");
91 my $kelvin_fold = qr/(?[ \N{KELVIN SIGN} ])/i;
92 my $still_fold = qr/(?[ $kelvin_fold ])/;
93 like("K", $still_fold, "/i on interpolated (?[ ]) is retained in outer without /i");
94 like("k", $still_fold, "/i on interpolated (?[ ]) is retained in outer without /i");
96 eval 'my $x = qr/(?[ [a] ])/; qr/(?[ $x ])/';
97 is($@, "", 'qr/(?[ [a] ])/ can be interpolated');
99 like("B", qr/(?[ [B] | ! ( [^B] ) ])/, "[perl #125892]");
101 like("a", qr/(?[ (?#comment) [a]])/, "Can have (?#comments)");
103 if (! is_miniperl() && locales_enabled('LC_CTYPE')) {
104 my $utf8_locale = find_utf8_ctype_locale;
106 skip("No utf8 locale available on this platform", 8) unless $utf8_locale;
108 setlocale(&POSIX::LC_ALL, "C");
111 $kelvin_fold = qr/(?[ \N{KELVIN SIGN} ])/i;
112 my $single_char_class = qr/(?[ \: ])/;
114 setlocale(&POSIX::LC_ALL, $utf8_locale);
116 like("\N{KELVIN SIGN}", $kelvin_fold,
117 '(?[ \N{KELVIN SIGN} ]) matches itself under /i in UTF8-locale');
118 like("K", $kelvin_fold,
119 '(?[ \N{KELVIN SIGN} ]) matches "K" under /i in UTF8-locale');
120 like("k", $kelvin_fold,
121 '(?[ \N{KELVIN SIGN} ]) matches "k" under /i in UTF8-locale');
122 like(":", $single_char_class,
123 '(?[ : ]) matches itself in UTF8-locale (a single character class)');
125 setlocale(&POSIX::LC_ALL, "C");
127 # These should generate warnings (the above 4 shouldn't), but like()
128 # suppresses them, so the warnings tests are in t/lib/warnings/regexec
129 $^W = 0; # Suppress the warnings that occur when run by hand with
131 like("\N{KELVIN SIGN}", $kelvin_fold,
132 '(?[ \N{KELVIN SIGN} ]) matches itself under /i in C locale');
133 like("K", $kelvin_fold,
134 '(?[ \N{KELVIN SIGN} ]) matches "K" under /i in C locale');
135 like("k", $kelvin_fold,
136 '(?[ \N{KELVIN SIGN} ]) matches "k" under /i in C locale');
137 like(":", $single_char_class,
138 '(?[ : ]) matches itself in C locale (a single character class)');
142 # RT #126181: \cX behaves strangely inside (?[])
144 no warnings qw(syntax regexp);
146 eval { $_ = '/(?[(\c]) /'; qr/$_/ };
147 like($@, qr/^Syntax error/, '/(?[(\c]) / should not panic');
148 eval { $_ = '(?[\c#]' . "\n])"; qr/$_/ };
149 like($@, qr/^Syntax error/, '/(?[(\c]) / should not panic');
150 eval { $_ = '(?[(\c])'; qr/$_/ };
151 like($@, qr/^Syntax error/, '/(?[(\c])/ should be a syntax error');
152 eval { $_ = '(?[(\c]) ]\b'; qr/$_/ };
153 like($@, qr/^Syntax error/, '/(?[(\c]) ]\b/ should be a syntax error');
154 eval { $_ = '(?[\c[]](])'; qr/$_/ };
155 like($@, qr/^Syntax error/, '/(?[\c[]](])/ should be a syntax error');
156 like("\c#", qr/(?[\c#])/, '\c# should match itself');
157 like("\c[", qr/(?[\c[])/, '\c[ should match itself');
158 like("\c\ ", qr/(?[\c\])/, '\c\ should match itself');
159 like("\c]", qr/(?[\c]])/, '\c] should match itself');
162 # RT #126481 !! with syntax error panics
164 fresh_perl_like('no warnings "experimental::regex_sets"; qr/(?[ ! ! (\w])/',
165 qr/^Unmatched \(/, {},
166 'qr/(?[ ! ! (\w])/ doesnt panic');
167 # The following didn't panic before, but easy to add this here with a
168 # paren between the !!
169 fresh_perl_like('no warnings "experimental::regex_sets";qr/(?[ ! ( ! (\w)])/',
170 qr/^Unmatched \(/, {},
171 'qr/qr/(?[ ! ( ! (\w)])/');