2 * Copyright (C) 2005 - 2006, Marco Barisione <marco@barisione.org>
3 * Copyright (C) 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #undef G_DISABLE_ASSERT
28 /* U+20AC EURO SIGN (symbol, currency) */
29 #define EURO "\xe2\x82\xac"
30 /* U+00E0 LATIN SMALL LETTER A WITH GRAVE (letter, lowercase) */
31 #define AGRAVE "\xc3\xa0"
32 /* U+00C0 LATIN CAPITAL LETTER A WITH GRAVE (letter, uppercase) */
33 #define AGRAVE_UPPER "\xc3\x80"
34 /* U+00E8 LATIN SMALL LETTER E WITH GRAVE (letter, lowercase) */
35 #define EGRAVE "\xc3\xa8"
36 /* U+00F2 LATIN SMALL LETTER O WITH GRAVE (letter, lowercase) */
37 #define OGRAVE "\xc3\xb2"
38 /* U+014B LATIN SMALL LETTER ENG (letter, lowercase) */
39 #define ENG "\xc5\x8b"
40 /* U+0127 LATIN SMALL LETTER H WITH STROKE (letter, lowercase) */
41 #define HSTROKE "\xc4\xa7"
42 /* U+0634 ARABIC LETTER SHEEN (letter, other) */
43 #define SHEEN "\xd8\xb4"
44 /* U+1374 ETHIOPIC NUMBER THIRTY (number, other) */
45 #define ETH30 "\xe1\x8d\xb4"
47 /* A random value use to mark untouched integer variables. */
48 #define UNTOUCHED -559038737
54 GRegexCompileFlags compile_opts;
55 GRegexMatchFlags match_opts;
58 GRegexCompileFlags real_compile_opts;
59 GRegexMatchFlags real_match_opts;
63 test_new (gconstpointer d)
65 const TestNewData *data = d;
69 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
70 g_assert (regex != NULL);
71 g_assert_no_error (error);
72 g_assert_cmpstr (data->pattern, ==, g_regex_get_pattern (regex));
74 if (data->check_flags)
76 g_assert_cmphex (g_regex_get_compile_flags (regex), ==, data->real_compile_opts);
77 g_assert_cmphex (g_regex_get_match_flags (regex), ==, data->real_match_opts);
80 g_regex_unref (regex);
83 #define TEST_NEW(_pattern, _compile_opts, _match_opts) { \
86 data = g_new0 (TestNewData, 1); \
87 data->pattern = _pattern; \
88 data->compile_opts = _compile_opts; \
89 data->match_opts = _match_opts; \
90 data->expected_error = 0; \
91 data->check_flags = FALSE; \
92 path = g_strdup_printf ("/regex/new/%d", ++total); \
93 g_test_add_data_func_full (path, data, test_new, g_free); \
97 #define TEST_NEW_CHECK_FLAGS(_pattern, _compile_opts, _match_opts, _real_compile_opts, _real_match_opts) { \
100 data = g_new0 (TestNewData, 1); \
101 data->pattern = _pattern; \
102 data->compile_opts = _compile_opts; \
103 data->match_opts = 0; \
104 data->expected_error = 0; \
105 data->check_flags = TRUE; \
106 data->real_compile_opts = _real_compile_opts; \
107 data->real_match_opts = _real_match_opts; \
108 path = g_strdup_printf ("/regex/new-check-flags/%d", ++total); \
109 g_test_add_data_func_full (path, data, test_new, g_free); \
114 test_new_fail (gconstpointer d)
116 const TestNewData *data = d;
118 GError *error = NULL;
120 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
122 g_assert (regex == NULL);
123 g_assert_error (error, G_REGEX_ERROR, data->expected_error);
124 g_error_free (error);
127 #define TEST_NEW_FAIL(_pattern, _compile_opts, _expected_error) { \
130 data = g_new0 (TestNewData, 1); \
131 data->pattern = _pattern; \
132 data->compile_opts = _compile_opts; \
133 data->match_opts = 0; \
134 data->expected_error = _expected_error; \
135 path = g_strdup_printf ("/regex/new-fail/%d", ++total); \
136 g_test_add_data_func_full (path, data, test_new_fail, g_free); \
141 const gchar *pattern;
143 GRegexCompileFlags compile_opts;
144 GRegexMatchFlags match_opts;
148 GRegexMatchFlags match_opts2;
152 test_match_simple (gconstpointer d)
154 const TestMatchData *data = d;
157 match = g_regex_match_simple (data->pattern, data->string, data->compile_opts, data->match_opts);
158 g_assert_cmpint (match, ==, data->expected);
161 #define TEST_MATCH_SIMPLE_NAMED(_name, _pattern, _string, _compile_opts, _match_opts, _expected) { \
162 TestMatchData *data; \
164 data = g_new0 (TestMatchData, 1); \
165 data->pattern = _pattern; \
166 data->string = _string; \
167 data->compile_opts = _compile_opts; \
168 data->match_opts = _match_opts; \
169 data->expected = _expected; \
170 path = g_strdup_printf ("/regex/match-%s/%d", _name, ++total); \
171 g_test_add_data_func_full (path, data, test_match_simple, g_free); \
175 #define TEST_MATCH_SIMPLE(_pattern, _string, _compile_opts, _match_opts, _expected) \
176 TEST_MATCH_SIMPLE_NAMED("simple", _pattern, _string, _compile_opts, _match_opts, _expected)
177 #define TEST_MATCH_NOTEMPTY(_pattern, _string, _expected) \
178 TEST_MATCH_SIMPLE_NAMED("notempty", _pattern, _string, 0, G_REGEX_MATCH_NOTEMPTY, _expected)
179 #define TEST_MATCH_NOTEMPTY_ATSTART(_pattern, _string, _expected) \
180 TEST_MATCH_SIMPLE_NAMED("notempty-atstart", _pattern, _string, 0, G_REGEX_MATCH_NOTEMPTY_ATSTART, _expected)
183 test_match (gconstpointer d)
185 const TestMatchData *data = d;
188 GError *error = NULL;
190 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
191 g_assert (regex != NULL);
192 g_assert_no_error (error);
194 match = g_regex_match_full (regex, data->string, data->string_len,
195 data->start_position, data->match_opts2, NULL, NULL);
197 g_assert_cmpint (match, ==, data->expected);
199 if (data->string_len == -1 && data->start_position == 0)
201 match = g_regex_match (regex, data->string, data->match_opts2, NULL);
202 g_assert_cmpint (match, ==, data->expected);
205 g_regex_unref (regex);
208 #define TEST_MATCH(_pattern, _compile_opts, _match_opts, _string, \
209 _string_len, _start_position, _match_opts2, _expected) { \
210 TestMatchData *data; \
212 data = g_new0 (TestMatchData, 1); \
213 data->pattern = _pattern; \
214 data->compile_opts = _compile_opts; \
215 data->match_opts = _match_opts; \
216 data->string = _string; \
217 data->string_len = _string_len; \
218 data->start_position = _start_position; \
219 data->match_opts2 = _match_opts2; \
220 data->expected = _expected; \
221 path = g_strdup_printf ("/regex/match/%d", ++total); \
222 g_test_add_data_func_full (path, data, test_match, g_free); \
231 typedef struct _Match Match;
234 free_match (gpointer data)
239 g_free (match->string);
244 const gchar *pattern;
252 test_match_next (gconstpointer d)
254 const TestMatchNextData *data = d;
256 GMatchInfo *match_info;
258 GSList *l_exp, *l_match;
260 regex = g_regex_new (data->pattern, 0, 0, NULL);
262 g_assert (regex != NULL);
264 g_regex_match_full (regex, data->string, data->string_len,
265 data->start_position, 0, &match_info, NULL);
267 while (g_match_info_matches (match_info))
269 Match *match = g_new0 (Match, 1);
270 match->string = g_match_info_fetch (match_info, 0);
271 match->start = UNTOUCHED;
272 match->end = UNTOUCHED;
273 g_match_info_fetch_pos (match_info, 0, &match->start, &match->end);
274 matches = g_slist_prepend (matches, match);
275 g_match_info_next (match_info, NULL);
277 g_assert (regex == g_match_info_get_regex (match_info));
278 g_assert_cmpstr (data->string, ==, g_match_info_get_string (match_info));
279 g_match_info_free (match_info);
280 matches = g_slist_reverse (matches);
282 g_assert_cmpint (g_slist_length (matches), ==, g_slist_length (data->expected));
284 l_exp = data->expected;
286 while (l_exp != NULL)
288 Match *exp = l_exp->data;
289 Match *match = l_match->data;
291 g_assert_cmpstr (exp->string, ==, match->string);
292 g_assert_cmpint (exp->start, ==, match->start);
293 g_assert_cmpint (exp->end, ==, match->end);
295 l_exp = g_slist_next (l_exp);
296 l_match = g_slist_next (l_match);
299 g_regex_unref (regex);
300 g_slist_free_full (matches, free_match);
304 free_match_next_data (gpointer _data)
306 TestMatchNextData *data = _data;
308 g_slist_free_full (data->expected, g_free);
312 #define TEST_MATCH_NEXT0(_pattern, _string, _string_len, _start_position) { \
313 TestMatchNextData *data; \
315 data = g_new0 (TestMatchNextData, 1); \
316 data->pattern = _pattern; \
317 data->string = _string; \
318 data->string_len = _string_len; \
319 data->start_position = _start_position; \
320 data->expected = NULL; \
321 path = g_strdup_printf ("/regex/match/next0/%d", ++total); \
322 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
326 #define TEST_MATCH_NEXT1(_pattern, _string, _string_len, _start_position, \
328 TestMatchNextData *data; \
331 data = g_new0 (TestMatchNextData, 1); \
332 data->pattern = _pattern; \
333 data->string = _string; \
334 data->string_len = _string_len; \
335 data->start_position = _start_position; \
336 match = g_new0 (Match, 1); \
337 match->string = t1; \
340 data->expected = g_slist_append (NULL, match); \
341 path = g_strdup_printf ("/regex/match/next1/%d", ++total); \
342 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
346 #define TEST_MATCH_NEXT2(_pattern, _string, _string_len, _start_position, \
347 t1, s1, e1, t2, s2, e2) { \
348 TestMatchNextData *data; \
351 data = g_new0 (TestMatchNextData, 1); \
352 data->pattern = _pattern; \
353 data->string = _string; \
354 data->string_len = _string_len; \
355 data->start_position = _start_position; \
356 match = g_new0 (Match, 1); \
357 match->string = t1; \
360 data->expected = g_slist_append (NULL, match); \
361 match = g_new0 (Match, 1); \
362 match->string = t2; \
365 data->expected = g_slist_append (data->expected, match); \
366 path = g_strdup_printf ("/regex/match/next2/%d", ++total); \
367 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
371 #define TEST_MATCH_NEXT3(_pattern, _string, _string_len, _start_position, \
372 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
373 TestMatchNextData *data; \
376 data = g_new0 (TestMatchNextData, 1); \
377 data->pattern = _pattern; \
378 data->string = _string; \
379 data->string_len = _string_len; \
380 data->start_position = _start_position; \
381 match = g_new0 (Match, 1); \
382 match->string = t1; \
385 data->expected = g_slist_append (NULL, match); \
386 match = g_new0 (Match, 1); \
387 match->string = t2; \
390 data->expected = g_slist_append (data->expected, match); \
391 match = g_new0 (Match, 1); \
392 match->string = t3; \
395 data->expected = g_slist_append (data->expected, match); \
396 path = g_strdup_printf ("/regex/match/next3/%d", ++total); \
397 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
401 #define TEST_MATCH_NEXT4(_pattern, _string, _string_len, _start_position, \
402 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
403 TestMatchNextData *data; \
406 data = g_new0 (TestMatchNextData, 1); \
407 data->pattern = _pattern; \
408 data->string = _string; \
409 data->string_len = _string_len; \
410 data->start_position = _start_position; \
411 match = g_new0 (Match, 1); \
412 match->string = t1; \
415 data->expected = g_slist_append (NULL, match); \
416 match = g_new0 (Match, 1); \
417 match->string = t2; \
420 data->expected = g_slist_append (data->expected, match); \
421 match = g_new0 (Match, 1); \
422 match->string = t3; \
425 data->expected = g_slist_append (data->expected, match); \
426 match = g_new0 (Match, 1); \
427 match->string = t4; \
430 data->expected = g_slist_append (data->expected, match); \
431 path = g_strdup_printf ("/regex/match/next4/%d", ++total); \
432 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
437 const gchar *pattern;
440 GRegexMatchFlags match_opts;
442 } TestMatchCountData;
445 test_match_count (gconstpointer d)
447 const TestMatchCountData *data = d;
449 GMatchInfo *match_info;
452 regex = g_regex_new (data->pattern, 0, 0, NULL);
454 g_assert (regex != NULL);
456 g_regex_match_full (regex, data->string, -1, data->start_position,
457 data->match_opts, &match_info, NULL);
458 count = g_match_info_get_match_count (match_info);
460 g_assert_cmpint (count, ==, data->expected_count);
462 g_match_info_ref (match_info);
463 g_match_info_unref (match_info);
464 g_match_info_unref (match_info);
465 g_regex_unref (regex);
468 #define TEST_MATCH_COUNT(_pattern, _string, _start_position, _match_opts, _expected_count) { \
469 TestMatchCountData *data; \
471 data = g_new0 (TestMatchCountData, 1); \
472 data->pattern = _pattern; \
473 data->string = _string; \
474 data->start_position = _start_position; \
475 data->match_opts = _match_opts; \
476 data->expected_count = _expected_count; \
477 path = g_strdup_printf ("/regex/match/count/%d", ++total); \
478 g_test_add_data_func_full (path, data, test_match_count, g_free); \
483 test_partial (gconstpointer d)
485 const TestMatchData *data = d;
487 GMatchInfo *match_info;
489 regex = g_regex_new (data->pattern, 0, 0, NULL);
491 g_assert (regex != NULL);
493 g_regex_match (regex, data->string, data->match_opts, &match_info);
495 g_assert_cmpint (data->expected, ==, g_match_info_is_partial_match (match_info));
499 g_assert (!g_match_info_fetch_pos (match_info, 0, NULL, NULL));
500 g_assert (!g_match_info_fetch_pos (match_info, 1, NULL, NULL));
503 g_match_info_free (match_info);
504 g_regex_unref (regex);
507 #define TEST_PARTIAL_FULL(_pattern, _string, _match_opts, _expected) { \
508 TestMatchData *data; \
510 data = g_new0 (TestMatchData, 1); \
511 data->pattern = _pattern; \
512 data->string = _string; \
513 data->match_opts = _match_opts; \
514 data->expected = _expected; \
515 path = g_strdup_printf ("/regex/match/partial/%d", ++total); \
516 g_test_add_data_func_full (path, data, test_partial, g_free); \
520 #define TEST_PARTIAL(_pattern, _string, _expected) TEST_PARTIAL_FULL(_pattern, _string, G_REGEX_MATCH_PARTIAL, _expected)
523 const gchar *pattern;
527 const gchar *expected_sub;
533 test_sub_pattern (gconstpointer d)
535 const TestSubData *data = d;
537 GMatchInfo *match_info;
539 gint start = UNTOUCHED, end = UNTOUCHED;
541 regex = g_regex_new (data->pattern, 0, 0, NULL);
543 g_assert (regex != NULL);
545 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
547 sub_expr = g_match_info_fetch (match_info, data->sub_n);
548 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
551 g_match_info_fetch_pos (match_info, data->sub_n, &start, &end);
552 g_assert_cmpint (start, ==, data->expected_start);
553 g_assert_cmpint (end, ==, data->expected_end);
555 g_match_info_free (match_info);
556 g_regex_unref (regex);
559 #define TEST_SUB_PATTERN(_pattern, _string, _start_position, _sub_n, _expected_sub, \
560 _expected_start, _expected_end) { \
563 data = g_new0 (TestSubData, 1); \
564 data->pattern = _pattern; \
565 data->string = _string; \
566 data->start_position = _start_position; \
567 data->sub_n = _sub_n; \
568 data->expected_sub = _expected_sub; \
569 data->expected_start = _expected_start; \
570 data->expected_end = _expected_end; \
571 path = g_strdup_printf ("/regex/match/subpattern/%d", ++total); \
572 g_test_add_data_func_full (path, data, test_sub_pattern, g_free); \
577 const gchar *pattern;
578 GRegexCompileFlags flags;
581 const gchar *sub_name;
582 const gchar *expected_sub;
588 test_named_sub_pattern (gconstpointer d)
590 const TestNamedSubData *data = d;
592 GMatchInfo *match_info;
593 gint start = UNTOUCHED, end = UNTOUCHED;
596 regex = g_regex_new (data->pattern, data->flags, 0, NULL);
598 g_assert (regex != NULL);
600 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
601 sub_expr = g_match_info_fetch_named (match_info, data->sub_name);
602 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
605 g_match_info_fetch_named_pos (match_info, data->sub_name, &start, &end);
606 g_assert_cmpint (start, ==, data->expected_start);
607 g_assert_cmpint (end, ==, data->expected_end);
609 g_match_info_free (match_info);
610 g_regex_unref (regex);
613 #define TEST_NAMED_SUB_PATTERN(_pattern, _string, _start_position, _sub_name, \
614 _expected_sub, _expected_start, _expected_end) { \
615 TestNamedSubData *data; \
617 data = g_new0 (TestNamedSubData, 1); \
618 data->pattern = _pattern; \
619 data->string = _string; \
621 data->start_position = _start_position; \
622 data->sub_name = _sub_name; \
623 data->expected_sub = _expected_sub; \
624 data->expected_start = _expected_start; \
625 data->expected_end = _expected_end; \
626 path = g_strdup_printf ("/regex/match/named/subpattern/%d", ++total); \
627 g_test_add_data_func_full (path, data, test_named_sub_pattern, g_free); \
631 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(_pattern, _string, _start_position, _sub_name, \
632 _expected_sub, _expected_start, _expected_end) { \
633 TestNamedSubData *data; \
635 data = g_new0 (TestNamedSubData, 1); \
636 data->pattern = _pattern; \
637 data->string = _string; \
638 data->flags = G_REGEX_DUPNAMES; \
639 data->start_position = _start_position; \
640 data->sub_name = _sub_name; \
641 data->expected_sub = _expected_sub; \
642 data->expected_start = _expected_start; \
643 data->expected_end = _expected_end; \
644 path = g_strdup_printf ("/regex/match/subpattern/named/dupnames/%d", ++total); \
645 g_test_add_data_func_full (path, data, test_named_sub_pattern, g_free); \
650 const gchar *pattern;
658 test_fetch_all (gconstpointer d)
660 const TestFetchAllData *data = d;
662 GMatchInfo *match_info;
668 regex = g_regex_new (data->pattern, 0, 0, NULL);
670 g_assert (regex != NULL);
672 g_regex_match (regex, data->string, 0, &match_info);
673 matches = g_match_info_fetch_all (match_info);
675 match_count = g_strv_length (matches);
679 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
681 l_exp = data->expected;
682 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
684 g_assert_cmpstr (l_exp->data, ==, matches[i]);
687 g_match_info_free (match_info);
688 g_regex_unref (regex);
689 g_strfreev (matches);
693 free_fetch_all_data (gpointer _data)
695 TestFetchAllData *data = _data;
697 g_slist_free (data->expected);
701 #define TEST_FETCH_ALL0(_pattern, _string) { \
702 TestFetchAllData *data; \
704 data = g_new0 (TestFetchAllData, 1); \
705 data->pattern = _pattern; \
706 data->string = _string; \
707 data->expected = NULL; \
708 path = g_strdup_printf ("/regex/fetch-all0/%d", ++total); \
709 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
713 #define TEST_FETCH_ALL1(_pattern, _string, e1) { \
714 TestFetchAllData *data; \
716 data = g_new0 (TestFetchAllData, 1); \
717 data->pattern = _pattern; \
718 data->string = _string; \
719 data->expected = g_slist_append (NULL, e1); \
720 path = g_strdup_printf ("/regex/fetch-all1/%d", ++total); \
721 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
725 #define TEST_FETCH_ALL2(_pattern, _string, e1, e2) { \
726 TestFetchAllData *data; \
728 data = g_new0 (TestFetchAllData, 1); \
729 data->pattern = _pattern; \
730 data->string = _string; \
731 data->expected = g_slist_append (NULL, e1); \
732 data->expected = g_slist_append (data->expected, e2); \
733 path = g_strdup_printf ("/regex/fetch-all2/%d", ++total); \
734 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
738 #define TEST_FETCH_ALL3(_pattern, _string, e1, e2, e3) { \
739 TestFetchAllData *data; \
741 data = g_new0 (TestFetchAllData, 1); \
742 data->pattern = _pattern; \
743 data->string = _string; \
744 data->expected = g_slist_append (NULL, e1); \
745 data->expected = g_slist_append (data->expected, e2); \
746 data->expected = g_slist_append (data->expected, e3); \
747 path = g_strdup_printf ("/regex/fetch-all3/%d", ++total); \
748 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
753 test_split_simple (gconstpointer d)
755 const TestFetchAllData *data = d;
761 tokens = g_regex_split_simple (data->pattern, data->string, 0, 0);
763 token_count = g_strv_length (tokens);
767 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
769 l_exp = data->expected;
770 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
772 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
778 #define TEST_SPLIT_SIMPLE0(_pattern, _string) { \
779 TestFetchAllData *data; \
781 data = g_new0 (TestFetchAllData, 1); \
782 data->pattern = _pattern; \
783 data->string = _string; \
784 data->expected = NULL; \
785 path = g_strdup_printf ("/regex/split/simple0/%d", ++total); \
786 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
790 #define TEST_SPLIT_SIMPLE1(_pattern, _string, e1) { \
791 TestFetchAllData *data; \
793 data = g_new0 (TestFetchAllData, 1); \
794 data->pattern = _pattern; \
795 data->string = _string; \
796 data->expected = g_slist_append (NULL, e1); \
797 path = g_strdup_printf ("/regex/split/simple1/%d", ++total); \
798 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
802 #define TEST_SPLIT_SIMPLE2(_pattern, _string, e1, e2) { \
803 TestFetchAllData *data; \
805 data = g_new0 (TestFetchAllData, 1); \
806 data->pattern = _pattern; \
807 data->string = _string; \
808 data->expected = g_slist_append (NULL, e1); \
809 data->expected = g_slist_append (data->expected, e2); \
810 path = g_strdup_printf ("/regex/split/simple2/%d", ++total); \
811 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
815 #define TEST_SPLIT_SIMPLE3(_pattern, _string, e1, e2, e3) { \
816 TestFetchAllData *data; \
818 data = g_new0 (TestFetchAllData, 1); \
819 data->pattern = _pattern; \
820 data->string = _string; \
821 data->expected = g_slist_append (NULL, e1); \
822 data->expected = g_slist_append (data->expected, e2); \
823 data->expected = g_slist_append (data->expected, e3); \
824 path = g_strdup_printf ("/regex/split/simple3/%d", ++total); \
825 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
830 test_split_full (gconstpointer d)
832 const TestFetchAllData *data = d;
839 regex = g_regex_new (data->pattern, 0, 0, NULL);
841 g_assert (regex != NULL);
843 tokens = g_regex_split_full (regex, data->string, -1, data->start_position,
844 0, data->max_tokens, NULL);
846 token_count = g_strv_length (tokens);
850 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
852 l_exp = data->expected;
853 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
855 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
858 g_regex_unref (regex);
863 test_split (gconstpointer d)
865 const TestFetchAllData *data = d;
872 regex = g_regex_new (data->pattern, 0, 0, NULL);
874 g_assert (regex != NULL);
876 tokens = g_regex_split (regex, data->string, 0);
878 token_count = g_strv_length (tokens);
882 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
884 l_exp = data->expected;
885 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
887 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
890 g_regex_unref (regex);
894 #define TEST_SPLIT0(_pattern, _string, _start_position, _max_tokens) { \
895 TestFetchAllData *data; \
897 data = g_new0 (TestFetchAllData, 1); \
898 data->pattern = _pattern; \
899 data->string = _string; \
900 data->start_position = _start_position; \
901 data->max_tokens = _max_tokens; \
902 data->expected = NULL; \
903 if (_start_position == 0 && _max_tokens <= 0) { \
904 path = g_strdup_printf ("/regex/split0/%d", ++total); \
905 g_test_add_data_func (path, data, test_split); \
908 path = g_strdup_printf ("/regex/full-split0/%d", ++total); \
909 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
913 #define TEST_SPLIT1(_pattern, _string, _start_position, _max_tokens, e1) { \
914 TestFetchAllData *data; \
916 data = g_new0 (TestFetchAllData, 1); \
917 data->pattern = _pattern; \
918 data->string = _string; \
919 data->start_position = _start_position; \
920 data->max_tokens = _max_tokens; \
921 data->expected = NULL; \
922 data->expected = g_slist_append (data->expected, e1); \
923 if (_start_position == 0 && _max_tokens <= 0) { \
924 path = g_strdup_printf ("/regex/split1/%d", ++total); \
925 g_test_add_data_func (path, data, test_split); \
928 path = g_strdup_printf ("/regex/full-split1/%d", ++total); \
929 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
933 #define TEST_SPLIT2(_pattern, _string, _start_position, _max_tokens, e1, e2) { \
934 TestFetchAllData *data; \
936 data = g_new0 (TestFetchAllData, 1); \
937 data->pattern = _pattern; \
938 data->string = _string; \
939 data->start_position = _start_position; \
940 data->max_tokens = _max_tokens; \
941 data->expected = NULL; \
942 data->expected = g_slist_append (data->expected, e1); \
943 data->expected = g_slist_append (data->expected, e2); \
944 if (_start_position == 0 && _max_tokens <= 0) { \
945 path = g_strdup_printf ("/regex/split2/%d", ++total); \
946 g_test_add_data_func (path, data, test_split); \
949 path = g_strdup_printf ("/regex/full-split2/%d", ++total); \
950 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
954 #define TEST_SPLIT3(_pattern, _string, _start_position, _max_tokens, e1, e2, e3) { \
955 TestFetchAllData *data; \
957 data = g_new0 (TestFetchAllData, 1); \
958 data->pattern = _pattern; \
959 data->string = _string; \
960 data->start_position = _start_position; \
961 data->max_tokens = _max_tokens; \
962 data->expected = NULL; \
963 data->expected = g_slist_append (data->expected, e1); \
964 data->expected = g_slist_append (data->expected, e2); \
965 data->expected = g_slist_append (data->expected, e3); \
966 if (_start_position == 0 && _max_tokens <= 0) { \
967 path = g_strdup_printf ("/regex/split3/%d", ++total); \
968 g_test_add_data_func (path, data, test_split); \
971 path = g_strdup_printf ("/regex/full-split3/%d", ++total); \
972 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
977 const gchar *string_to_expand;
979 gboolean expected_refs;
980 } TestCheckReplacementData;
983 test_check_replacement (gconstpointer d)
985 const TestCheckReplacementData *data = d;
989 result = g_regex_check_replacement (data->string_to_expand, &has_refs, NULL);
990 g_assert_cmpint (data->expected, ==, result);
993 g_assert_cmpint (data->expected_refs, ==, has_refs);
996 #define TEST_CHECK_REPLACEMENT(_string_to_expand, _expected, _expected_refs) { \
997 TestCheckReplacementData *data; \
999 data = g_new0 (TestCheckReplacementData, 1); \
1000 data->string_to_expand = _string_to_expand; \
1001 data->expected = _expected; \
1002 data->expected_refs = _expected_refs; \
1003 path = g_strdup_printf ("/regex/check-repacement/%d", ++total); \
1004 g_test_add_data_func_full (path, data, test_check_replacement, g_free); \
1009 const gchar *pattern;
1010 const gchar *string;
1011 const gchar *string_to_expand;
1013 const gchar *expected;
1017 test_expand (gconstpointer d)
1019 const TestExpandData *data = d;
1020 GRegex *regex = NULL;
1021 GMatchInfo *match_info = NULL;
1026 regex = g_regex_new (data->pattern, data->raw ? G_REGEX_RAW : 0, 0, NULL);
1027 g_regex_match (regex, data->string, 0, &match_info);
1030 res = g_match_info_expand_references (match_info, data->string_to_expand, NULL);
1031 g_assert_cmpstr (res, ==, data->expected);
1033 g_match_info_free (match_info);
1035 g_regex_unref (regex);
1038 #define TEST_EXPAND(_pattern, _string, _string_to_expand, _raw, _expected) { \
1039 TestExpandData *data; \
1041 data = g_new0 (TestExpandData, 1); \
1042 data->pattern = _pattern; \
1043 data->string = _string; \
1044 data->string_to_expand = _string_to_expand; \
1046 data->expected = _expected; \
1047 path = g_strdup_printf ("/regex/expand/%d", ++total); \
1048 g_test_add_data_func_full (path, data, test_expand, g_free); \
1053 const gchar *pattern;
1054 const gchar *string;
1055 gint start_position;
1056 const gchar *replacement;
1057 const gchar *expected;
1061 test_replace (gconstpointer d)
1063 const TestReplaceData *data = d;
1067 regex = g_regex_new (data->pattern, 0, 0, NULL);
1068 res = g_regex_replace (regex, data->string, -1, data->start_position, data->replacement, 0, NULL);
1070 g_assert_cmpstr (res, ==, data->expected);
1073 g_regex_unref (regex);
1076 #define TEST_REPLACE(_pattern, _string, _start_position, _replacement, _expected) { \
1077 TestReplaceData *data; \
1079 data = g_new0 (TestReplaceData, 1); \
1080 data->pattern = _pattern; \
1081 data->string = _string; \
1082 data->start_position = _start_position; \
1083 data->replacement = _replacement; \
1084 data->expected = _expected; \
1085 path = g_strdup_printf ("/regex/replace/%d", ++total); \
1086 g_test_add_data_func_full (path, data, test_replace, g_free); \
1091 test_replace_lit (gconstpointer d)
1093 const TestReplaceData *data = d;
1097 regex = g_regex_new (data->pattern, 0, 0, NULL);
1098 res = g_regex_replace_literal (regex, data->string, -1, data->start_position,
1099 data->replacement, 0, NULL);
1100 g_assert_cmpstr (res, ==, data->expected);
1103 g_regex_unref (regex);
1106 #define TEST_REPLACE_LIT(_pattern, _string, _start_position, _replacement, _expected) { \
1107 TestReplaceData *data; \
1109 data = g_new0 (TestReplaceData, 1); \
1110 data->pattern = _pattern; \
1111 data->string = _string; \
1112 data->start_position = _start_position; \
1113 data->replacement = _replacement; \
1114 data->expected = _expected; \
1115 path = g_strdup_printf ("/regex/replace-literally/%d", ++total); \
1116 g_test_add_data_func_full (path, data, test_replace_lit, g_free); \
1121 const gchar *pattern;
1124 } TestStringNumData;
1127 test_get_string_number (gconstpointer d)
1129 const TestStringNumData *data = d;
1133 regex = g_regex_new (data->pattern, 0, 0, NULL);
1134 num = g_regex_get_string_number (regex, data->name);
1136 g_assert_cmpint (num, ==, data->expected_num);
1137 g_regex_unref (regex);
1140 #define TEST_GET_STRING_NUMBER(_pattern, _name, _expected_num) { \
1141 TestStringNumData *data; \
1143 data = g_new0 (TestStringNumData, 1); \
1144 data->pattern = _pattern; \
1145 data->name = _name; \
1146 data->expected_num = _expected_num; \
1147 path = g_strdup_printf ("/regex/string-number/%d", ++total); \
1148 g_test_add_data_func_full (path, data, test_get_string_number, g_free); \
1153 const gchar *string;
1155 const gchar *expected;
1159 test_escape (gconstpointer d)
1161 const TestEscapeData *data = d;
1164 escaped = g_regex_escape_string (data->string, data->length);
1166 g_assert_cmpstr (escaped, ==, data->expected);
1171 #define TEST_ESCAPE(_string, _length, _expected) { \
1172 TestEscapeData *data; \
1174 data = g_new0 (TestEscapeData, 1); \
1175 data->string = _string; \
1176 data->length = _length; \
1177 data->expected = _expected; \
1178 path = g_strdup_printf ("/regex/escape/%d", ++total); \
1179 g_test_add_data_func_full (path, data, test_escape, g_free); \
1184 test_escape_nul (gconstpointer d)
1186 const TestEscapeData *data = d;
1189 escaped = g_regex_escape_nul (data->string, data->length);
1191 g_assert_cmpstr (escaped, ==, data->expected);
1196 #define TEST_ESCAPE_NUL(_string, _length, _expected) { \
1197 TestEscapeData *data; \
1199 data = g_new0 (TestEscapeData, 1); \
1200 data->string = _string; \
1201 data->length = _length; \
1202 data->expected = _expected; \
1203 path = g_strdup_printf ("/regex/escape_nul/%d", ++total); \
1204 g_test_add_data_func_full (path, data, test_escape_nul, g_free); \
1209 const gchar *pattern;
1210 const gchar *string;
1212 gint start_position;
1217 test_match_all_full (gconstpointer d)
1219 const TestMatchAllData *data = d;
1221 GMatchInfo *match_info;
1227 regex = g_regex_new (data->pattern, 0, 0, NULL);
1228 match_ok = g_regex_match_all_full (regex, data->string, data->string_len, data->start_position,
1229 0, &match_info, NULL);
1231 if (g_slist_length (data->expected) == 0)
1232 g_assert (!match_ok);
1234 g_assert (match_ok);
1236 match_count = g_match_info_get_match_count (match_info);
1237 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1239 l_exp = data->expected;
1240 for (i = 0; i < match_count; i++)
1243 gchar *matched_string;
1244 Match *exp = l_exp->data;
1246 matched_string = g_match_info_fetch (match_info, i);
1247 g_match_info_fetch_pos (match_info, i, &start, &end);
1249 g_assert_cmpstr (exp->string, ==, matched_string);
1250 g_assert_cmpint (exp->start, ==, start);
1251 g_assert_cmpint (exp->end, ==, end);
1253 g_free (matched_string);
1255 l_exp = g_slist_next (l_exp);
1258 g_match_info_free (match_info);
1259 g_regex_unref (regex);
1263 test_match_all (gconstpointer d)
1265 const TestMatchAllData *data = d;
1267 GMatchInfo *match_info;
1273 regex = g_regex_new (data->pattern, 0, 0, NULL);
1274 match_ok = g_regex_match_all (regex, data->string, 0, &match_info);
1276 if (g_slist_length (data->expected) == 0)
1277 g_assert (!match_ok);
1279 g_assert (match_ok);
1281 match_count = g_match_info_get_match_count (match_info);
1282 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1284 l_exp = data->expected;
1285 for (i = 0; i < match_count; i++)
1288 gchar *matched_string;
1289 Match *exp = l_exp->data;
1291 matched_string = g_match_info_fetch (match_info, i);
1292 g_match_info_fetch_pos (match_info, i, &start, &end);
1294 g_assert_cmpstr (exp->string, ==, matched_string);
1295 g_assert_cmpint (exp->start, ==, start);
1296 g_assert_cmpint (exp->end, ==, end);
1298 g_free (matched_string);
1300 l_exp = g_slist_next (l_exp);
1303 g_match_info_free (match_info);
1304 g_regex_unref (regex);
1308 free_match_all_data (gpointer _data)
1310 TestMatchAllData *data = _data;
1312 g_slist_free_full (data->expected, g_free);
1316 #define TEST_MATCH_ALL0(_pattern, _string, _string_len, _start_position) { \
1317 TestMatchAllData *data; \
1319 data = g_new0 (TestMatchAllData, 1); \
1320 data->pattern = _pattern; \
1321 data->string = _string; \
1322 data->string_len = _string_len; \
1323 data->start_position = _start_position; \
1324 data->expected = NULL; \
1325 if (_string_len == -1 && _start_position == 0) { \
1326 path = g_strdup_printf ("/regex/match-all0/%d", ++total); \
1327 g_test_add_data_func (path, data, test_match_all); \
1330 path = g_strdup_printf ("/regex/match-all-full0/%d", ++total); \
1331 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1335 #define TEST_MATCH_ALL1(_pattern, _string, _string_len, _start_position, \
1337 TestMatchAllData *data; \
1340 data = g_new0 (TestMatchAllData, 1); \
1341 data->pattern = _pattern; \
1342 data->string = _string; \
1343 data->string_len = _string_len; \
1344 data->start_position = _start_position; \
1345 data->expected = NULL; \
1346 match = g_new0 (Match, 1); \
1347 match->string = t1; \
1348 match->start = s1; \
1350 data->expected = g_slist_append (data->expected, match); \
1351 if (_string_len == -1 && _start_position == 0) { \
1352 path = g_strdup_printf ("/regex/match-all1/%d", ++total); \
1353 g_test_add_data_func (path, data, test_match_all); \
1356 path = g_strdup_printf ("/regex/match-all-full1/%d", ++total); \
1357 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1361 #define TEST_MATCH_ALL2(_pattern, _string, _string_len, _start_position, \
1362 t1, s1, e1, t2, s2, e2) { \
1363 TestMatchAllData *data; \
1366 data = g_new0 (TestMatchAllData, 1); \
1367 data->pattern = _pattern; \
1368 data->string = _string; \
1369 data->string_len = _string_len; \
1370 data->start_position = _start_position; \
1371 data->expected = NULL; \
1372 match = g_new0 (Match, 1); \
1373 match->string = t1; \
1374 match->start = s1; \
1376 data->expected = g_slist_append (data->expected, match); \
1377 match = g_new0 (Match, 1); \
1378 match->string = t2; \
1379 match->start = s2; \
1381 data->expected = g_slist_append (data->expected, match); \
1382 if (_string_len == -1 && _start_position == 0) { \
1383 path = g_strdup_printf ("/regex/match-all2/%d", ++total); \
1384 g_test_add_data_func (path, data, test_match_all); \
1387 path = g_strdup_printf ("/regex/match-all-full2/%d", ++total); \
1388 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1392 #define TEST_MATCH_ALL3(_pattern, _string, _string_len, _start_position, \
1393 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
1394 TestMatchAllData *data; \
1397 data = g_new0 (TestMatchAllData, 1); \
1398 data->pattern = _pattern; \
1399 data->string = _string; \
1400 data->string_len = _string_len; \
1401 data->start_position = _start_position; \
1402 data->expected = NULL; \
1403 match = g_new0 (Match, 1); \
1404 match->string = t1; \
1405 match->start = s1; \
1407 data->expected = g_slist_append (data->expected, match); \
1408 match = g_new0 (Match, 1); \
1409 match->string = t2; \
1410 match->start = s2; \
1412 data->expected = g_slist_append (data->expected, match); \
1413 match = g_new0 (Match, 1); \
1414 match->string = t3; \
1415 match->start = s3; \
1417 data->expected = g_slist_append (data->expected, match); \
1418 if (_string_len == -1 && _start_position == 0) { \
1419 path = g_strdup_printf ("/regex/match-all3/%d", ++total); \
1420 g_test_add_data_func (path, data, test_match_all); \
1423 path = g_strdup_printf ("/regex/match-all-full3/%d", ++total); \
1424 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1429 test_properties (void)
1438 regex = g_regex_new ("\\p{L}\\p{Ll}\\p{Lu}\\p{L&}\\p{N}\\p{Nd}", G_REGEX_OPTIMIZE, 0, &error);
1439 res = g_regex_match (regex, "ppPP01", 0, &match);
1441 str = g_match_info_fetch (match, 0);
1442 g_assert_cmpstr (str, ==, "ppPP01");
1445 g_match_info_free (match);
1446 g_regex_unref (regex);
1459 regex = g_regex_new ("[abc\\x{0B1E}\\p{Mn}\\x{0391}-\\x{03A9}]", G_REGEX_OPTIMIZE, 0, &error);
1460 res = g_regex_match (regex, "a:b:\340\254\236:\333\253:\316\240", 0, &match);
1462 str = g_match_info_fetch (match, 0);
1463 g_assert_cmpstr (str, ==, "a");
1465 res = g_match_info_next (match, NULL);
1467 str = g_match_info_fetch (match, 0);
1468 g_assert_cmpstr (str, ==, "b");
1470 res = g_match_info_next (match, NULL);
1472 str = g_match_info_fetch (match, 0);
1473 g_assert_cmpstr (str, ==, "\340\254\236");
1475 res = g_match_info_next (match, NULL);
1477 str = g_match_info_fetch (match, 0);
1478 g_assert_cmpstr (str, ==, "\333\253");
1480 res = g_match_info_next (match, NULL);
1482 str = g_match_info_fetch (match, 0);
1483 g_assert_cmpstr (str, ==, "\316\240");
1486 res = g_match_info_next (match, NULL);
1489 g_match_info_free (match);
1490 g_regex_unref (regex);
1493 /* examples for lookahead assertions taken from pcrepattern(3) */
1495 test_lookahead (void)
1505 regex = g_regex_new ("\\w+(?=;)", G_REGEX_OPTIMIZE, 0, &error);
1507 g_assert_no_error (error);
1508 res = g_regex_match (regex, "word1 word2: word3;", 0, &match);
1510 g_assert (g_match_info_matches (match));
1511 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1512 str = g_match_info_fetch (match, 0);
1513 g_assert_cmpstr (str, ==, "word3");
1515 g_match_info_free (match);
1516 g_regex_unref (regex);
1519 regex = g_regex_new ("foo(?!bar)", G_REGEX_OPTIMIZE, 0, &error);
1521 g_assert_no_error (error);
1522 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1524 g_assert (g_match_info_matches (match));
1525 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1526 res = g_match_info_fetch_pos (match, 0, &start, &end);
1528 g_assert_cmpint (start, ==, 7);
1529 g_assert_cmpint (end, ==, 10);
1530 g_match_info_free (match);
1531 g_regex_unref (regex);
1534 regex = g_regex_new ("(?!bar)foo", G_REGEX_OPTIMIZE, 0, &error);
1536 g_assert_no_error (error);
1537 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1539 g_assert (g_match_info_matches (match));
1540 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1541 res = g_match_info_fetch_pos (match, 0, &start, &end);
1543 g_assert_cmpint (start, ==, 0);
1544 g_assert_cmpint (end, ==, 3);
1545 res = g_match_info_next (match, &error);
1547 g_assert_no_error (error);
1548 res = g_match_info_fetch_pos (match, 0, &start, &end);
1550 g_assert_cmpint (start, ==, 7);
1551 g_assert_cmpint (end, ==, 10);
1552 g_match_info_free (match);
1553 g_regex_unref (regex);
1556 /* examples for lookbehind assertions taken from pcrepattern(3) */
1558 test_lookbehind (void)
1567 regex = g_regex_new ("(?<!foo)bar", G_REGEX_OPTIMIZE, 0, &error);
1569 g_assert_no_error (error);
1570 res = g_regex_match (regex, "foobar boobar", 0, &match);
1572 g_assert (g_match_info_matches (match));
1573 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1574 res = g_match_info_fetch_pos (match, 0, &start, &end);
1576 g_assert_cmpint (start, ==, 10);
1577 g_assert_cmpint (end, ==, 13);
1578 g_match_info_free (match);
1579 g_regex_unref (regex);
1582 regex = g_regex_new ("(?<=bullock|donkey) poo", G_REGEX_OPTIMIZE, 0, &error);
1584 g_assert_no_error (error);
1585 res = g_regex_match (regex, "don poo, and bullock poo", 0, &match);
1587 g_assert (g_match_info_matches (match));
1588 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1589 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1591 g_assert_cmpint (start, ==, 20);
1592 g_match_info_free (match);
1593 g_regex_unref (regex);
1595 regex = g_regex_new ("(?<!dogs?|cats?) x", G_REGEX_OPTIMIZE, 0, &error);
1596 g_assert (regex == NULL);
1597 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1598 g_clear_error (&error);
1600 regex = g_regex_new ("(?<=ab(c|de)) foo", G_REGEX_OPTIMIZE, 0, &error);
1601 g_assert (regex == NULL);
1602 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1603 g_clear_error (&error);
1605 regex = g_regex_new ("(?<=abc|abde)foo", G_REGEX_OPTIMIZE, 0, &error);
1607 g_assert_no_error (error);
1608 res = g_regex_match (regex, "abfoo, abdfoo, abcfoo", 0, &match);
1610 g_assert (g_match_info_matches (match));
1611 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1613 g_assert_cmpint (start, ==, 18);
1614 g_match_info_free (match);
1615 g_regex_unref (regex);
1617 regex = g_regex_new ("^.*+(?<=abcd)", G_REGEX_OPTIMIZE, 0, &error);
1619 g_assert_no_error (error);
1620 res = g_regex_match (regex, "abcabcabcabcabcabcabcabcabcd", 0, &match);
1622 g_assert (g_match_info_matches (match));
1623 g_match_info_free (match);
1624 g_regex_unref (regex);
1626 regex = g_regex_new ("(?<=\\d{3})(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1628 g_assert_no_error (error);
1629 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1631 g_assert (g_match_info_matches (match));
1632 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1634 g_assert_cmpint (start, ==, 20);
1635 g_match_info_free (match);
1636 g_regex_unref (regex);
1638 regex = g_regex_new ("(?<=\\d{3}...)(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1640 g_assert_no_error (error);
1641 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1643 g_assert (g_match_info_matches (match));
1644 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1646 g_assert_cmpint (start, ==, 13);
1647 g_match_info_free (match);
1648 g_regex_unref (regex);
1650 regex = g_regex_new ("(?<=\\d{3}(?!999)...)foo", G_REGEX_OPTIMIZE, 0, &error);
1652 g_assert_no_error (error);
1653 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1655 g_assert (g_match_info_matches (match));
1656 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1658 g_assert_cmpint (start, ==, 13);
1659 g_match_info_free (match);
1660 g_regex_unref (regex);
1662 regex = g_regex_new ("(?<=(?<!foo)bar)baz", G_REGEX_OPTIMIZE, 0, &error);
1664 g_assert_no_error (error);
1665 res = g_regex_match (regex, "foobarbaz barfoobaz barbarbaz", 0, &match);
1667 g_assert (g_match_info_matches (match));
1668 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1670 g_assert_cmpint (start, ==, 26);
1671 g_match_info_free (match);
1672 g_regex_unref (regex);
1675 /* examples for subpatterns taken from pcrepattern(3) */
1677 test_subpattern (void)
1687 regex = g_regex_new ("cat(aract|erpillar|)", G_REGEX_OPTIMIZE, 0, &error);
1689 g_assert_no_error (error);
1690 g_assert_cmpint (g_regex_get_capture_count (regex), ==, 1);
1691 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1692 res = g_regex_match_all (regex, "caterpillar", 0, &match);
1694 g_assert (g_match_info_matches (match));
1695 g_assert_cmpint (g_match_info_get_match_count (match), ==, 2);
1696 str = g_match_info_fetch (match, 0);
1697 g_assert_cmpstr (str, ==, "caterpillar");
1699 str = g_match_info_fetch (match, 1);
1700 g_assert_cmpstr (str, ==, "cat");
1702 g_match_info_free (match);
1703 g_regex_unref (regex);
1705 regex = g_regex_new ("the ((red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1707 g_assert_no_error (error);
1708 g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3);
1709 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1710 res = g_regex_match (regex, "the red king", 0, &match);
1712 g_assert (g_match_info_matches (match));
1713 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1714 str = g_match_info_fetch (match, 0);
1715 g_assert_cmpstr (str, ==, "the red king");
1717 str = g_match_info_fetch (match, 1);
1718 g_assert_cmpstr (str, ==, "red king");
1720 str = g_match_info_fetch (match, 2);
1721 g_assert_cmpstr (str, ==, "red");
1723 str = g_match_info_fetch (match, 3);
1724 g_assert_cmpstr (str, ==, "king");
1726 g_match_info_free (match);
1727 g_regex_unref (regex);
1729 regex = g_regex_new ("the ((?:red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1731 g_assert_no_error (error);
1732 res = g_regex_match (regex, "the white queen", 0, &match);
1734 g_assert (g_match_info_matches (match));
1735 g_assert_cmpint (g_match_info_get_match_count (match), ==, 3);
1736 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1737 str = g_match_info_fetch (match, 0);
1738 g_assert_cmpstr (str, ==, "the white queen");
1740 str = g_match_info_fetch (match, 1);
1741 g_assert_cmpstr (str, ==, "white queen");
1743 str = g_match_info_fetch (match, 2);
1744 g_assert_cmpstr (str, ==, "queen");
1746 g_match_info_free (match);
1747 g_regex_unref (regex);
1749 regex = g_regex_new ("(?|(Sat)(ur)|(Sun))day (morning|afternoon)", G_REGEX_OPTIMIZE, 0, &error);
1751 g_assert_no_error (error);
1752 g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3);
1753 res = g_regex_match (regex, "Saturday morning", 0, &match);
1755 g_assert (g_match_info_matches (match));
1756 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1757 str = g_match_info_fetch (match, 1);
1758 g_assert_cmpstr (str, ==, "Sat");
1760 str = g_match_info_fetch (match, 2);
1761 g_assert_cmpstr (str, ==, "ur");
1763 str = g_match_info_fetch (match, 3);
1764 g_assert_cmpstr (str, ==, "morning");
1766 g_match_info_free (match);
1767 g_regex_unref (regex);
1769 regex = g_regex_new ("(?|(abc)|(def))\\1", G_REGEX_OPTIMIZE, 0, &error);
1771 g_assert_no_error (error);
1772 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 1);
1773 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1775 g_assert (g_match_info_matches (match));
1776 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1778 g_assert_cmpint (start, ==, 0);
1779 res = g_match_info_next (match, &error);
1781 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1783 g_assert_cmpint (start, ==, 21);
1784 g_match_info_free (match);
1785 g_regex_unref (regex);
1787 regex = g_regex_new ("(?|(abc)|(def))(?1)", G_REGEX_OPTIMIZE, 0, &error);
1789 g_assert_no_error (error);
1790 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1792 g_assert (g_match_info_matches (match));
1793 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1795 g_assert_cmpint (start, ==, 0);
1796 res = g_match_info_next (match, &error);
1798 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1800 g_assert_cmpint (start, ==, 14);
1801 g_match_info_free (match);
1802 g_regex_unref (regex);
1804 regex = g_regex_new ("(?<DN>Mon|Fri|Sun)(?:day)?|(?<DN>Tue)(?:sday)?|(?<DN>Wed)(?:nesday)?|(?<DN>Thu)(?:rsday)?|(?<DN>Sat)(?:urday)?", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1806 g_assert_no_error (error);
1807 res = g_regex_match (regex, "Mon Tuesday Wed Saturday", 0, &match);
1809 g_assert (g_match_info_matches (match));
1810 str = g_match_info_fetch_named (match, "DN");
1811 g_assert_cmpstr (str, ==, "Mon");
1813 res = g_match_info_next (match, &error);
1815 str = g_match_info_fetch_named (match, "DN");
1816 g_assert_cmpstr (str, ==, "Tue");
1818 res = g_match_info_next (match, &error);
1820 str = g_match_info_fetch_named (match, "DN");
1821 g_assert_cmpstr (str, ==, "Wed");
1823 res = g_match_info_next (match, &error);
1825 str = g_match_info_fetch_named (match, "DN");
1826 g_assert_cmpstr (str, ==, "Sat");
1828 g_match_info_free (match);
1829 g_regex_unref (regex);
1831 regex = g_regex_new ("^(a|b\\1)+$", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1833 g_assert_no_error (error);
1834 res = g_regex_match (regex, "aaaaaaaaaaaaaaaa", 0, &match);
1836 g_assert (g_match_info_matches (match));
1837 g_match_info_free (match);
1838 res = g_regex_match (regex, "ababbaa", 0, &match);
1840 g_assert (g_match_info_matches (match));
1841 g_match_info_free (match);
1842 g_regex_unref (regex);
1845 /* examples for conditions taken from pcrepattern(3) */
1847 test_condition (void)
1855 regex = g_regex_new ("^(a+)(\\()?[^()]+(?(-1)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1857 g_assert_no_error (error);
1858 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1860 g_assert (g_match_info_matches (match));
1861 g_match_info_free (match);
1862 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1864 g_assert (g_match_info_matches (match));
1865 g_match_info_free (match);
1866 g_regex_unref (regex);
1869 regex = g_regex_new ("^(a+)(?<OPEN>\\()?[^()]+(?(<OPEN>)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1871 g_assert_no_error (error);
1872 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1874 g_assert (g_match_info_matches (match));
1875 g_match_info_free (match);
1876 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1878 g_assert (g_match_info_matches (match));
1879 g_match_info_free (match);
1880 g_regex_unref (regex);
1882 regex = g_regex_new ("^(a+)(?(+1)\\[|\\<)?[^()]+(\\])?(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1884 g_assert_no_error (error);
1885 res = g_regex_match (regex, "a[zzzzzz]b", 0, &match);
1887 g_assert (g_match_info_matches (match));
1888 g_match_info_free (match);
1889 res = g_regex_match (regex, "aaa<zzzzzzbbb", 0, &match);
1891 g_assert (g_match_info_matches (match));
1892 g_match_info_free (match);
1893 g_regex_unref (regex);
1895 regex = g_regex_new ("(?(DEFINE) (?<byte> 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d) )"
1896 "\\b (?&byte) (\\.(?&byte)){3} \\b",
1897 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1899 g_assert_no_error (error);
1900 res = g_regex_match (regex, "128.0.0.1", 0, &match);
1902 g_assert (g_match_info_matches (match));
1903 g_match_info_free (match);
1904 res = g_regex_match (regex, "192.168.1.1", 0, &match);
1906 g_assert (g_match_info_matches (match));
1907 g_match_info_free (match);
1908 res = g_regex_match (regex, "209.132.180.167", 0, &match);
1910 g_assert (g_match_info_matches (match));
1911 g_match_info_free (match);
1912 g_regex_unref (regex);
1914 regex = g_regex_new ("^(?(?=[^a-z]*[a-z])"
1915 "\\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )$",
1916 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1918 g_assert_no_error (error);
1919 res = g_regex_match (regex, "01-abc-24", 0, &match);
1921 g_assert (g_match_info_matches (match));
1922 g_match_info_free (match);
1923 res = g_regex_match (regex, "01-23-45", 0, &match);
1925 g_assert (g_match_info_matches (match));
1926 g_match_info_free (match);
1927 res = g_regex_match (regex, "01-uv-45", 0, &match);
1929 g_assert (!g_match_info_matches (match));
1930 g_match_info_free (match);
1931 res = g_regex_match (regex, "01-234-45", 0, &match);
1933 g_assert (!g_match_info_matches (match));
1934 g_match_info_free (match);
1935 g_regex_unref (regex);
1938 /* examples for recursion taken from pcrepattern(3) */
1940 test_recursion (void)
1949 regex = g_regex_new ("\\( ( [^()]++ | (?R) )* \\)", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1951 g_assert_no_error (error);
1952 res = g_regex_match (regex, "(middle)", 0, &match);
1954 g_assert (g_match_info_matches (match));
1955 g_match_info_free (match);
1956 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
1958 g_assert (g_match_info_matches (match));
1959 g_match_info_free (match);
1960 res = g_regex_match (regex, "(((xxx(((", 0, &match);
1962 g_assert (!g_match_info_matches (match));
1963 g_match_info_free (match);
1964 g_regex_unref (regex);
1966 regex = g_regex_new ("^( \\( ( [^()]++ | (?1) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1968 g_assert_no_error (error);
1969 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
1971 g_assert (g_match_info_matches (match));
1972 g_match_info_free (match);
1973 res = g_regex_match (regex, "(((xxx((()", 0, &match);
1975 g_assert (!g_match_info_matches (match));
1976 g_match_info_free (match);
1977 g_regex_unref (regex);
1979 regex = g_regex_new ("^(?<pn> \\( ( [^()]++ | (?&pn) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1981 g_assert_no_error (error);
1982 g_regex_match (regex, "(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()", 0, &match);
1984 g_assert (!g_match_info_matches (match));
1985 g_match_info_free (match);
1986 g_regex_unref (regex);
1988 regex = g_regex_new ("< (?: (?(R) \\d++ | [^<>]*+) | (?R)) * >", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1990 g_assert_no_error (error);
1991 res = g_regex_match (regex, "<ab<01<23<4>>>>", 0, &match);
1993 g_assert (g_match_info_matches (match));
1994 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1996 g_assert_cmpint (start, ==, 0);
1997 g_match_info_free (match);
1998 res = g_regex_match (regex, "<ab<01<xx<x>>>>", 0, &match);
2000 g_assert (g_match_info_matches (match));
2001 res = g_match_info_fetch_pos (match, 0, &start, NULL);
2003 g_assert_cmpint (start, >, 0);
2004 g_match_info_free (match);
2005 g_regex_unref (regex);
2007 regex = g_regex_new ("^((.)(?1)\\2|.)$", G_REGEX_OPTIMIZE, 0, &error);
2009 g_assert_no_error (error);
2010 res = g_regex_match (regex, "abcdcba", 0, &match);
2012 g_assert (g_match_info_matches (match));
2013 g_match_info_free (match);
2014 res = g_regex_match (regex, "abcddcba", 0, &match);
2016 g_assert (!g_match_info_matches (match));
2017 g_match_info_free (match);
2018 g_regex_unref (regex);
2020 regex = g_regex_new ("^(?:((.)(?1)\\2|)|((.)(?3)\\4|.))$", G_REGEX_OPTIMIZE, 0, &error);
2022 g_assert_no_error (error);
2023 res = g_regex_match (regex, "abcdcba", 0, &match);
2025 g_assert (g_match_info_matches (match));
2026 g_match_info_free (match);
2027 res = g_regex_match (regex, "abcddcba", 0, &match);
2029 g_assert (g_match_info_matches (match));
2030 g_match_info_free (match);
2031 g_regex_unref (regex);
2033 regex = g_regex_new ("^\\W*+(?:((.)\\W*+(?1)\\W*+\\2|)|((.)\\W*+(?3)\\W*+\\4|\\W*+.\\W*+))\\W*+$", G_REGEX_OPTIMIZE|G_REGEX_CASELESS, 0, &error);
2035 g_assert_no_error (error);
2036 res = g_regex_match (regex, "abcdcba", 0, &match);
2038 g_assert (g_match_info_matches (match));
2039 g_match_info_free (match);
2040 res = g_regex_match (regex, "A man, a plan, a canal: Panama!", 0, &match);
2042 g_assert (g_match_info_matches (match));
2043 g_match_info_free (match);
2044 res = g_regex_match (regex, "Oozy rat in a sanitary zoo", 0, &match);
2046 g_assert (g_match_info_matches (match));
2047 g_match_info_free (match);
2048 g_regex_unref (regex);
2052 test_multiline (void)
2058 g_test_bug ("640489");
2060 regex = g_regex_new ("^a$", G_REGEX_MULTILINE|G_REGEX_DOTALL, 0, NULL);
2063 g_regex_match (regex, "a\nb\na", 0, &info);
2064 while (g_match_info_matches (info))
2067 g_match_info_next (info, NULL);
2069 g_match_info_free (info);
2070 g_regex_unref (regex);
2072 g_assert_cmpint (count, ==, 2);
2076 test_explicit_crlf (void)
2080 regex = g_regex_new ("[\r\n]a", 0, 0, NULL);
2081 g_assert_cmpint (g_regex_get_has_cr_or_lf (regex), ==, TRUE);
2082 g_regex_unref (regex);
2086 test_max_lookbehind (void)
2090 regex = g_regex_new ("abc", 0, 0, NULL);
2091 g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 0);
2092 g_regex_unref (regex);
2094 regex = g_regex_new ("\\babc", 0, 0, NULL);
2095 g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 1);
2096 g_regex_unref (regex);
2098 regex = g_regex_new ("(?<=123)abc", 0, 0, NULL);
2099 g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 3);
2100 g_regex_unref (regex);
2104 main (int argc, char *argv[])
2106 setlocale (LC_ALL, "");
2108 g_test_init (&argc, &argv, NULL);
2110 g_test_bug_base ("http://bugzilla.gnome.org/");
2112 g_test_add_func ("/regex/properties", test_properties);
2113 g_test_add_func ("/regex/class", test_class);
2114 g_test_add_func ("/regex/lookahead", test_lookahead);
2115 g_test_add_func ("/regex/lookbehind", test_lookbehind);
2116 g_test_add_func ("/regex/subpattern", test_subpattern);
2117 g_test_add_func ("/regex/condition", test_condition);
2118 g_test_add_func ("/regex/recursion", test_recursion);
2119 g_test_add_func ("/regex/multiline", test_multiline);
2120 g_test_add_func ("/regex/explicit-crlf", test_explicit_crlf);
2121 g_test_add_func ("/regex/max-lookbehind", test_max_lookbehind);
2123 /* TEST_NEW(pattern, compile_opts, match_opts) */
2124 TEST_NEW("[A-Z]+", G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL);
2126 TEST_NEW(".*", 0, 0);
2127 TEST_NEW(".*", G_REGEX_OPTIMIZE, 0);
2128 TEST_NEW(".*", G_REGEX_MULTILINE, 0);
2129 TEST_NEW(".*", G_REGEX_DOTALL, 0);
2130 TEST_NEW(".*", G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL);
2131 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
2132 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS, 0);
2133 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0);
2134 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES, 0);
2135 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0);
2136 /* This gives "internal error: code overflow" with pcre 6.0 */
2137 TEST_NEW("(?i)(?-i)", 0, 0);
2139 /* Check that flags are correct if the pattern modifies them */
2140 /* TEST_NEW_CHECK_FLAGS(pattern, compile_opts, match_ops, real_compile_opts, real_match_opts) */
2141 TEST_NEW_CHECK_FLAGS ("a", G_REGEX_OPTIMIZE, 0, G_REGEX_OPTIMIZE, 0);
2142 TEST_NEW_CHECK_FLAGS ("a", G_REGEX_RAW, 0, G_REGEX_RAW, 0);
2143 TEST_NEW_CHECK_FLAGS ("(?i)a", 0, 0, G_REGEX_CASELESS, 0);
2144 TEST_NEW_CHECK_FLAGS ("(?m)a", 0, 0, G_REGEX_MULTILINE, 0);
2145 TEST_NEW_CHECK_FLAGS ("(?s)a", 0, 0, G_REGEX_DOTALL, 0);
2146 TEST_NEW_CHECK_FLAGS ("(?x)a", 0, 0, G_REGEX_EXTENDED, 0);
2147 TEST_NEW_CHECK_FLAGS ("(?J)a", 0, 0, G_REGEX_DUPNAMES, 0);
2148 TEST_NEW_CHECK_FLAGS ("(?U)[a-z]+", 0, 0, G_REGEX_UNGREEDY, 0);
2149 TEST_NEW_CHECK_FLAGS ("(?X)a", 0, 0, 0 /* not exposed by GRegex */, 0);
2150 TEST_NEW_CHECK_FLAGS ("^.*", 0, 0, G_REGEX_ANCHORED, 0);
2151 TEST_NEW_CHECK_FLAGS ("(*UTF8)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2152 TEST_NEW_CHECK_FLAGS ("(*UCP)a", 0, 0, 0 /* this always on in GRegex */, 0);
2153 TEST_NEW_CHECK_FLAGS ("(*CR)a", 0, 0, G_REGEX_NEWLINE_CR, 0);
2154 TEST_NEW_CHECK_FLAGS ("(*LF)a", 0, 0, G_REGEX_NEWLINE_LF, 0);
2155 TEST_NEW_CHECK_FLAGS ("(*CRLF)a", 0, 0, G_REGEX_NEWLINE_CRLF, 0);
2156 TEST_NEW_CHECK_FLAGS ("(*ANY)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2157 TEST_NEW_CHECK_FLAGS ("(*ANYCRLF)a", 0, 0, G_REGEX_NEWLINE_ANYCRLF, 0);
2158 TEST_NEW_CHECK_FLAGS ("(*BSR_ANYCRLF)a", 0, 0, G_REGEX_BSR_ANYCRLF, 0);
2159 TEST_NEW_CHECK_FLAGS ("(*BSR_UNICODE)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2160 TEST_NEW_CHECK_FLAGS ("(*NO_START_OPT)a", 0, 0, 0 /* not exposed in GRegex */, 0);
2162 /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */
2163 TEST_NEW_FAIL("(", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2164 TEST_NEW_FAIL(")", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2165 TEST_NEW_FAIL("[", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2166 TEST_NEW_FAIL("*", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2167 TEST_NEW_FAIL("?", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2168 TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2170 /* Check all GRegexError codes */
2171 TEST_NEW_FAIL ("a\\", 0, G_REGEX_ERROR_STRAY_BACKSLASH);
2172 TEST_NEW_FAIL ("a\\c", 0, G_REGEX_ERROR_MISSING_CONTROL_CHAR);
2173 TEST_NEW_FAIL ("a\\l", 0, G_REGEX_ERROR_UNRECOGNIZED_ESCAPE);
2174 TEST_NEW_FAIL ("a{4,2}", 0, G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER);
2175 TEST_NEW_FAIL ("a{999999,}", 0, G_REGEX_ERROR_QUANTIFIER_TOO_BIG);
2176 TEST_NEW_FAIL ("[a-z", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2177 TEST_NEW_FAIL ("(?X)[\\B]", 0, G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS);
2178 TEST_NEW_FAIL ("[z-a]", 0, G_REGEX_ERROR_RANGE_OUT_OF_ORDER);
2179 TEST_NEW_FAIL ("{2,4}", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2180 TEST_NEW_FAIL ("a(?u)", 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
2181 TEST_NEW_FAIL ("a(?<$foo)bar", 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
2182 TEST_NEW_FAIL ("a[:alpha:]b", 0, G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS);
2183 TEST_NEW_FAIL ("a(b", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2184 TEST_NEW_FAIL ("a)b", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2185 TEST_NEW_FAIL ("a(?R", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2186 TEST_NEW_FAIL ("a(?-54", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2187 TEST_NEW_FAIL ("(ab\\2)", 0, G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE);
2188 TEST_NEW_FAIL ("a(?#abc", 0, G_REGEX_ERROR_UNTERMINATED_COMMENT);
2189 TEST_NEW_FAIL ("(?<=a+)b", 0, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
2190 TEST_NEW_FAIL ("(?(1?)a|b)", 0, G_REGEX_ERROR_MALFORMED_CONDITION);
2191 TEST_NEW_FAIL ("(a)(?(1)a|b|c)", 0, G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES);
2192 TEST_NEW_FAIL ("(?(?i))", 0, G_REGEX_ERROR_ASSERTION_EXPECTED);
2193 TEST_NEW_FAIL ("a[[:fubar:]]b", 0, G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME);
2194 TEST_NEW_FAIL ("[[.ch.]]", 0, G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED);
2195 TEST_NEW_FAIL ("\\x{110000}", 0, G_REGEX_ERROR_HEX_CODE_TOO_LARGE);
2196 TEST_NEW_FAIL ("^(?(0)f|b)oo", 0, G_REGEX_ERROR_INVALID_CONDITION);
2197 TEST_NEW_FAIL ("(?<=\\C)X", 0, G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND);
2198 TEST_NEW_FAIL ("(?!\\w)(?R)", 0, G_REGEX_ERROR_INFINITE_LOOP);
2199 TEST_NEW_FAIL ("(?(?<ab))", 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR);
2200 TEST_NEW_FAIL ("(?P<x>eks)(?P<x>eccs)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2202 TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_MALFORMED_PROPERTY);
2203 TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_UNKNOWN_PROPERTY);
2205 TEST_NEW_FAIL ("\\666", G_REGEX_RAW, G_REGEX_ERROR_INVALID_OCTAL_VALUE);
2206 TEST_NEW_FAIL ("^(?(DEFINE) abc | xyz ) ", 0, G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE);
2207 TEST_NEW_FAIL ("a", G_REGEX_NEWLINE_CRLF | G_REGEX_NEWLINE_ANYCRLF, G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS);
2208 TEST_NEW_FAIL ("^(a)\\g{3", 0, G_REGEX_ERROR_MISSING_BACK_REFERENCE);
2209 TEST_NEW_FAIL ("^(a)\\g{0}", 0, G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE);
2210 TEST_NEW_FAIL ("abc(*FAIL:123)xyz", 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN);
2211 TEST_NEW_FAIL ("a(*FOOBAR)b", 0, G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB);
2212 TEST_NEW_FAIL ("(?i:A{1,}\\6666666666)", 0, G_REGEX_ERROR_NUMBER_TOO_BIG);
2213 TEST_NEW_FAIL ("(?<a>)(?&)", 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME);
2214 TEST_NEW_FAIL ("(?+-a)", 0, G_REGEX_ERROR_MISSING_DIGIT);
2215 TEST_NEW_FAIL ("TA]", G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_INVALID_DATA_CHARACTER);
2216 TEST_NEW_FAIL ("(?|(?<a>A)|(?<b>B))", 0, G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME);
2217 TEST_NEW_FAIL ("a(*MARK)b", 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED);
2218 TEST_NEW_FAIL ("^\\c€", 0, G_REGEX_ERROR_INVALID_CONTROL_CHAR);
2219 TEST_NEW_FAIL ("\\k", 0, G_REGEX_ERROR_MISSING_NAME);
2220 TEST_NEW_FAIL ("a[\\NB]c", 0, G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS);
2221 TEST_NEW_FAIL ("(*:0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFG)XX", 0, G_REGEX_ERROR_NAME_TOO_LONG);
2222 TEST_NEW_FAIL ("\\u0100", G_REGEX_RAW | G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_CHARACTER_VALUE_TOO_LARGE);
2224 /* These errors can't really be tested sanely:
2225 * G_REGEX_ERROR_EXPRESSION_TOO_LARGE
2226 * G_REGEX_ERROR_MEMORY_ERROR
2227 * G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG
2228 * G_REGEX_ERROR_TOO_MANY_SUBPATTERNS
2229 * G_REGEX_ERROR_TOO_MANY_FORWARD_REFERENCES
2231 * These errors are obsolete and never raised by PCRE:
2232 * G_REGEX_ERROR_DEFINE_REPETION
2235 /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
2236 TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE);
2237 TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE);
2238 TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE);
2239 TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE);
2240 TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED, 0, FALSE);
2241 TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED, FALSE);
2242 TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED, 0, TRUE);
2243 TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED, TRUE);
2244 TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS, 0, TRUE);
2245 TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS, 0, TRUE);
2246 /* These are needed to test extended properties. */
2247 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE);
2248 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE);
2249 TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE);
2250 TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE);
2251 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE, 0, 0, TRUE);
2252 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER, 0, 0, TRUE);
2253 TEST_MATCH_SIMPLE("\\p{L}", SHEEN, 0, 0, TRUE);
2254 TEST_MATCH_SIMPLE("\\p{L}", ETH30, 0, 0, FALSE);
2255 TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE);
2256 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE, 0, 0, TRUE);
2257 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER, 0, 0, FALSE);
2258 TEST_MATCH_SIMPLE("\\p{Ll}", ETH30, 0, 0, FALSE);
2259 TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE, 0, 0, FALSE);
2260 TEST_MATCH_SIMPLE("\\p{Sc}", EURO, 0, 0, TRUE);
2261 TEST_MATCH_SIMPLE("\\p{Sc}", ETH30, 0, 0, FALSE);
2262 TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE);
2263 TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE);
2264 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE, 0, 0, FALSE);
2265 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER, 0, 0, FALSE);
2266 TEST_MATCH_SIMPLE("\\p{N}", SHEEN, 0, 0, FALSE);
2267 TEST_MATCH_SIMPLE("\\p{N}", ETH30, 0, 0, TRUE);
2268 TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE);
2269 TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE);
2270 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE, 0, 0, FALSE);
2271 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER, 0, 0, FALSE);
2272 TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN, 0, 0, FALSE);
2273 TEST_MATCH_SIMPLE("\\p{Nd}", ETH30, 0, 0, FALSE);
2274 TEST_MATCH_SIMPLE("\\p{Common}", SHEEN, 0, 0, FALSE);
2275 TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE);
2276 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE, 0, 0, FALSE);
2277 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER, 0, 0, FALSE);
2278 TEST_MATCH_SIMPLE("\\p{Common}", ETH30, 0, 0, FALSE);
2279 TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE);
2280 TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE);
2281 TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN, 0, 0, TRUE);
2282 TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE);
2283 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE, 0, 0, FALSE);
2284 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER, 0, 0, FALSE);
2285 TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30, 0, 0, FALSE);
2286 TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE);
2287 TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE);
2288 TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN, 0, 0, FALSE);
2289 TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE);
2290 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE, 0, 0, TRUE);
2291 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER, 0, 0, TRUE);
2292 TEST_MATCH_SIMPLE("\\p{Latin}", ETH30, 0, 0, FALSE);
2293 TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE);
2294 TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE);
2295 TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN, 0, 0, FALSE);
2296 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE);
2297 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE, 0, 0, FALSE);
2298 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER, 0, 0, FALSE);
2299 TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30, 0, 0, TRUE);
2300 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE);
2301 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE);
2302 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN, 0, 0, TRUE);
2303 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN, 0, 0, FALSE);
2304 /* Invalid patterns. */
2305 TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE);
2306 TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE);
2308 /* TEST_MATCH(pattern, compile_opts, match_opts, string,
2309 * string_len, start_position, match_opts2, expected) */
2310 TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
2311 TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE);
2312 TEST_MATCH("a", G_REGEX_CASELESS, 0, "A", -1, 0, 0, TRUE);
2313 TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE);
2314 TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE);
2315 TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE);
2316 TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE);
2317 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "a", -1, 0, 0, TRUE);
2318 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ab", -1, 1, 0, FALSE);
2319 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ba", 1, 0, 0, FALSE);
2320 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "bab", -1, 0, 0, FALSE);
2321 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "b", -1, 0, 0, FALSE);
2322 TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED, TRUE);
2323 TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED, FALSE);
2324 TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED, FALSE);
2325 TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED, FALSE);
2326 TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED, FALSE);
2327 TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE);
2328 TEST_MATCH("\\d", 0, 0, EURO, -1, 0, 0, FALSE);
2329 TEST_MATCH("^.$", 0, 0, EURO, -1, 0, 0, TRUE);
2330 TEST_MATCH("^.{3}$", 0, 0, EURO, -1, 0, 0, FALSE);
2331 TEST_MATCH("^.$", G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE);
2332 TEST_MATCH("^.{3}$", G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE);
2333 TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE);
2335 /* New lines handling. */
2336 TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2337 TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE);
2338 TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE);
2339 TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE);
2340 TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE);
2341 TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE);
2342 TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2344 TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE);
2345 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\nb\nc", -1, 0, 0, TRUE);
2346 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2347 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\rb\rc", -1, 0, 0, TRUE);
2348 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc", -1, 0, 0, FALSE);
2349 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc", -1, 0, 0, TRUE);
2350 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc", -1, 0, 0, FALSE);
2351 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2352 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2353 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2354 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc", -1, 0, 0, TRUE);
2355 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2356 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2357 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc", -1, 0, 0, FALSE);
2358 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2359 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc", -1, 0, 0, FALSE);
2360 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2361 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2362 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2363 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc", -1, 0, 0, TRUE);
2364 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2365 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2367 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc", -1, 0, 0, TRUE);
2368 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc", -1, 0, 0, TRUE);
2369 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2370 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2371 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2372 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2373 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2375 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2376 TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2377 TEST_MATCH("a#\rb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2378 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a", -1, 0, 0, FALSE);
2379 TEST_MATCH("a#\nb", G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a", -1, 0, 0, TRUE);
2381 TEST_MATCH("line\nbreak", G_REGEX_MULTILINE, 0, "this is a line\nbreak", -1, 0, 0, TRUE);
2382 TEST_MATCH("line\nbreak", G_REGEX_MULTILINE | G_REGEX_FIRSTLINE, 0, "first line\na line\nbreak", -1, 0, 0, FALSE);
2384 /* This failed with PCRE 7.2 (gnome bug #455640) */
2385 TEST_MATCH(".*$", 0, 0, "\xe1\xbb\x85", -1, 0, 0, TRUE);
2387 /* Test that othercasing in our pcre/glib integration is bug-for-bug compatible
2388 * with pcre's internal tables. Bug #678273 */
2389 TEST_MATCH("[Ç„]", G_REGEX_CASELESS, 0, "Ç„", -1, 0, 0, TRUE);
2390 TEST_MATCH("[Ç„]", G_REGEX_CASELESS, 0, "Ç…", -1, 0, 0, FALSE);
2391 TEST_MATCH("[DŽ]", G_REGEX_CASELESS, 0, "dž", -1, 0, 0, TRUE);
2393 /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
2394 TEST_MATCH_NEXT0("a", "x", -1, 0);
2395 TEST_MATCH_NEXT0("a", "ax", -1, 1);
2396 TEST_MATCH_NEXT0("a", "xa", 1, 0);
2397 TEST_MATCH_NEXT0("a", "axa", 1, 2);
2398 TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
2399 TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
2400 TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5);
2401 TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
2402 TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
2403 TEST_MATCH_NEXT2(EURO "*", EURO EURO, -1, 0, EURO EURO, 0, 6, "", 6, 6);
2404 TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
2405 TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
2406 TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
2407 TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
2408 TEST_MATCH_NEXT2(EURO "+", EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7);
2409 TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
2410 TEST_MATCH_NEXT3("", AGRAVE "b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
2411 TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
2412 TEST_MATCH_NEXT3("a", "aa" OGRAVE "a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
2413 TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
2414 TEST_MATCH_NEXT3("(?=[A-Z0-9])", "RegExTest", -1, 0, "", 0, 0, "", 3, 3, "", 5, 5);
2415 TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
2417 /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
2418 TEST_MATCH_COUNT("a", "", 0, 0, 0);
2419 TEST_MATCH_COUNT("a", "a", 0, 0, 1);
2420 TEST_MATCH_COUNT("a", "a", 1, 0, 0);
2421 TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
2422 TEST_MATCH_COUNT("(.)", EURO, 0, 0, 2);
2423 TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
2424 TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
2425 TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL, 0);
2426 TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
2427 TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
2429 /* TEST_PARTIAL(pattern, string, expected) */
2430 TEST_PARTIAL("^ab", "a", TRUE);
2431 TEST_PARTIAL("^ab", "xa", FALSE);
2432 TEST_PARTIAL("ab", "xa", TRUE);
2433 TEST_PARTIAL("ab", "ab", FALSE); /* normal match. */
2434 TEST_PARTIAL("a+b", "aa", TRUE);
2435 TEST_PARTIAL("(a)+b", "aa", TRUE);
2436 TEST_PARTIAL("a?b", "a", TRUE);
2438 /* Test soft vs. hard partial matching */
2439 TEST_PARTIAL_FULL("cat(fish)?", "cat", G_REGEX_MATCH_PARTIAL_SOFT, FALSE);
2440 TEST_PARTIAL_FULL("cat(fish)?", "cat", G_REGEX_MATCH_PARTIAL_HARD, TRUE);
2442 /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
2443 * expected_start, expected_end) */
2444 TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
2445 TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
2446 TEST_SUB_PATTERN("a(.)", "a" EURO, 0, 1, EURO, 1, 4);
2447 TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG, 0, 2, ENG, 3, 5);
2448 TEST_SUB_PATTERN("(" HSTROKE ")", "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3);
2449 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2450 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2451 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
2452 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
2453 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
2455 /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
2456 * expected_sub, expected_start, expected_end) */
2457 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
2458 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
2459 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "A", "b", 4, 5);
2460 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "B", NULL, UNTOUCHED, UNTOUCHED);
2461 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "C", NULL, UNTOUCHED, UNTOUCHED);
2462 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "A", EGRAVE, 1, 3);
2463 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "B", "x", 3, 4);
2464 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
2465 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
2467 /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
2468 * expected_sub, expected_start, expected_end) */
2469 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2470 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2471 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2472 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2473 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2475 /* DUPNAMES option inside the pattern */
2476 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2477 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2478 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2479 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2480 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2482 /* TEST_FETCH_ALL#(pattern, string, ...) */
2483 TEST_FETCH_ALL0("a", "");
2484 TEST_FETCH_ALL0("a", "b");
2485 TEST_FETCH_ALL1("a", "a", "a");
2486 TEST_FETCH_ALL1("a+", "aa", "aa");
2487 TEST_FETCH_ALL1("(?:a)", "a", "a");
2488 TEST_FETCH_ALL2("(a)", "a", "a", "a");
2489 TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
2490 TEST_FETCH_ALL2("a(.)", "a" HSTROKE, "a" HSTROKE, HSTROKE);
2491 TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
2492 TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
2493 TEST_FETCH_ALL3("(?P<A>.)(a)", ENG "a", ENG "a", ENG, "a");
2494 TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
2495 TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
2497 /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
2498 TEST_SPLIT_SIMPLE0("", "");
2499 TEST_SPLIT_SIMPLE0("a", "");
2500 TEST_SPLIT_SIMPLE1(",", "a", "a");
2501 TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
2502 TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
2503 TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
2504 TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
2505 TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
2506 TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
2507 TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
2508 TEST_SPLIT_SIMPLE2("\\s", "ab c", "ab", "c");
2509 TEST_SPLIT_SIMPLE3("\\s*", "ab c", "a", "b", "c");
2510 /* Not matched sub-strings. */
2511 TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
2512 TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
2513 /* Empty matches. */
2514 TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
2515 TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
2516 /* Invalid patterns. */
2517 TEST_SPLIT_SIMPLE0("\\", "");
2518 TEST_SPLIT_SIMPLE0("[", "");
2520 /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
2521 TEST_SPLIT0("", "", 0, 0);
2522 TEST_SPLIT0("a", "", 0, 0);
2523 TEST_SPLIT0("a", "", 0, 1);
2524 TEST_SPLIT0("a", "", 0, 2);
2525 TEST_SPLIT0("a", "a", 1, 0);
2526 TEST_SPLIT1(",", "a", 0, 0, "a");
2527 TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
2528 TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
2529 TEST_SPLIT1(",", "a,b", 2, 0, "b");
2530 TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
2531 TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
2532 TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
2533 TEST_SPLIT2(",", "a,", 0, 0, "a", "");
2534 TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
2535 TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
2536 TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
2537 TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
2538 TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
2539 /* Not matched sub-strings. */
2540 TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
2541 TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
2542 /* Empty matches. */
2543 TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
2544 TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
2545 TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
2546 TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
2547 TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
2548 TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
2549 TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
2551 /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
2552 TEST_CHECK_REPLACEMENT("", TRUE, FALSE);
2553 TEST_CHECK_REPLACEMENT("a", TRUE, FALSE);
2554 TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE, FALSE);
2555 TEST_CHECK_REPLACEMENT("\\0", TRUE, TRUE);
2556 TEST_CHECK_REPLACEMENT("\\n\\2", TRUE, TRUE);
2557 TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE, TRUE);
2558 /* Invalid strings */
2559 TEST_CHECK_REPLACEMENT("\\Q", FALSE, FALSE);
2560 TEST_CHECK_REPLACEMENT("x\\Ay", FALSE, FALSE);
2562 /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
2563 TEST_EXPAND("a", "a", "", FALSE, "");
2564 TEST_EXPAND("a", "a", "\\0", FALSE, "a");
2565 TEST_EXPAND("a", "a", "\\1", FALSE, "");
2566 TEST_EXPAND("(a)", "ab", "\\1", FALSE, "a");
2567 TEST_EXPAND("(a)", "a", "\\1", FALSE, "a");
2568 TEST_EXPAND("(a)", "a", "\\g<1>", FALSE, "a");
2569 TEST_EXPAND("a", "a", "\\0130", FALSE, "X");
2570 TEST_EXPAND("a", "a", "\\\\\\0", FALSE, "\\a");
2571 TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE, "XbX");
2572 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE, "a");
2573 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE, "a");
2574 TEST_EXPAND(".", EURO, "\\0", FALSE, EURO);
2575 TEST_EXPAND("(.)", EURO, "\\1", FALSE, EURO);
2576 TEST_EXPAND("(?P<G>.)", EURO, "\\g<G>", FALSE, EURO);
2577 TEST_EXPAND(".", "a", EURO, FALSE, EURO);
2578 TEST_EXPAND(".", "a", EURO "\\0", FALSE, EURO "a");
2579 TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE, "abc");
2580 TEST_EXPAND(".", "", "\\LaB\\EC", FALSE, "abC");
2581 TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE, "ABc");
2582 TEST_EXPAND(".", "", "a\\ubc", FALSE, "aBc");
2583 TEST_EXPAND(".", "", "a\\lbc", FALSE, "abc");
2584 TEST_EXPAND(".", "", "A\\uBC", FALSE, "ABC");
2585 TEST_EXPAND(".", "", "A\\lBC", FALSE, "AbC");
2586 TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE, "A\\BC");
2587 TEST_EXPAND(".", "", "\\L" AGRAVE "\\E", FALSE, AGRAVE);
2588 TEST_EXPAND(".", "", "\\U" AGRAVE "\\E", FALSE, AGRAVE_UPPER);
2589 TEST_EXPAND(".", "", "\\u" AGRAVE "a", FALSE, AGRAVE_UPPER "a");
2590 TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE, "xAYz");
2591 TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE, "xbyz");
2592 TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE, "xAyz");
2593 TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE, "xbyz");
2594 TEST_EXPAND(".(.)", "a" AGRAVE_UPPER, "x\\l\\1y", FALSE, "x" AGRAVE "y");
2595 TEST_EXPAND("a", "bab", "\\x{61}", FALSE, "a");
2596 TEST_EXPAND("a", "bab", "\\x61", FALSE, "a");
2597 TEST_EXPAND("a", "bab", "\\x5a", FALSE, "Z");
2598 TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE, "aZ");
2599 TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE, "Z");
2600 TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE, AGRAVE);
2601 TEST_EXPAND("", "bab", "\\x{0634}", FALSE, SHEEN);
2602 TEST_EXPAND("", "bab", "\\x{634}", FALSE, SHEEN);
2603 TEST_EXPAND("", "", "\\t", FALSE, "\t");
2604 TEST_EXPAND("", "", "\\v", FALSE, "\v");
2605 TEST_EXPAND("", "", "\\r", FALSE, "\r");
2606 TEST_EXPAND("", "", "\\n", FALSE, "\n");
2607 TEST_EXPAND("", "", "\\f", FALSE, "\f");
2608 TEST_EXPAND("", "", "\\a", FALSE, "\a");
2609 TEST_EXPAND("", "", "\\b", FALSE, "\b");
2610 TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE, "ab\bb");
2611 TEST_EXPAND("a(.)", "abc", "\\0141", FALSE, "a");
2612 TEST_EXPAND("a(.)", "abc", "\\078", FALSE, "\a8");
2613 TEST_EXPAND("a(.)", "abc", "\\077", FALSE, "?");
2614 TEST_EXPAND("a(.)", "abc", "\\0778", FALSE, "?8");
2615 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", FALSE, AGRAVE);
2616 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", TRUE, "\xc3");
2617 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\0", TRUE, "a\xc3");
2618 /* Invalid strings. */
2619 TEST_EXPAND("", "", "\\Q", FALSE, NULL);
2620 TEST_EXPAND("", "", "x\\Ay", FALSE, NULL);
2621 TEST_EXPAND("", "", "\\g<", FALSE, NULL);
2622 TEST_EXPAND("", "", "\\g<>", FALSE, NULL);
2623 TEST_EXPAND("", "", "\\g<1a>", FALSE, NULL);
2624 TEST_EXPAND("", "", "\\g<a$>", FALSE, NULL);
2625 TEST_EXPAND("", "", "\\", FALSE, NULL);
2626 TEST_EXPAND("a", "a", "\\x{61", FALSE, NULL);
2627 TEST_EXPAND("a", "a", "\\x6X", FALSE, NULL);
2629 TEST_EXPAND(NULL, NULL, "", FALSE, "");
2630 TEST_EXPAND(NULL, NULL, "\\n", FALSE, "\n");
2631 /* Invalid strings */
2632 TEST_EXPAND(NULL, NULL, "\\Q", FALSE, NULL);
2633 TEST_EXPAND(NULL, NULL, "x\\Ay", FALSE, NULL);
2635 /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
2636 TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
2637 TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
2638 TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
2639 TEST_REPLACE("a", "ababa", 3, "A", "ababA");
2640 TEST_REPLACE("a", "ababa", 4, "A", "ababA");
2641 TEST_REPLACE("a", "ababa", 5, "A", "ababa");
2642 TEST_REPLACE("a", "ababa", 6, "A", "ababa");
2643 TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
2644 TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2645 TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2646 TEST_REPLACE("a", "bab", 0, "A", "bAb");
2647 TEST_REPLACE("$^", "abc", 0, "X", "abc");
2648 TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2649 TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2650 TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2651 TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2652 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2653 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2654 TEST_REPLACE("[^-]", "-" EURO "-x-" HSTROKE, 0, "a", "-a-a-a");
2655 TEST_REPLACE("[^-]", "-" EURO "-" HSTROKE, 0, "a\\g<0>a", "-a" EURO "a-a" HSTROKE "a");
2656 TEST_REPLACE("-", "-" EURO "-" HSTROKE, 0, "", EURO HSTROKE);
2657 TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2658 TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2659 TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2660 TEST_REPLACE(".", "a", 0, "\\A", NULL);
2661 TEST_REPLACE(".", "a", 0, "\\g", NULL);
2663 /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2664 TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2665 TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2666 TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2667 TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2668 TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2669 TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2670 TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2671 TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2672 TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2673 TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2674 TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2675 TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2676 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2677 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2678 TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a", "-a" ENG "a-" HSTROKE);
2679 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a", "-a-a-a");
2680 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2681 TEST_REPLACE_LIT("-", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "", EURO AGRAVE HSTROKE);
2682 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 0, "_", "_Reg_Ex_Test");
2683 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 1, "_", "Reg_Ex_Test");
2685 /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2686 TEST_GET_STRING_NUMBER("", "A", -1);
2687 TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2688 TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2689 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2690 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2691 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2692 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2693 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2694 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2695 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2696 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2698 /* TEST_ESCAPE_NUL(string, length, expected) */
2699 TEST_ESCAPE_NUL("hello world", -1, "hello world");
2700 TEST_ESCAPE_NUL("hello\0world", -1, "hello");
2701 TEST_ESCAPE_NUL("\0world", -1, "");
2702 TEST_ESCAPE_NUL("hello world", 5, "hello");
2703 TEST_ESCAPE_NUL("hello.world", 11, "hello.world");
2704 TEST_ESCAPE_NUL("a(b\\b.$", 7, "a(b\\b.$");
2705 TEST_ESCAPE_NUL("hello\0", 6, "hello\\x00");
2706 TEST_ESCAPE_NUL("\0world", 6, "\\x00world");
2707 TEST_ESCAPE_NUL("\0\0", 2, "\\x00\\x00");
2708 TEST_ESCAPE_NUL("hello\0world", 11, "hello\\x00world");
2709 TEST_ESCAPE_NUL("hello\0world\0", 12, "hello\\x00world\\x00");
2710 TEST_ESCAPE_NUL("hello\\\0world", 12, "hello\\x00world");
2711 TEST_ESCAPE_NUL("hello\\\\\0world", 13, "hello\\\\\\x00world");
2712 TEST_ESCAPE_NUL("|()[]{}^$*+?.", 13, "|()[]{}^$*+?.");
2713 TEST_ESCAPE_NUL("|()[]{}^$*+?.\\\\", 15, "|()[]{}^$*+?.\\\\");
2715 /* TEST_ESCAPE(string, length, expected) */
2716 TEST_ESCAPE("hello world", -1, "hello world");
2717 TEST_ESCAPE("hello world", 5, "hello");
2718 TEST_ESCAPE("hello.world", -1, "hello\\.world");
2719 TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2720 TEST_ESCAPE("hello\0world", -1, "hello");
2721 TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2722 TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG);
2723 TEST_ESCAPE("a$", -1, "a\\$");
2724 TEST_ESCAPE("$a", -1, "\\$a");
2725 TEST_ESCAPE("a$a", -1, "a\\$a");
2726 TEST_ESCAPE("$a$", -1, "\\$a\\$");
2727 TEST_ESCAPE("$a$", 0, "");
2728 TEST_ESCAPE("$a$", 1, "\\$");
2729 TEST_ESCAPE("$a$", 2, "\\$a");
2730 TEST_ESCAPE("$a$", 3, "\\$a\\$");
2731 TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2732 TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2733 TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2734 "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2736 /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2737 TEST_MATCH_ALL0("<.*>", "", -1, 0);
2738 TEST_MATCH_ALL0("a+", "", -1, 0);
2739 TEST_MATCH_ALL0("a+", "a", 0, 0);
2740 TEST_MATCH_ALL0("a+", "a", -1, 1);
2741 TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2742 TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2743 TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2744 TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2745 TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2746 TEST_MATCH_ALL1(".+", ENG, -1, 0, ENG, 0, 2);
2747 TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2748 TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2749 TEST_MATCH_ALL2(".+", ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2);
2750 TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2751 "<a><b>", 0, 6, "<a>", 0, 3);
2752 TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2754 /* NOTEMPTY matching */
2755 TEST_MATCH_NOTEMPTY("a?b?", "xyz", FALSE);
2756 TEST_MATCH_NOTEMPTY_ATSTART("a?b?", "xyz", TRUE);
2758 return g_test_run ();