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 #ifdef USE_SYSTEM_PCRE
31 #include "glib/pcre/pcre.h"
34 /* U+20AC EURO SIGN (symbol, currency) */
35 #define EURO "\xe2\x82\xac"
36 /* U+00E0 LATIN SMALL LETTER A WITH GRAVE (letter, lowercase) */
37 #define AGRAVE "\xc3\xa0"
38 /* U+00C0 LATIN CAPITAL LETTER A WITH GRAVE (letter, uppercase) */
39 #define AGRAVE_UPPER "\xc3\x80"
40 /* U+00E8 LATIN SMALL LETTER E WITH GRAVE (letter, lowercase) */
41 #define EGRAVE "\xc3\xa8"
42 /* U+00F2 LATIN SMALL LETTER O WITH GRAVE (letter, lowercase) */
43 #define OGRAVE "\xc3\xb2"
44 /* U+014B LATIN SMALL LETTER ENG (letter, lowercase) */
45 #define ENG "\xc5\x8b"
46 /* U+0127 LATIN SMALL LETTER H WITH STROKE (letter, lowercase) */
47 #define HSTROKE "\xc4\xa7"
48 /* U+0634 ARABIC LETTER SHEEN (letter, other) */
49 #define SHEEN "\xd8\xb4"
50 /* U+1374 ETHIOPIC NUMBER THIRTY (number, other) */
51 #define ETH30 "\xe1\x8d\xb4"
53 /* A random value use to mark untouched integer variables. */
54 #define UNTOUCHED -559038737
60 GRegexCompileFlags compile_opts;
61 GRegexMatchFlags match_opts;
64 GRegexCompileFlags real_compile_opts;
65 GRegexMatchFlags real_match_opts;
69 test_new (gconstpointer d)
71 const TestNewData *data = d;
75 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
76 g_assert (regex != NULL);
77 g_assert_no_error (error);
78 g_assert_cmpstr (data->pattern, ==, g_regex_get_pattern (regex));
80 if (data->check_flags)
82 g_assert_cmphex (g_regex_get_compile_flags (regex), ==, data->real_compile_opts);
83 g_assert_cmphex (g_regex_get_match_flags (regex), ==, data->real_match_opts);
86 g_regex_unref (regex);
89 #define TEST_NEW(_pattern, _compile_opts, _match_opts) { \
92 data = g_new0 (TestNewData, 1); \
93 data->pattern = _pattern; \
94 data->compile_opts = _compile_opts; \
95 data->match_opts = _match_opts; \
96 data->expected_error = 0; \
97 data->check_flags = FALSE; \
98 path = g_strdup_printf ("/regex/new/%d", ++total); \
99 g_test_add_data_func_full (path, data, test_new, g_free); \
103 #define TEST_NEW_CHECK_FLAGS(_pattern, _compile_opts, _match_opts, _real_compile_opts, _real_match_opts) { \
106 data = g_new0 (TestNewData, 1); \
107 data->pattern = _pattern; \
108 data->compile_opts = _compile_opts; \
109 data->match_opts = 0; \
110 data->expected_error = 0; \
111 data->check_flags = TRUE; \
112 data->real_compile_opts = _real_compile_opts; \
113 data->real_match_opts = _real_match_opts; \
114 path = g_strdup_printf ("/regex/new-check-flags/%d", ++total); \
115 g_test_add_data_func_full (path, data, test_new, g_free); \
120 test_new_fail (gconstpointer d)
122 const TestNewData *data = d;
124 GError *error = NULL;
126 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
128 g_assert (regex == NULL);
129 g_assert_error (error, G_REGEX_ERROR, data->expected_error);
130 g_error_free (error);
133 #define TEST_NEW_FAIL(_pattern, _compile_opts, _expected_error) { \
136 data = g_new0 (TestNewData, 1); \
137 data->pattern = _pattern; \
138 data->compile_opts = _compile_opts; \
139 data->match_opts = 0; \
140 data->expected_error = _expected_error; \
141 path = g_strdup_printf ("/regex/new-fail/%d", ++total); \
142 g_test_add_data_func_full (path, data, test_new_fail, g_free); \
147 const gchar *pattern;
149 GRegexCompileFlags compile_opts;
150 GRegexMatchFlags match_opts;
154 GRegexMatchFlags match_opts2;
158 test_match_simple (gconstpointer d)
160 const TestMatchData *data = d;
163 match = g_regex_match_simple (data->pattern, data->string, data->compile_opts, data->match_opts);
164 g_assert_cmpint (match, ==, data->expected);
167 #define TEST_MATCH_SIMPLE_NAMED(_name, _pattern, _string, _compile_opts, _match_opts, _expected) { \
168 TestMatchData *data; \
170 data = g_new0 (TestMatchData, 1); \
171 data->pattern = _pattern; \
172 data->string = _string; \
173 data->compile_opts = _compile_opts; \
174 data->match_opts = _match_opts; \
175 data->expected = _expected; \
176 path = g_strdup_printf ("/regex/match-%s/%d", _name, ++total); \
177 g_test_add_data_func_full (path, data, test_match_simple, g_free); \
181 #define TEST_MATCH_SIMPLE(_pattern, _string, _compile_opts, _match_opts, _expected) \
182 TEST_MATCH_SIMPLE_NAMED("simple", _pattern, _string, _compile_opts, _match_opts, _expected)
183 #define TEST_MATCH_NOTEMPTY(_pattern, _string, _expected) \
184 TEST_MATCH_SIMPLE_NAMED("notempty", _pattern, _string, 0, G_REGEX_MATCH_NOTEMPTY, _expected)
185 #define TEST_MATCH_NOTEMPTY_ATSTART(_pattern, _string, _expected) \
186 TEST_MATCH_SIMPLE_NAMED("notempty-atstart", _pattern, _string, 0, G_REGEX_MATCH_NOTEMPTY_ATSTART, _expected)
189 test_match (gconstpointer d)
191 const TestMatchData *data = d;
194 GError *error = NULL;
196 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
197 g_assert (regex != NULL);
198 g_assert_no_error (error);
200 match = g_regex_match_full (regex, data->string, data->string_len,
201 data->start_position, data->match_opts2, NULL, NULL);
206 g_error ("Regex '%s' (with compile options %u and "
207 "match options %u) should have matched '%.*s' "
208 "(of length %d, at position %d, with match options %u) but did not",
209 data->pattern, data->compile_opts, data->match_opts,
210 data->string_len == -1 ? (int) strlen (data->string) :
211 (int) data->string_len,
212 data->string, (int) data->string_len,
213 data->start_position, data->match_opts2);
215 g_assert_cmpint (match, ==, TRUE);
220 g_error ("Regex '%s' (with compile options %u and "
221 "match options %u) should not have matched '%.*s' "
222 "(of length %d, at position %d, with match options %u) but did",
223 data->pattern, data->compile_opts, data->match_opts,
224 data->string_len == -1 ? (int) strlen (data->string) :
225 (int) data->string_len,
226 data->string, (int) data->string_len,
227 data->start_position, data->match_opts2);
230 if (data->string_len == -1 && data->start_position == 0)
232 match = g_regex_match (regex, data->string, data->match_opts2, NULL);
233 g_assert_cmpint (match, ==, data->expected);
236 g_regex_unref (regex);
239 #define TEST_MATCH(_pattern, _compile_opts, _match_opts, _string, \
240 _string_len, _start_position, _match_opts2, _expected) { \
241 TestMatchData *data; \
243 data = g_new0 (TestMatchData, 1); \
244 data->pattern = _pattern; \
245 data->compile_opts = _compile_opts; \
246 data->match_opts = _match_opts; \
247 data->string = _string; \
248 data->string_len = _string_len; \
249 data->start_position = _start_position; \
250 data->match_opts2 = _match_opts2; \
251 data->expected = _expected; \
252 path = g_strdup_printf ("/regex/match/%d", ++total); \
253 g_test_add_data_func_full (path, data, test_match, g_free); \
262 typedef struct _Match Match;
265 free_match (gpointer data)
270 g_free (match->string);
275 const gchar *pattern;
283 test_match_next (gconstpointer d)
285 const TestMatchNextData *data = d;
287 GMatchInfo *match_info;
289 GSList *l_exp, *l_match;
291 regex = g_regex_new (data->pattern, 0, 0, NULL);
293 g_assert (regex != NULL);
295 g_regex_match_full (regex, data->string, data->string_len,
296 data->start_position, 0, &match_info, NULL);
298 while (g_match_info_matches (match_info))
300 Match *match = g_new0 (Match, 1);
301 match->string = g_match_info_fetch (match_info, 0);
302 match->start = UNTOUCHED;
303 match->end = UNTOUCHED;
304 g_match_info_fetch_pos (match_info, 0, &match->start, &match->end);
305 matches = g_slist_prepend (matches, match);
306 g_match_info_next (match_info, NULL);
308 g_assert (regex == g_match_info_get_regex (match_info));
309 g_assert_cmpstr (data->string, ==, g_match_info_get_string (match_info));
310 g_match_info_free (match_info);
311 matches = g_slist_reverse (matches);
313 g_assert_cmpint (g_slist_length (matches), ==, g_slist_length (data->expected));
315 l_exp = data->expected;
317 while (l_exp != NULL)
319 Match *exp = l_exp->data;
320 Match *match = l_match->data;
322 g_assert_cmpstr (exp->string, ==, match->string);
323 g_assert_cmpint (exp->start, ==, match->start);
324 g_assert_cmpint (exp->end, ==, match->end);
326 l_exp = g_slist_next (l_exp);
327 l_match = g_slist_next (l_match);
330 g_regex_unref (regex);
331 g_slist_free_full (matches, free_match);
335 free_match_next_data (gpointer _data)
337 TestMatchNextData *data = _data;
339 g_slist_free_full (data->expected, g_free);
343 #define TEST_MATCH_NEXT0(_pattern, _string, _string_len, _start_position) { \
344 TestMatchNextData *data; \
346 data = g_new0 (TestMatchNextData, 1); \
347 data->pattern = _pattern; \
348 data->string = _string; \
349 data->string_len = _string_len; \
350 data->start_position = _start_position; \
351 data->expected = NULL; \
352 path = g_strdup_printf ("/regex/match/next0/%d", ++total); \
353 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
357 #define TEST_MATCH_NEXT1(_pattern, _string, _string_len, _start_position, \
359 TestMatchNextData *data; \
362 data = g_new0 (TestMatchNextData, 1); \
363 data->pattern = _pattern; \
364 data->string = _string; \
365 data->string_len = _string_len; \
366 data->start_position = _start_position; \
367 match = g_new0 (Match, 1); \
368 match->string = t1; \
371 data->expected = g_slist_append (NULL, match); \
372 path = g_strdup_printf ("/regex/match/next1/%d", ++total); \
373 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
377 #define TEST_MATCH_NEXT2(_pattern, _string, _string_len, _start_position, \
378 t1, s1, e1, t2, s2, e2) { \
379 TestMatchNextData *data; \
382 data = g_new0 (TestMatchNextData, 1); \
383 data->pattern = _pattern; \
384 data->string = _string; \
385 data->string_len = _string_len; \
386 data->start_position = _start_position; \
387 match = g_new0 (Match, 1); \
388 match->string = t1; \
391 data->expected = g_slist_append (NULL, match); \
392 match = g_new0 (Match, 1); \
393 match->string = t2; \
396 data->expected = g_slist_append (data->expected, match); \
397 path = g_strdup_printf ("/regex/match/next2/%d", ++total); \
398 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
402 #define TEST_MATCH_NEXT3(_pattern, _string, _string_len, _start_position, \
403 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
404 TestMatchNextData *data; \
407 data = g_new0 (TestMatchNextData, 1); \
408 data->pattern = _pattern; \
409 data->string = _string; \
410 data->string_len = _string_len; \
411 data->start_position = _start_position; \
412 match = g_new0 (Match, 1); \
413 match->string = t1; \
416 data->expected = g_slist_append (NULL, match); \
417 match = g_new0 (Match, 1); \
418 match->string = t2; \
421 data->expected = g_slist_append (data->expected, match); \
422 match = g_new0 (Match, 1); \
423 match->string = t3; \
426 data->expected = g_slist_append (data->expected, match); \
427 path = g_strdup_printf ("/regex/match/next3/%d", ++total); \
428 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
432 #define TEST_MATCH_NEXT4(_pattern, _string, _string_len, _start_position, \
433 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
434 TestMatchNextData *data; \
437 data = g_new0 (TestMatchNextData, 1); \
438 data->pattern = _pattern; \
439 data->string = _string; \
440 data->string_len = _string_len; \
441 data->start_position = _start_position; \
442 match = g_new0 (Match, 1); \
443 match->string = t1; \
446 data->expected = g_slist_append (NULL, match); \
447 match = g_new0 (Match, 1); \
448 match->string = t2; \
451 data->expected = g_slist_append (data->expected, match); \
452 match = g_new0 (Match, 1); \
453 match->string = t3; \
456 data->expected = g_slist_append (data->expected, match); \
457 match = g_new0 (Match, 1); \
458 match->string = t4; \
461 data->expected = g_slist_append (data->expected, match); \
462 path = g_strdup_printf ("/regex/match/next4/%d", ++total); \
463 g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
468 const gchar *pattern;
471 GRegexMatchFlags match_opts;
473 } TestMatchCountData;
476 test_match_count (gconstpointer d)
478 const TestMatchCountData *data = d;
480 GMatchInfo *match_info;
483 regex = g_regex_new (data->pattern, 0, 0, NULL);
485 g_assert (regex != NULL);
487 g_regex_match_full (regex, data->string, -1, data->start_position,
488 data->match_opts, &match_info, NULL);
489 count = g_match_info_get_match_count (match_info);
491 g_assert_cmpint (count, ==, data->expected_count);
493 g_match_info_ref (match_info);
494 g_match_info_unref (match_info);
495 g_match_info_unref (match_info);
496 g_regex_unref (regex);
499 #define TEST_MATCH_COUNT(_pattern, _string, _start_position, _match_opts, _expected_count) { \
500 TestMatchCountData *data; \
502 data = g_new0 (TestMatchCountData, 1); \
503 data->pattern = _pattern; \
504 data->string = _string; \
505 data->start_position = _start_position; \
506 data->match_opts = _match_opts; \
507 data->expected_count = _expected_count; \
508 path = g_strdup_printf ("/regex/match/count/%d", ++total); \
509 g_test_add_data_func_full (path, data, test_match_count, g_free); \
514 test_partial (gconstpointer d)
516 const TestMatchData *data = d;
518 GMatchInfo *match_info;
520 regex = g_regex_new (data->pattern, 0, 0, NULL);
522 g_assert (regex != NULL);
524 g_regex_match (regex, data->string, data->match_opts, &match_info);
526 g_assert_cmpint (data->expected, ==, g_match_info_is_partial_match (match_info));
530 g_assert (!g_match_info_fetch_pos (match_info, 0, NULL, NULL));
531 g_assert (!g_match_info_fetch_pos (match_info, 1, NULL, NULL));
534 g_match_info_free (match_info);
535 g_regex_unref (regex);
538 #define TEST_PARTIAL_FULL(_pattern, _string, _match_opts, _expected) { \
539 TestMatchData *data; \
541 data = g_new0 (TestMatchData, 1); \
542 data->pattern = _pattern; \
543 data->string = _string; \
544 data->match_opts = _match_opts; \
545 data->expected = _expected; \
546 path = g_strdup_printf ("/regex/match/partial/%d", ++total); \
547 g_test_add_data_func_full (path, data, test_partial, g_free); \
551 #define TEST_PARTIAL(_pattern, _string, _expected) TEST_PARTIAL_FULL(_pattern, _string, G_REGEX_MATCH_PARTIAL, _expected)
554 const gchar *pattern;
558 const gchar *expected_sub;
564 test_sub_pattern (gconstpointer d)
566 const TestSubData *data = d;
568 GMatchInfo *match_info;
570 gint start = UNTOUCHED, end = UNTOUCHED;
572 regex = g_regex_new (data->pattern, 0, 0, NULL);
574 g_assert (regex != NULL);
576 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
578 sub_expr = g_match_info_fetch (match_info, data->sub_n);
579 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
582 g_match_info_fetch_pos (match_info, data->sub_n, &start, &end);
583 g_assert_cmpint (start, ==, data->expected_start);
584 g_assert_cmpint (end, ==, data->expected_end);
586 g_match_info_free (match_info);
587 g_regex_unref (regex);
590 #define TEST_SUB_PATTERN(_pattern, _string, _start_position, _sub_n, _expected_sub, \
591 _expected_start, _expected_end) { \
594 data = g_new0 (TestSubData, 1); \
595 data->pattern = _pattern; \
596 data->string = _string; \
597 data->start_position = _start_position; \
598 data->sub_n = _sub_n; \
599 data->expected_sub = _expected_sub; \
600 data->expected_start = _expected_start; \
601 data->expected_end = _expected_end; \
602 path = g_strdup_printf ("/regex/match/subpattern/%d", ++total); \
603 g_test_add_data_func_full (path, data, test_sub_pattern, g_free); \
608 const gchar *pattern;
609 GRegexCompileFlags flags;
612 const gchar *sub_name;
613 const gchar *expected_sub;
619 test_named_sub_pattern (gconstpointer d)
621 const TestNamedSubData *data = d;
623 GMatchInfo *match_info;
624 gint start = UNTOUCHED, end = UNTOUCHED;
627 regex = g_regex_new (data->pattern, data->flags, 0, NULL);
629 g_assert (regex != NULL);
631 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
632 sub_expr = g_match_info_fetch_named (match_info, data->sub_name);
633 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
636 g_match_info_fetch_named_pos (match_info, data->sub_name, &start, &end);
637 g_assert_cmpint (start, ==, data->expected_start);
638 g_assert_cmpint (end, ==, data->expected_end);
640 g_match_info_free (match_info);
641 g_regex_unref (regex);
644 #define TEST_NAMED_SUB_PATTERN(_pattern, _string, _start_position, _sub_name, \
645 _expected_sub, _expected_start, _expected_end) { \
646 TestNamedSubData *data; \
648 data = g_new0 (TestNamedSubData, 1); \
649 data->pattern = _pattern; \
650 data->string = _string; \
652 data->start_position = _start_position; \
653 data->sub_name = _sub_name; \
654 data->expected_sub = _expected_sub; \
655 data->expected_start = _expected_start; \
656 data->expected_end = _expected_end; \
657 path = g_strdup_printf ("/regex/match/named/subpattern/%d", ++total); \
658 g_test_add_data_func_full (path, data, test_named_sub_pattern, g_free); \
662 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(_pattern, _string, _start_position, _sub_name, \
663 _expected_sub, _expected_start, _expected_end) { \
664 TestNamedSubData *data; \
666 data = g_new0 (TestNamedSubData, 1); \
667 data->pattern = _pattern; \
668 data->string = _string; \
669 data->flags = G_REGEX_DUPNAMES; \
670 data->start_position = _start_position; \
671 data->sub_name = _sub_name; \
672 data->expected_sub = _expected_sub; \
673 data->expected_start = _expected_start; \
674 data->expected_end = _expected_end; \
675 path = g_strdup_printf ("/regex/match/subpattern/named/dupnames/%d", ++total); \
676 g_test_add_data_func_full (path, data, test_named_sub_pattern, g_free); \
681 const gchar *pattern;
689 test_fetch_all (gconstpointer d)
691 const TestFetchAllData *data = d;
693 GMatchInfo *match_info;
699 regex = g_regex_new (data->pattern, 0, 0, NULL);
701 g_assert (regex != NULL);
703 g_regex_match (regex, data->string, 0, &match_info);
704 matches = g_match_info_fetch_all (match_info);
706 match_count = g_strv_length (matches);
710 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
712 l_exp = data->expected;
713 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
715 g_assert_cmpstr (l_exp->data, ==, matches[i]);
718 g_match_info_free (match_info);
719 g_regex_unref (regex);
720 g_strfreev (matches);
724 free_fetch_all_data (gpointer _data)
726 TestFetchAllData *data = _data;
728 g_slist_free (data->expected);
732 #define TEST_FETCH_ALL0(_pattern, _string) { \
733 TestFetchAllData *data; \
735 data = g_new0 (TestFetchAllData, 1); \
736 data->pattern = _pattern; \
737 data->string = _string; \
738 data->expected = NULL; \
739 path = g_strdup_printf ("/regex/fetch-all0/%d", ++total); \
740 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
744 #define TEST_FETCH_ALL1(_pattern, _string, e1) { \
745 TestFetchAllData *data; \
747 data = g_new0 (TestFetchAllData, 1); \
748 data->pattern = _pattern; \
749 data->string = _string; \
750 data->expected = g_slist_append (NULL, e1); \
751 path = g_strdup_printf ("/regex/fetch-all1/%d", ++total); \
752 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
756 #define TEST_FETCH_ALL2(_pattern, _string, e1, e2) { \
757 TestFetchAllData *data; \
759 data = g_new0 (TestFetchAllData, 1); \
760 data->pattern = _pattern; \
761 data->string = _string; \
762 data->expected = g_slist_append (NULL, e1); \
763 data->expected = g_slist_append (data->expected, e2); \
764 path = g_strdup_printf ("/regex/fetch-all2/%d", ++total); \
765 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
769 #define TEST_FETCH_ALL3(_pattern, _string, e1, e2, e3) { \
770 TestFetchAllData *data; \
772 data = g_new0 (TestFetchAllData, 1); \
773 data->pattern = _pattern; \
774 data->string = _string; \
775 data->expected = g_slist_append (NULL, e1); \
776 data->expected = g_slist_append (data->expected, e2); \
777 data->expected = g_slist_append (data->expected, e3); \
778 path = g_strdup_printf ("/regex/fetch-all3/%d", ++total); \
779 g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
784 test_split_simple (gconstpointer d)
786 const TestFetchAllData *data = d;
792 tokens = g_regex_split_simple (data->pattern, data->string, 0, 0);
794 token_count = g_strv_length (tokens);
798 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
800 l_exp = data->expected;
801 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
803 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
809 #define TEST_SPLIT_SIMPLE0(_pattern, _string) { \
810 TestFetchAllData *data; \
812 data = g_new0 (TestFetchAllData, 1); \
813 data->pattern = _pattern; \
814 data->string = _string; \
815 data->expected = NULL; \
816 path = g_strdup_printf ("/regex/split/simple0/%d", ++total); \
817 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
821 #define TEST_SPLIT_SIMPLE1(_pattern, _string, e1) { \
822 TestFetchAllData *data; \
824 data = g_new0 (TestFetchAllData, 1); \
825 data->pattern = _pattern; \
826 data->string = _string; \
827 data->expected = g_slist_append (NULL, e1); \
828 path = g_strdup_printf ("/regex/split/simple1/%d", ++total); \
829 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
833 #define TEST_SPLIT_SIMPLE2(_pattern, _string, e1, e2) { \
834 TestFetchAllData *data; \
836 data = g_new0 (TestFetchAllData, 1); \
837 data->pattern = _pattern; \
838 data->string = _string; \
839 data->expected = g_slist_append (NULL, e1); \
840 data->expected = g_slist_append (data->expected, e2); \
841 path = g_strdup_printf ("/regex/split/simple2/%d", ++total); \
842 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
846 #define TEST_SPLIT_SIMPLE3(_pattern, _string, e1, e2, e3) { \
847 TestFetchAllData *data; \
849 data = g_new0 (TestFetchAllData, 1); \
850 data->pattern = _pattern; \
851 data->string = _string; \
852 data->expected = g_slist_append (NULL, e1); \
853 data->expected = g_slist_append (data->expected, e2); \
854 data->expected = g_slist_append (data->expected, e3); \
855 path = g_strdup_printf ("/regex/split/simple3/%d", ++total); \
856 g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
861 test_split_full (gconstpointer d)
863 const TestFetchAllData *data = d;
870 regex = g_regex_new (data->pattern, 0, 0, NULL);
872 g_assert (regex != NULL);
874 tokens = g_regex_split_full (regex, data->string, -1, data->start_position,
875 0, data->max_tokens, NULL);
877 token_count = g_strv_length (tokens);
881 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
883 l_exp = data->expected;
884 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
886 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
889 g_regex_unref (regex);
894 test_split (gconstpointer d)
896 const TestFetchAllData *data = d;
903 regex = g_regex_new (data->pattern, 0, 0, NULL);
905 g_assert (regex != NULL);
907 tokens = g_regex_split (regex, data->string, 0);
909 token_count = g_strv_length (tokens);
913 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
915 l_exp = data->expected;
916 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
918 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
921 g_regex_unref (regex);
925 #define TEST_SPLIT0(_pattern, _string, _start_position, _max_tokens) { \
926 TestFetchAllData *data; \
928 data = g_new0 (TestFetchAllData, 1); \
929 data->pattern = _pattern; \
930 data->string = _string; \
931 data->start_position = _start_position; \
932 data->max_tokens = _max_tokens; \
933 data->expected = NULL; \
934 if (_start_position == 0 && _max_tokens <= 0) { \
935 path = g_strdup_printf ("/regex/split0/%d", ++total); \
936 g_test_add_data_func (path, data, test_split); \
939 path = g_strdup_printf ("/regex/full-split0/%d", ++total); \
940 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
944 #define TEST_SPLIT1(_pattern, _string, _start_position, _max_tokens, e1) { \
945 TestFetchAllData *data; \
947 data = g_new0 (TestFetchAllData, 1); \
948 data->pattern = _pattern; \
949 data->string = _string; \
950 data->start_position = _start_position; \
951 data->max_tokens = _max_tokens; \
952 data->expected = NULL; \
953 data->expected = g_slist_append (data->expected, e1); \
954 if (_start_position == 0 && _max_tokens <= 0) { \
955 path = g_strdup_printf ("/regex/split1/%d", ++total); \
956 g_test_add_data_func (path, data, test_split); \
959 path = g_strdup_printf ("/regex/full-split1/%d", ++total); \
960 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
964 #define TEST_SPLIT2(_pattern, _string, _start_position, _max_tokens, e1, e2) { \
965 TestFetchAllData *data; \
967 data = g_new0 (TestFetchAllData, 1); \
968 data->pattern = _pattern; \
969 data->string = _string; \
970 data->start_position = _start_position; \
971 data->max_tokens = _max_tokens; \
972 data->expected = NULL; \
973 data->expected = g_slist_append (data->expected, e1); \
974 data->expected = g_slist_append (data->expected, e2); \
975 if (_start_position == 0 && _max_tokens <= 0) { \
976 path = g_strdup_printf ("/regex/split2/%d", ++total); \
977 g_test_add_data_func (path, data, test_split); \
980 path = g_strdup_printf ("/regex/full-split2/%d", ++total); \
981 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
985 #define TEST_SPLIT3(_pattern, _string, _start_position, _max_tokens, e1, e2, e3) { \
986 TestFetchAllData *data; \
988 data = g_new0 (TestFetchAllData, 1); \
989 data->pattern = _pattern; \
990 data->string = _string; \
991 data->start_position = _start_position; \
992 data->max_tokens = _max_tokens; \
993 data->expected = NULL; \
994 data->expected = g_slist_append (data->expected, e1); \
995 data->expected = g_slist_append (data->expected, e2); \
996 data->expected = g_slist_append (data->expected, e3); \
997 if (_start_position == 0 && _max_tokens <= 0) { \
998 path = g_strdup_printf ("/regex/split3/%d", ++total); \
999 g_test_add_data_func (path, data, test_split); \
1002 path = g_strdup_printf ("/regex/full-split3/%d", ++total); \
1003 g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
1008 const gchar *string_to_expand;
1010 gboolean expected_refs;
1011 } TestCheckReplacementData;
1014 test_check_replacement (gconstpointer d)
1016 const TestCheckReplacementData *data = d;
1020 result = g_regex_check_replacement (data->string_to_expand, &has_refs, NULL);
1021 g_assert_cmpint (data->expected, ==, result);
1024 g_assert_cmpint (data->expected_refs, ==, has_refs);
1027 #define TEST_CHECK_REPLACEMENT(_string_to_expand, _expected, _expected_refs) { \
1028 TestCheckReplacementData *data; \
1030 data = g_new0 (TestCheckReplacementData, 1); \
1031 data->string_to_expand = _string_to_expand; \
1032 data->expected = _expected; \
1033 data->expected_refs = _expected_refs; \
1034 path = g_strdup_printf ("/regex/check-repacement/%d", ++total); \
1035 g_test_add_data_func_full (path, data, test_check_replacement, g_free); \
1040 const gchar *pattern;
1041 const gchar *string;
1042 const gchar *string_to_expand;
1044 const gchar *expected;
1048 test_expand (gconstpointer d)
1050 const TestExpandData *data = d;
1051 GRegex *regex = NULL;
1052 GMatchInfo *match_info = NULL;
1054 GError *error = NULL;
1058 regex = g_regex_new (data->pattern, data->raw ? G_REGEX_RAW : 0, 0,
1060 g_assert_no_error (error);
1061 g_regex_match (regex, data->string, 0, &match_info);
1064 res = g_match_info_expand_references (match_info, data->string_to_expand, NULL);
1065 g_assert_cmpstr (res, ==, data->expected);
1067 g_match_info_free (match_info);
1069 g_regex_unref (regex);
1072 #define TEST_EXPAND(_pattern, _string, _string_to_expand, _raw, _expected) { \
1073 TestExpandData *data; \
1075 data = g_new0 (TestExpandData, 1); \
1076 data->pattern = _pattern; \
1077 data->string = _string; \
1078 data->string_to_expand = _string_to_expand; \
1080 data->expected = _expected; \
1081 path = g_strdup_printf ("/regex/expand/%d", ++total); \
1082 g_test_add_data_func_full (path, data, test_expand, g_free); \
1087 const gchar *pattern;
1088 const gchar *string;
1089 gint start_position;
1090 const gchar *replacement;
1091 const gchar *expected;
1095 test_replace (gconstpointer d)
1097 const TestReplaceData *data = d;
1101 regex = g_regex_new (data->pattern, 0, 0, NULL);
1102 res = g_regex_replace (regex, data->string, -1, data->start_position, data->replacement, 0, NULL);
1104 g_assert_cmpstr (res, ==, data->expected);
1107 g_regex_unref (regex);
1110 #define TEST_REPLACE(_pattern, _string, _start_position, _replacement, _expected) { \
1111 TestReplaceData *data; \
1113 data = g_new0 (TestReplaceData, 1); \
1114 data->pattern = _pattern; \
1115 data->string = _string; \
1116 data->start_position = _start_position; \
1117 data->replacement = _replacement; \
1118 data->expected = _expected; \
1119 path = g_strdup_printf ("/regex/replace/%d", ++total); \
1120 g_test_add_data_func_full (path, data, test_replace, g_free); \
1125 test_replace_lit (gconstpointer d)
1127 const TestReplaceData *data = d;
1131 regex = g_regex_new (data->pattern, 0, 0, NULL);
1132 res = g_regex_replace_literal (regex, data->string, -1, data->start_position,
1133 data->replacement, 0, NULL);
1134 g_assert_cmpstr (res, ==, data->expected);
1137 g_regex_unref (regex);
1140 #define TEST_REPLACE_LIT(_pattern, _string, _start_position, _replacement, _expected) { \
1141 TestReplaceData *data; \
1143 data = g_new0 (TestReplaceData, 1); \
1144 data->pattern = _pattern; \
1145 data->string = _string; \
1146 data->start_position = _start_position; \
1147 data->replacement = _replacement; \
1148 data->expected = _expected; \
1149 path = g_strdup_printf ("/regex/replace-literally/%d", ++total); \
1150 g_test_add_data_func_full (path, data, test_replace_lit, g_free); \
1155 const gchar *pattern;
1158 } TestStringNumData;
1161 test_get_string_number (gconstpointer d)
1163 const TestStringNumData *data = d;
1167 regex = g_regex_new (data->pattern, 0, 0, NULL);
1168 num = g_regex_get_string_number (regex, data->name);
1170 g_assert_cmpint (num, ==, data->expected_num);
1171 g_regex_unref (regex);
1174 #define TEST_GET_STRING_NUMBER(_pattern, _name, _expected_num) { \
1175 TestStringNumData *data; \
1177 data = g_new0 (TestStringNumData, 1); \
1178 data->pattern = _pattern; \
1179 data->name = _name; \
1180 data->expected_num = _expected_num; \
1181 path = g_strdup_printf ("/regex/string-number/%d", ++total); \
1182 g_test_add_data_func_full (path, data, test_get_string_number, g_free); \
1187 const gchar *string;
1189 const gchar *expected;
1193 test_escape (gconstpointer d)
1195 const TestEscapeData *data = d;
1198 escaped = g_regex_escape_string (data->string, data->length);
1200 g_assert_cmpstr (escaped, ==, data->expected);
1205 #define TEST_ESCAPE(_string, _length, _expected) { \
1206 TestEscapeData *data; \
1208 data = g_new0 (TestEscapeData, 1); \
1209 data->string = _string; \
1210 data->length = _length; \
1211 data->expected = _expected; \
1212 path = g_strdup_printf ("/regex/escape/%d", ++total); \
1213 g_test_add_data_func_full (path, data, test_escape, g_free); \
1218 test_escape_nul (gconstpointer d)
1220 const TestEscapeData *data = d;
1223 escaped = g_regex_escape_nul (data->string, data->length);
1225 g_assert_cmpstr (escaped, ==, data->expected);
1230 #define TEST_ESCAPE_NUL(_string, _length, _expected) { \
1231 TestEscapeData *data; \
1233 data = g_new0 (TestEscapeData, 1); \
1234 data->string = _string; \
1235 data->length = _length; \
1236 data->expected = _expected; \
1237 path = g_strdup_printf ("/regex/escape_nul/%d", ++total); \
1238 g_test_add_data_func_full (path, data, test_escape_nul, g_free); \
1243 const gchar *pattern;
1244 const gchar *string;
1246 gint start_position;
1251 test_match_all_full (gconstpointer d)
1253 const TestMatchAllData *data = d;
1255 GMatchInfo *match_info;
1261 regex = g_regex_new (data->pattern, 0, 0, NULL);
1262 match_ok = g_regex_match_all_full (regex, data->string, data->string_len, data->start_position,
1263 0, &match_info, NULL);
1265 if (g_slist_length (data->expected) == 0)
1266 g_assert (!match_ok);
1268 g_assert (match_ok);
1270 match_count = g_match_info_get_match_count (match_info);
1271 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1273 l_exp = data->expected;
1274 for (i = 0; i < match_count; i++)
1277 gchar *matched_string;
1278 Match *exp = l_exp->data;
1280 matched_string = g_match_info_fetch (match_info, i);
1281 g_match_info_fetch_pos (match_info, i, &start, &end);
1283 g_assert_cmpstr (exp->string, ==, matched_string);
1284 g_assert_cmpint (exp->start, ==, start);
1285 g_assert_cmpint (exp->end, ==, end);
1287 g_free (matched_string);
1289 l_exp = g_slist_next (l_exp);
1292 g_match_info_free (match_info);
1293 g_regex_unref (regex);
1297 test_match_all (gconstpointer d)
1299 const TestMatchAllData *data = d;
1301 GMatchInfo *match_info;
1307 regex = g_regex_new (data->pattern, 0, 0, NULL);
1308 match_ok = g_regex_match_all (regex, data->string, 0, &match_info);
1310 if (g_slist_length (data->expected) == 0)
1311 g_assert (!match_ok);
1313 g_assert (match_ok);
1315 match_count = g_match_info_get_match_count (match_info);
1317 if (match_count != g_slist_length (data->expected))
1319 g_message ("regex: %s", data->pattern);
1320 g_message ("string: %s", data->string);
1321 g_message ("matched strings:");
1323 for (i = 0; i < match_count; i++)
1326 gchar *matched_string;
1328 matched_string = g_match_info_fetch (match_info, i);
1329 g_match_info_fetch_pos (match_info, i, &start, &end);
1330 g_message ("%d. %d-%d '%s'", i, start, end, matched_string);
1331 g_free (matched_string);
1334 g_message ("expected strings:");
1337 for (l_exp = data->expected; l_exp != NULL; l_exp = l_exp->next)
1339 Match *exp = l_exp->data;
1341 g_message ("%d. %d-%d '%s'", i, exp->start, exp->end, exp->string);
1345 g_error ("match_count not as expected: %d != %d",
1346 match_count, g_slist_length (data->expected));
1349 l_exp = data->expected;
1350 for (i = 0; i < match_count; i++)
1353 gchar *matched_string;
1354 Match *exp = l_exp->data;
1356 matched_string = g_match_info_fetch (match_info, i);
1357 g_match_info_fetch_pos (match_info, i, &start, &end);
1359 g_assert_cmpstr (exp->string, ==, matched_string);
1360 g_assert_cmpint (exp->start, ==, start);
1361 g_assert_cmpint (exp->end, ==, end);
1363 g_free (matched_string);
1365 l_exp = g_slist_next (l_exp);
1368 g_match_info_free (match_info);
1369 g_regex_unref (regex);
1373 free_match_all_data (gpointer _data)
1375 TestMatchAllData *data = _data;
1377 g_slist_free_full (data->expected, g_free);
1381 #define TEST_MATCH_ALL0(_pattern, _string, _string_len, _start_position) { \
1382 TestMatchAllData *data; \
1384 data = g_new0 (TestMatchAllData, 1); \
1385 data->pattern = _pattern; \
1386 data->string = _string; \
1387 data->string_len = _string_len; \
1388 data->start_position = _start_position; \
1389 data->expected = NULL; \
1390 if (_string_len == -1 && _start_position == 0) { \
1391 path = g_strdup_printf ("/regex/match-all0/%d", ++total); \
1392 g_test_add_data_func (path, data, test_match_all); \
1395 path = g_strdup_printf ("/regex/match-all-full0/%d", ++total); \
1396 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1400 #define TEST_MATCH_ALL1(_pattern, _string, _string_len, _start_position, \
1402 TestMatchAllData *data; \
1405 data = g_new0 (TestMatchAllData, 1); \
1406 data->pattern = _pattern; \
1407 data->string = _string; \
1408 data->string_len = _string_len; \
1409 data->start_position = _start_position; \
1410 data->expected = NULL; \
1411 match = g_new0 (Match, 1); \
1412 match->string = t1; \
1413 match->start = s1; \
1415 data->expected = g_slist_append (data->expected, match); \
1416 if (_string_len == -1 && _start_position == 0) { \
1417 path = g_strdup_printf ("/regex/match-all1/%d", ++total); \
1418 g_test_add_data_func (path, data, test_match_all); \
1421 path = g_strdup_printf ("/regex/match-all-full1/%d", ++total); \
1422 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1426 #define TEST_MATCH_ALL2(_pattern, _string, _string_len, _start_position, \
1427 t1, s1, e1, t2, s2, e2) { \
1428 TestMatchAllData *data; \
1431 data = g_new0 (TestMatchAllData, 1); \
1432 data->pattern = _pattern; \
1433 data->string = _string; \
1434 data->string_len = _string_len; \
1435 data->start_position = _start_position; \
1436 data->expected = NULL; \
1437 match = g_new0 (Match, 1); \
1438 match->string = t1; \
1439 match->start = s1; \
1441 data->expected = g_slist_append (data->expected, match); \
1442 match = g_new0 (Match, 1); \
1443 match->string = t2; \
1444 match->start = s2; \
1446 data->expected = g_slist_append (data->expected, match); \
1447 if (_string_len == -1 && _start_position == 0) { \
1448 path = g_strdup_printf ("/regex/match-all2/%d", ++total); \
1449 g_test_add_data_func (path, data, test_match_all); \
1452 path = g_strdup_printf ("/regex/match-all-full2/%d", ++total); \
1453 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1457 #define TEST_MATCH_ALL3(_pattern, _string, _string_len, _start_position, \
1458 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
1459 TestMatchAllData *data; \
1462 data = g_new0 (TestMatchAllData, 1); \
1463 data->pattern = _pattern; \
1464 data->string = _string; \
1465 data->string_len = _string_len; \
1466 data->start_position = _start_position; \
1467 data->expected = NULL; \
1468 match = g_new0 (Match, 1); \
1469 match->string = t1; \
1470 match->start = s1; \
1472 data->expected = g_slist_append (data->expected, match); \
1473 match = g_new0 (Match, 1); \
1474 match->string = t2; \
1475 match->start = s2; \
1477 data->expected = g_slist_append (data->expected, match); \
1478 match = g_new0 (Match, 1); \
1479 match->string = t3; \
1480 match->start = s3; \
1482 data->expected = g_slist_append (data->expected, match); \
1483 if (_string_len == -1 && _start_position == 0) { \
1484 path = g_strdup_printf ("/regex/match-all3/%d", ++total); \
1485 g_test_add_data_func (path, data, test_match_all); \
1488 path = g_strdup_printf ("/regex/match-all-full3/%d", ++total); \
1489 g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1494 test_properties (void)
1503 regex = g_regex_new ("\\p{L}\\p{Ll}\\p{Lu}\\p{L&}\\p{N}\\p{Nd}", G_REGEX_OPTIMIZE, 0, &error);
1504 res = g_regex_match (regex, "ppPP01", 0, &match);
1506 str = g_match_info_fetch (match, 0);
1507 g_assert_cmpstr (str, ==, "ppPP01");
1510 g_match_info_free (match);
1511 g_regex_unref (regex);
1524 regex = g_regex_new ("[abc\\x{0B1E}\\p{Mn}\\x{0391}-\\x{03A9}]", G_REGEX_OPTIMIZE, 0, &error);
1525 res = g_regex_match (regex, "a:b:\340\254\236:\333\253:\316\240", 0, &match);
1527 str = g_match_info_fetch (match, 0);
1528 g_assert_cmpstr (str, ==, "a");
1530 res = g_match_info_next (match, NULL);
1532 str = g_match_info_fetch (match, 0);
1533 g_assert_cmpstr (str, ==, "b");
1535 res = g_match_info_next (match, NULL);
1537 str = g_match_info_fetch (match, 0);
1538 g_assert_cmpstr (str, ==, "\340\254\236");
1540 res = g_match_info_next (match, NULL);
1542 str = g_match_info_fetch (match, 0);
1543 g_assert_cmpstr (str, ==, "\333\253");
1545 res = g_match_info_next (match, NULL);
1547 str = g_match_info_fetch (match, 0);
1548 g_assert_cmpstr (str, ==, "\316\240");
1551 res = g_match_info_next (match, NULL);
1554 g_match_info_free (match);
1555 g_regex_unref (regex);
1558 /* examples for lookahead assertions taken from pcrepattern(3) */
1560 test_lookahead (void)
1570 regex = g_regex_new ("\\w+(?=;)", G_REGEX_OPTIMIZE, 0, &error);
1572 g_assert_no_error (error);
1573 res = g_regex_match (regex, "word1 word2: word3;", 0, &match);
1575 g_assert (g_match_info_matches (match));
1576 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1577 str = g_match_info_fetch (match, 0);
1578 g_assert_cmpstr (str, ==, "word3");
1580 g_match_info_free (match);
1581 g_regex_unref (regex);
1584 regex = g_regex_new ("foo(?!bar)", G_REGEX_OPTIMIZE, 0, &error);
1586 g_assert_no_error (error);
1587 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1589 g_assert (g_match_info_matches (match));
1590 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1591 res = g_match_info_fetch_pos (match, 0, &start, &end);
1593 g_assert_cmpint (start, ==, 7);
1594 g_assert_cmpint (end, ==, 10);
1595 g_match_info_free (match);
1596 g_regex_unref (regex);
1599 regex = g_regex_new ("(?!bar)foo", G_REGEX_OPTIMIZE, 0, &error);
1601 g_assert_no_error (error);
1602 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1604 g_assert (g_match_info_matches (match));
1605 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1606 res = g_match_info_fetch_pos (match, 0, &start, &end);
1608 g_assert_cmpint (start, ==, 0);
1609 g_assert_cmpint (end, ==, 3);
1610 res = g_match_info_next (match, &error);
1612 g_assert_no_error (error);
1613 res = g_match_info_fetch_pos (match, 0, &start, &end);
1615 g_assert_cmpint (start, ==, 7);
1616 g_assert_cmpint (end, ==, 10);
1617 g_match_info_free (match);
1618 g_regex_unref (regex);
1621 /* examples for lookbehind assertions taken from pcrepattern(3) */
1623 test_lookbehind (void)
1632 regex = g_regex_new ("(?<!foo)bar", G_REGEX_OPTIMIZE, 0, &error);
1634 g_assert_no_error (error);
1635 res = g_regex_match (regex, "foobar boobar", 0, &match);
1637 g_assert (g_match_info_matches (match));
1638 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1639 res = g_match_info_fetch_pos (match, 0, &start, &end);
1641 g_assert_cmpint (start, ==, 10);
1642 g_assert_cmpint (end, ==, 13);
1643 g_match_info_free (match);
1644 g_regex_unref (regex);
1647 regex = g_regex_new ("(?<=bullock|donkey) poo", G_REGEX_OPTIMIZE, 0, &error);
1649 g_assert_no_error (error);
1650 res = g_regex_match (regex, "don poo, and bullock poo", 0, &match);
1652 g_assert (g_match_info_matches (match));
1653 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1654 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1656 g_assert_cmpint (start, ==, 20);
1657 g_match_info_free (match);
1658 g_regex_unref (regex);
1660 regex = g_regex_new ("(?<!dogs?|cats?) x", G_REGEX_OPTIMIZE, 0, &error);
1661 g_assert (regex == NULL);
1662 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1663 g_clear_error (&error);
1665 regex = g_regex_new ("(?<=ab(c|de)) foo", G_REGEX_OPTIMIZE, 0, &error);
1666 g_assert (regex == NULL);
1667 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1668 g_clear_error (&error);
1670 regex = g_regex_new ("(?<=abc|abde)foo", G_REGEX_OPTIMIZE, 0, &error);
1672 g_assert_no_error (error);
1673 res = g_regex_match (regex, "abfoo, abdfoo, abcfoo", 0, &match);
1675 g_assert (g_match_info_matches (match));
1676 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1678 g_assert_cmpint (start, ==, 18);
1679 g_match_info_free (match);
1680 g_regex_unref (regex);
1682 regex = g_regex_new ("^.*+(?<=abcd)", G_REGEX_OPTIMIZE, 0, &error);
1684 g_assert_no_error (error);
1685 res = g_regex_match (regex, "abcabcabcabcabcabcabcabcabcd", 0, &match);
1687 g_assert (g_match_info_matches (match));
1688 g_match_info_free (match);
1689 g_regex_unref (regex);
1691 regex = g_regex_new ("(?<=\\d{3})(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1693 g_assert_no_error (error);
1694 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1696 g_assert (g_match_info_matches (match));
1697 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1699 g_assert_cmpint (start, ==, 20);
1700 g_match_info_free (match);
1701 g_regex_unref (regex);
1703 regex = g_regex_new ("(?<=\\d{3}...)(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1705 g_assert_no_error (error);
1706 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1708 g_assert (g_match_info_matches (match));
1709 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1711 g_assert_cmpint (start, ==, 13);
1712 g_match_info_free (match);
1713 g_regex_unref (regex);
1715 regex = g_regex_new ("(?<=\\d{3}(?!999)...)foo", G_REGEX_OPTIMIZE, 0, &error);
1717 g_assert_no_error (error);
1718 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1720 g_assert (g_match_info_matches (match));
1721 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1723 g_assert_cmpint (start, ==, 13);
1724 g_match_info_free (match);
1725 g_regex_unref (regex);
1727 regex = g_regex_new ("(?<=(?<!foo)bar)baz", G_REGEX_OPTIMIZE, 0, &error);
1729 g_assert_no_error (error);
1730 res = g_regex_match (regex, "foobarbaz barfoobaz barbarbaz", 0, &match);
1732 g_assert (g_match_info_matches (match));
1733 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1735 g_assert_cmpint (start, ==, 26);
1736 g_match_info_free (match);
1737 g_regex_unref (regex);
1740 /* examples for subpatterns taken from pcrepattern(3) */
1742 test_subpattern (void)
1752 regex = g_regex_new ("cat(aract|erpillar|)", G_REGEX_OPTIMIZE, 0, &error);
1754 g_assert_no_error (error);
1755 g_assert_cmpint (g_regex_get_capture_count (regex), ==, 1);
1756 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1757 res = g_regex_match_all (regex, "caterpillar", 0, &match);
1759 g_assert (g_match_info_matches (match));
1760 g_assert_cmpint (g_match_info_get_match_count (match), ==, 2);
1761 str = g_match_info_fetch (match, 0);
1762 g_assert_cmpstr (str, ==, "caterpillar");
1764 str = g_match_info_fetch (match, 1);
1765 g_assert_cmpstr (str, ==, "cat");
1767 g_match_info_free (match);
1768 g_regex_unref (regex);
1770 regex = g_regex_new ("the ((red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1772 g_assert_no_error (error);
1773 g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3);
1774 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1775 res = g_regex_match (regex, "the red king", 0, &match);
1777 g_assert (g_match_info_matches (match));
1778 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1779 str = g_match_info_fetch (match, 0);
1780 g_assert_cmpstr (str, ==, "the red king");
1782 str = g_match_info_fetch (match, 1);
1783 g_assert_cmpstr (str, ==, "red king");
1785 str = g_match_info_fetch (match, 2);
1786 g_assert_cmpstr (str, ==, "red");
1788 str = g_match_info_fetch (match, 3);
1789 g_assert_cmpstr (str, ==, "king");
1791 g_match_info_free (match);
1792 g_regex_unref (regex);
1794 regex = g_regex_new ("the ((?:red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1796 g_assert_no_error (error);
1797 res = g_regex_match (regex, "the white queen", 0, &match);
1799 g_assert (g_match_info_matches (match));
1800 g_assert_cmpint (g_match_info_get_match_count (match), ==, 3);
1801 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1802 str = g_match_info_fetch (match, 0);
1803 g_assert_cmpstr (str, ==, "the white queen");
1805 str = g_match_info_fetch (match, 1);
1806 g_assert_cmpstr (str, ==, "white queen");
1808 str = g_match_info_fetch (match, 2);
1809 g_assert_cmpstr (str, ==, "queen");
1811 g_match_info_free (match);
1812 g_regex_unref (regex);
1814 regex = g_regex_new ("(?|(Sat)(ur)|(Sun))day (morning|afternoon)", G_REGEX_OPTIMIZE, 0, &error);
1816 g_assert_no_error (error);
1817 g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3);
1818 res = g_regex_match (regex, "Saturday morning", 0, &match);
1820 g_assert (g_match_info_matches (match));
1821 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1822 str = g_match_info_fetch (match, 1);
1823 g_assert_cmpstr (str, ==, "Sat");
1825 str = g_match_info_fetch (match, 2);
1826 g_assert_cmpstr (str, ==, "ur");
1828 str = g_match_info_fetch (match, 3);
1829 g_assert_cmpstr (str, ==, "morning");
1831 g_match_info_free (match);
1832 g_regex_unref (regex);
1834 regex = g_regex_new ("(?|(abc)|(def))\\1", G_REGEX_OPTIMIZE, 0, &error);
1836 g_assert_no_error (error);
1837 g_assert_cmpint (g_regex_get_max_backref (regex), ==, 1);
1838 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1840 g_assert (g_match_info_matches (match));
1841 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1843 g_assert_cmpint (start, ==, 0);
1844 res = g_match_info_next (match, &error);
1846 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1848 g_assert_cmpint (start, ==, 21);
1849 g_match_info_free (match);
1850 g_regex_unref (regex);
1852 regex = g_regex_new ("(?|(abc)|(def))(?1)", G_REGEX_OPTIMIZE, 0, &error);
1854 g_assert_no_error (error);
1855 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1857 g_assert (g_match_info_matches (match));
1858 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1860 g_assert_cmpint (start, ==, 0);
1861 res = g_match_info_next (match, &error);
1863 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1865 g_assert_cmpint (start, ==, 14);
1866 g_match_info_free (match);
1867 g_regex_unref (regex);
1869 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);
1871 g_assert_no_error (error);
1872 res = g_regex_match (regex, "Mon Tuesday Wed Saturday", 0, &match);
1874 g_assert (g_match_info_matches (match));
1875 str = g_match_info_fetch_named (match, "DN");
1876 g_assert_cmpstr (str, ==, "Mon");
1878 res = g_match_info_next (match, &error);
1880 str = g_match_info_fetch_named (match, "DN");
1881 g_assert_cmpstr (str, ==, "Tue");
1883 res = g_match_info_next (match, &error);
1885 str = g_match_info_fetch_named (match, "DN");
1886 g_assert_cmpstr (str, ==, "Wed");
1888 res = g_match_info_next (match, &error);
1890 str = g_match_info_fetch_named (match, "DN");
1891 g_assert_cmpstr (str, ==, "Sat");
1893 g_match_info_free (match);
1894 g_regex_unref (regex);
1896 regex = g_regex_new ("^(a|b\\1)+$", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1898 g_assert_no_error (error);
1899 res = g_regex_match (regex, "aaaaaaaaaaaaaaaa", 0, &match);
1901 g_assert (g_match_info_matches (match));
1902 g_match_info_free (match);
1903 res = g_regex_match (regex, "ababbaa", 0, &match);
1905 g_assert (g_match_info_matches (match));
1906 g_match_info_free (match);
1907 g_regex_unref (regex);
1910 /* examples for conditions taken from pcrepattern(3) */
1912 test_condition (void)
1920 regex = g_regex_new ("^(a+)(\\()?[^()]+(?(-1)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1922 g_assert_no_error (error);
1923 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1925 g_assert (g_match_info_matches (match));
1926 g_match_info_free (match);
1927 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1929 g_assert (g_match_info_matches (match));
1930 g_match_info_free (match);
1931 g_regex_unref (regex);
1934 regex = g_regex_new ("^(a+)(?<OPEN>\\()?[^()]+(?(<OPEN>)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1936 g_assert_no_error (error);
1937 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1939 g_assert (g_match_info_matches (match));
1940 g_match_info_free (match);
1941 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1943 g_assert (g_match_info_matches (match));
1944 g_match_info_free (match);
1945 g_regex_unref (regex);
1947 regex = g_regex_new ("^(a+)(?(+1)\\[|\\<)?[^()]+(\\])?(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1949 g_assert_no_error (error);
1950 res = g_regex_match (regex, "a[zzzzzz]b", 0, &match);
1952 g_assert (g_match_info_matches (match));
1953 g_match_info_free (match);
1954 res = g_regex_match (regex, "aaa<zzzzzzbbb", 0, &match);
1956 g_assert (g_match_info_matches (match));
1957 g_match_info_free (match);
1958 g_regex_unref (regex);
1960 regex = g_regex_new ("(?(DEFINE) (?<byte> 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d) )"
1961 "\\b (?&byte) (\\.(?&byte)){3} \\b",
1962 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1964 g_assert_no_error (error);
1965 res = g_regex_match (regex, "128.0.0.1", 0, &match);
1967 g_assert (g_match_info_matches (match));
1968 g_match_info_free (match);
1969 res = g_regex_match (regex, "192.168.1.1", 0, &match);
1971 g_assert (g_match_info_matches (match));
1972 g_match_info_free (match);
1973 res = g_regex_match (regex, "209.132.180.167", 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 ("^(?(?=[^a-z]*[a-z])"
1980 "\\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )$",
1981 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1983 g_assert_no_error (error);
1984 res = g_regex_match (regex, "01-abc-24", 0, &match);
1986 g_assert (g_match_info_matches (match));
1987 g_match_info_free (match);
1988 res = g_regex_match (regex, "01-23-45", 0, &match);
1990 g_assert (g_match_info_matches (match));
1991 g_match_info_free (match);
1992 res = g_regex_match (regex, "01-uv-45", 0, &match);
1994 g_assert (!g_match_info_matches (match));
1995 g_match_info_free (match);
1996 res = g_regex_match (regex, "01-234-45", 0, &match);
1998 g_assert (!g_match_info_matches (match));
1999 g_match_info_free (match);
2000 g_regex_unref (regex);
2003 /* examples for recursion taken from pcrepattern(3) */
2005 test_recursion (void)
2014 regex = g_regex_new ("\\( ( [^()]++ | (?R) )* \\)", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2016 g_assert_no_error (error);
2017 res = g_regex_match (regex, "(middle)", 0, &match);
2019 g_assert (g_match_info_matches (match));
2020 g_match_info_free (match);
2021 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
2023 g_assert (g_match_info_matches (match));
2024 g_match_info_free (match);
2025 res = g_regex_match (regex, "(((xxx(((", 0, &match);
2027 g_assert (!g_match_info_matches (match));
2028 g_match_info_free (match);
2029 g_regex_unref (regex);
2031 regex = g_regex_new ("^( \\( ( [^()]++ | (?1) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2033 g_assert_no_error (error);
2034 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
2036 g_assert (g_match_info_matches (match));
2037 g_match_info_free (match);
2038 res = g_regex_match (regex, "(((xxx((()", 0, &match);
2040 g_assert (!g_match_info_matches (match));
2041 g_match_info_free (match);
2042 g_regex_unref (regex);
2044 regex = g_regex_new ("^(?<pn> \\( ( [^()]++ | (?&pn) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2046 g_assert_no_error (error);
2047 g_regex_match (regex, "(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()", 0, &match);
2049 g_assert (!g_match_info_matches (match));
2050 g_match_info_free (match);
2051 g_regex_unref (regex);
2053 regex = g_regex_new ("< (?: (?(R) \\d++ | [^<>]*+) | (?R)) * >", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2055 g_assert_no_error (error);
2056 res = g_regex_match (regex, "<ab<01<23<4>>>>", 0, &match);
2058 g_assert (g_match_info_matches (match));
2059 res = g_match_info_fetch_pos (match, 0, &start, NULL);
2061 g_assert_cmpint (start, ==, 0);
2062 g_match_info_free (match);
2063 res = g_regex_match (regex, "<ab<01<xx<x>>>>", 0, &match);
2065 g_assert (g_match_info_matches (match));
2066 res = g_match_info_fetch_pos (match, 0, &start, NULL);
2068 g_assert_cmpint (start, >, 0);
2069 g_match_info_free (match);
2070 g_regex_unref (regex);
2072 regex = g_regex_new ("^((.)(?1)\\2|.)$", G_REGEX_OPTIMIZE, 0, &error);
2074 g_assert_no_error (error);
2075 res = g_regex_match (regex, "abcdcba", 0, &match);
2077 g_assert (g_match_info_matches (match));
2078 g_match_info_free (match);
2079 res = g_regex_match (regex, "abcddcba", 0, &match);
2081 g_assert (!g_match_info_matches (match));
2082 g_match_info_free (match);
2083 g_regex_unref (regex);
2085 regex = g_regex_new ("^(?:((.)(?1)\\2|)|((.)(?3)\\4|.))$", G_REGEX_OPTIMIZE, 0, &error);
2087 g_assert_no_error (error);
2088 res = g_regex_match (regex, "abcdcba", 0, &match);
2090 g_assert (g_match_info_matches (match));
2091 g_match_info_free (match);
2092 res = g_regex_match (regex, "abcddcba", 0, &match);
2094 g_assert (g_match_info_matches (match));
2095 g_match_info_free (match);
2096 g_regex_unref (regex);
2098 regex = g_regex_new ("^\\W*+(?:((.)\\W*+(?1)\\W*+\\2|)|((.)\\W*+(?3)\\W*+\\4|\\W*+.\\W*+))\\W*+$", G_REGEX_OPTIMIZE|G_REGEX_CASELESS, 0, &error);
2100 g_assert_no_error (error);
2101 res = g_regex_match (regex, "abcdcba", 0, &match);
2103 g_assert (g_match_info_matches (match));
2104 g_match_info_free (match);
2105 res = g_regex_match (regex, "A man, a plan, a canal: Panama!", 0, &match);
2107 g_assert (g_match_info_matches (match));
2108 g_match_info_free (match);
2109 res = g_regex_match (regex, "Oozy rat in a sanitary zoo", 0, &match);
2111 g_assert (g_match_info_matches (match));
2112 g_match_info_free (match);
2113 g_regex_unref (regex);
2117 test_multiline (void)
2123 g_test_bug ("640489");
2125 regex = g_regex_new ("^a$", G_REGEX_MULTILINE|G_REGEX_DOTALL, 0, NULL);
2128 g_regex_match (regex, "a\nb\na", 0, &info);
2129 while (g_match_info_matches (info))
2132 g_match_info_next (info, NULL);
2134 g_match_info_free (info);
2135 g_regex_unref (regex);
2137 g_assert_cmpint (count, ==, 2);
2141 test_explicit_crlf (void)
2145 regex = g_regex_new ("[\r\n]a", 0, 0, NULL);
2146 g_assert_cmpint (g_regex_get_has_cr_or_lf (regex), ==, TRUE);
2147 g_regex_unref (regex);
2151 test_max_lookbehind (void)
2155 regex = g_regex_new ("abc", 0, 0, NULL);
2156 g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 0);
2157 g_regex_unref (regex);
2159 regex = g_regex_new ("\\babc", 0, 0, NULL);
2160 g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 1);
2161 g_regex_unref (regex);
2163 regex = g_regex_new ("(?<=123)abc", 0, 0, NULL);
2164 g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 3);
2165 g_regex_unref (regex);
2169 main (int argc, char *argv[])
2171 setlocale (LC_ALL, "");
2173 g_test_init (&argc, &argv, NULL);
2175 g_test_bug_base ("http://bugzilla.gnome.org/");
2177 g_test_add_func ("/regex/properties", test_properties);
2178 g_test_add_func ("/regex/class", test_class);
2179 g_test_add_func ("/regex/lookahead", test_lookahead);
2180 g_test_add_func ("/regex/lookbehind", test_lookbehind);
2181 g_test_add_func ("/regex/subpattern", test_subpattern);
2182 g_test_add_func ("/regex/condition", test_condition);
2183 g_test_add_func ("/regex/recursion", test_recursion);
2184 g_test_add_func ("/regex/multiline", test_multiline);
2185 g_test_add_func ("/regex/explicit-crlf", test_explicit_crlf);
2186 g_test_add_func ("/regex/max-lookbehind", test_max_lookbehind);
2188 /* TEST_NEW(pattern, compile_opts, match_opts) */
2189 TEST_NEW("[A-Z]+", G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL);
2191 TEST_NEW(".*", 0, 0);
2192 TEST_NEW(".*", G_REGEX_OPTIMIZE, 0);
2193 TEST_NEW(".*", G_REGEX_MULTILINE, 0);
2194 TEST_NEW(".*", G_REGEX_DOTALL, 0);
2195 TEST_NEW(".*", G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL);
2196 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
2197 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS, 0);
2198 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0);
2199 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES, 0);
2200 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0);
2201 /* This gives "internal error: code overflow" with pcre 6.0 */
2202 TEST_NEW("(?i)(?-i)", 0, 0);
2204 /* Check that flags are correct if the pattern modifies them */
2205 /* TEST_NEW_CHECK_FLAGS(pattern, compile_opts, match_ops, real_compile_opts, real_match_opts) */
2206 TEST_NEW_CHECK_FLAGS ("a", G_REGEX_OPTIMIZE, 0, G_REGEX_OPTIMIZE, 0);
2207 TEST_NEW_CHECK_FLAGS ("a", G_REGEX_RAW, 0, G_REGEX_RAW, 0);
2208 TEST_NEW_CHECK_FLAGS ("(?i)a", 0, 0, G_REGEX_CASELESS, 0);
2209 TEST_NEW_CHECK_FLAGS ("(?m)a", 0, 0, G_REGEX_MULTILINE, 0);
2210 TEST_NEW_CHECK_FLAGS ("(?s)a", 0, 0, G_REGEX_DOTALL, 0);
2211 TEST_NEW_CHECK_FLAGS ("(?x)a", 0, 0, G_REGEX_EXTENDED, 0);
2212 TEST_NEW_CHECK_FLAGS ("(?J)a", 0, 0, G_REGEX_DUPNAMES, 0);
2213 TEST_NEW_CHECK_FLAGS ("(?U)[a-z]+", 0, 0, G_REGEX_UNGREEDY, 0);
2214 TEST_NEW_CHECK_FLAGS ("(?X)a", 0, 0, 0 /* not exposed by GRegex */, 0);
2215 TEST_NEW_CHECK_FLAGS ("^.*", 0, 0, G_REGEX_ANCHORED, 0);
2216 TEST_NEW_CHECK_FLAGS ("(*UTF8)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2217 TEST_NEW_CHECK_FLAGS ("(*UCP)a", 0, 0, 0 /* this always on in GRegex */, 0);
2218 TEST_NEW_CHECK_FLAGS ("(*CR)a", 0, 0, G_REGEX_NEWLINE_CR, 0);
2219 TEST_NEW_CHECK_FLAGS ("(*LF)a", 0, 0, G_REGEX_NEWLINE_LF, 0);
2220 TEST_NEW_CHECK_FLAGS ("(*CRLF)a", 0, 0, G_REGEX_NEWLINE_CRLF, 0);
2221 TEST_NEW_CHECK_FLAGS ("(*ANY)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2222 TEST_NEW_CHECK_FLAGS ("(*ANYCRLF)a", 0, 0, G_REGEX_NEWLINE_ANYCRLF, 0);
2223 TEST_NEW_CHECK_FLAGS ("(*BSR_ANYCRLF)a", 0, 0, G_REGEX_BSR_ANYCRLF, 0);
2224 TEST_NEW_CHECK_FLAGS ("(*BSR_UNICODE)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2225 TEST_NEW_CHECK_FLAGS ("(*NO_START_OPT)a", 0, 0, 0 /* not exposed in GRegex */, 0);
2227 /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */
2228 TEST_NEW_FAIL("(", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2229 TEST_NEW_FAIL(")", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2230 TEST_NEW_FAIL("[", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2231 TEST_NEW_FAIL("*", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2232 TEST_NEW_FAIL("?", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2233 TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2235 /* Check all GRegexError codes */
2236 TEST_NEW_FAIL ("a\\", 0, G_REGEX_ERROR_STRAY_BACKSLASH);
2237 TEST_NEW_FAIL ("a\\c", 0, G_REGEX_ERROR_MISSING_CONTROL_CHAR);
2238 TEST_NEW_FAIL ("a\\l", 0, G_REGEX_ERROR_UNRECOGNIZED_ESCAPE);
2239 TEST_NEW_FAIL ("a{4,2}", 0, G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER);
2240 TEST_NEW_FAIL ("a{999999,}", 0, G_REGEX_ERROR_QUANTIFIER_TOO_BIG);
2241 TEST_NEW_FAIL ("[a-z", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2242 TEST_NEW_FAIL ("(?X)[\\B]", 0, G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS);
2243 TEST_NEW_FAIL ("[z-a]", 0, G_REGEX_ERROR_RANGE_OUT_OF_ORDER);
2244 TEST_NEW_FAIL ("{2,4}", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2245 TEST_NEW_FAIL ("a(?u)", 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
2246 TEST_NEW_FAIL ("a(?<$foo)bar", 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
2247 TEST_NEW_FAIL ("a[:alpha:]b", 0, G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS);
2248 TEST_NEW_FAIL ("a(b", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2249 TEST_NEW_FAIL ("a)b", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2250 TEST_NEW_FAIL ("a(?R", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2251 TEST_NEW_FAIL ("a(?-54", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2252 TEST_NEW_FAIL ("(ab\\2)", 0, G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE);
2253 TEST_NEW_FAIL ("a(?#abc", 0, G_REGEX_ERROR_UNTERMINATED_COMMENT);
2254 TEST_NEW_FAIL ("(?<=a+)b", 0, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
2255 TEST_NEW_FAIL ("(?(1?)a|b)", 0, G_REGEX_ERROR_MALFORMED_CONDITION);
2256 TEST_NEW_FAIL ("(a)(?(1)a|b|c)", 0, G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES);
2257 TEST_NEW_FAIL ("(?(?i))", 0, G_REGEX_ERROR_ASSERTION_EXPECTED);
2258 TEST_NEW_FAIL ("a[[:fubar:]]b", 0, G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME);
2259 TEST_NEW_FAIL ("[[.ch.]]", 0, G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED);
2260 TEST_NEW_FAIL ("\\x{110000}", 0, G_REGEX_ERROR_HEX_CODE_TOO_LARGE);
2261 TEST_NEW_FAIL ("^(?(0)f|b)oo", 0, G_REGEX_ERROR_INVALID_CONDITION);
2262 TEST_NEW_FAIL ("(?<=\\C)X", 0, G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND);
2263 TEST_NEW_FAIL ("(?!\\w)(?R)", 0, G_REGEX_ERROR_INFINITE_LOOP);
2264 TEST_NEW_FAIL ("(?(?<ab))", 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR);
2265 TEST_NEW_FAIL ("(?P<x>eks)(?P<x>eccs)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2267 TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_MALFORMED_PROPERTY);
2268 TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_UNKNOWN_PROPERTY);
2270 TEST_NEW_FAIL ("\\666", G_REGEX_RAW, G_REGEX_ERROR_INVALID_OCTAL_VALUE);
2271 TEST_NEW_FAIL ("^(?(DEFINE) abc | xyz ) ", 0, G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE);
2272 TEST_NEW_FAIL ("a", G_REGEX_NEWLINE_CRLF | G_REGEX_NEWLINE_ANYCRLF, G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS);
2273 TEST_NEW_FAIL ("^(a)\\g{3", 0, G_REGEX_ERROR_MISSING_BACK_REFERENCE);
2274 TEST_NEW_FAIL ("^(a)\\g{0}", 0, G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE);
2275 TEST_NEW_FAIL ("abc(*FAIL:123)xyz", 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN);
2276 TEST_NEW_FAIL ("a(*FOOBAR)b", 0, G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB);
2277 TEST_NEW_FAIL ("(?i:A{1,}\\6666666666)", 0, G_REGEX_ERROR_NUMBER_TOO_BIG);
2278 TEST_NEW_FAIL ("(?<a>)(?&)", 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME);
2279 TEST_NEW_FAIL ("(?+-a)", 0, G_REGEX_ERROR_MISSING_DIGIT);
2280 TEST_NEW_FAIL ("TA]", G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_INVALID_DATA_CHARACTER);
2281 TEST_NEW_FAIL ("(?|(?<a>A)|(?<b>B))", 0, G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME);
2282 TEST_NEW_FAIL ("a(*MARK)b", 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED);
2283 TEST_NEW_FAIL ("^\\c€", 0, G_REGEX_ERROR_INVALID_CONTROL_CHAR);
2284 TEST_NEW_FAIL ("\\k", 0, G_REGEX_ERROR_MISSING_NAME);
2285 TEST_NEW_FAIL ("a[\\NB]c", 0, G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS);
2286 TEST_NEW_FAIL ("(*:0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFG)XX", 0, G_REGEX_ERROR_NAME_TOO_LONG);
2287 TEST_NEW_FAIL ("\\u0100", G_REGEX_RAW | G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_CHARACTER_VALUE_TOO_LARGE);
2289 /* These errors can't really be tested sanely:
2290 * G_REGEX_ERROR_EXPRESSION_TOO_LARGE
2291 * G_REGEX_ERROR_MEMORY_ERROR
2292 * G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG
2293 * G_REGEX_ERROR_TOO_MANY_SUBPATTERNS
2294 * G_REGEX_ERROR_TOO_MANY_FORWARD_REFERENCES
2296 * These errors are obsolete and never raised by PCRE:
2297 * G_REGEX_ERROR_DEFINE_REPETION
2300 /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
2301 TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE);
2302 TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE);
2303 TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE);
2304 TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE);
2305 TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED, 0, FALSE);
2306 TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED, FALSE);
2307 TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED, 0, TRUE);
2308 TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED, TRUE);
2309 TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS, 0, TRUE);
2310 TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS, 0, TRUE);
2311 /* These are needed to test extended properties. */
2312 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE);
2313 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE);
2314 TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE);
2315 TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE);
2316 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE, 0, 0, TRUE);
2317 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER, 0, 0, TRUE);
2318 TEST_MATCH_SIMPLE("\\p{L}", SHEEN, 0, 0, TRUE);
2319 TEST_MATCH_SIMPLE("\\p{L}", ETH30, 0, 0, FALSE);
2320 TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE);
2321 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE, 0, 0, TRUE);
2322 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER, 0, 0, FALSE);
2323 TEST_MATCH_SIMPLE("\\p{Ll}", ETH30, 0, 0, FALSE);
2324 TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE, 0, 0, FALSE);
2325 TEST_MATCH_SIMPLE("\\p{Sc}", EURO, 0, 0, TRUE);
2326 TEST_MATCH_SIMPLE("\\p{Sc}", ETH30, 0, 0, FALSE);
2327 TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE);
2328 TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE);
2329 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE, 0, 0, FALSE);
2330 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER, 0, 0, FALSE);
2331 TEST_MATCH_SIMPLE("\\p{N}", SHEEN, 0, 0, FALSE);
2332 TEST_MATCH_SIMPLE("\\p{N}", ETH30, 0, 0, TRUE);
2333 TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE);
2334 TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE);
2335 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE, 0, 0, FALSE);
2336 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER, 0, 0, FALSE);
2337 TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN, 0, 0, FALSE);
2338 TEST_MATCH_SIMPLE("\\p{Nd}", ETH30, 0, 0, FALSE);
2339 TEST_MATCH_SIMPLE("\\p{Common}", SHEEN, 0, 0, FALSE);
2340 TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE);
2341 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE, 0, 0, FALSE);
2342 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER, 0, 0, FALSE);
2343 TEST_MATCH_SIMPLE("\\p{Common}", ETH30, 0, 0, FALSE);
2344 TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE);
2345 TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE);
2346 TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN, 0, 0, TRUE);
2347 TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE);
2348 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE, 0, 0, FALSE);
2349 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER, 0, 0, FALSE);
2350 TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30, 0, 0, FALSE);
2351 TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE);
2352 TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE);
2353 TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN, 0, 0, FALSE);
2354 TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE);
2355 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE, 0, 0, TRUE);
2356 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER, 0, 0, TRUE);
2357 TEST_MATCH_SIMPLE("\\p{Latin}", ETH30, 0, 0, FALSE);
2358 TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE);
2359 TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE);
2360 TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN, 0, 0, FALSE);
2361 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE);
2362 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE, 0, 0, FALSE);
2363 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER, 0, 0, FALSE);
2364 TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30, 0, 0, TRUE);
2365 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE);
2366 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE);
2367 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN, 0, 0, TRUE);
2368 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN, 0, 0, FALSE);
2369 /* Invalid patterns. */
2370 TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE);
2371 TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE);
2373 /* TEST_MATCH(pattern, compile_opts, match_opts, string,
2374 * string_len, start_position, match_opts2, expected) */
2375 TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
2376 TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE);
2377 TEST_MATCH("a", G_REGEX_CASELESS, 0, "A", -1, 0, 0, TRUE);
2378 TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE);
2379 TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE);
2380 TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE);
2381 TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE);
2382 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "a", -1, 0, 0, TRUE);
2383 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ab", -1, 1, 0, FALSE);
2384 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ba", 1, 0, 0, FALSE);
2385 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "bab", -1, 0, 0, FALSE);
2386 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "b", -1, 0, 0, FALSE);
2387 TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED, TRUE);
2388 TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED, FALSE);
2389 TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED, FALSE);
2390 TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED, FALSE);
2391 TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED, FALSE);
2392 TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE);
2393 TEST_MATCH("\\d", 0, 0, EURO, -1, 0, 0, FALSE);
2394 TEST_MATCH("^.$", 0, 0, EURO, -1, 0, 0, TRUE);
2395 TEST_MATCH("^.{3}$", 0, 0, EURO, -1, 0, 0, FALSE);
2396 TEST_MATCH("^.$", G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE);
2397 TEST_MATCH("^.{3}$", G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE);
2398 TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE);
2400 /* New lines handling. */
2401 TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2402 TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE);
2403 TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE);
2404 TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE);
2405 TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE);
2406 TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE);
2407 TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2409 TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE);
2410 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\nb\nc", -1, 0, 0, TRUE);
2411 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2412 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\rb\rc", -1, 0, 0, TRUE);
2413 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc", -1, 0, 0, FALSE);
2414 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc", -1, 0, 0, TRUE);
2415 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc", -1, 0, 0, FALSE);
2416 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2417 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2418 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2419 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc", -1, 0, 0, TRUE);
2420 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2421 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2422 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc", -1, 0, 0, FALSE);
2423 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2424 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc", -1, 0, 0, FALSE);
2425 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2426 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2427 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2428 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc", -1, 0, 0, TRUE);
2429 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2430 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2432 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc", -1, 0, 0, TRUE);
2433 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc", -1, 0, 0, TRUE);
2434 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2435 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2436 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2437 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2438 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2440 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2441 TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2442 TEST_MATCH("a#\rb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2443 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a", -1, 0, 0, FALSE);
2444 TEST_MATCH("a#\nb", G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a", -1, 0, 0, TRUE);
2446 TEST_MATCH("line\nbreak", G_REGEX_MULTILINE, 0, "this is a line\nbreak", -1, 0, 0, TRUE);
2447 TEST_MATCH("line\nbreak", G_REGEX_MULTILINE | G_REGEX_FIRSTLINE, 0, "first line\na line\nbreak", -1, 0, 0, FALSE);
2449 /* This failed with PCRE 7.2 (gnome bug #455640) */
2450 TEST_MATCH(".*$", 0, 0, "\xe1\xbb\x85", -1, 0, 0, TRUE);
2452 /* Test that othercasing in our pcre/glib integration is bug-for-bug compatible
2453 * with pcre's internal tables. Bug #678273 */
2454 TEST_MATCH("[Ç„]", G_REGEX_CASELESS, 0, "Ç„", -1, 0, 0, TRUE);
2455 TEST_MATCH("[DŽ]", G_REGEX_CASELESS, 0, "dž", -1, 0, 0, TRUE);
2456 #if PCRE_MAJOR > 8 || (PCRE_MAJOR == 8 && PCRE_MINOR >= 32)
2457 /* This would incorrectly fail to match in pcre < 8.32, so only assert
2458 * this for known-good pcre. */
2459 TEST_MATCH("[Ç„]", G_REGEX_CASELESS, 0, "Ç…", -1, 0, 0, TRUE);
2462 /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
2463 TEST_MATCH_NEXT0("a", "x", -1, 0);
2464 TEST_MATCH_NEXT0("a", "ax", -1, 1);
2465 TEST_MATCH_NEXT0("a", "xa", 1, 0);
2466 TEST_MATCH_NEXT0("a", "axa", 1, 2);
2467 TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
2468 TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
2469 TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5);
2470 TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
2471 TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
2472 TEST_MATCH_NEXT2(EURO "*", EURO EURO, -1, 0, EURO EURO, 0, 6, "", 6, 6);
2473 TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
2474 TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
2475 TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
2476 TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
2477 TEST_MATCH_NEXT2(EURO "+", EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7);
2478 TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
2479 TEST_MATCH_NEXT3("", AGRAVE "b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
2480 TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
2481 TEST_MATCH_NEXT3("a", "aa" OGRAVE "a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
2482 TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
2483 TEST_MATCH_NEXT3("(?=[A-Z0-9])", "RegExTest", -1, 0, "", 0, 0, "", 3, 3, "", 5, 5);
2484 TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
2486 /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
2487 TEST_MATCH_COUNT("a", "", 0, 0, 0);
2488 TEST_MATCH_COUNT("a", "a", 0, 0, 1);
2489 TEST_MATCH_COUNT("a", "a", 1, 0, 0);
2490 TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
2491 TEST_MATCH_COUNT("(.)", EURO, 0, 0, 2);
2492 TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
2493 TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
2494 TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL, 0);
2495 TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
2496 TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
2498 /* TEST_PARTIAL(pattern, string, expected) */
2499 TEST_PARTIAL("^ab", "a", TRUE);
2500 TEST_PARTIAL("^ab", "xa", FALSE);
2501 TEST_PARTIAL("ab", "xa", TRUE);
2502 TEST_PARTIAL("ab", "ab", FALSE); /* normal match. */
2503 TEST_PARTIAL("a+b", "aa", TRUE);
2504 TEST_PARTIAL("(a)+b", "aa", TRUE);
2505 TEST_PARTIAL("a?b", "a", TRUE);
2507 /* Test soft vs. hard partial matching */
2508 TEST_PARTIAL_FULL("cat(fish)?", "cat", G_REGEX_MATCH_PARTIAL_SOFT, FALSE);
2509 TEST_PARTIAL_FULL("cat(fish)?", "cat", G_REGEX_MATCH_PARTIAL_HARD, TRUE);
2511 /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
2512 * expected_start, expected_end) */
2513 TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
2514 TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
2515 TEST_SUB_PATTERN("a(.)", "a" EURO, 0, 1, EURO, 1, 4);
2516 TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG, 0, 2, ENG, 3, 5);
2517 TEST_SUB_PATTERN("(" HSTROKE ")", "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3);
2518 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2519 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2520 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
2521 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
2522 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
2524 /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
2525 * expected_sub, expected_start, expected_end) */
2526 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
2527 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
2528 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "A", "b", 4, 5);
2529 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "B", NULL, UNTOUCHED, UNTOUCHED);
2530 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "C", NULL, UNTOUCHED, UNTOUCHED);
2531 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "A", EGRAVE, 1, 3);
2532 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "B", "x", 3, 4);
2533 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
2534 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
2536 /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
2537 * expected_sub, expected_start, expected_end) */
2538 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2539 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2540 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2541 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2542 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2544 /* DUPNAMES option inside the pattern */
2545 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2546 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2547 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2548 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2549 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2551 /* TEST_FETCH_ALL#(pattern, string, ...) */
2552 TEST_FETCH_ALL0("a", "");
2553 TEST_FETCH_ALL0("a", "b");
2554 TEST_FETCH_ALL1("a", "a", "a");
2555 TEST_FETCH_ALL1("a+", "aa", "aa");
2556 TEST_FETCH_ALL1("(?:a)", "a", "a");
2557 TEST_FETCH_ALL2("(a)", "a", "a", "a");
2558 TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
2559 TEST_FETCH_ALL2("a(.)", "a" HSTROKE, "a" HSTROKE, HSTROKE);
2560 TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
2561 TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
2562 TEST_FETCH_ALL3("(?P<A>.)(a)", ENG "a", ENG "a", ENG, "a");
2563 TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
2564 TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
2566 /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
2567 TEST_SPLIT_SIMPLE0("", "");
2568 TEST_SPLIT_SIMPLE0("a", "");
2569 TEST_SPLIT_SIMPLE1(",", "a", "a");
2570 TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
2571 TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
2572 TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
2573 TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
2574 TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
2575 TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
2576 TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
2577 TEST_SPLIT_SIMPLE2("\\s", "ab c", "ab", "c");
2578 TEST_SPLIT_SIMPLE3("\\s*", "ab c", "a", "b", "c");
2579 /* Not matched sub-strings. */
2580 TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
2581 TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
2582 /* Empty matches. */
2583 TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
2584 TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
2585 /* Invalid patterns. */
2586 TEST_SPLIT_SIMPLE0("\\", "");
2587 TEST_SPLIT_SIMPLE0("[", "");
2589 /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
2590 TEST_SPLIT0("", "", 0, 0);
2591 TEST_SPLIT0("a", "", 0, 0);
2592 TEST_SPLIT0("a", "", 0, 1);
2593 TEST_SPLIT0("a", "", 0, 2);
2594 TEST_SPLIT0("a", "a", 1, 0);
2595 TEST_SPLIT1(",", "a", 0, 0, "a");
2596 TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
2597 TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
2598 TEST_SPLIT1(",", "a,b", 2, 0, "b");
2599 TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
2600 TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
2601 TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
2602 TEST_SPLIT2(",", "a,", 0, 0, "a", "");
2603 TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
2604 TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
2605 TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
2606 TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
2607 TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
2608 /* Not matched sub-strings. */
2609 TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
2610 TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
2611 /* Empty matches. */
2612 TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
2613 TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
2614 TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
2615 TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
2616 TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
2617 TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
2618 TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
2620 /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
2621 TEST_CHECK_REPLACEMENT("", TRUE, FALSE);
2622 TEST_CHECK_REPLACEMENT("a", TRUE, FALSE);
2623 TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE, FALSE);
2624 TEST_CHECK_REPLACEMENT("\\0", TRUE, TRUE);
2625 TEST_CHECK_REPLACEMENT("\\n\\2", TRUE, TRUE);
2626 TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE, TRUE);
2627 /* Invalid strings */
2628 TEST_CHECK_REPLACEMENT("\\Q", FALSE, FALSE);
2629 TEST_CHECK_REPLACEMENT("x\\Ay", FALSE, FALSE);
2631 /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
2632 TEST_EXPAND("a", "a", "", FALSE, "");
2633 TEST_EXPAND("a", "a", "\\0", FALSE, "a");
2634 TEST_EXPAND("a", "a", "\\1", FALSE, "");
2635 TEST_EXPAND("(a)", "ab", "\\1", FALSE, "a");
2636 TEST_EXPAND("(a)", "a", "\\1", FALSE, "a");
2637 TEST_EXPAND("(a)", "a", "\\g<1>", FALSE, "a");
2638 TEST_EXPAND("a", "a", "\\0130", FALSE, "X");
2639 TEST_EXPAND("a", "a", "\\\\\\0", FALSE, "\\a");
2640 TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE, "XbX");
2641 #ifndef USE_SYSTEM_PCRE
2642 /* PCRE >= 8.34 no longer allows this usage. */
2643 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE, "a");
2644 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE, "a");
2646 TEST_EXPAND(".", EURO, "\\0", FALSE, EURO);
2647 TEST_EXPAND("(.)", EURO, "\\1", FALSE, EURO);
2648 TEST_EXPAND("(?P<G>.)", EURO, "\\g<G>", FALSE, EURO);
2649 TEST_EXPAND(".", "a", EURO, FALSE, EURO);
2650 TEST_EXPAND(".", "a", EURO "\\0", FALSE, EURO "a");
2651 TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE, "abc");
2652 TEST_EXPAND(".", "", "\\LaB\\EC", FALSE, "abC");
2653 TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE, "ABc");
2654 TEST_EXPAND(".", "", "a\\ubc", FALSE, "aBc");
2655 TEST_EXPAND(".", "", "a\\lbc", FALSE, "abc");
2656 TEST_EXPAND(".", "", "A\\uBC", FALSE, "ABC");
2657 TEST_EXPAND(".", "", "A\\lBC", FALSE, "AbC");
2658 TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE, "A\\BC");
2659 TEST_EXPAND(".", "", "\\L" AGRAVE "\\E", FALSE, AGRAVE);
2660 TEST_EXPAND(".", "", "\\U" AGRAVE "\\E", FALSE, AGRAVE_UPPER);
2661 TEST_EXPAND(".", "", "\\u" AGRAVE "a", FALSE, AGRAVE_UPPER "a");
2662 TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE, "xAYz");
2663 TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE, "xbyz");
2664 TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE, "xAyz");
2665 TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE, "xbyz");
2666 TEST_EXPAND(".(.)", "a" AGRAVE_UPPER, "x\\l\\1y", FALSE, "x" AGRAVE "y");
2667 TEST_EXPAND("a", "bab", "\\x{61}", FALSE, "a");
2668 TEST_EXPAND("a", "bab", "\\x61", FALSE, "a");
2669 TEST_EXPAND("a", "bab", "\\x5a", FALSE, "Z");
2670 TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE, "aZ");
2671 TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE, "Z");
2672 TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE, AGRAVE);
2673 TEST_EXPAND("", "bab", "\\x{0634}", FALSE, SHEEN);
2674 TEST_EXPAND("", "bab", "\\x{634}", FALSE, SHEEN);
2675 TEST_EXPAND("", "", "\\t", FALSE, "\t");
2676 TEST_EXPAND("", "", "\\v", FALSE, "\v");
2677 TEST_EXPAND("", "", "\\r", FALSE, "\r");
2678 TEST_EXPAND("", "", "\\n", FALSE, "\n");
2679 TEST_EXPAND("", "", "\\f", FALSE, "\f");
2680 TEST_EXPAND("", "", "\\a", FALSE, "\a");
2681 TEST_EXPAND("", "", "\\b", FALSE, "\b");
2682 TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE, "ab\bb");
2683 TEST_EXPAND("a(.)", "abc", "\\0141", FALSE, "a");
2684 TEST_EXPAND("a(.)", "abc", "\\078", FALSE, "\a8");
2685 TEST_EXPAND("a(.)", "abc", "\\077", FALSE, "?");
2686 TEST_EXPAND("a(.)", "abc", "\\0778", FALSE, "?8");
2687 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", FALSE, AGRAVE);
2688 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", TRUE, "\xc3");
2689 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\0", TRUE, "a\xc3");
2690 /* Invalid strings. */
2691 TEST_EXPAND("", "", "\\Q", FALSE, NULL);
2692 TEST_EXPAND("", "", "x\\Ay", FALSE, NULL);
2693 TEST_EXPAND("", "", "\\g<", FALSE, NULL);
2694 TEST_EXPAND("", "", "\\g<>", FALSE, NULL);
2695 TEST_EXPAND("", "", "\\g<1a>", FALSE, NULL);
2696 TEST_EXPAND("", "", "\\g<a$>", FALSE, NULL);
2697 TEST_EXPAND("", "", "\\", FALSE, NULL);
2698 TEST_EXPAND("a", "a", "\\x{61", FALSE, NULL);
2699 TEST_EXPAND("a", "a", "\\x6X", FALSE, NULL);
2701 TEST_EXPAND(NULL, NULL, "", FALSE, "");
2702 TEST_EXPAND(NULL, NULL, "\\n", FALSE, "\n");
2703 /* Invalid strings */
2704 TEST_EXPAND(NULL, NULL, "\\Q", FALSE, NULL);
2705 TEST_EXPAND(NULL, NULL, "x\\Ay", FALSE, NULL);
2707 /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
2708 TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
2709 TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
2710 TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
2711 TEST_REPLACE("a", "ababa", 3, "A", "ababA");
2712 TEST_REPLACE("a", "ababa", 4, "A", "ababA");
2713 TEST_REPLACE("a", "ababa", 5, "A", "ababa");
2714 TEST_REPLACE("a", "ababa", 6, "A", "ababa");
2715 TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
2716 TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2717 TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2718 TEST_REPLACE("a", "bab", 0, "A", "bAb");
2719 TEST_REPLACE("$^", "abc", 0, "X", "abc");
2720 TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2721 TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2722 TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2723 TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2724 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2725 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2726 TEST_REPLACE("[^-]", "-" EURO "-x-" HSTROKE, 0, "a", "-a-a-a");
2727 TEST_REPLACE("[^-]", "-" EURO "-" HSTROKE, 0, "a\\g<0>a", "-a" EURO "a-a" HSTROKE "a");
2728 TEST_REPLACE("-", "-" EURO "-" HSTROKE, 0, "", EURO HSTROKE);
2729 TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2730 TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2731 TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2732 TEST_REPLACE(".", "a", 0, "\\A", NULL);
2733 TEST_REPLACE(".", "a", 0, "\\g", NULL);
2735 /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2736 TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2737 TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2738 TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2739 TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2740 TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2741 TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2742 TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2743 TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2744 TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2745 TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2746 TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2747 TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2748 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2749 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2750 TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a", "-a" ENG "a-" HSTROKE);
2751 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a", "-a-a-a");
2752 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2753 TEST_REPLACE_LIT("-", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "", EURO AGRAVE HSTROKE);
2754 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 0, "_", "_Reg_Ex_Test");
2755 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 1, "_", "Reg_Ex_Test");
2757 /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2758 TEST_GET_STRING_NUMBER("", "A", -1);
2759 TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2760 TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2761 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2762 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2763 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2764 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2765 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2766 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2767 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2768 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2770 /* TEST_ESCAPE_NUL(string, length, expected) */
2771 TEST_ESCAPE_NUL("hello world", -1, "hello world");
2772 TEST_ESCAPE_NUL("hello\0world", -1, "hello");
2773 TEST_ESCAPE_NUL("\0world", -1, "");
2774 TEST_ESCAPE_NUL("hello world", 5, "hello");
2775 TEST_ESCAPE_NUL("hello.world", 11, "hello.world");
2776 TEST_ESCAPE_NUL("a(b\\b.$", 7, "a(b\\b.$");
2777 TEST_ESCAPE_NUL("hello\0", 6, "hello\\x00");
2778 TEST_ESCAPE_NUL("\0world", 6, "\\x00world");
2779 TEST_ESCAPE_NUL("\0\0", 2, "\\x00\\x00");
2780 TEST_ESCAPE_NUL("hello\0world", 11, "hello\\x00world");
2781 TEST_ESCAPE_NUL("hello\0world\0", 12, "hello\\x00world\\x00");
2782 TEST_ESCAPE_NUL("hello\\\0world", 12, "hello\\x00world");
2783 TEST_ESCAPE_NUL("hello\\\\\0world", 13, "hello\\\\\\x00world");
2784 TEST_ESCAPE_NUL("|()[]{}^$*+?.", 13, "|()[]{}^$*+?.");
2785 TEST_ESCAPE_NUL("|()[]{}^$*+?.\\\\", 15, "|()[]{}^$*+?.\\\\");
2787 /* TEST_ESCAPE(string, length, expected) */
2788 TEST_ESCAPE("hello world", -1, "hello world");
2789 TEST_ESCAPE("hello world", 5, "hello");
2790 TEST_ESCAPE("hello.world", -1, "hello\\.world");
2791 TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2792 TEST_ESCAPE("hello\0world", -1, "hello");
2793 TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2794 TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG);
2795 TEST_ESCAPE("a$", -1, "a\\$");
2796 TEST_ESCAPE("$a", -1, "\\$a");
2797 TEST_ESCAPE("a$a", -1, "a\\$a");
2798 TEST_ESCAPE("$a$", -1, "\\$a\\$");
2799 TEST_ESCAPE("$a$", 0, "");
2800 TEST_ESCAPE("$a$", 1, "\\$");
2801 TEST_ESCAPE("$a$", 2, "\\$a");
2802 TEST_ESCAPE("$a$", 3, "\\$a\\$");
2803 TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2804 TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2805 TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2806 "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2808 /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2809 TEST_MATCH_ALL0("<.*>", "", -1, 0);
2810 TEST_MATCH_ALL0("a+", "", -1, 0);
2811 TEST_MATCH_ALL0("a+", "a", 0, 0);
2812 TEST_MATCH_ALL0("a+", "a", -1, 1);
2813 TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2814 TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2815 TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2816 TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2817 TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2818 TEST_MATCH_ALL1(".+", ENG, -1, 0, ENG, 0, 2);
2819 TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2820 TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2821 TEST_MATCH_ALL2(".+", ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2);
2822 TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2823 "<a><b>", 0, 6, "<a>", 0, 3);
2824 TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2826 /* NOTEMPTY matching */
2827 TEST_MATCH_NOTEMPTY("a?b?", "xyz", FALSE);
2828 TEST_MATCH_NOTEMPTY_ATSTART("a?b?", "xyz", TRUE);
2830 return g_test_run ();