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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #undef G_DISABLE_ASSERT
32 /* U+20AC EURO SIGN (symbol, currency) */
33 #define EURO "\xe2\x82\xac"
34 /* U+00E0 LATIN SMALL LETTER A WITH GRAVE (letter, lowercase) */
35 #define AGRAVE "\xc3\xa0"
36 /* U+00C0 LATIN CAPITAL LETTER A WITH GRAVE (letter, uppercase) */
37 #define AGRAVE_UPPER "\xc3\x80"
38 /* U+00E8 LATIN SMALL LETTER E WITH GRAVE (letter, lowercase) */
39 #define EGRAVE "\xc3\xa8"
40 /* U+00F2 LATIN SMALL LETTER O WITH GRAVE (letter, lowercase) */
41 #define OGRAVE "\xc3\xb2"
42 /* U+014B LATIN SMALL LETTER ENG (letter, lowercase) */
43 #define ENG "\xc5\x8b"
44 /* U+0127 LATIN SMALL LETTER H WITH STROKE (letter, lowercase) */
45 #define HSTROKE "\xc4\xa7"
46 /* U+0634 ARABIC LETTER SHEEN (letter, other) */
47 #define SHEEN "\xd8\xb4"
48 /* U+1374 ETHIOPIC NUMBER THIRTY (number, other) */
49 #define ETH30 "\xe1\x8d\xb4"
51 /* A random value use to mark untouched integer variables. */
52 #define UNTOUCHED -559038737
58 GRegexCompileFlags compile_opts;
59 GRegexMatchFlags match_opts;
64 test_new (gconstpointer d)
66 const TestNewData *data = d;
70 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
71 g_assert (regex != NULL);
72 g_assert_no_error (error);
73 g_assert_cmpstr (data->pattern, ==, g_regex_get_pattern (regex));
75 g_regex_unref (regex);
78 #define TEST_NEW(_pattern, _compile_opts, _match_opts) { \
81 data = g_new0 (TestNewData, 1); \
82 data->pattern = _pattern; \
83 data->compile_opts = _compile_opts; \
84 data->match_opts = _match_opts; \
85 data->expected_error = 0; \
86 path = g_strdup_printf ("/regex/new/%d", ++total); \
87 g_test_add_data_func (path, data, test_new); \
92 test_new_fail (gconstpointer d)
94 const TestNewData *data = d;
98 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
100 g_assert (regex == NULL);
101 g_assert_error (error, G_REGEX_ERROR, data->expected_error);
102 g_error_free (error);
105 #define TEST_NEW_FAIL(_pattern, _compile_opts, _expected_error) { \
108 data = g_new0 (TestNewData, 1); \
109 data->pattern = _pattern; \
110 data->compile_opts = _compile_opts; \
111 data->match_opts = 0; \
112 data->expected_error = _expected_error; \
113 path = g_strdup_printf ("/regex/new-fail/%d", ++total); \
114 g_test_add_data_func (path, data, test_new_fail); \
119 const gchar *pattern;
121 GRegexCompileFlags compile_opts;
122 GRegexMatchFlags match_opts;
126 GRegexMatchFlags match_opts2;
130 test_match_simple (gconstpointer d)
132 const TestMatchData *data = d;
135 match = g_regex_match_simple (data->pattern, data->string, data->compile_opts, data->match_opts);
136 g_assert_cmpint (match, ==, data->expected);
139 #define TEST_MATCH_SIMPLE(_pattern, _string, _compile_opts, _match_opts, _expected) { \
140 TestMatchData *data; \
142 data = g_new0 (TestMatchData, 1); \
143 data->pattern = _pattern; \
144 data->string = _string; \
145 data->compile_opts = _compile_opts; \
146 data->match_opts = _match_opts; \
147 data->expected = _expected; \
148 path = g_strdup_printf ("/regex/match-simple/%d", ++total); \
149 g_test_add_data_func (path, data, test_match_simple); \
154 test_match (gconstpointer d)
156 const TestMatchData *data = d;
159 GError *error = NULL;
161 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
162 g_assert (regex != NULL);
163 g_assert_no_error (error);
165 match = g_regex_match_full (regex, data->string, data->string_len,
166 data->start_position, data->match_opts2, NULL, NULL);
168 g_assert_cmpint (match, ==, data->expected);
170 if (data->string_len == -1 && data->start_position == 0)
172 match = g_regex_match (regex, data->string, data->match_opts2, NULL);
173 g_assert_cmpint (match, ==, data->expected);
176 g_regex_unref (regex);
179 #define TEST_MATCH(_pattern, _compile_opts, _match_opts, _string, \
180 _string_len, _start_position, _match_opts2, _expected) { \
181 TestMatchData *data; \
183 data = g_new0 (TestMatchData, 1); \
184 data->pattern = _pattern; \
185 data->compile_opts = _compile_opts; \
186 data->match_opts = _match_opts; \
187 data->string = _string; \
188 data->string_len = _string_len; \
189 data->start_position = _start_position; \
190 data->match_opts2 = _match_opts2; \
191 data->expected = _expected; \
192 path = g_strdup_printf ("/regex/match/%d", ++total); \
193 g_test_add_data_func (path, data, test_match); \
202 typedef struct _Match Match;
205 free_match (gpointer data, gpointer user_data)
210 g_free (match->string);
215 const gchar *pattern;
223 test_match_next (gconstpointer d)
225 const TestMatchNextData *data = d;
227 GMatchInfo *match_info;
229 GSList *l_exp, *l_match;
231 regex = g_regex_new (data->pattern, 0, 0, NULL);
233 g_assert (regex != NULL);
235 g_regex_match_full (regex, data->string, data->string_len,
236 data->start_position, 0, &match_info, NULL);
238 while (g_match_info_matches (match_info))
240 Match *match = g_new0 (Match, 1);
241 match->string = g_match_info_fetch (match_info, 0);
242 match->start = UNTOUCHED;
243 match->end = UNTOUCHED;
244 g_match_info_fetch_pos (match_info, 0, &match->start, &match->end);
245 matches = g_slist_prepend (matches, match);
246 g_match_info_next (match_info, NULL);
248 g_assert (regex == g_match_info_get_regex (match_info));
249 g_assert_cmpstr (data->string, ==, g_match_info_get_string (match_info));
250 g_match_info_free (match_info);
251 matches = g_slist_reverse (matches);
253 g_assert_cmpint (g_slist_length (matches), ==, g_slist_length (data->expected));
255 l_exp = data->expected;
257 while (l_exp != NULL)
259 Match *exp = l_exp->data;
260 Match *match = l_match->data;
262 g_assert_cmpstr (exp->string, ==, match->string);
263 g_assert_cmpint (exp->start, ==, match->start);
264 g_assert_cmpint (exp->end, ==, match->end);
266 l_exp = g_slist_next (l_exp);
267 l_match = g_slist_next (l_match);
270 g_regex_unref (regex);
271 g_slist_foreach (matches, free_match, NULL);
272 g_slist_free (matches);
275 #define TEST_MATCH_NEXT0(_pattern, _string, _string_len, _start_position) { \
276 TestMatchNextData *data; \
278 data = g_new0 (TestMatchNextData, 1); \
279 data->pattern = _pattern; \
280 data->string = _string; \
281 data->string_len = _string_len; \
282 data->start_position = _start_position; \
283 data->expected = NULL; \
284 path = g_strdup_printf ("/regex/match/next0/%d", ++total); \
285 g_test_add_data_func (path, data, test_match_next); \
289 #define TEST_MATCH_NEXT1(_pattern, _string, _string_len, _start_position, \
291 TestMatchNextData *data; \
294 data = g_new0 (TestMatchNextData, 1); \
295 data->pattern = _pattern; \
296 data->string = _string; \
297 data->string_len = _string_len; \
298 data->start_position = _start_position; \
299 match = g_new0 (Match, 1); \
300 match->string = t1; \
303 data->expected = g_slist_append (NULL, match); \
304 path = g_strdup_printf ("/regex/match/next1/%d", ++total); \
305 g_test_add_data_func (path, data, test_match_next); \
309 #define TEST_MATCH_NEXT2(_pattern, _string, _string_len, _start_position, \
310 t1, s1, e1, t2, s2, e2) { \
311 TestMatchNextData *data; \
314 data = g_new0 (TestMatchNextData, 1); \
315 data->pattern = _pattern; \
316 data->string = _string; \
317 data->string_len = _string_len; \
318 data->start_position = _start_position; \
319 match = g_new0 (Match, 1); \
320 match->string = t1; \
323 data->expected = g_slist_append (NULL, match); \
324 match = g_new0 (Match, 1); \
325 match->string = t2; \
328 data->expected = g_slist_append (data->expected, match); \
329 path = g_strdup_printf ("/regex/match/next2/%d", ++total); \
330 g_test_add_data_func (path, data, test_match_next); \
334 #define TEST_MATCH_NEXT3(_pattern, _string, _string_len, _start_position, \
335 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
336 TestMatchNextData *data; \
339 data = g_new0 (TestMatchNextData, 1); \
340 data->pattern = _pattern; \
341 data->string = _string; \
342 data->string_len = _string_len; \
343 data->start_position = _start_position; \
344 match = g_new0 (Match, 1); \
345 match->string = t1; \
348 data->expected = g_slist_append (NULL, match); \
349 match = g_new0 (Match, 1); \
350 match->string = t2; \
353 data->expected = g_slist_append (data->expected, match); \
354 match = g_new0 (Match, 1); \
355 match->string = t3; \
358 data->expected = g_slist_append (data->expected, match); \
359 path = g_strdup_printf ("/regex/match/next3/%d", ++total); \
360 g_test_add_data_func (path, data, test_match_next); \
364 #define TEST_MATCH_NEXT4(_pattern, _string, _string_len, _start_position, \
365 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
366 TestMatchNextData *data; \
369 data = g_new0 (TestMatchNextData, 1); \
370 data->pattern = _pattern; \
371 data->string = _string; \
372 data->string_len = _string_len; \
373 data->start_position = _start_position; \
374 match = g_new0 (Match, 1); \
375 match->string = t1; \
378 data->expected = g_slist_append (NULL, match); \
379 match = g_new0 (Match, 1); \
380 match->string = t2; \
383 data->expected = g_slist_append (data->expected, match); \
384 match = g_new0 (Match, 1); \
385 match->string = t3; \
388 data->expected = g_slist_append (data->expected, match); \
389 match = g_new0 (Match, 1); \
390 match->string = t4; \
393 data->expected = g_slist_append (data->expected, match); \
394 path = g_strdup_printf ("/regex/match/next4/%d", ++total); \
395 g_test_add_data_func (path, data, test_match_next); \
400 const gchar *pattern;
403 GRegexMatchFlags match_opts;
405 } TestMatchCountData;
408 test_match_count (gconstpointer d)
410 const TestMatchCountData *data = d;
412 GMatchInfo *match_info;
415 regex = g_regex_new (data->pattern, 0, 0, NULL);
417 g_assert (regex != NULL);
419 g_regex_match_full (regex, data->string, -1, data->start_position,
420 data->match_opts, &match_info, NULL);
421 count = g_match_info_get_match_count (match_info);
423 g_assert_cmpint (count, ==, data->expected_count);
425 g_match_info_free (match_info);
426 g_regex_unref (regex);
429 #define TEST_MATCH_COUNT(_pattern, _string, _start_position, _match_opts, _expected_count) { \
430 TestMatchCountData *data; \
432 data = g_new0 (TestMatchCountData, 1); \
433 data->pattern = _pattern; \
434 data->string = _string; \
435 data->start_position = _start_position; \
436 data->match_opts = _match_opts; \
437 data->expected_count = _expected_count; \
438 path = g_strdup_printf ("/regex/match/count/%d", ++total); \
439 g_test_add_data_func (path, data, test_match_count); \
444 test_partial (gconstpointer d)
446 const TestMatchData *data = d;
448 GMatchInfo *match_info;
450 regex = g_regex_new (data->pattern, 0, 0, NULL);
452 g_assert (regex != NULL);
454 g_regex_match (regex, data->string, G_REGEX_MATCH_PARTIAL, &match_info);
456 g_assert_cmpint (data->expected, ==, g_match_info_is_partial_match (match_info));
460 g_assert (!g_match_info_fetch_pos (match_info, 0, NULL, NULL));
461 g_assert (!g_match_info_fetch_pos (match_info, 1, NULL, NULL));
464 g_match_info_free (match_info);
465 g_regex_unref (regex);
468 #define TEST_PARTIAL(_pattern, _string, _expected) { \
469 TestMatchData *data; \
471 data = g_new0 (TestMatchData, 1); \
472 data->pattern = _pattern; \
473 data->string = _string; \
474 data->expected = _expected; \
475 path = g_strdup_printf ("/regex/match/partial/%d", ++total); \
476 g_test_add_data_func (path, data, test_partial); \
481 const gchar *pattern;
485 const gchar *expected_sub;
491 test_sub_pattern (gconstpointer d)
493 const TestSubData *data = d;
495 GMatchInfo *match_info;
497 gint start = UNTOUCHED, end = UNTOUCHED;
499 regex = g_regex_new (data->pattern, 0, 0, NULL);
501 g_assert (regex != NULL);
503 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
505 sub_expr = g_match_info_fetch (match_info, data->sub_n);
506 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
509 g_match_info_fetch_pos (match_info, data->sub_n, &start, &end);
510 g_assert_cmpint (start, ==, data->expected_start);
511 g_assert_cmpint (end, ==, data->expected_end);
513 g_match_info_free (match_info);
514 g_regex_unref (regex);
517 #define TEST_SUB_PATTERN(_pattern, _string, _start_position, _sub_n, _expected_sub, \
518 _expected_start, _expected_end) { \
521 data = g_new0 (TestSubData, 1); \
522 data->pattern = _pattern; \
523 data->string = _string; \
524 data->start_position = _start_position; \
525 data->sub_n = _sub_n; \
526 data->expected_sub = _expected_sub; \
527 data->expected_start = _expected_start; \
528 data->expected_end = _expected_end; \
529 path = g_strdup_printf ("/regex/match/subpattern/%d", ++total); \
530 g_test_add_data_func (path, data, test_sub_pattern); \
535 const gchar *pattern;
536 GRegexCompileFlags flags;
539 const gchar *sub_name;
540 const gchar *expected_sub;
546 test_named_sub_pattern (gconstpointer d)
548 const TestNamedSubData *data = d;
550 GMatchInfo *match_info;
551 gint start = UNTOUCHED, end = UNTOUCHED;
554 regex = g_regex_new (data->pattern, data->flags, 0, NULL);
556 g_assert (regex != NULL);
558 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
559 sub_expr = g_match_info_fetch_named (match_info, data->sub_name);
560 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
563 g_match_info_fetch_named_pos (match_info, data->sub_name, &start, &end);
564 g_assert_cmpint (start, ==, data->expected_start);
565 g_assert_cmpint (end, ==, data->expected_end);
567 g_match_info_free (match_info);
568 g_regex_unref (regex);
571 #define TEST_NAMED_SUB_PATTERN(_pattern, _string, _start_position, _sub_name, \
572 _expected_sub, _expected_start, _expected_end) { \
573 TestNamedSubData *data; \
575 data = g_new0 (TestNamedSubData, 1); \
576 data->pattern = _pattern; \
577 data->string = _string; \
579 data->start_position = _start_position; \
580 data->sub_name = _sub_name; \
581 data->expected_sub = _expected_sub; \
582 data->expected_start = _expected_start; \
583 data->expected_end = _expected_end; \
584 path = g_strdup_printf ("/regex/match/named/subpattern/%d", ++total); \
585 g_test_add_data_func (path, data, test_named_sub_pattern); \
589 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(_pattern, _string, _start_position, _sub_name, \
590 _expected_sub, _expected_start, _expected_end) { \
591 TestNamedSubData *data; \
593 data = g_new0 (TestNamedSubData, 1); \
594 data->pattern = _pattern; \
595 data->string = _string; \
596 data->flags = G_REGEX_DUPNAMES; \
597 data->start_position = _start_position; \
598 data->sub_name = _sub_name; \
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/named/dupnames/%d", ++total); \
603 g_test_add_data_func (path, data, test_named_sub_pattern); \
608 const gchar *pattern;
616 test_fetch_all (gconstpointer d)
618 const TestFetchAllData *data = d;
620 GMatchInfo *match_info;
626 regex = g_regex_new (data->pattern, 0, 0, NULL);
628 g_assert (regex != NULL);
630 g_regex_match (regex, data->string, 0, &match_info);
631 matches = g_match_info_fetch_all (match_info);
633 match_count = g_strv_length (matches);
637 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
639 l_exp = data->expected;
640 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
642 g_assert_cmpstr (l_exp->data, ==, matches[i]);
645 g_match_info_free (match_info);
646 g_regex_unref (regex);
647 g_strfreev (matches);
650 #define TEST_FETCH_ALL0(_pattern, _string) { \
651 TestFetchAllData *data; \
653 data = g_new0 (TestFetchAllData, 1); \
654 data->pattern = _pattern; \
655 data->string = _string; \
656 data->expected = NULL; \
657 path = g_strdup_printf ("/regex/fetch-all0/%d", ++total); \
658 g_test_add_data_func (path, data, test_fetch_all); \
662 #define TEST_FETCH_ALL1(_pattern, _string, e1) { \
663 TestFetchAllData *data; \
665 data = g_new0 (TestFetchAllData, 1); \
666 data->pattern = _pattern; \
667 data->string = _string; \
668 data->expected = g_slist_append (NULL, e1); \
669 path = g_strdup_printf ("/regex/fetch-all1/%d", ++total); \
670 g_test_add_data_func (path, data, test_fetch_all); \
674 #define TEST_FETCH_ALL2(_pattern, _string, e1, e2) { \
675 TestFetchAllData *data; \
677 data = g_new0 (TestFetchAllData, 1); \
678 data->pattern = _pattern; \
679 data->string = _string; \
680 data->expected = g_slist_append (NULL, e1); \
681 data->expected = g_slist_append (data->expected, e2); \
682 path = g_strdup_printf ("/regex/fetch-all2/%d", ++total); \
683 g_test_add_data_func (path, data, test_fetch_all); \
687 #define TEST_FETCH_ALL3(_pattern, _string, e1, e2, e3) { \
688 TestFetchAllData *data; \
690 data = g_new0 (TestFetchAllData, 1); \
691 data->pattern = _pattern; \
692 data->string = _string; \
693 data->expected = g_slist_append (NULL, e1); \
694 data->expected = g_slist_append (data->expected, e2); \
695 data->expected = g_slist_append (data->expected, e3); \
696 path = g_strdup_printf ("/regex/fetch-all3/%d", ++total); \
697 g_test_add_data_func (path, data, test_fetch_all); \
702 test_split_simple (gconstpointer d)
704 const TestFetchAllData *data = d;
710 tokens = g_regex_split_simple (data->pattern, data->string, 0, 0);
712 token_count = g_strv_length (tokens);
716 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
718 l_exp = data->expected;
719 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
721 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
727 #define TEST_SPLIT_SIMPLE0(_pattern, _string) { \
728 TestFetchAllData *data; \
730 data = g_new0 (TestFetchAllData, 1); \
731 data->pattern = _pattern; \
732 data->string = _string; \
733 data->expected = NULL; \
734 path = g_strdup_printf ("/regex/split/simple0/%d", ++total); \
735 g_test_add_data_func (path, data, test_split_simple); \
739 #define TEST_SPLIT_SIMPLE1(_pattern, _string, e1) { \
740 TestFetchAllData *data; \
742 data = g_new0 (TestFetchAllData, 1); \
743 data->pattern = _pattern; \
744 data->string = _string; \
745 data->expected = g_slist_append (NULL, e1); \
746 path = g_strdup_printf ("/regex/split/simple1/%d", ++total); \
747 g_test_add_data_func (path, data, test_split_simple); \
751 #define TEST_SPLIT_SIMPLE2(_pattern, _string, e1, e2) { \
752 TestFetchAllData *data; \
754 data = g_new0 (TestFetchAllData, 1); \
755 data->pattern = _pattern; \
756 data->string = _string; \
757 data->expected = g_slist_append (NULL, e1); \
758 data->expected = g_slist_append (data->expected, e2); \
759 path = g_strdup_printf ("/regex/split/simple2/%d", ++total); \
760 g_test_add_data_func (path, data, test_split_simple); \
764 #define TEST_SPLIT_SIMPLE3(_pattern, _string, e1, e2, e3) { \
765 TestFetchAllData *data; \
767 data = g_new0 (TestFetchAllData, 1); \
768 data->pattern = _pattern; \
769 data->string = _string; \
770 data->expected = g_slist_append (NULL, e1); \
771 data->expected = g_slist_append (data->expected, e2); \
772 data->expected = g_slist_append (data->expected, e3); \
773 path = g_strdup_printf ("/regex/split/simple3/%d", ++total); \
774 g_test_add_data_func (path, data, test_split_simple); \
779 test_split_full (gconstpointer d)
781 const TestFetchAllData *data = d;
788 regex = g_regex_new (data->pattern, 0, 0, NULL);
790 g_assert (regex != NULL);
792 tokens = g_regex_split_full (regex, data->string, -1, data->start_position,
793 0, data->max_tokens, NULL);
795 token_count = g_strv_length (tokens);
799 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
801 l_exp = data->expected;
802 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
804 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
807 g_regex_unref (regex);
812 test_split (gconstpointer d)
814 const TestFetchAllData *data = d;
821 regex = g_regex_new (data->pattern, 0, 0, NULL);
823 g_assert (regex != NULL);
825 tokens = g_regex_split (regex, data->string, 0);
827 token_count = g_strv_length (tokens);
831 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
833 l_exp = data->expected;
834 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
836 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
839 g_regex_unref (regex);
843 #define TEST_SPLIT0(_pattern, _string, _start_position, _max_tokens) { \
844 TestFetchAllData *data; \
846 data = g_new0 (TestFetchAllData, 1); \
847 data->pattern = _pattern; \
848 data->string = _string; \
849 data->start_position = _start_position; \
850 data->max_tokens = _max_tokens; \
851 data->expected = NULL; \
852 path = g_strdup_printf ("/regex/full-split0/%d", ++total); \
853 g_test_add_data_func (path, data, test_split_full); \
855 if (_start_position == 0 && _max_tokens <= 0) { \
856 path = g_strdup_printf ("/regex/split0/%d", ++total); \
857 g_test_add_data_func (path, data, test_split); \
862 #define TEST_SPLIT1(_pattern, _string, _start_position, _max_tokens, e1) { \
863 TestFetchAllData *data; \
865 data = g_new0 (TestFetchAllData, 1); \
866 data->pattern = _pattern; \
867 data->string = _string; \
868 data->start_position = _start_position; \
869 data->max_tokens = _max_tokens; \
870 data->expected = NULL; \
871 data->expected = g_slist_append (data->expected, e1); \
872 path = g_strdup_printf ("/regex/full-split1/%d", ++total); \
873 g_test_add_data_func (path, data, test_split_full); \
875 if (_start_position == 0 && _max_tokens <= 0) { \
876 path = g_strdup_printf ("/regex/split1/%d", ++total); \
877 g_test_add_data_func (path, data, test_split); \
882 #define TEST_SPLIT2(_pattern, _string, _start_position, _max_tokens, e1, e2) { \
883 TestFetchAllData *data; \
885 data = g_new0 (TestFetchAllData, 1); \
886 data->pattern = _pattern; \
887 data->string = _string; \
888 data->start_position = _start_position; \
889 data->max_tokens = _max_tokens; \
890 data->expected = NULL; \
891 data->expected = g_slist_append (data->expected, e1); \
892 data->expected = g_slist_append (data->expected, e2); \
893 path = g_strdup_printf ("/regex/full-split2/%d", ++total); \
894 g_test_add_data_func (path, data, test_split_full); \
896 if (_start_position == 0 && _max_tokens <= 0) { \
897 path = g_strdup_printf ("/regex/split2/%d", ++total); \
898 g_test_add_data_func (path, data, test_split); \
903 #define TEST_SPLIT3(_pattern, _string, _start_position, _max_tokens, e1, e2, e3) { \
904 TestFetchAllData *data; \
906 data = g_new0 (TestFetchAllData, 1); \
907 data->pattern = _pattern; \
908 data->string = _string; \
909 data->start_position = _start_position; \
910 data->max_tokens = _max_tokens; \
911 data->expected = NULL; \
912 data->expected = g_slist_append (data->expected, e1); \
913 data->expected = g_slist_append (data->expected, e2); \
914 data->expected = g_slist_append (data->expected, e3); \
915 path = g_strdup_printf ("/regex/full-split3/%d", ++total); \
916 g_test_add_data_func (path, data, test_split_full); \
918 if (_start_position == 0 && _max_tokens <= 0) { \
919 path = g_strdup_printf ("/regex/split3/%d", ++total); \
920 g_test_add_data_func (path, data, test_split); \
926 const gchar *string_to_expand;
928 gboolean expected_refs;
929 } TestCheckReplacementData;
932 test_check_replacement (gconstpointer d)
934 const TestCheckReplacementData *data = d;
938 result = g_regex_check_replacement (data->string_to_expand, &has_refs, NULL);
939 g_assert_cmpint (data->expected, ==, result);
942 g_assert_cmpint (data->expected_refs, ==, has_refs);
945 #define TEST_CHECK_REPLACEMENT(_string_to_expand, _expected, _expected_refs) { \
946 TestCheckReplacementData *data; \
948 data = g_new0 (TestCheckReplacementData, 1); \
949 data->string_to_expand = _string_to_expand; \
950 data->expected = _expected; \
951 data->expected_refs = _expected_refs; \
952 path = g_strdup_printf ("/regex/check-repacement/%d", ++total); \
953 g_test_add_data_func (path, data, test_check_replacement); \
958 const gchar *pattern;
960 const gchar *string_to_expand;
962 const gchar *expected;
966 test_expand (gconstpointer d)
968 const TestExpandData *data = d;
969 GRegex *regex = NULL;
970 GMatchInfo *match_info = NULL;
975 regex = g_regex_new (data->pattern, data->raw ? G_REGEX_RAW : 0, 0, NULL);
976 g_regex_match (regex, data->string, 0, &match_info);
979 res = g_match_info_expand_references (match_info, data->string_to_expand, NULL);
980 g_assert_cmpstr (res, ==, data->expected);
982 g_match_info_free (match_info);
984 g_regex_unref (regex);
987 #define TEST_EXPAND(_pattern, _string, _string_to_expand, _raw, _expected) { \
988 TestExpandData *data; \
990 data = g_new0 (TestExpandData, 1); \
991 data->pattern = _pattern; \
992 data->string = _string; \
993 data->string_to_expand = _string_to_expand; \
995 data->expected = _expected; \
996 path = g_strdup_printf ("/regex/expand/%d", ++total); \
997 g_test_add_data_func (path, data, test_expand); \
1002 const gchar *pattern;
1003 const gchar *string;
1004 gint start_position;
1005 const gchar *replacement;
1006 const gchar *expected;
1010 test_replace (gconstpointer d)
1012 const TestReplaceData *data = d;
1016 regex = g_regex_new (data->pattern, 0, 0, NULL);
1017 res = g_regex_replace (regex, data->string, -1, data->start_position, data->replacement, 0, NULL);
1019 g_assert_cmpstr (res, ==, data->expected);
1022 g_regex_unref (regex);
1025 #define TEST_REPLACE(_pattern, _string, _start_position, _replacement, _expected) { \
1026 TestReplaceData *data; \
1028 data = g_new0 (TestReplaceData, 1); \
1029 data->pattern = _pattern; \
1030 data->string = _string; \
1031 data->start_position = _start_position; \
1032 data->replacement = _replacement; \
1033 data->expected = _expected; \
1034 path = g_strdup_printf ("/regex/replace/%d", ++total); \
1035 g_test_add_data_func (path, data, test_replace); \
1040 test_replace_lit (gconstpointer d)
1042 const TestReplaceData *data = d;
1046 regex = g_regex_new (data->pattern, 0, 0, NULL);
1047 res = g_regex_replace_literal (regex, data->string, -1, data->start_position,
1048 data->replacement, 0, NULL);
1049 g_assert_cmpstr (res, ==, data->expected);
1052 g_regex_unref (regex);
1055 #define TEST_REPLACE_LIT(_pattern, _string, _start_position, _replacement, _expected) { \
1056 TestReplaceData *data; \
1058 data = g_new0 (TestReplaceData, 1); \
1059 data->pattern = _pattern; \
1060 data->string = _string; \
1061 data->start_position = _start_position; \
1062 data->replacement = _replacement; \
1063 data->expected = _expected; \
1064 path = g_strdup_printf ("/regex/replace-literally/%d", ++total); \
1065 g_test_add_data_func (path, data, test_replace_lit); \
1070 const gchar *pattern;
1073 } TestStringNumData;
1076 test_get_string_number (gconstpointer d)
1078 const TestStringNumData *data = d;
1082 regex = g_regex_new (data->pattern, 0, 0, NULL);
1083 num = g_regex_get_string_number (regex, data->name);
1085 g_assert_cmpint (num, ==, data->expected_num);
1086 g_regex_unref (regex);
1089 #define TEST_GET_STRING_NUMBER(_pattern, _name, _expected_num) { \
1090 TestStringNumData *data; \
1092 data = g_new0 (TestStringNumData, 1); \
1093 data->pattern = _pattern; \
1094 data->name = _name; \
1095 data->expected_num = _expected_num; \
1096 path = g_strdup_printf ("/regex/string-number/%d", ++total); \
1097 g_test_add_data_func (path, data, test_get_string_number); \
1102 const gchar *string;
1104 const gchar *expected;
1108 test_escape (gconstpointer d)
1110 const TestEscapeData *data = d;
1113 escaped = g_regex_escape_string (data->string, data->length);
1115 g_assert_cmpstr (escaped, ==, data->expected);
1120 #define TEST_ESCAPE(_string, _length, _expected) { \
1121 TestEscapeData *data; \
1123 data = g_new0 (TestEscapeData, 1); \
1124 data->string = _string; \
1125 data->length = _length; \
1126 data->expected = _expected; \
1127 path = g_strdup_printf ("/regex/escape/%d", ++total); \
1128 g_test_add_data_func (path, data, test_escape); \
1133 test_escape_nul (gconstpointer d)
1135 const TestEscapeData *data = d;
1138 escaped = g_regex_escape_nul (data->string, data->length);
1140 g_assert_cmpstr (escaped, ==, data->expected);
1145 #define TEST_ESCAPE_NUL(_string, _length, _expected) { \
1146 TestEscapeData *data; \
1148 data = g_new0 (TestEscapeData, 1); \
1149 data->string = _string; \
1150 data->length = _length; \
1151 data->expected = _expected; \
1152 path = g_strdup_printf ("/regex/escape_nul/%d", ++total); \
1153 g_test_add_data_func (path, data, test_escape_nul); \
1158 const gchar *pattern;
1159 const gchar *string;
1161 gint start_position;
1166 test_match_all_full (gconstpointer d)
1168 const TestMatchAllData *data = d;
1170 GMatchInfo *match_info;
1176 regex = g_regex_new (data->pattern, 0, 0, NULL);
1177 match_ok = g_regex_match_all_full (regex, data->string, data->string_len, data->start_position,
1178 0, &match_info, NULL);
1180 if (g_slist_length (data->expected) == 0)
1181 g_assert (!match_ok);
1183 g_assert (match_ok);
1185 match_count = g_match_info_get_match_count (match_info);
1186 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1188 l_exp = data->expected;
1189 for (i = 0; i < match_count; i++)
1192 gchar *matched_string;
1193 Match *exp = l_exp->data;
1195 matched_string = g_match_info_fetch (match_info, i);
1196 g_match_info_fetch_pos (match_info, i, &start, &end);
1198 g_assert_cmpstr (exp->string, ==, matched_string);
1199 g_assert_cmpint (exp->start, ==, start);
1200 g_assert_cmpint (exp->end, ==, end);
1202 g_free (matched_string);
1204 l_exp = g_slist_next (l_exp);
1207 g_match_info_free (match_info);
1208 g_regex_unref (regex);
1212 test_match_all (gconstpointer d)
1214 const TestMatchAllData *data = d;
1216 GMatchInfo *match_info;
1222 regex = g_regex_new (data->pattern, 0, 0, NULL);
1223 match_ok = g_regex_match_all (regex, data->string, 0, &match_info);
1225 if (g_slist_length (data->expected) == 0)
1226 g_assert (!match_ok);
1228 g_assert (match_ok);
1230 match_count = g_match_info_get_match_count (match_info);
1231 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1233 l_exp = data->expected;
1234 for (i = 0; i < match_count; i++)
1237 gchar *matched_string;
1238 Match *exp = l_exp->data;
1240 matched_string = g_match_info_fetch (match_info, i);
1241 g_match_info_fetch_pos (match_info, i, &start, &end);
1243 g_assert_cmpstr (exp->string, ==, matched_string);
1244 g_assert_cmpint (exp->start, ==, start);
1245 g_assert_cmpint (exp->end, ==, end);
1247 g_free (matched_string);
1249 l_exp = g_slist_next (l_exp);
1252 g_match_info_free (match_info);
1253 g_regex_unref (regex);
1256 #define TEST_MATCH_ALL0(_pattern, _string, _string_len, _start_position) { \
1257 TestMatchAllData *data; \
1259 data = g_new0 (TestMatchAllData, 1); \
1260 data->pattern = _pattern; \
1261 data->string = _string; \
1262 data->string_len = _string_len; \
1263 data->start_position = _start_position; \
1264 data->expected = NULL; \
1265 path = g_strdup_printf ("/regex/match-all-full0/%d", ++total); \
1266 g_test_add_data_func (path, data, test_match_all_full); \
1268 if (_string_len == -1 && _start_position == 0) { \
1269 path = g_strdup_printf ("/regex/match-all0/%d", ++total); \
1270 g_test_add_data_func (path, data, test_match_all); \
1275 #define TEST_MATCH_ALL1(_pattern, _string, _string_len, _start_position, \
1277 TestMatchAllData *data; \
1280 data = g_new0 (TestMatchAllData, 1); \
1281 data->pattern = _pattern; \
1282 data->string = _string; \
1283 data->string_len = _string_len; \
1284 data->start_position = _start_position; \
1285 data->expected = NULL; \
1286 match = g_new0 (Match, 1); \
1287 match->string = t1; \
1288 match->start = s1; \
1290 data->expected = g_slist_append (data->expected, match); \
1291 path = g_strdup_printf ("/regex/match-all-full1/%d", ++total); \
1292 g_test_add_data_func (path, data, test_match_all_full); \
1294 if (_string_len == -1 && _start_position == 0) { \
1295 path = g_strdup_printf ("/regex/match-all1/%d", ++total); \
1296 g_test_add_data_func (path, data, test_match_all); \
1301 #define TEST_MATCH_ALL2(_pattern, _string, _string_len, _start_position, \
1302 t1, s1, e1, t2, s2, e2) { \
1303 TestMatchAllData *data; \
1306 data = g_new0 (TestMatchAllData, 1); \
1307 data->pattern = _pattern; \
1308 data->string = _string; \
1309 data->string_len = _string_len; \
1310 data->start_position = _start_position; \
1311 data->expected = NULL; \
1312 match = g_new0 (Match, 1); \
1313 match->string = t1; \
1314 match->start = s1; \
1316 data->expected = g_slist_append (data->expected, match); \
1317 match = g_new0 (Match, 1); \
1318 match->string = t2; \
1319 match->start = s2; \
1321 data->expected = g_slist_append (data->expected, match); \
1322 path = g_strdup_printf ("/regex/match-all-full2/%d", ++total); \
1323 g_test_add_data_func (path, data, test_match_all_full); \
1325 if (_string_len == -1 && _start_position == 0) { \
1326 path = g_strdup_printf ("/regex/match-all2/%d", ++total); \
1327 g_test_add_data_func (path, data, test_match_all); \
1332 #define TEST_MATCH_ALL3(_pattern, _string, _string_len, _start_position, \
1333 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
1334 TestMatchAllData *data; \
1337 data = g_new0 (TestMatchAllData, 1); \
1338 data->pattern = _pattern; \
1339 data->string = _string; \
1340 data->string_len = _string_len; \
1341 data->start_position = _start_position; \
1342 data->expected = NULL; \
1343 match = g_new0 (Match, 1); \
1344 match->string = t1; \
1345 match->start = s1; \
1347 data->expected = g_slist_append (data->expected, match); \
1348 match = g_new0 (Match, 1); \
1349 match->string = t2; \
1350 match->start = s2; \
1352 data->expected = g_slist_append (data->expected, match); \
1353 match = g_new0 (Match, 1); \
1354 match->string = t3; \
1355 match->start = s3; \
1357 data->expected = g_slist_append (data->expected, match); \
1358 path = g_strdup_printf ("/regex/match-all-full3/%d", ++total); \
1359 g_test_add_data_func (path, data, test_match_all_full); \
1361 if (_string_len == -1 && _start_position == 0) { \
1362 path = g_strdup_printf ("/regex/match-all3/%d", ++total); \
1363 g_test_add_data_func (path, data, test_match_all); \
1368 #define PCRE_UTF8 0x00000800
1369 #define PCRE_NO_UTF8_CHECK 0x00002000
1370 #define PCRE_NEWLINE_ANY 0x00400000
1371 #define PCRE_UCP 0x20000000
1376 GRegexCompileFlags cflags = G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE;
1377 GRegexMatchFlags mflags = G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL;
1380 regex = g_regex_new ("[A-Z]+", cflags, mflags, NULL);
1382 g_assert (regex != NULL);
1383 g_assert_cmpint (g_regex_get_compile_flags (regex), ==, cflags|PCRE_UTF8|PCRE_NO_UTF8_CHECK|PCRE_NEWLINE_ANY|PCRE_UCP );
1384 g_assert_cmpint (g_regex_get_match_flags (regex), ==, mflags|PCRE_NO_UTF8_CHECK);
1386 g_regex_unref (regex);
1396 regex = g_regex_new ("a\\", 0, 0, &error);
1397 g_assert (regex == NULL);
1398 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_STRAY_BACKSLASH);
1399 g_clear_error (&error);
1400 regex = g_regex_new ("a\\c", 0, 0, &error);
1401 g_assert (regex == NULL);
1402 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MISSING_CONTROL_CHAR);
1403 g_clear_error (&error);
1404 regex = g_regex_new ("a\\l", 0, 0, &error);
1405 g_assert (regex == NULL);
1406 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNRECOGNIZED_ESCAPE);
1407 g_clear_error (&error);
1408 regex = g_regex_new ("a{4,2}", 0, 0, &error);
1409 g_assert (regex == NULL);
1410 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER);
1411 g_clear_error (&error);
1412 regex = g_regex_new ("a{999999,}", 0, 0, &error);
1413 g_assert (regex == NULL);
1414 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_QUANTIFIER_TOO_BIG);
1415 g_clear_error (&error);
1416 regex = g_regex_new ("[a-z", 0, 0, &error);
1417 g_assert (regex == NULL);
1418 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
1419 g_clear_error (&error);
1421 regex = g_regex_new ("[\\b]", 0, 0, &error);
1422 g_assert (regex == NULL);
1423 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS);
1424 g_clear_error (&error);
1426 regex = g_regex_new ("[z-a]", 0, 0, &error);
1427 g_assert (regex == NULL);
1428 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_RANGE_OUT_OF_ORDER);
1429 g_clear_error (&error);
1430 regex = g_regex_new ("{2,4}", 0, 0, &error);
1431 g_assert (regex == NULL);
1432 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_NOTHING_TO_REPEAT);
1433 g_clear_error (&error);
1434 regex = g_regex_new ("a(?u)", 0, 0, &error);
1435 g_assert (regex == NULL);
1436 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
1437 g_clear_error (&error);
1438 regex = g_regex_new ("a(?<$foo)bar", 0, 0, &error);
1439 g_assert (regex == NULL);
1440 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
1441 g_clear_error (&error);
1442 regex = g_regex_new ("a[:alpha:]b", 0, 0, &error);
1443 g_assert (regex == NULL);
1444 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS);
1445 g_clear_error (&error);
1446 regex = g_regex_new ("a(b", 0, 0, &error);
1447 g_assert (regex == NULL);
1448 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1449 g_clear_error (&error);
1450 regex = g_regex_new ("a)b", 0, 0, &error);
1451 g_assert (regex == NULL);
1452 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1453 g_clear_error (&error);
1454 regex = g_regex_new ("a(?R", 0, 0, &error);
1455 g_assert (regex == NULL);
1456 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1457 g_clear_error (&error);
1458 regex = g_regex_new ("a(?-54", 0, 0, &error);
1459 g_assert (regex == NULL);
1460 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1461 g_clear_error (&error);
1462 regex = g_regex_new ("a(?#abc", 0, 0, &error);
1463 g_assert (regex == NULL);
1464 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNTERMINATED_COMMENT);
1465 g_clear_error (&error);
1466 regex = g_regex_new ("a[[:fubar:]]b", 0, 0, &error);
1467 g_assert (regex == NULL);
1468 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME);
1469 g_clear_error (&error);
1473 test_properties (void)
1482 regex = g_regex_new ("\\p{L}\\p{Ll}\\p{Lu}\\p{L&}\\p{N}\\p{Nd}", G_REGEX_OPTIMIZE, 0, &error);
1483 res = g_regex_match (regex, "ppPP01", 0, &match);
1485 str = g_match_info_fetch (match, 0);
1486 g_assert_cmpstr (str, ==, "ppPP01");
1489 g_match_info_free (match);
1490 g_regex_unref (regex);
1503 regex = g_regex_new ("[abc\\x{0B1E}\\p{Mn}\\x{0391}-\\x{03A9}]", G_REGEX_OPTIMIZE, 0, &error);
1504 res = g_regex_match (regex, "a:b:\340\254\236:\333\253:\316\240", 0, &match);
1506 str = g_match_info_fetch (match, 0);
1507 g_assert_cmpstr (str, ==, "a");
1509 res = g_match_info_next (match, NULL);
1511 str = g_match_info_fetch (match, 0);
1512 g_assert_cmpstr (str, ==, "b");
1514 res = g_match_info_next (match, NULL);
1516 str = g_match_info_fetch (match, 0);
1517 g_assert_cmpstr (str, ==, "\340\254\236");
1519 res = g_match_info_next (match, NULL);
1521 str = g_match_info_fetch (match, 0);
1522 g_assert_cmpstr (str, ==, "\333\253");
1524 res = g_match_info_next (match, NULL);
1526 str = g_match_info_fetch (match, 0);
1527 g_assert_cmpstr (str, ==, "\316\240");
1530 res = g_match_info_next (match, NULL);
1533 g_match_info_free (match);
1534 g_regex_unref (regex);
1537 /* examples for lookahead assertions taken from pcrepattern(3) */
1539 test_lookahead (void)
1549 regex = g_regex_new ("\\w+(?=;)", G_REGEX_OPTIMIZE, 0, &error);
1551 g_assert_no_error (error);
1552 res = g_regex_match (regex, "word1 word2: word3;", 0, &match);
1554 g_assert (g_match_info_matches (match));
1555 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1556 str = g_match_info_fetch (match, 0);
1557 g_assert_cmpstr (str, ==, "word3");
1559 g_match_info_free (match);
1560 g_regex_unref (regex);
1563 regex = g_regex_new ("foo(?!bar)", G_REGEX_OPTIMIZE, 0, &error);
1565 g_assert_no_error (error);
1566 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1568 g_assert (g_match_info_matches (match));
1569 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1570 res = g_match_info_fetch_pos (match, 0, &start, &end);
1572 g_assert_cmpint (start, ==, 7);
1573 g_assert_cmpint (end, ==, 10);
1574 g_match_info_free (match);
1575 g_regex_unref (regex);
1578 regex = g_regex_new ("(?!bar)foo", G_REGEX_OPTIMIZE, 0, &error);
1580 g_assert_no_error (error);
1581 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1583 g_assert (g_match_info_matches (match));
1584 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1585 res = g_match_info_fetch_pos (match, 0, &start, &end);
1587 g_assert_cmpint (start, ==, 0);
1588 g_assert_cmpint (end, ==, 3);
1589 res = g_match_info_next (match, &error);
1591 g_assert_no_error (error);
1592 res = g_match_info_fetch_pos (match, 0, &start, &end);
1594 g_assert_cmpint (start, ==, 7);
1595 g_assert_cmpint (end, ==, 10);
1596 g_match_info_free (match);
1597 g_regex_unref (regex);
1600 /* examples for lookbehind assertions taken from pcrepattern(3) */
1602 test_lookbehind (void)
1611 regex = g_regex_new ("(?<!foo)bar", G_REGEX_OPTIMIZE, 0, &error);
1613 g_assert_no_error (error);
1614 res = g_regex_match (regex, "foobar boobar", 0, &match);
1616 g_assert (g_match_info_matches (match));
1617 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1618 res = g_match_info_fetch_pos (match, 0, &start, &end);
1620 g_assert_cmpint (start, ==, 10);
1621 g_assert_cmpint (end, ==, 13);
1622 g_match_info_free (match);
1623 g_regex_unref (regex);
1626 regex = g_regex_new ("(?<=bullock|donkey) poo", G_REGEX_OPTIMIZE, 0, &error);
1628 g_assert_no_error (error);
1629 res = g_regex_match (regex, "don poo, and bullock poo", 0, &match);
1631 g_assert (g_match_info_matches (match));
1632 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1633 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1635 g_assert_cmpint (start, ==, 20);
1636 g_match_info_free (match);
1637 g_regex_unref (regex);
1639 regex = g_regex_new ("(?<!dogs?|cats?) x", G_REGEX_OPTIMIZE, 0, &error);
1640 g_assert (regex == NULL);
1641 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1642 g_clear_error (&error);
1644 regex = g_regex_new ("(?<=ab(c|de)) foo", G_REGEX_OPTIMIZE, 0, &error);
1645 g_assert (regex == NULL);
1646 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1647 g_clear_error (&error);
1649 regex = g_regex_new ("(?<=abc|abde)foo", G_REGEX_OPTIMIZE, 0, &error);
1651 g_assert_no_error (error);
1652 res = g_regex_match (regex, "abfoo, abdfoo, abcfoo", 0, &match);
1654 g_assert (g_match_info_matches (match));
1655 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1657 g_assert_cmpint (start, ==, 18);
1658 g_match_info_free (match);
1659 g_regex_unref (regex);
1661 regex = g_regex_new ("^.*+(?<=abcd)", G_REGEX_OPTIMIZE, 0, &error);
1663 g_assert_no_error (error);
1664 res = g_regex_match (regex, "abcabcabcabcabcabcabcabcabcd", 0, &match);
1666 g_assert (g_match_info_matches (match));
1667 g_match_info_free (match);
1668 g_regex_unref (regex);
1670 regex = g_regex_new ("(?<=\\d{3})(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1672 g_assert_no_error (error);
1673 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 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, ==, 20);
1679 g_match_info_free (match);
1680 g_regex_unref (regex);
1682 regex = g_regex_new ("(?<=\\d{3}...)(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1684 g_assert_no_error (error);
1685 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1687 g_assert (g_match_info_matches (match));
1688 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1690 g_assert_cmpint (start, ==, 13);
1691 g_match_info_free (match);
1692 g_regex_unref (regex);
1694 regex = g_regex_new ("(?<=\\d{3}(?!999)...)foo", G_REGEX_OPTIMIZE, 0, &error);
1696 g_assert_no_error (error);
1697 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1699 g_assert (g_match_info_matches (match));
1700 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1702 g_assert_cmpint (start, ==, 13);
1703 g_match_info_free (match);
1704 g_regex_unref (regex);
1706 regex = g_regex_new ("(?<=(?<!foo)bar)baz", G_REGEX_OPTIMIZE, 0, &error);
1708 g_assert_no_error (error);
1709 res = g_regex_match (regex, "foobarbaz barfoobaz barbarbaz", 0, &match);
1711 g_assert (g_match_info_matches (match));
1712 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1714 g_assert_cmpint (start, ==, 26);
1715 g_match_info_free (match);
1716 g_regex_unref (regex);
1719 /* examples for subpatterns taken from pcrepattern(3) */
1721 test_subpattern (void)
1731 regex = g_regex_new ("cat(aract|erpillar|)", G_REGEX_OPTIMIZE, 0, &error);
1733 g_assert_no_error (error);
1734 res = g_regex_match_all (regex, "caterpillar", 0, &match);
1736 g_assert (g_match_info_matches (match));
1737 g_assert_cmpint (g_match_info_get_match_count (match), ==, 2);
1738 str = g_match_info_fetch (match, 0);
1739 g_assert_cmpstr (str, ==, "caterpillar");
1741 str = g_match_info_fetch (match, 1);
1742 g_assert_cmpstr (str, ==, "cat");
1744 g_match_info_free (match);
1745 g_regex_unref (regex);
1747 regex = g_regex_new ("the ((red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1749 g_assert_no_error (error);
1750 res = g_regex_match (regex, "the red king", 0, &match);
1752 g_assert (g_match_info_matches (match));
1753 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1754 str = g_match_info_fetch (match, 0);
1755 g_assert_cmpstr (str, ==, "the red king");
1757 str = g_match_info_fetch (match, 1);
1758 g_assert_cmpstr (str, ==, "red king");
1760 str = g_match_info_fetch (match, 2);
1761 g_assert_cmpstr (str, ==, "red");
1763 str = g_match_info_fetch (match, 3);
1764 g_assert_cmpstr (str, ==, "king");
1766 g_match_info_free (match);
1767 g_regex_unref (regex);
1769 regex = g_regex_new ("the ((?:red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1771 g_assert_no_error (error);
1772 res = g_regex_match (regex, "the white queen", 0, &match);
1774 g_assert (g_match_info_matches (match));
1775 g_assert_cmpint (g_match_info_get_match_count (match), ==, 3);
1776 str = g_match_info_fetch (match, 0);
1777 g_assert_cmpstr (str, ==, "the white queen");
1779 str = g_match_info_fetch (match, 1);
1780 g_assert_cmpstr (str, ==, "white queen");
1782 str = g_match_info_fetch (match, 2);
1783 g_assert_cmpstr (str, ==, "queen");
1785 g_match_info_free (match);
1786 g_regex_unref (regex);
1788 regex = g_regex_new ("(?|(Sat)(ur)|(Sun))day (morning|afternoon)", G_REGEX_OPTIMIZE, 0, &error);
1790 g_assert_no_error (error);
1791 res = g_regex_match (regex, "Saturday morning", 0, &match);
1793 g_assert (g_match_info_matches (match));
1794 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1795 str = g_match_info_fetch (match, 1);
1796 g_assert_cmpstr (str, ==, "Sat");
1798 str = g_match_info_fetch (match, 2);
1799 g_assert_cmpstr (str, ==, "ur");
1801 str = g_match_info_fetch (match, 3);
1802 g_assert_cmpstr (str, ==, "morning");
1804 g_match_info_free (match);
1805 g_regex_unref (regex);
1807 regex = g_regex_new ("(?|(abc)|(def))\\1", G_REGEX_OPTIMIZE, 0, &error);
1809 g_assert_no_error (error);
1810 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1812 g_assert (g_match_info_matches (match));
1813 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1815 g_assert_cmpint (start, ==, 0);
1816 res = g_match_info_next (match, &error);
1818 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1820 g_assert_cmpint (start, ==, 21);
1821 g_match_info_free (match);
1822 g_regex_unref (regex);
1824 regex = g_regex_new ("(?|(abc)|(def))(?1)", G_REGEX_OPTIMIZE, 0, &error);
1826 g_assert_no_error (error);
1827 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1829 g_assert (g_match_info_matches (match));
1830 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1832 g_assert_cmpint (start, ==, 0);
1833 res = g_match_info_next (match, &error);
1835 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1837 g_assert_cmpint (start, ==, 14);
1838 g_match_info_free (match);
1839 g_regex_unref (regex);
1841 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);
1843 g_assert_no_error (error);
1844 res = g_regex_match (regex, "Mon Tuesday Wed Saturday", 0, &match);
1846 g_assert (g_match_info_matches (match));
1847 str = g_match_info_fetch_named (match, "DN");
1848 g_assert_cmpstr (str, ==, "Mon");
1850 res = g_match_info_next (match, &error);
1852 str = g_match_info_fetch_named (match, "DN");
1853 g_assert_cmpstr (str, ==, "Tue");
1855 res = g_match_info_next (match, &error);
1857 str = g_match_info_fetch_named (match, "DN");
1858 g_assert_cmpstr (str, ==, "Wed");
1860 res = g_match_info_next (match, &error);
1862 str = g_match_info_fetch_named (match, "DN");
1863 g_assert_cmpstr (str, ==, "Sat");
1865 g_match_info_free (match);
1866 g_regex_unref (regex);
1868 regex = g_regex_new ("^(a|b\\1)+$", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1870 g_assert_no_error (error);
1871 res = g_regex_match (regex, "aaaaaaaaaaaaaaaa", 0, &match);
1873 g_assert (g_match_info_matches (match));
1874 g_match_info_free (match);
1875 res = g_regex_match (regex, "ababbaa", 0, &match);
1877 g_assert (g_match_info_matches (match));
1878 g_match_info_free (match);
1879 g_regex_unref (regex);
1882 /* examples for conditions taken from pcrepattern(3) */
1884 test_condition (void)
1892 regex = g_regex_new ("^(a+)(\\()?[^()]+(?(-1)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1894 g_assert_no_error (error);
1895 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1897 g_assert (g_match_info_matches (match));
1898 g_match_info_free (match);
1899 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1901 g_assert (g_match_info_matches (match));
1902 g_match_info_free (match);
1903 g_regex_unref (regex);
1906 regex = g_regex_new ("^(a+)(?<OPEN>\\()?[^()]+(?(<OPEN>)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1908 g_assert_no_error (error);
1909 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1911 g_assert (g_match_info_matches (match));
1912 g_match_info_free (match);
1913 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1915 g_assert (g_match_info_matches (match));
1916 g_match_info_free (match);
1917 g_regex_unref (regex);
1919 regex = g_regex_new ("^(a+)(?(+1)\\[|\\<)?[^()]+(\\])?(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1921 g_assert_no_error (error);
1922 res = g_regex_match (regex, "a[zzzzzz]b", 0, &match);
1924 g_assert (g_match_info_matches (match));
1925 g_match_info_free (match);
1926 res = g_regex_match (regex, "aaa<zzzzzzbbb", 0, &match);
1928 g_assert (g_match_info_matches (match));
1929 g_match_info_free (match);
1930 g_regex_unref (regex);
1932 regex = g_regex_new ("(?(DEFINE) (?<byte> 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d) )"
1933 "\\b (?&byte) (\\.(?&byte)){3} \\b",
1934 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1936 g_assert_no_error (error);
1937 res = g_regex_match (regex, "128.0.0.1", 0, &match);
1939 g_assert (g_match_info_matches (match));
1940 g_match_info_free (match);
1941 res = g_regex_match (regex, "192.168.1.1", 0, &match);
1943 g_assert (g_match_info_matches (match));
1944 g_match_info_free (match);
1945 res = g_regex_match (regex, "209.132.180.167", 0, &match);
1947 g_assert (g_match_info_matches (match));
1948 g_match_info_free (match);
1949 g_regex_unref (regex);
1951 regex = g_regex_new ("^(?(?=[^a-z]*[a-z])"
1952 "\\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )$",
1953 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1955 g_assert_no_error (error);
1956 res = g_regex_match (regex, "01-abc-24", 0, &match);
1958 g_assert (g_match_info_matches (match));
1959 g_match_info_free (match);
1960 res = g_regex_match (regex, "01-23-45", 0, &match);
1962 g_assert (g_match_info_matches (match));
1963 g_match_info_free (match);
1964 res = g_regex_match (regex, "01-uv-45", 0, &match);
1966 g_assert (!g_match_info_matches (match));
1967 g_match_info_free (match);
1968 res = g_regex_match (regex, "01-234-45", 0, &match);
1970 g_assert (!g_match_info_matches (match));
1971 g_match_info_free (match);
1972 g_regex_unref (regex);
1975 /* examples for recursion taken from pcrepattern(3) */
1977 test_recursion (void)
1986 regex = g_regex_new ("\\( ( [^()]++ | (?R) )* \\)", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1988 g_assert_no_error (error);
1989 res = g_regex_match (regex, "(middle)", 0, &match);
1991 g_assert (g_match_info_matches (match));
1992 g_match_info_free (match);
1993 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
1995 g_assert (g_match_info_matches (match));
1996 g_match_info_free (match);
1997 res = g_regex_match (regex, "(((xxx(((", 0, &match);
1999 g_assert (!g_match_info_matches (match));
2000 g_match_info_free (match);
2001 g_regex_unref (regex);
2003 regex = g_regex_new ("^( \\( ( [^()]++ | (?1) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2005 g_assert_no_error (error);
2006 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
2008 g_assert (g_match_info_matches (match));
2009 g_match_info_free (match);
2010 res = g_regex_match (regex, "(((xxx((()", 0, &match);
2012 g_assert (!g_match_info_matches (match));
2013 g_match_info_free (match);
2014 g_regex_unref (regex);
2016 regex = g_regex_new ("^(?<pn> \\( ( [^()]++ | (?&pn) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2018 g_assert_no_error (error);
2019 g_regex_match (regex, "(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()", 0, &match);
2021 g_assert (!g_match_info_matches (match));
2022 g_match_info_free (match);
2023 g_regex_unref (regex);
2025 regex = g_regex_new ("< (?: (?(R) \\d++ | [^<>]*+) | (?R)) * >", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2027 g_assert_no_error (error);
2028 res = g_regex_match (regex, "<ab<01<23<4>>>>", 0, &match);
2030 g_assert (g_match_info_matches (match));
2031 res = g_match_info_fetch_pos (match, 0, &start, NULL);
2033 g_assert_cmpint (start, ==, 0);
2034 g_match_info_free (match);
2035 res = g_regex_match (regex, "<ab<01<xx<x>>>>", 0, &match);
2037 g_assert (g_match_info_matches (match));
2038 res = g_match_info_fetch_pos (match, 0, &start, NULL);
2040 g_assert_cmpint (start, >, 0);
2041 g_match_info_free (match);
2042 g_regex_unref (regex);
2044 regex = g_regex_new ("^((.)(?1)\\2|.)$", G_REGEX_OPTIMIZE, 0, &error);
2046 g_assert_no_error (error);
2047 res = g_regex_match (regex, "abcdcba", 0, &match);
2049 g_assert (g_match_info_matches (match));
2050 g_match_info_free (match);
2051 res = g_regex_match (regex, "abcddcba", 0, &match);
2053 g_assert (!g_match_info_matches (match));
2054 g_match_info_free (match);
2055 g_regex_unref (regex);
2057 regex = g_regex_new ("^(?:((.)(?1)\\2|)|((.)(?3)\\4|.))$", G_REGEX_OPTIMIZE, 0, &error);
2059 g_assert_no_error (error);
2060 res = g_regex_match (regex, "abcdcba", 0, &match);
2062 g_assert (g_match_info_matches (match));
2063 g_match_info_free (match);
2064 res = g_regex_match (regex, "abcddcba", 0, &match);
2066 g_assert (g_match_info_matches (match));
2067 g_match_info_free (match);
2068 g_regex_unref (regex);
2070 regex = g_regex_new ("^\\W*+(?:((.)\\W*+(?1)\\W*+\\2|)|((.)\\W*+(?3)\\W*+\\4|\\W*+.\\W*+))\\W*+$", G_REGEX_OPTIMIZE|G_REGEX_CASELESS, 0, &error);
2072 g_assert_no_error (error);
2073 res = g_regex_match (regex, "abcdcba", 0, &match);
2075 g_assert (g_match_info_matches (match));
2076 g_match_info_free (match);
2077 res = g_regex_match (regex, "A man, a plan, a canal: Panama!", 0, &match);
2079 g_assert (g_match_info_matches (match));
2080 g_match_info_free (match);
2081 res = g_regex_match (regex, "Oozy rat in a sanitary zoo", 0, &match);
2083 g_assert (g_match_info_matches (match));
2084 g_match_info_free (match);
2085 g_regex_unref (regex);
2089 test_multiline (void)
2095 g_test_bug ("640489");
2097 regex = g_regex_new ("^a$", G_REGEX_MULTILINE|G_REGEX_DOTALL, 0, NULL);
2100 g_regex_match (regex, "a\nb\na", 0, &info);
2101 while (g_match_info_matches (info))
2104 g_match_info_next (info, NULL);
2106 g_match_info_free (info);
2107 g_regex_unref (regex);
2109 g_assert_cmpint (count, ==, 2);
2113 main (int argc, char *argv[])
2115 setlocale (LC_ALL, "");
2117 g_test_init (&argc, &argv, NULL);
2119 g_test_bug_base ("http://bugzilla.gnome.org/");
2121 g_test_add_func ("/regex/basic", test_basic);
2122 g_test_add_func ("/regex/compile", test_compile);
2123 g_test_add_func ("/regex/properties", test_properties);
2124 g_test_add_func ("/regex/class", test_class);
2125 g_test_add_func ("/regex/lookahead", test_lookahead);
2126 g_test_add_func ("/regex/lookbehind", test_lookbehind);
2127 g_test_add_func ("/regex/subpattern", test_subpattern);
2128 g_test_add_func ("/regex/condition", test_condition);
2129 g_test_add_func ("/regex/recursion", test_recursion);
2130 g_test_add_func ("/regex/multiline", test_multiline);
2132 /* TEST_NEW(pattern, compile_opts, match_opts) */
2134 TEST_NEW(".*", 0, 0);
2135 TEST_NEW(".*", G_REGEX_OPTIMIZE, 0);
2136 TEST_NEW(".*", G_REGEX_MULTILINE, 0);
2137 TEST_NEW(".*", G_REGEX_DOTALL, 0);
2138 TEST_NEW(".*", G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL);
2139 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
2140 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS, 0);
2141 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0);
2142 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES, 0);
2143 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0);
2144 /* This gives "internal error: code overflow" with pcre 6.0 */
2145 TEST_NEW("(?i)(?-i)", 0, 0);
2147 /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */
2148 TEST_NEW_FAIL("(", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2149 TEST_NEW_FAIL(")", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2150 TEST_NEW_FAIL("[", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2151 TEST_NEW_FAIL("*", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2152 TEST_NEW_FAIL("?", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2153 TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2155 /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
2156 TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE);
2157 TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE);
2158 TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE);
2159 TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE);
2160 TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED, 0, FALSE);
2161 TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED, FALSE);
2162 TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED, 0, TRUE);
2163 TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED, TRUE);
2164 TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS, 0, TRUE);
2165 TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS, 0, TRUE);
2166 /* These are needed to test extended properties. */
2167 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE);
2168 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE);
2169 TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE);
2170 TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE);
2171 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE, 0, 0, TRUE);
2172 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER, 0, 0, TRUE);
2173 TEST_MATCH_SIMPLE("\\p{L}", SHEEN, 0, 0, TRUE);
2174 TEST_MATCH_SIMPLE("\\p{L}", ETH30, 0, 0, FALSE);
2175 TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE);
2176 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE, 0, 0, TRUE);
2177 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER, 0, 0, FALSE);
2178 TEST_MATCH_SIMPLE("\\p{Ll}", ETH30, 0, 0, FALSE);
2179 TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE, 0, 0, FALSE);
2180 TEST_MATCH_SIMPLE("\\p{Sc}", EURO, 0, 0, TRUE);
2181 TEST_MATCH_SIMPLE("\\p{Sc}", ETH30, 0, 0, FALSE);
2182 TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE);
2183 TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE);
2184 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE, 0, 0, FALSE);
2185 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER, 0, 0, FALSE);
2186 TEST_MATCH_SIMPLE("\\p{N}", SHEEN, 0, 0, FALSE);
2187 TEST_MATCH_SIMPLE("\\p{N}", ETH30, 0, 0, TRUE);
2188 TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE);
2189 TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE);
2190 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE, 0, 0, FALSE);
2191 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER, 0, 0, FALSE);
2192 TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN, 0, 0, FALSE);
2193 TEST_MATCH_SIMPLE("\\p{Nd}", ETH30, 0, 0, FALSE);
2194 TEST_MATCH_SIMPLE("\\p{Common}", SHEEN, 0, 0, FALSE);
2195 TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE);
2196 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE, 0, 0, FALSE);
2197 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER, 0, 0, FALSE);
2198 TEST_MATCH_SIMPLE("\\p{Common}", ETH30, 0, 0, FALSE);
2199 TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE);
2200 TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE);
2201 TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN, 0, 0, TRUE);
2202 TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE);
2203 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE, 0, 0, FALSE);
2204 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER, 0, 0, FALSE);
2205 TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30, 0, 0, FALSE);
2206 TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE);
2207 TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE);
2208 TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN, 0, 0, FALSE);
2209 TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE);
2210 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE, 0, 0, TRUE);
2211 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER, 0, 0, TRUE);
2212 TEST_MATCH_SIMPLE("\\p{Latin}", ETH30, 0, 0, FALSE);
2213 TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE);
2214 TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE);
2215 TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN, 0, 0, FALSE);
2216 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE);
2217 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE, 0, 0, FALSE);
2218 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER, 0, 0, FALSE);
2219 TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30, 0, 0, TRUE);
2220 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE);
2221 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE);
2222 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN, 0, 0, TRUE);
2223 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN, 0, 0, FALSE);
2224 /* Invalid patterns. */
2225 TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE);
2226 TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE);
2228 /* TEST_MATCH(pattern, compile_opts, match_opts, string,
2229 * string_len, start_position, match_opts2, expected) */
2230 TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
2231 TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE);
2232 TEST_MATCH("a", G_REGEX_CASELESS, 0, "A", -1, 0, 0, TRUE);
2233 TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE);
2234 TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE);
2235 TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE);
2236 TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE);
2237 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "a", -1, 0, 0, TRUE);
2238 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ab", -1, 1, 0, FALSE);
2239 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ba", 1, 0, 0, FALSE);
2240 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "bab", -1, 0, 0, FALSE);
2241 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "b", -1, 0, 0, FALSE);
2242 TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED, TRUE);
2243 TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED, FALSE);
2244 TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED, FALSE);
2245 TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED, FALSE);
2246 TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED, FALSE);
2247 TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE);
2248 TEST_MATCH("\\d", 0, 0, EURO, -1, 0, 0, FALSE);
2249 TEST_MATCH("^.$", 0, 0, EURO, -1, 0, 0, TRUE);
2250 TEST_MATCH("^.{3}$", 0, 0, EURO, -1, 0, 0, FALSE);
2251 TEST_MATCH("^.$", G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE);
2252 TEST_MATCH("^.{3}$", G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE);
2253 TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE);
2255 /* New lines handling. */
2256 TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2257 TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE);
2258 TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE);
2259 TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE);
2260 TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE);
2261 TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE);
2262 TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2264 TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE);
2265 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\nb\nc", -1, 0, 0, TRUE);
2266 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2267 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\rb\rc", -1, 0, 0, TRUE);
2268 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc", -1, 0, 0, FALSE);
2269 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc", -1, 0, 0, TRUE);
2270 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc", -1, 0, 0, FALSE);
2271 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2272 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2273 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2274 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc", -1, 0, 0, TRUE);
2275 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2276 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2277 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc", -1, 0, 0, FALSE);
2278 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2279 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc", -1, 0, 0, FALSE);
2280 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2281 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2282 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2283 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc", -1, 0, 0, TRUE);
2284 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2285 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2287 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc", -1, 0, 0, TRUE);
2288 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc", -1, 0, 0, TRUE);
2289 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2290 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2291 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2292 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2293 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2295 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2296 TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2297 TEST_MATCH("a#\rb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2298 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a", -1, 0, 0, FALSE);
2299 TEST_MATCH("a#\nb", G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a", -1, 0, 0, TRUE);
2301 /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
2302 TEST_MATCH_NEXT0("a", "x", -1, 0);
2303 TEST_MATCH_NEXT0("a", "ax", -1, 1);
2304 TEST_MATCH_NEXT0("a", "xa", 1, 0);
2305 TEST_MATCH_NEXT0("a", "axa", 1, 2);
2306 TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
2307 TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
2308 TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5);
2309 TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
2310 TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
2311 TEST_MATCH_NEXT2(EURO "*", EURO EURO, -1, 0, EURO EURO, 0, 6, "", 6, 6);
2312 TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
2313 TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
2314 TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
2315 TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
2316 TEST_MATCH_NEXT2(EURO "+", EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7);
2317 TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
2318 TEST_MATCH_NEXT3("", AGRAVE "b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
2319 TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
2320 TEST_MATCH_NEXT3("a", "aa" OGRAVE "a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
2321 TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
2322 TEST_MATCH_NEXT3("(?=[A-Z0-9])", "RegExTest", -1, 0, "", 0, 0, "", 3, 3, "", 5, 5);
2323 TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
2325 /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
2326 TEST_MATCH_COUNT("a", "", 0, 0, 0);
2327 TEST_MATCH_COUNT("a", "a", 0, 0, 1);
2328 TEST_MATCH_COUNT("a", "a", 1, 0, 0);
2329 TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
2330 TEST_MATCH_COUNT("(.)", EURO, 0, 0, 2);
2331 TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
2332 TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
2333 TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL, 0);
2334 TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
2335 TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
2337 /* TEST_PARTIAL(pattern, string, expected) */
2338 TEST_PARTIAL("^ab", "a", TRUE);
2339 TEST_PARTIAL("^ab", "xa", FALSE);
2340 TEST_PARTIAL("ab", "xa", TRUE);
2341 TEST_PARTIAL("ab", "ab", FALSE); /* normal match. */
2342 TEST_PARTIAL("a+b", "aa", TRUE);
2343 TEST_PARTIAL("(a)+b", "aa", TRUE);
2344 TEST_PARTIAL("a?b", "a", TRUE);
2346 /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
2347 * expected_start, expected_end) */
2348 TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
2349 TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
2350 TEST_SUB_PATTERN("a(.)", "a" EURO, 0, 1, EURO, 1, 4);
2351 TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG, 0, 2, ENG, 3, 5);
2352 TEST_SUB_PATTERN("(" HSTROKE ")", "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3);
2353 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2354 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2355 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
2356 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
2357 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
2359 /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
2360 * expected_sub, expected_start, expected_end) */
2361 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
2362 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
2363 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "A", "b", 4, 5);
2364 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "B", NULL, UNTOUCHED, UNTOUCHED);
2365 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "C", NULL, UNTOUCHED, UNTOUCHED);
2366 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "A", EGRAVE, 1, 3);
2367 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "B", "x", 3, 4);
2368 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
2369 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
2371 /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
2372 * expected_sub, expected_start, expected_end) */
2373 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2374 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2375 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2376 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2377 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2379 /* DUPNAMES option inside the pattern */
2380 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2381 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2382 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2383 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2384 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2386 /* TEST_FETCH_ALL#(pattern, string, ...) */
2387 TEST_FETCH_ALL0("a", "");
2388 TEST_FETCH_ALL0("a", "b");
2389 TEST_FETCH_ALL1("a", "a", "a");
2390 TEST_FETCH_ALL1("a+", "aa", "aa");
2391 TEST_FETCH_ALL1("(?:a)", "a", "a");
2392 TEST_FETCH_ALL2("(a)", "a", "a", "a");
2393 TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
2394 TEST_FETCH_ALL2("a(.)", "a" HSTROKE, "a" HSTROKE, HSTROKE);
2395 TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
2396 TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
2397 TEST_FETCH_ALL3("(?P<A>.)(a)", ENG "a", ENG "a", ENG, "a");
2398 TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
2399 TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
2401 /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
2402 TEST_SPLIT_SIMPLE0("", "");
2403 TEST_SPLIT_SIMPLE0("a", "");
2404 TEST_SPLIT_SIMPLE1(",", "a", "a");
2405 TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
2406 TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
2407 TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
2408 TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
2409 TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
2410 TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
2411 TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
2412 TEST_SPLIT_SIMPLE2("\\s", "ab c", "ab", "c");
2413 TEST_SPLIT_SIMPLE3("\\s*", "ab c", "a", "b", "c");
2414 /* Not matched sub-strings. */
2415 TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
2416 TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
2417 /* Empty matches. */
2418 TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
2419 TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
2420 /* Invalid patterns. */
2421 TEST_SPLIT_SIMPLE0("\\", "");
2422 TEST_SPLIT_SIMPLE0("[", "");
2424 /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
2425 TEST_SPLIT0("", "", 0, 0);
2426 TEST_SPLIT0("a", "", 0, 0);
2427 TEST_SPLIT0("a", "", 0, 1);
2428 TEST_SPLIT0("a", "", 0, 2);
2429 TEST_SPLIT0("a", "a", 1, 0);
2430 TEST_SPLIT1(",", "a", 0, 0, "a");
2431 TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
2432 TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
2433 TEST_SPLIT1(",", "a,b", 2, 0, "b");
2434 TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
2435 TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
2436 TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
2437 TEST_SPLIT2(",", "a,", 0, 0, "a", "");
2438 TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
2439 TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
2440 TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
2441 TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
2442 TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
2443 /* Not matched sub-strings. */
2444 TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
2445 TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
2446 /* Empty matches. */
2447 TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
2448 TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
2449 TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
2450 TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
2451 TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
2452 TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
2453 TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
2455 /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
2456 TEST_CHECK_REPLACEMENT("", TRUE, FALSE);
2457 TEST_CHECK_REPLACEMENT("a", TRUE, FALSE);
2458 TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE, FALSE);
2459 TEST_CHECK_REPLACEMENT("\\0", TRUE, TRUE);
2460 TEST_CHECK_REPLACEMENT("\\n\\2", TRUE, TRUE);
2461 TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE, TRUE);
2462 /* Invalid strings */
2463 TEST_CHECK_REPLACEMENT("\\Q", FALSE, FALSE);
2464 TEST_CHECK_REPLACEMENT("x\\Ay", FALSE, FALSE);
2466 /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
2467 TEST_EXPAND("a", "a", "", FALSE, "");
2468 TEST_EXPAND("a", "a", "\\0", FALSE, "a");
2469 TEST_EXPAND("a", "a", "\\1", FALSE, "");
2470 TEST_EXPAND("(a)", "ab", "\\1", FALSE, "a");
2471 TEST_EXPAND("(a)", "a", "\\1", FALSE, "a");
2472 TEST_EXPAND("(a)", "a", "\\g<1>", FALSE, "a");
2473 TEST_EXPAND("a", "a", "\\0130", FALSE, "X");
2474 TEST_EXPAND("a", "a", "\\\\\\0", FALSE, "\\a");
2475 TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE, "XbX");
2476 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE, "a");
2477 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE, "a");
2478 TEST_EXPAND(".", EURO, "\\0", FALSE, EURO);
2479 TEST_EXPAND("(.)", EURO, "\\1", FALSE, EURO);
2480 TEST_EXPAND("(?P<G>.)", EURO, "\\g<G>", FALSE, EURO);
2481 TEST_EXPAND(".", "a", EURO, FALSE, EURO);
2482 TEST_EXPAND(".", "a", EURO "\\0", FALSE, EURO "a");
2483 TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE, "abc");
2484 TEST_EXPAND(".", "", "\\LaB\\EC", FALSE, "abC");
2485 TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE, "ABc");
2486 TEST_EXPAND(".", "", "a\\ubc", FALSE, "aBc");
2487 TEST_EXPAND(".", "", "a\\lbc", FALSE, "abc");
2488 TEST_EXPAND(".", "", "A\\uBC", FALSE, "ABC");
2489 TEST_EXPAND(".", "", "A\\lBC", FALSE, "AbC");
2490 TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE, "A\\BC");
2491 TEST_EXPAND(".", "", "\\L" AGRAVE "\\E", FALSE, AGRAVE);
2492 TEST_EXPAND(".", "", "\\U" AGRAVE "\\E", FALSE, AGRAVE_UPPER);
2493 TEST_EXPAND(".", "", "\\u" AGRAVE "a", FALSE, AGRAVE_UPPER "a");
2494 TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE, "xAYz");
2495 TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE, "xbyz");
2496 TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE, "xAyz");
2497 TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE, "xbyz");
2498 TEST_EXPAND(".(.)", "a" AGRAVE_UPPER, "x\\l\\1y", FALSE, "x" AGRAVE "y");
2499 TEST_EXPAND("a", "bab", "\\x{61}", FALSE, "a");
2500 TEST_EXPAND("a", "bab", "\\x61", FALSE, "a");
2501 TEST_EXPAND("a", "bab", "\\x5a", FALSE, "Z");
2502 TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE, "aZ");
2503 TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE, "Z");
2504 TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE, AGRAVE);
2505 TEST_EXPAND("", "bab", "\\x{0634}", FALSE, SHEEN);
2506 TEST_EXPAND("", "bab", "\\x{634}", FALSE, SHEEN);
2507 TEST_EXPAND("", "", "\\t", FALSE, "\t");
2508 TEST_EXPAND("", "", "\\v", FALSE, "\v");
2509 TEST_EXPAND("", "", "\\r", FALSE, "\r");
2510 TEST_EXPAND("", "", "\\n", FALSE, "\n");
2511 TEST_EXPAND("", "", "\\f", FALSE, "\f");
2512 TEST_EXPAND("", "", "\\a", FALSE, "\a");
2513 TEST_EXPAND("", "", "\\b", FALSE, "\b");
2514 TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE, "ab\bb");
2515 TEST_EXPAND("a(.)", "abc", "\\0141", FALSE, "a");
2516 TEST_EXPAND("a(.)", "abc", "\\078", FALSE, "\a8");
2517 TEST_EXPAND("a(.)", "abc", "\\077", FALSE, "?");
2518 TEST_EXPAND("a(.)", "abc", "\\0778", FALSE, "?8");
2519 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", FALSE, AGRAVE);
2520 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", TRUE, "\xc3");
2521 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\0", TRUE, "a\xc3");
2522 /* Invalid strings. */
2523 TEST_EXPAND("", "", "\\Q", FALSE, NULL);
2524 TEST_EXPAND("", "", "x\\Ay", FALSE, NULL);
2525 TEST_EXPAND("", "", "\\g<", FALSE, NULL);
2526 TEST_EXPAND("", "", "\\g<>", FALSE, NULL);
2527 TEST_EXPAND("", "", "\\g<1a>", FALSE, NULL);
2528 TEST_EXPAND("", "", "\\g<a$>", FALSE, NULL);
2529 TEST_EXPAND("", "", "\\", FALSE, NULL);
2530 TEST_EXPAND("a", "a", "\\x{61", FALSE, NULL);
2531 TEST_EXPAND("a", "a", "\\x6X", FALSE, NULL);
2533 TEST_EXPAND(NULL, NULL, "", FALSE, "");
2534 TEST_EXPAND(NULL, NULL, "\\n", FALSE, "\n");
2535 /* Invalid strings */
2536 TEST_EXPAND(NULL, NULL, "\\Q", FALSE, NULL);
2537 TEST_EXPAND(NULL, NULL, "x\\Ay", FALSE, NULL);
2539 /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
2540 TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
2541 TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
2542 TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
2543 TEST_REPLACE("a", "ababa", 3, "A", "ababA");
2544 TEST_REPLACE("a", "ababa", 4, "A", "ababA");
2545 TEST_REPLACE("a", "ababa", 5, "A", "ababa");
2546 TEST_REPLACE("a", "ababa", 6, "A", "ababa");
2547 TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
2548 TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2549 TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2550 TEST_REPLACE("a", "bab", 0, "A", "bAb");
2551 TEST_REPLACE("$^", "abc", 0, "X", "abc");
2552 TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2553 TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2554 TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2555 TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2556 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2557 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2558 TEST_REPLACE("[^-]", "-" EURO "-x-" HSTROKE, 0, "a", "-a-a-a");
2559 TEST_REPLACE("[^-]", "-" EURO "-" HSTROKE, 0, "a\\g<0>a", "-a" EURO "a-a" HSTROKE "a");
2560 TEST_REPLACE("-", "-" EURO "-" HSTROKE, 0, "", EURO HSTROKE);
2561 TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2562 TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2563 TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2564 TEST_REPLACE(".", "a", 0, "\\A", NULL);
2565 TEST_REPLACE(".", "a", 0, "\\g", NULL);
2567 /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2568 TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2569 TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2570 TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2571 TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2572 TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2573 TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2574 TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2575 TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2576 TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2577 TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2578 TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2579 TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2580 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2581 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2582 TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a", "-a" ENG "a-" HSTROKE);
2583 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a", "-a-a-a");
2584 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2585 TEST_REPLACE_LIT("-", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "", EURO AGRAVE HSTROKE);
2586 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 0, "_", "_Reg_Ex_Test");
2587 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 1, "_", "Reg_Ex_Test");
2589 /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2590 TEST_GET_STRING_NUMBER("", "A", -1);
2591 TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2592 TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2593 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2594 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2595 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2596 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2597 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2598 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2599 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2600 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2602 /* TEST_ESCAPE_NUL(string, length, expected) */
2603 TEST_ESCAPE_NUL("hello world", -1, "hello world");
2604 TEST_ESCAPE_NUL("hello\0world", -1, "hello");
2605 TEST_ESCAPE_NUL("\0world", -1, "");
2606 TEST_ESCAPE_NUL("hello world", 5, "hello");
2607 TEST_ESCAPE_NUL("hello.world", 11, "hello.world");
2608 TEST_ESCAPE_NUL("a(b\\b.$", 7, "a(b\\b.$");
2609 TEST_ESCAPE_NUL("hello\0", 6, "hello\\x00");
2610 TEST_ESCAPE_NUL("\0world", 6, "\\x00world");
2611 TEST_ESCAPE_NUL("\0\0", 2, "\\x00\\x00");
2612 TEST_ESCAPE_NUL("hello\0world", 11, "hello\\x00world");
2613 TEST_ESCAPE_NUL("hello\0world\0", 12, "hello\\x00world\\x00");
2614 TEST_ESCAPE_NUL("hello\\\0world", 12, "hello\\x00world");
2615 TEST_ESCAPE_NUL("hello\\\\\0world", 13, "hello\\\\\\x00world");
2616 TEST_ESCAPE_NUL("|()[]{}^$*+?.", 13, "|()[]{}^$*+?.");
2617 TEST_ESCAPE_NUL("|()[]{}^$*+?.\\\\", 15, "|()[]{}^$*+?.\\\\");
2619 /* TEST_ESCAPE(string, length, expected) */
2620 TEST_ESCAPE("hello world", -1, "hello world");
2621 TEST_ESCAPE("hello world", 5, "hello");
2622 TEST_ESCAPE("hello.world", -1, "hello\\.world");
2623 TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2624 TEST_ESCAPE("hello\0world", -1, "hello");
2625 TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2626 TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG);
2627 TEST_ESCAPE("a$", -1, "a\\$");
2628 TEST_ESCAPE("$a", -1, "\\$a");
2629 TEST_ESCAPE("a$a", -1, "a\\$a");
2630 TEST_ESCAPE("$a$", -1, "\\$a\\$");
2631 TEST_ESCAPE("$a$", 0, "");
2632 TEST_ESCAPE("$a$", 1, "\\$");
2633 TEST_ESCAPE("$a$", 2, "\\$a");
2634 TEST_ESCAPE("$a$", 3, "\\$a\\$");
2635 TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2636 TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2637 TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2638 "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2640 /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2641 TEST_MATCH_ALL0("<.*>", "", -1, 0);
2642 TEST_MATCH_ALL0("a+", "", -1, 0);
2643 TEST_MATCH_ALL0("a+", "a", 0, 0);
2644 TEST_MATCH_ALL0("a+", "a", -1, 1);
2645 TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2646 TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2647 TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2648 TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2649 TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2650 TEST_MATCH_ALL1(".+", ENG, -1, 0, ENG, 0, 2);
2651 TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2652 TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2653 TEST_MATCH_ALL2(".+", ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2);
2654 TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2655 "<a><b>", 0, 6, "<a>", 0, 3);
2656 TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2658 return g_test_run ();
2661 #else /* ENABLE_REGEX false */
2664 main (int argc, char *argv[])
2666 g_print ("GRegex is disabled.\n");
2670 #endif /* ENABLE_REGEX */