convert ***sort builtins to use inout instead of references
[hiphop-php.git] / hphp / test / slow / ext_preg / hackarrdvarrs / ext_preg.php
blob855b17fd74e9c7a6cd4ecd15680d224efd80b3f8
1 <?hh
3 function VS($x, $y) {
4 var_dump($x === $y);
5 if ($x !== $y) { echo "Failed: $y\n"; var_dump(debug_backtrace()); }
7 function VERIFY($x) { VS($x, true); }
9 //////////////////////////////////////////////////////////////////////
11 function test_preg_rep($a,$b,$c) {
12 return strtoupper($c).$a;
15 function test_preg_grep() {
16 $array = array("foo 123.1", "fg 24bar", "123.1", "24");
17 $fl_array = preg_grep("/^(\\d+)?\\.\\d+$/", $array);
18 VS(count($fl_array), 1);
19 VS($fl_array[2], "123.1");
21 VS(preg_grep("/a/", array("c", "b")), array());
24 function test_preg_match() {
25 // The "i" after the pattern delimiter indicates a case-insensitive search
26 VS(preg_match("/php/i", "PHP is a scripting language."), 1);
28 // The \b in the pattern indicates a word boundary, so only the distinct
29 // word "web" is matched, and not a word partial like "webbing" or "cobweb"
30 VS(preg_match("/\\bweb\\b/i", "is the web scripting"), 1);
32 // get host name from URL
33 $matches = null;
34 preg_match_with_matches(
35 "@^(?:http://)?([^/]+)@i",
36 "http://www.php.net/index.html",
37 inout $matches,
39 $host = $matches[1];
40 VS($host, "www.php.net");
42 // get last two segments of host name
43 preg_match_with_matches("/[^.]+\\.[^.]+$/", $host, inout $matches);
44 VS($matches[0], "php.net");
46 $str = "foobar: 2008";
47 preg_match_with_matches("/(?<name>\\w+): (?<digit>\\d+)/", $str, inout $matches);
48 VS(print_r($matches, true),
49 "Dict\n".
50 "(\n".
51 " [0] => foobar: 2008\n".
52 " [name] => foobar\n".
53 " [1] => foobar\n".
54 " [digit] => 2008\n".
55 " [2] => 2008\n".
56 ")\n");
60 function test_preg_match_all() {
61 $matches = null;
62 preg_match_all_with_matches(
63 "/\\(? (\\d{3})? \\)? (?(1) [\\-\\s] ) \\d{3}-\\d{4}/x",
64 "Call 555-1212 or 1-800-555-1212",
65 inout $matches,
67 VS(print_r($matches, true),
68 "Dict\n".
69 "(\n".
70 " [0] => Dict\n".
71 " (\n".
72 " [0] => 555-1212\n".
73 " [1] => 800-555-1212\n".
74 " )\n".
75 "\n".
76 " [1] => Dict\n".
77 " (\n".
78 " [0] => \n".
79 " [1] => 800\n".
80 " )\n".
81 "\n".
82 ")\n");
84 // The \\2 is an example of backreferencing. This tells pcre that
85 // it must match the second set of parentheses in the regular expression
86 // itself, which would be the ([\w]+) in this case. The extra backslash is
87 // required because the string is in double quotes.
88 $html = "<b>bold text</b><a href=howdy.html>click me</a>";
89 preg_match_all_with_matches(
90 "/(<([\\w]+)[^>]*>)(.*)(<\\/\\2>)/",
91 $html,
92 inout $matches,
93 PREG_SET_ORDER,
95 VS(print_r($matches, true),
96 "Dict\n".
97 "(\n".
98 " [0] => Dict\n".
99 " (\n".
100 " [0] => <b>bold text</b>\n".
101 " [1] => <b>\n".
102 " [2] => b\n".
103 " [3] => bold text\n".
104 " [4] => </b>\n".
105 " )\n".
106 "\n".
107 " [1] => Dict\n".
108 " (\n".
109 " [0] => <a href=howdy.html>click me</a>\n".
110 " [1] => <a href=howdy.html>\n".
111 " [2] => a\n".
112 " [3] => click me\n".
113 " [4] => </a>\n".
114 " )\n".
115 "\n".
116 ")\n");
118 $str = "a: 1\nb: 2\nc: 3\n";
119 preg_match_all_with_matches(
120 "/(?<name>\\w+): (?<digit>\\d+)/",
121 $str,
122 inout $matches,
124 VS(print_r($matches, true),
125 "Dict\n".
126 "(\n".
127 " [0] => Dict\n".
128 " (\n".
129 " [0] => a: 1\n".
130 " [1] => b: 2\n".
131 " [2] => c: 3\n".
132 " )\n".
133 "\n".
134 " [name] => Dict\n".
135 " (\n".
136 " [0] => a\n".
137 " [1] => b\n".
138 " [2] => c\n".
139 " )\n".
140 "\n".
141 " [1] => Dict\n".
142 " (\n".
143 " [0] => a\n".
144 " [1] => b\n".
145 " [2] => c\n".
146 " )\n".
147 "\n".
148 " [digit] => Dict\n".
149 " (\n".
150 " [0] => 1\n".
151 " [1] => 2\n".
152 " [2] => 3\n".
153 " )\n".
154 "\n".
155 " [2] => Dict\n".
156 " (\n".
157 " [0] => 1\n".
158 " [1] => 2\n".
159 " [2] => 3\n".
160 " )\n".
161 "\n".
162 ")\n");
165 function test_preg_replace() {
166 $str = "April 15, 2003";
167 $pattern = "/(\\w+) (\\d+), (\\d+)/i";
168 $replacement = "\${1}1,\$3";
169 VS(preg_replace($pattern, $replacement, $str), "April1,2003");
171 $str = "The quick brown fox jumped over the lazy dog.";
172 $patterns = array();
173 $replacements = array();
174 $patterns[0] = "/quick/";
175 $patterns[1] = "/brown/";
176 $patterns[2] = "/fox/";
177 $replacements[2] = "bear";
178 $replacements[1] = "black";
179 $replacements[0] = "slow";
180 VS(preg_replace($patterns, $replacements, $str),
181 "The bear black slow jumped over the lazy dog.");
183 ksort(inout $patterns);
184 ksort(inout $replacements);
185 VS(preg_replace($patterns, $replacements, $str),
186 "The slow black bear jumped over the lazy dog.");
188 $foos = array();
189 $foos[0] = "foo";
190 $foos[1] = "Foo";
191 $foos[2] = "FOO";
192 $expFoo = dict[];
193 $expFoo[0] = "FOO";
194 $expFoo[1] = "FOO";
195 $expFoo[2] = "FOO";
196 VS(preg_replace("/some pattern/", "", array()), dict[]);
197 VS(preg_replace("/foo/i", "FOO", $foos), $expFoo);
199 $patterns = array("/(19|20)(\\d{2})-(\\d{1,2})-(\\d{1,2})/",
200 "/^\\s*{(\\w+)}\\s*=/");
201 $replace = array("\\3/\\4/\\1\\2", "$\\1 =");
202 VS(preg_replace($patterns, $replace, "{startDate} = 1999-5-27"),
203 "\$startDate = 5/27/1999");
205 $str = "foo o";
206 $str = preg_replace("/\\s\\s+/", " ", $str);
207 VS($str, "foo o");
209 $count = 0;
210 preg_replace_with_count(array("/\\d/", "/\\s/"), "*", "xp 4 to", -1, inout $count);
211 VS($count, 3);
213 VS(preg_replace("/xxx", "w", "xxxx"), NULL);
214 VS(preg_replace("/xxx/", "w", "xxxx"), "wx");
215 VS(preg_replace("/xxy/", "w", "xxxx"), "xxxx");
217 VS(preg_replace("/xxx", "w", array("xxxx")), dict[]);
218 VS(preg_replace("/xxx/", "w", array("xxxx")), dict(array("wx")));
219 VS(preg_replace("/xxx/", "w", array("xxxx", "yyyy")), dict(array("wx", "yyyy")));
220 VS(preg_replace(array("/xxx/", "/xxx"), "w", array("xxxx")), dict[]);
221 VS(preg_replace(array("/xxx/", "/xxx/"), "w", array("xxxx")), dict(array("wx")));
223 VS(preg_replace("/xxx", array("w"), array("xxxx")), false);
224 VS(preg_replace(array("/xxx"), array("w"), array("xxxx")), dict[]);
225 VS(preg_replace(array("/xxx/"), array("w"), array("xxxx")), dict(array("wx")));
228 function next_year($m) {
229 return $m[1].((int)$m[2] + 1);
232 function test_preg_replace_callback() {
233 $text = "April fools day is 04/01/2002\n".
234 "Last christmas was 12/24/2001\n";
235 $count = -1;
236 $text = preg_replace_callback("|(\\d{2}/\\d{2}/)(\\d{4})|", fun("next_year"),
237 $text, -1, inout $count);
238 VS($text, "April fools day is 04/01/2003\nLast christmas was 12/24/2002\n");
241 function test_preg_split() {
242 $keywords = preg_split("/[\\s,]+/",
243 "hypertext language, programming");
244 VS(count($keywords), 3);
245 VS($keywords[0], "hypertext");
246 VS($keywords[1], "language");
247 VS($keywords[2], "programming");
249 $str = "string";
250 $chars = preg_split("//", $str, -1, PREG_SPLIT_NO_EMPTY);
251 VS(count($chars), 6);
252 VS($chars[0], "s");
253 VS($chars[1], "t");
254 VS($chars[2], "r");
255 VS($chars[3], "i");
256 VS($chars[4], "n");
257 VS($chars[5], "g");
259 $chars = preg_split("//", $str, null, PREG_SPLIT_NO_EMPTY);
260 VS(count($chars), 6);
261 VS($chars[0], "s");
262 VS($chars[1], "t");
263 VS($chars[2], "r");
264 VS($chars[3], "i");
265 VS($chars[4], "n");
266 VS($chars[5], "g");
268 $chars = preg_split("//", $str, 3, PREG_SPLIT_NO_EMPTY);
269 VS(count($chars), 3);
270 VS($chars[0], "s");
271 VS($chars[1], "t");
272 VS($chars[2], "ring");
275 $str = "hypertext language programming";
276 $chars = preg_split("/ /", $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
277 VS(print_r($chars, true),
278 "Array\n".
279 "(\n".
280 " [0] => Array\n".
281 " (\n".
282 " [0] => hypertext\n".
283 " [1] => 0\n".
284 " )\n".
285 "\n".
286 " [1] => Array\n".
287 " (\n".
288 " [0] => language\n".
289 " [1] => 10\n".
290 " )\n".
291 "\n".
292 " [2] => Array\n".
293 " (\n".
294 " [0] => programming\n".
295 " [1] => 19\n".
296 " )\n".
297 "\n".
298 ")\n");
301 function test_preg_quote() {
302 $keywords = "$40 for a g3/400";
303 $keywords = preg_quote($keywords, "/");
304 VS($keywords, "\\$40 for a g3\\/400");
306 // In this example, preg_quote($word) is used to keep the
307 // asterisks from having special meaning to the regular
308 // expression.
309 $textbody = "This book is *very* difficult to find.";
310 $word = "*very*";
311 $textbody = preg_replace("/".preg_quote($word)."/",
312 "<i>". $word ."</i>",
313 $textbody);
314 VS($textbody, "This book is <i>*very*</i> difficult to find.");
317 function test_ereg_replace() {
318 $str = "This is a test";
319 VS(str_replace(" is", " was", $str), "This was a test");
320 VS(ereg_replace("( )is", "\\1was", $str), "This was a test");
321 VS(ereg_replace("(( )is)", "\\2was", $str), "This was a test");
323 $num = '4';
324 $str = "This string has four words.";
325 $str = ereg_replace("four", $num, $str);
326 VS($str, "This string has 4 words.");
328 $test = "http://test.com/test";
329 $test = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
330 "<a href=\"\\0\">\\0</a>", $test);
331 VS($test, "<a href=\"http://test.com/test\">http://test.com/test</a>");
334 function test_eregi_replace() {
335 $pattern = "(>[^<]*)(suffix)";
336 $replacement = "\\1<span class=\"search\">\\2</span>";
337 $body = ">whateversuffix";
338 $body = eregi_replace($pattern, $replacement, $body);
339 VS($body, ">whatever<span class=\"search\">suffix</span>");
342 function test_split() {
343 $mb = "\xe4\xbf\xa1\xe6\x81\xaf\x01 2366797";
344 $ret = split("\x01", $mb);
345 VS($ret[0], "\xe4\xbf\xa1\xe6\x81\xaf");
346 VS($ret[1], " 2366797");
348 $date = "04/30/1973";
349 $ret = split("[/.-]", $date);
350 VS($ret[0], "04");
351 VS($ret[1], "30");
352 VS($ret[2], "1973");
355 function test_spliti() {
356 $str = "aBBBaCCCADDDaEEEaGGGA";
357 $chunks = spliti("a", $str, 5);
358 VS($chunks[0], "");
359 VS($chunks[1], "BBB");
360 VS($chunks[2], "CCC");
361 VS($chunks[3], "DDD");
362 VS($chunks[4], "EEEaGGGA");
365 function test_sql_regcase() {
366 VS(sql_regcase("Foo - bar."), "[Ff][Oo][Oo] - [Bb][Aa][Rr].");
370 <<__EntryPoint>>
371 function main_ext_preg() {
372 test_preg_grep();
373 test_preg_match();
374 test_preg_match_all();
375 test_preg_replace();
376 test_preg_replace_callback();
377 test_preg_split();
378 test_preg_quote();
379 test_ereg_replace();
380 test_eregi_replace();
381 test_split();
382 test_spliti();
383 test_sql_regcase();