1 | /* |
2 | * Copyright (C) 2005 - 2006, Marco Barisione <marco@barisione.org> |
3 | * Copyright (C) 2010 Red Hat, Inc. |
4 | * |
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.1 of the License, or (at your option) any later version. |
9 | * |
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. |
14 | * |
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/>. |
17 | */ |
18 | |
19 | #undef G_DISABLE_ASSERT |
20 | #undef G_LOG_DOMAIN |
21 | |
22 | #include "config.h" |
23 | |
24 | #include <string.h> |
25 | #include <locale.h> |
26 | #include "glib.h" |
27 | |
28 | #ifdef USE_SYSTEM_PCRE |
29 | #include <pcre.h> |
30 | #else |
31 | #include "glib/pcre/pcre.h" |
32 | #endif |
33 | |
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" |
52 | |
53 | /* A random value use to mark untouched integer variables. */ |
54 | #define UNTOUCHED -559038737 |
55 | |
56 | static gint total; |
57 | |
58 | typedef struct { |
59 | const gchar *pattern; |
60 | GRegexCompileFlags compile_opts; |
61 | GRegexMatchFlags match_opts; |
62 | gint expected_error; |
63 | gboolean check_flags; |
64 | GRegexCompileFlags real_compile_opts; |
65 | GRegexMatchFlags real_match_opts; |
66 | } TestNewData; |
67 | |
68 | static void |
69 | test_new (gconstpointer d) |
70 | { |
71 | const TestNewData *data = d; |
72 | GRegex *regex; |
73 | GError *error = NULL; |
74 | |
75 | regex = g_regex_new (pattern: data->pattern, compile_options: data->compile_opts, match_options: data->match_opts, error: &error); |
76 | g_assert (regex != NULL); |
77 | g_assert_no_error (error); |
78 | g_assert_cmpstr (data->pattern, ==, g_regex_get_pattern (regex)); |
79 | |
80 | if (data->check_flags) |
81 | { |
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); |
84 | } |
85 | |
86 | g_regex_unref (regex); |
87 | } |
88 | |
89 | #define TEST_NEW(_pattern, _compile_opts, _match_opts) { \ |
90 | TestNewData *data; \ |
91 | gchar *path; \ |
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); \ |
100 | g_free (path); \ |
101 | } |
102 | |
103 | #define TEST_NEW_CHECK_FLAGS(_pattern, _compile_opts, _match_opts, _real_compile_opts, _real_match_opts) { \ |
104 | TestNewData *data; \ |
105 | gchar *path; \ |
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); \ |
116 | g_free (path); \ |
117 | } |
118 | |
119 | static void |
120 | test_new_fail (gconstpointer d) |
121 | { |
122 | const TestNewData *data = d; |
123 | GRegex *regex; |
124 | GError *error = NULL; |
125 | |
126 | regex = g_regex_new (pattern: data->pattern, compile_options: data->compile_opts, match_options: data->match_opts, error: &error); |
127 | |
128 | g_assert (regex == NULL); |
129 | g_assert_error (error, G_REGEX_ERROR, data->expected_error); |
130 | g_error_free (error); |
131 | } |
132 | |
133 | #define TEST_NEW_FAIL(_pattern, _compile_opts, _expected_error) { \ |
134 | TestNewData *data; \ |
135 | gchar *path; \ |
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); \ |
143 | g_free (path); \ |
144 | } |
145 | |
146 | typedef struct { |
147 | const gchar *pattern; |
148 | const gchar *string; |
149 | GRegexCompileFlags compile_opts; |
150 | GRegexMatchFlags match_opts; |
151 | gboolean expected; |
152 | gssize string_len; |
153 | gint start_position; |
154 | GRegexMatchFlags match_opts2; |
155 | } TestMatchData; |
156 | |
157 | static void |
158 | test_match_simple (gconstpointer d) |
159 | { |
160 | const TestMatchData *data = d; |
161 | gboolean match; |
162 | |
163 | match = g_regex_match_simple (pattern: data->pattern, string: data->string, compile_options: data->compile_opts, match_options: data->match_opts); |
164 | g_assert_cmpint (match, ==, data->expected); |
165 | } |
166 | |
167 | #define TEST_MATCH_SIMPLE_NAMED(_name, _pattern, _string, _compile_opts, _match_opts, _expected) { \ |
168 | TestMatchData *data; \ |
169 | gchar *path; \ |
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); \ |
178 | g_free (path); \ |
179 | } |
180 | |
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) |
187 | |
188 | static void |
189 | test_match (gconstpointer d) |
190 | { |
191 | const TestMatchData *data = d; |
192 | GRegex *regex; |
193 | gboolean match; |
194 | GError *error = NULL; |
195 | |
196 | regex = g_regex_new (pattern: data->pattern, compile_options: data->compile_opts, match_options: data->match_opts, error: &error); |
197 | g_assert (regex != NULL); |
198 | g_assert_no_error (error); |
199 | |
200 | match = g_regex_match_full (regex, string: data->string, string_len: data->string_len, |
201 | start_position: data->start_position, match_options: data->match_opts2, NULL, NULL); |
202 | |
203 | if (data->expected) |
204 | { |
205 | if (!match) |
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); |
214 | |
215 | g_assert_cmpint (match, ==, TRUE); |
216 | } |
217 | else |
218 | { |
219 | if (match) |
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); |
228 | } |
229 | |
230 | if (data->string_len == -1 && data->start_position == 0) |
231 | { |
232 | match = g_regex_match (regex, string: data->string, match_options: data->match_opts2, NULL); |
233 | g_assert_cmpint (match, ==, data->expected); |
234 | } |
235 | |
236 | g_regex_unref (regex); |
237 | } |
238 | |
239 | #define TEST_MATCH(_pattern, _compile_opts, _match_opts, _string, \ |
240 | _string_len, _start_position, _match_opts2, _expected) { \ |
241 | TestMatchData *data; \ |
242 | gchar *path; \ |
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); \ |
254 | g_free (path); \ |
255 | } |
256 | |
257 | struct _Match |
258 | { |
259 | gchar *string; |
260 | gint start, end; |
261 | }; |
262 | typedef struct _Match Match; |
263 | |
264 | static void |
265 | free_match (gpointer data) |
266 | { |
267 | Match *match = data; |
268 | if (match == NULL) |
269 | return; |
270 | g_free (mem: match->string); |
271 | g_free (mem: match); |
272 | } |
273 | |
274 | typedef struct { |
275 | const gchar *pattern; |
276 | const gchar *string; |
277 | gssize string_len; |
278 | gint start_position; |
279 | GSList *expected; |
280 | } TestMatchNextData; |
281 | |
282 | static void |
283 | test_match_next (gconstpointer d) |
284 | { |
285 | const TestMatchNextData *data = d; |
286 | GRegex *regex; |
287 | GMatchInfo *match_info; |
288 | GSList *matches; |
289 | GSList *l_exp, *l_match; |
290 | |
291 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
292 | |
293 | g_assert (regex != NULL); |
294 | |
295 | g_regex_match_full (regex, string: data->string, string_len: data->string_len, |
296 | start_position: data->start_position, match_options: 0, match_info: &match_info, NULL); |
297 | matches = NULL; |
298 | while (g_match_info_matches (match_info)) |
299 | { |
300 | Match *match = g_new0 (Match, 1); |
301 | match->string = g_match_info_fetch (match_info, match_num: 0); |
302 | match->start = UNTOUCHED; |
303 | match->end = UNTOUCHED; |
304 | g_match_info_fetch_pos (match_info, match_num: 0, start_pos: &match->start, end_pos: &match->end); |
305 | matches = g_slist_prepend (list: matches, data: match); |
306 | g_match_info_next (match_info, NULL); |
307 | } |
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 (list: matches); |
312 | |
313 | g_assert_cmpint (g_slist_length (matches), ==, g_slist_length (data->expected)); |
314 | |
315 | l_exp = data->expected; |
316 | l_match = matches; |
317 | while (l_exp != NULL) |
318 | { |
319 | Match *exp = l_exp->data; |
320 | Match *match = l_match->data; |
321 | |
322 | g_assert_cmpstr (exp->string, ==, match->string); |
323 | g_assert_cmpint (exp->start, ==, match->start); |
324 | g_assert_cmpint (exp->end, ==, match->end); |
325 | |
326 | l_exp = g_slist_next (l_exp); |
327 | l_match = g_slist_next (l_match); |
328 | } |
329 | |
330 | g_regex_unref (regex); |
331 | g_slist_free_full (list: matches, free_func: free_match); |
332 | } |
333 | |
334 | static void |
335 | free_match_next_data (gpointer _data) |
336 | { |
337 | TestMatchNextData *data = _data; |
338 | |
339 | g_slist_free_full (list: data->expected, free_func: g_free); |
340 | g_free (mem: data); |
341 | } |
342 | |
343 | #define TEST_MATCH_NEXT0(_pattern, _string, _string_len, _start_position) { \ |
344 | TestMatchNextData *data; \ |
345 | gchar *path; \ |
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); \ |
354 | g_free (path); \ |
355 | } |
356 | |
357 | #define TEST_MATCH_NEXT1(_pattern, _string, _string_len, _start_position, \ |
358 | t1, s1, e1) { \ |
359 | TestMatchNextData *data; \ |
360 | Match *match; \ |
361 | gchar *path; \ |
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; \ |
369 | match->start = s1; \ |
370 | match->end = e1; \ |
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); \ |
374 | g_free (path); \ |
375 | } |
376 | |
377 | #define TEST_MATCH_NEXT2(_pattern, _string, _string_len, _start_position, \ |
378 | t1, s1, e1, t2, s2, e2) { \ |
379 | TestMatchNextData *data; \ |
380 | Match *match; \ |
381 | gchar *path; \ |
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; \ |
389 | match->start = s1; \ |
390 | match->end = e1; \ |
391 | data->expected = g_slist_append (NULL, match); \ |
392 | match = g_new0 (Match, 1); \ |
393 | match->string = t2; \ |
394 | match->start = s2; \ |
395 | match->end = e2; \ |
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); \ |
399 | g_free (path); \ |
400 | } |
401 | |
402 | #define TEST_MATCH_NEXT3(_pattern, _string, _string_len, _start_position, \ |
403 | t1, s1, e1, t2, s2, e2, t3, s3, e3) { \ |
404 | TestMatchNextData *data; \ |
405 | Match *match; \ |
406 | gchar *path; \ |
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; \ |
414 | match->start = s1; \ |
415 | match->end = e1; \ |
416 | data->expected = g_slist_append (NULL, match); \ |
417 | match = g_new0 (Match, 1); \ |
418 | match->string = t2; \ |
419 | match->start = s2; \ |
420 | match->end = e2; \ |
421 | data->expected = g_slist_append (data->expected, match); \ |
422 | match = g_new0 (Match, 1); \ |
423 | match->string = t3; \ |
424 | match->start = s3; \ |
425 | match->end = e3; \ |
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); \ |
429 | g_free (path); \ |
430 | } |
431 | |
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; \ |
435 | Match *match; \ |
436 | gchar *path; \ |
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; \ |
444 | match->start = s1; \ |
445 | match->end = e1; \ |
446 | data->expected = g_slist_append (NULL, match); \ |
447 | match = g_new0 (Match, 1); \ |
448 | match->string = t2; \ |
449 | match->start = s2; \ |
450 | match->end = e2; \ |
451 | data->expected = g_slist_append (data->expected, match); \ |
452 | match = g_new0 (Match, 1); \ |
453 | match->string = t3; \ |
454 | match->start = s3; \ |
455 | match->end = e3; \ |
456 | data->expected = g_slist_append (data->expected, match); \ |
457 | match = g_new0 (Match, 1); \ |
458 | match->string = t4; \ |
459 | match->start = s4; \ |
460 | match->end = e4; \ |
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); \ |
464 | g_free (path); \ |
465 | } |
466 | |
467 | typedef struct { |
468 | const gchar *pattern; |
469 | const gchar *string; |
470 | gint start_position; |
471 | GRegexMatchFlags match_opts; |
472 | gint expected_count; |
473 | } TestMatchCountData; |
474 | |
475 | static void |
476 | test_match_count (gconstpointer d) |
477 | { |
478 | const TestMatchCountData *data = d; |
479 | GRegex *regex; |
480 | GMatchInfo *match_info; |
481 | gint count; |
482 | |
483 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
484 | |
485 | g_assert (regex != NULL); |
486 | |
487 | g_regex_match_full (regex, string: data->string, string_len: -1, start_position: data->start_position, |
488 | match_options: data->match_opts, match_info: &match_info, NULL); |
489 | count = g_match_info_get_match_count (match_info); |
490 | |
491 | g_assert_cmpint (count, ==, data->expected_count); |
492 | |
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); |
497 | } |
498 | |
499 | #define TEST_MATCH_COUNT(_pattern, _string, _start_position, _match_opts, _expected_count) { \ |
500 | TestMatchCountData *data; \ |
501 | gchar *path; \ |
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); \ |
510 | g_free (path); \ |
511 | } |
512 | |
513 | static void |
514 | test_partial (gconstpointer d) |
515 | { |
516 | const TestMatchData *data = d; |
517 | GRegex *regex; |
518 | GMatchInfo *match_info; |
519 | |
520 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
521 | |
522 | g_assert (regex != NULL); |
523 | |
524 | g_regex_match (regex, string: data->string, match_options: data->match_opts, match_info: &match_info); |
525 | |
526 | g_assert_cmpint (data->expected, ==, g_match_info_is_partial_match (match_info)); |
527 | |
528 | if (data->expected) |
529 | { |
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)); |
532 | } |
533 | |
534 | g_match_info_free (match_info); |
535 | g_regex_unref (regex); |
536 | } |
537 | |
538 | #define TEST_PARTIAL_FULL(_pattern, _string, _match_opts, _expected) { \ |
539 | TestMatchData *data; \ |
540 | gchar *path; \ |
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); \ |
548 | g_free (path); \ |
549 | } |
550 | |
551 | #define TEST_PARTIAL(_pattern, _string, _expected) TEST_PARTIAL_FULL(_pattern, _string, G_REGEX_MATCH_PARTIAL, _expected) |
552 | |
553 | typedef struct { |
554 | const gchar *pattern; |
555 | const gchar *string; |
556 | gint start_position; |
557 | gint sub_n; |
558 | const gchar *expected_sub; |
559 | gint expected_start; |
560 | gint expected_end; |
561 | } TestSubData; |
562 | |
563 | static void |
564 | test_sub_pattern (gconstpointer d) |
565 | { |
566 | const TestSubData *data = d; |
567 | GRegex *regex; |
568 | GMatchInfo *match_info; |
569 | gchar *sub_expr; |
570 | gint start = UNTOUCHED, end = UNTOUCHED; |
571 | |
572 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
573 | |
574 | g_assert (regex != NULL); |
575 | |
576 | g_regex_match_full (regex, string: data->string, string_len: -1, start_position: data->start_position, match_options: 0, match_info: &match_info, NULL); |
577 | |
578 | sub_expr = g_match_info_fetch (match_info, match_num: data->sub_n); |
579 | g_assert_cmpstr (sub_expr, ==, data->expected_sub); |
580 | g_free (mem: sub_expr); |
581 | |
582 | g_match_info_fetch_pos (match_info, match_num: data->sub_n, start_pos: &start, end_pos: &end); |
583 | g_assert_cmpint (start, ==, data->expected_start); |
584 | g_assert_cmpint (end, ==, data->expected_end); |
585 | |
586 | g_match_info_free (match_info); |
587 | g_regex_unref (regex); |
588 | } |
589 | |
590 | #define TEST_SUB_PATTERN(_pattern, _string, _start_position, _sub_n, _expected_sub, \ |
591 | _expected_start, _expected_end) { \ |
592 | TestSubData *data; \ |
593 | gchar *path; \ |
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); \ |
604 | g_free (path); \ |
605 | } |
606 | |
607 | typedef struct { |
608 | const gchar *pattern; |
609 | GRegexCompileFlags flags; |
610 | const gchar *string; |
611 | gint start_position; |
612 | const gchar *sub_name; |
613 | const gchar *expected_sub; |
614 | gint expected_start; |
615 | gint expected_end; |
616 | } TestNamedSubData; |
617 | |
618 | static void |
619 | test_named_sub_pattern (gconstpointer d) |
620 | { |
621 | const TestNamedSubData *data = d; |
622 | GRegex *regex; |
623 | GMatchInfo *match_info; |
624 | gint start = UNTOUCHED, end = UNTOUCHED; |
625 | gchar *sub_expr; |
626 | |
627 | regex = g_regex_new (pattern: data->pattern, compile_options: data->flags, match_options: 0, NULL); |
628 | |
629 | g_assert (regex != NULL); |
630 | |
631 | g_regex_match_full (regex, string: data->string, string_len: -1, start_position: data->start_position, match_options: 0, match_info: &match_info, NULL); |
632 | sub_expr = g_match_info_fetch_named (match_info, name: data->sub_name); |
633 | g_assert_cmpstr (sub_expr, ==, data->expected_sub); |
634 | g_free (mem: sub_expr); |
635 | |
636 | g_match_info_fetch_named_pos (match_info, name: data->sub_name, start_pos: &start, end_pos: &end); |
637 | g_assert_cmpint (start, ==, data->expected_start); |
638 | g_assert_cmpint (end, ==, data->expected_end); |
639 | |
640 | g_match_info_free (match_info); |
641 | g_regex_unref (regex); |
642 | } |
643 | |
644 | #define TEST_NAMED_SUB_PATTERN(_pattern, _string, _start_position, _sub_name, \ |
645 | _expected_sub, _expected_start, _expected_end) { \ |
646 | TestNamedSubData *data; \ |
647 | gchar *path; \ |
648 | data = g_new0 (TestNamedSubData, 1); \ |
649 | data->pattern = _pattern; \ |
650 | data->string = _string; \ |
651 | data->flags = 0; \ |
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); \ |
659 | g_free (path); \ |
660 | } |
661 | |
662 | #define TEST_NAMED_SUB_PATTERN_DUPNAMES(_pattern, _string, _start_position, _sub_name, \ |
663 | _expected_sub, _expected_start, _expected_end) { \ |
664 | TestNamedSubData *data; \ |
665 | gchar *path; \ |
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); \ |
677 | g_free (path); \ |
678 | } |
679 | |
680 | typedef struct { |
681 | const gchar *pattern; |
682 | const gchar *string; |
683 | GSList *expected; |
684 | gint start_position; |
685 | gint max_tokens; |
686 | } TestFetchAllData; |
687 | |
688 | static void |
689 | test_fetch_all (gconstpointer d) |
690 | { |
691 | const TestFetchAllData *data = d; |
692 | GRegex *regex; |
693 | GMatchInfo *match_info; |
694 | GSList *l_exp; |
695 | gchar **matches; |
696 | gint match_count; |
697 | gint i; |
698 | |
699 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
700 | |
701 | g_assert (regex != NULL); |
702 | |
703 | g_regex_match (regex, string: data->string, match_options: 0, match_info: &match_info); |
704 | matches = g_match_info_fetch_all (match_info); |
705 | if (matches) |
706 | match_count = g_strv_length (str_array: matches); |
707 | else |
708 | match_count = 0; |
709 | |
710 | g_assert_cmpint (match_count, ==, g_slist_length (data->expected)); |
711 | |
712 | l_exp = data->expected; |
713 | for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp)) |
714 | { |
715 | g_assert_nonnull (matches); |
716 | g_assert_cmpstr (l_exp->data, ==, matches[i]); |
717 | } |
718 | |
719 | g_match_info_free (match_info); |
720 | g_regex_unref (regex); |
721 | g_strfreev (str_array: matches); |
722 | } |
723 | |
724 | static void |
725 | free_fetch_all_data (gpointer _data) |
726 | { |
727 | TestFetchAllData *data = _data; |
728 | |
729 | g_slist_free (list: data->expected); |
730 | g_free (mem: data); |
731 | } |
732 | |
733 | #define TEST_FETCH_ALL0(_pattern, _string) { \ |
734 | TestFetchAllData *data; \ |
735 | gchar *path; \ |
736 | data = g_new0 (TestFetchAllData, 1); \ |
737 | data->pattern = _pattern; \ |
738 | data->string = _string; \ |
739 | data->expected = NULL; \ |
740 | path = g_strdup_printf ("/regex/fetch-all0/%d", ++total); \ |
741 | g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \ |
742 | g_free (path); \ |
743 | } |
744 | |
745 | #define TEST_FETCH_ALL1(_pattern, _string, e1) { \ |
746 | TestFetchAllData *data; \ |
747 | gchar *path; \ |
748 | data = g_new0 (TestFetchAllData, 1); \ |
749 | data->pattern = _pattern; \ |
750 | data->string = _string; \ |
751 | data->expected = g_slist_append (NULL, e1); \ |
752 | path = g_strdup_printf ("/regex/fetch-all1/%d", ++total); \ |
753 | g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \ |
754 | g_free (path); \ |
755 | } |
756 | |
757 | #define TEST_FETCH_ALL2(_pattern, _string, e1, e2) { \ |
758 | TestFetchAllData *data; \ |
759 | gchar *path; \ |
760 | data = g_new0 (TestFetchAllData, 1); \ |
761 | data->pattern = _pattern; \ |
762 | data->string = _string; \ |
763 | data->expected = g_slist_append (NULL, e1); \ |
764 | data->expected = g_slist_append (data->expected, e2); \ |
765 | path = g_strdup_printf ("/regex/fetch-all2/%d", ++total); \ |
766 | g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \ |
767 | g_free (path); \ |
768 | } |
769 | |
770 | #define TEST_FETCH_ALL3(_pattern, _string, e1, e2, e3) { \ |
771 | TestFetchAllData *data; \ |
772 | gchar *path; \ |
773 | data = g_new0 (TestFetchAllData, 1); \ |
774 | data->pattern = _pattern; \ |
775 | data->string = _string; \ |
776 | data->expected = g_slist_append (NULL, e1); \ |
777 | data->expected = g_slist_append (data->expected, e2); \ |
778 | data->expected = g_slist_append (data->expected, e3); \ |
779 | path = g_strdup_printf ("/regex/fetch-all3/%d", ++total); \ |
780 | g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \ |
781 | g_free (path); \ |
782 | } |
783 | |
784 | static void |
785 | test_split_simple (gconstpointer d) |
786 | { |
787 | const TestFetchAllData *data = d; |
788 | GSList *l_exp; |
789 | gchar **tokens; |
790 | gint token_count; |
791 | gint i; |
792 | |
793 | tokens = g_regex_split_simple (pattern: data->pattern, string: data->string, compile_options: 0, match_options: 0); |
794 | if (tokens) |
795 | token_count = g_strv_length (str_array: tokens); |
796 | else |
797 | token_count = 0; |
798 | |
799 | g_assert_cmpint (token_count, ==, g_slist_length (data->expected)); |
800 | |
801 | l_exp = data->expected; |
802 | for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp)) |
803 | { |
804 | g_assert_nonnull (tokens); |
805 | g_assert_cmpstr (l_exp->data, ==, tokens[i]); |
806 | } |
807 | |
808 | g_strfreev (str_array: tokens); |
809 | } |
810 | |
811 | #define TEST_SPLIT_SIMPLE0(_pattern, _string) { \ |
812 | TestFetchAllData *data; \ |
813 | gchar *path; \ |
814 | data = g_new0 (TestFetchAllData, 1); \ |
815 | data->pattern = _pattern; \ |
816 | data->string = _string; \ |
817 | data->expected = NULL; \ |
818 | path = g_strdup_printf ("/regex/split/simple0/%d", ++total); \ |
819 | g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \ |
820 | g_free (path); \ |
821 | } |
822 | |
823 | #define TEST_SPLIT_SIMPLE1(_pattern, _string, e1) { \ |
824 | TestFetchAllData *data; \ |
825 | gchar *path; \ |
826 | data = g_new0 (TestFetchAllData, 1); \ |
827 | data->pattern = _pattern; \ |
828 | data->string = _string; \ |
829 | data->expected = g_slist_append (NULL, e1); \ |
830 | path = g_strdup_printf ("/regex/split/simple1/%d", ++total); \ |
831 | g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \ |
832 | g_free (path); \ |
833 | } |
834 | |
835 | #define TEST_SPLIT_SIMPLE2(_pattern, _string, e1, e2) { \ |
836 | TestFetchAllData *data; \ |
837 | gchar *path; \ |
838 | data = g_new0 (TestFetchAllData, 1); \ |
839 | data->pattern = _pattern; \ |
840 | data->string = _string; \ |
841 | data->expected = g_slist_append (NULL, e1); \ |
842 | data->expected = g_slist_append (data->expected, e2); \ |
843 | path = g_strdup_printf ("/regex/split/simple2/%d", ++total); \ |
844 | g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \ |
845 | g_free (path); \ |
846 | } |
847 | |
848 | #define TEST_SPLIT_SIMPLE3(_pattern, _string, e1, e2, e3) { \ |
849 | TestFetchAllData *data; \ |
850 | gchar *path; \ |
851 | data = g_new0 (TestFetchAllData, 1); \ |
852 | data->pattern = _pattern; \ |
853 | data->string = _string; \ |
854 | data->expected = g_slist_append (NULL, e1); \ |
855 | data->expected = g_slist_append (data->expected, e2); \ |
856 | data->expected = g_slist_append (data->expected, e3); \ |
857 | path = g_strdup_printf ("/regex/split/simple3/%d", ++total); \ |
858 | g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \ |
859 | g_free (path); \ |
860 | } |
861 | |
862 | static void |
863 | test_split_full (gconstpointer d) |
864 | { |
865 | const TestFetchAllData *data = d; |
866 | GRegex *regex; |
867 | GSList *l_exp; |
868 | gchar **tokens; |
869 | gint token_count; |
870 | gint i; |
871 | |
872 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
873 | |
874 | g_assert (regex != NULL); |
875 | |
876 | tokens = g_regex_split_full (regex, string: data->string, string_len: -1, start_position: data->start_position, |
877 | match_options: 0, max_tokens: data->max_tokens, NULL); |
878 | if (tokens) |
879 | token_count = g_strv_length (str_array: tokens); |
880 | else |
881 | token_count = 0; |
882 | |
883 | g_assert_cmpint (token_count, ==, g_slist_length (data->expected)); |
884 | |
885 | l_exp = data->expected; |
886 | for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp)) |
887 | { |
888 | g_assert_nonnull (tokens); |
889 | g_assert_cmpstr (l_exp->data, ==, tokens[i]); |
890 | } |
891 | |
892 | g_regex_unref (regex); |
893 | g_strfreev (str_array: tokens); |
894 | } |
895 | |
896 | static void |
897 | test_split (gconstpointer d) |
898 | { |
899 | const TestFetchAllData *data = d; |
900 | GRegex *regex; |
901 | GSList *l_exp; |
902 | gchar **tokens; |
903 | gint token_count; |
904 | gint i; |
905 | |
906 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
907 | |
908 | g_assert (regex != NULL); |
909 | |
910 | tokens = g_regex_split (regex, string: data->string, match_options: 0); |
911 | if (tokens) |
912 | token_count = g_strv_length (str_array: tokens); |
913 | else |
914 | token_count = 0; |
915 | |
916 | g_assert_cmpint (token_count, ==, g_slist_length (data->expected)); |
917 | |
918 | l_exp = data->expected; |
919 | for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp)) |
920 | { |
921 | g_assert_nonnull (tokens); |
922 | g_assert_cmpstr (l_exp->data, ==, tokens[i]); |
923 | } |
924 | |
925 | g_regex_unref (regex); |
926 | g_strfreev (str_array: tokens); |
927 | } |
928 | |
929 | #define TEST_SPLIT0(_pattern, _string, _start_position, _max_tokens) { \ |
930 | TestFetchAllData *data; \ |
931 | gchar *path; \ |
932 | data = g_new0 (TestFetchAllData, 1); \ |
933 | data->pattern = _pattern; \ |
934 | data->string = _string; \ |
935 | data->start_position = _start_position; \ |
936 | data->max_tokens = _max_tokens; \ |
937 | data->expected = NULL; \ |
938 | if (_start_position == 0 && _max_tokens <= 0) { \ |
939 | path = g_strdup_printf ("/regex/split0/%d", ++total); \ |
940 | g_test_add_data_func (path, data, test_split); \ |
941 | g_free (path); \ |
942 | } \ |
943 | path = g_strdup_printf ("/regex/full-split0/%d", ++total); \ |
944 | g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \ |
945 | g_free (path); \ |
946 | } |
947 | |
948 | #define TEST_SPLIT1(_pattern, _string, _start_position, _max_tokens, e1) { \ |
949 | TestFetchAllData *data; \ |
950 | gchar *path; \ |
951 | data = g_new0 (TestFetchAllData, 1); \ |
952 | data->pattern = _pattern; \ |
953 | data->string = _string; \ |
954 | data->start_position = _start_position; \ |
955 | data->max_tokens = _max_tokens; \ |
956 | data->expected = NULL; \ |
957 | data->expected = g_slist_append (data->expected, e1); \ |
958 | if (_start_position == 0 && _max_tokens <= 0) { \ |
959 | path = g_strdup_printf ("/regex/split1/%d", ++total); \ |
960 | g_test_add_data_func (path, data, test_split); \ |
961 | g_free (path); \ |
962 | } \ |
963 | path = g_strdup_printf ("/regex/full-split1/%d", ++total); \ |
964 | g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \ |
965 | g_free (path); \ |
966 | } |
967 | |
968 | #define TEST_SPLIT2(_pattern, _string, _start_position, _max_tokens, e1, e2) { \ |
969 | TestFetchAllData *data; \ |
970 | gchar *path; \ |
971 | data = g_new0 (TestFetchAllData, 1); \ |
972 | data->pattern = _pattern; \ |
973 | data->string = _string; \ |
974 | data->start_position = _start_position; \ |
975 | data->max_tokens = _max_tokens; \ |
976 | data->expected = NULL; \ |
977 | data->expected = g_slist_append (data->expected, e1); \ |
978 | data->expected = g_slist_append (data->expected, e2); \ |
979 | if (_start_position == 0 && _max_tokens <= 0) { \ |
980 | path = g_strdup_printf ("/regex/split2/%d", ++total); \ |
981 | g_test_add_data_func (path, data, test_split); \ |
982 | g_free (path); \ |
983 | } \ |
984 | path = g_strdup_printf ("/regex/full-split2/%d", ++total); \ |
985 | g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \ |
986 | g_free (path); \ |
987 | } |
988 | |
989 | #define TEST_SPLIT3(_pattern, _string, _start_position, _max_tokens, e1, e2, e3) { \ |
990 | TestFetchAllData *data; \ |
991 | gchar *path; \ |
992 | data = g_new0 (TestFetchAllData, 1); \ |
993 | data->pattern = _pattern; \ |
994 | data->string = _string; \ |
995 | data->start_position = _start_position; \ |
996 | data->max_tokens = _max_tokens; \ |
997 | data->expected = NULL; \ |
998 | data->expected = g_slist_append (data->expected, e1); \ |
999 | data->expected = g_slist_append (data->expected, e2); \ |
1000 | data->expected = g_slist_append (data->expected, e3); \ |
1001 | if (_start_position == 0 && _max_tokens <= 0) { \ |
1002 | path = g_strdup_printf ("/regex/split3/%d", ++total); \ |
1003 | g_test_add_data_func (path, data, test_split); \ |
1004 | g_free (path); \ |
1005 | } \ |
1006 | path = g_strdup_printf ("/regex/full-split3/%d", ++total); \ |
1007 | g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \ |
1008 | g_free (path); \ |
1009 | } |
1010 | |
1011 | typedef struct { |
1012 | const gchar *string_to_expand; |
1013 | gboolean expected; |
1014 | gboolean expected_refs; |
1015 | } TestCheckReplacementData; |
1016 | |
1017 | static void |
1018 | test_check_replacement (gconstpointer d) |
1019 | { |
1020 | const TestCheckReplacementData *data = d; |
1021 | gboolean has_refs; |
1022 | gboolean result; |
1023 | |
1024 | result = g_regex_check_replacement (replacement: data->string_to_expand, has_references: &has_refs, NULL); |
1025 | g_assert_cmpint (data->expected, ==, result); |
1026 | |
1027 | if (data->expected) |
1028 | g_assert_cmpint (data->expected_refs, ==, has_refs); |
1029 | } |
1030 | |
1031 | #define TEST_CHECK_REPLACEMENT(_string_to_expand, _expected, _expected_refs) { \ |
1032 | TestCheckReplacementData *data; \ |
1033 | gchar *path; \ |
1034 | data = g_new0 (TestCheckReplacementData, 1); \ |
1035 | data->string_to_expand = _string_to_expand; \ |
1036 | data->expected = _expected; \ |
1037 | data->expected_refs = _expected_refs; \ |
1038 | path = g_strdup_printf ("/regex/check-repacement/%d", ++total); \ |
1039 | g_test_add_data_func_full (path, data, test_check_replacement, g_free); \ |
1040 | g_free (path); \ |
1041 | } |
1042 | |
1043 | typedef struct { |
1044 | const gchar *pattern; |
1045 | const gchar *string; |
1046 | const gchar *string_to_expand; |
1047 | gboolean raw; |
1048 | const gchar *expected; |
1049 | } TestExpandData; |
1050 | |
1051 | static void |
1052 | test_expand (gconstpointer d) |
1053 | { |
1054 | const TestExpandData *data = d; |
1055 | GRegex *regex = NULL; |
1056 | GMatchInfo *match_info = NULL; |
1057 | gchar *res; |
1058 | GError *error = NULL; |
1059 | |
1060 | if (data->pattern) |
1061 | { |
1062 | regex = g_regex_new (pattern: data->pattern, compile_options: data->raw ? G_REGEX_RAW : 0, match_options: 0, |
1063 | error: &error); |
1064 | g_assert_no_error (error); |
1065 | g_regex_match (regex, string: data->string, match_options: 0, match_info: &match_info); |
1066 | } |
1067 | |
1068 | res = g_match_info_expand_references (match_info, string_to_expand: data->string_to_expand, NULL); |
1069 | g_assert_cmpstr (res, ==, data->expected); |
1070 | g_free (mem: res); |
1071 | g_match_info_free (match_info); |
1072 | if (regex) |
1073 | g_regex_unref (regex); |
1074 | } |
1075 | |
1076 | #define TEST_EXPAND(_pattern, _string, _string_to_expand, _raw, _expected) { \ |
1077 | TestExpandData *data; \ |
1078 | gchar *path; \ |
1079 | data = g_new0 (TestExpandData, 1); \ |
1080 | data->pattern = _pattern; \ |
1081 | data->string = _string; \ |
1082 | data->string_to_expand = _string_to_expand; \ |
1083 | data->raw = _raw; \ |
1084 | data->expected = _expected; \ |
1085 | path = g_strdup_printf ("/regex/expand/%d", ++total); \ |
1086 | g_test_add_data_func_full (path, data, test_expand, g_free); \ |
1087 | g_free (path); \ |
1088 | } |
1089 | |
1090 | typedef struct { |
1091 | const gchar *pattern; |
1092 | const gchar *string; |
1093 | gint start_position; |
1094 | const gchar *replacement; |
1095 | const gchar *expected; |
1096 | } TestReplaceData; |
1097 | |
1098 | static void |
1099 | test_replace (gconstpointer d) |
1100 | { |
1101 | const TestReplaceData *data = d; |
1102 | GRegex *regex; |
1103 | gchar *res; |
1104 | |
1105 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
1106 | res = g_regex_replace (regex, string: data->string, string_len: -1, start_position: data->start_position, replacement: data->replacement, match_options: 0, NULL); |
1107 | |
1108 | g_assert_cmpstr (res, ==, data->expected); |
1109 | |
1110 | g_free (mem: res); |
1111 | g_regex_unref (regex); |
1112 | } |
1113 | |
1114 | #define TEST_REPLACE(_pattern, _string, _start_position, _replacement, _expected) { \ |
1115 | TestReplaceData *data; \ |
1116 | gchar *path; \ |
1117 | data = g_new0 (TestReplaceData, 1); \ |
1118 | data->pattern = _pattern; \ |
1119 | data->string = _string; \ |
1120 | data->start_position = _start_position; \ |
1121 | data->replacement = _replacement; \ |
1122 | data->expected = _expected; \ |
1123 | path = g_strdup_printf ("/regex/replace/%d", ++total); \ |
1124 | g_test_add_data_func_full (path, data, test_replace, g_free); \ |
1125 | g_free (path); \ |
1126 | } |
1127 | |
1128 | static void |
1129 | test_replace_lit (gconstpointer d) |
1130 | { |
1131 | const TestReplaceData *data = d; |
1132 | GRegex *regex; |
1133 | gchar *res; |
1134 | |
1135 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
1136 | res = g_regex_replace_literal (regex, string: data->string, string_len: -1, start_position: data->start_position, |
1137 | replacement: data->replacement, match_options: 0, NULL); |
1138 | g_assert_cmpstr (res, ==, data->expected); |
1139 | |
1140 | g_free (mem: res); |
1141 | g_regex_unref (regex); |
1142 | } |
1143 | |
1144 | #define TEST_REPLACE_LIT(_pattern, _string, _start_position, _replacement, _expected) { \ |
1145 | TestReplaceData *data; \ |
1146 | gchar *path; \ |
1147 | data = g_new0 (TestReplaceData, 1); \ |
1148 | data->pattern = _pattern; \ |
1149 | data->string = _string; \ |
1150 | data->start_position = _start_position; \ |
1151 | data->replacement = _replacement; \ |
1152 | data->expected = _expected; \ |
1153 | path = g_strdup_printf ("/regex/replace-literally/%d", ++total); \ |
1154 | g_test_add_data_func_full (path, data, test_replace_lit, g_free); \ |
1155 | g_free (path); \ |
1156 | } |
1157 | |
1158 | typedef struct { |
1159 | const gchar *pattern; |
1160 | const gchar *name; |
1161 | gint expected_num; |
1162 | } TestStringNumData; |
1163 | |
1164 | static void |
1165 | test_get_string_number (gconstpointer d) |
1166 | { |
1167 | const TestStringNumData *data = d; |
1168 | GRegex *regex; |
1169 | gint num; |
1170 | |
1171 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
1172 | num = g_regex_get_string_number (regex, name: data->name); |
1173 | |
1174 | g_assert_cmpint (num, ==, data->expected_num); |
1175 | g_regex_unref (regex); |
1176 | } |
1177 | |
1178 | #define TEST_GET_STRING_NUMBER(_pattern, _name, _expected_num) { \ |
1179 | TestStringNumData *data; \ |
1180 | gchar *path; \ |
1181 | data = g_new0 (TestStringNumData, 1); \ |
1182 | data->pattern = _pattern; \ |
1183 | data->name = _name; \ |
1184 | data->expected_num = _expected_num; \ |
1185 | path = g_strdup_printf ("/regex/string-number/%d", ++total); \ |
1186 | g_test_add_data_func_full (path, data, test_get_string_number, g_free); \ |
1187 | g_free (path); \ |
1188 | } |
1189 | |
1190 | typedef struct { |
1191 | const gchar *string; |
1192 | gint length; |
1193 | const gchar *expected; |
1194 | } TestEscapeData; |
1195 | |
1196 | static void |
1197 | test_escape (gconstpointer d) |
1198 | { |
1199 | const TestEscapeData *data = d; |
1200 | gchar *escaped; |
1201 | |
1202 | escaped = g_regex_escape_string (string: data->string, length: data->length); |
1203 | |
1204 | g_assert_cmpstr (escaped, ==, data->expected); |
1205 | |
1206 | g_free (mem: escaped); |
1207 | } |
1208 | |
1209 | #define TEST_ESCAPE(_string, _length, _expected) { \ |
1210 | TestEscapeData *data; \ |
1211 | gchar *path; \ |
1212 | data = g_new0 (TestEscapeData, 1); \ |
1213 | data->string = _string; \ |
1214 | data->length = _length; \ |
1215 | data->expected = _expected; \ |
1216 | path = g_strdup_printf ("/regex/escape/%d", ++total); \ |
1217 | g_test_add_data_func_full (path, data, test_escape, g_free); \ |
1218 | g_free (path); \ |
1219 | } |
1220 | |
1221 | static void |
1222 | test_escape_nul (gconstpointer d) |
1223 | { |
1224 | const TestEscapeData *data = d; |
1225 | gchar *escaped; |
1226 | |
1227 | escaped = g_regex_escape_nul (string: data->string, length: data->length); |
1228 | |
1229 | g_assert_cmpstr (escaped, ==, data->expected); |
1230 | |
1231 | g_free (mem: escaped); |
1232 | } |
1233 | |
1234 | #define TEST_ESCAPE_NUL(_string, _length, _expected) { \ |
1235 | TestEscapeData *data; \ |
1236 | gchar *path; \ |
1237 | data = g_new0 (TestEscapeData, 1); \ |
1238 | data->string = _string; \ |
1239 | data->length = _length; \ |
1240 | data->expected = _expected; \ |
1241 | path = g_strdup_printf ("/regex/escape_nul/%d", ++total); \ |
1242 | g_test_add_data_func_full (path, data, test_escape_nul, g_free); \ |
1243 | g_free (path); \ |
1244 | } |
1245 | |
1246 | typedef struct { |
1247 | const gchar *pattern; |
1248 | const gchar *string; |
1249 | gssize string_len; |
1250 | gint start_position; |
1251 | GSList *expected; |
1252 | } TestMatchAllData; |
1253 | |
1254 | static void |
1255 | test_match_all_full (gconstpointer d) |
1256 | { |
1257 | const TestMatchAllData *data = d; |
1258 | GRegex *regex; |
1259 | GMatchInfo *match_info; |
1260 | GSList *l_exp; |
1261 | gboolean match_ok; |
1262 | gint match_count; |
1263 | gint i; |
1264 | |
1265 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
1266 | match_ok = g_regex_match_all_full (regex, string: data->string, string_len: data->string_len, start_position: data->start_position, |
1267 | match_options: 0, match_info: &match_info, NULL); |
1268 | |
1269 | if (g_slist_length (list: data->expected) == 0) |
1270 | g_assert (!match_ok); |
1271 | else |
1272 | g_assert (match_ok); |
1273 | |
1274 | match_count = g_match_info_get_match_count (match_info); |
1275 | g_assert_cmpint (match_count, ==, g_slist_length (data->expected)); |
1276 | |
1277 | l_exp = data->expected; |
1278 | for (i = 0; i < match_count; i++) |
1279 | { |
1280 | gint start, end; |
1281 | gchar *matched_string; |
1282 | Match *exp = l_exp->data; |
1283 | |
1284 | matched_string = g_match_info_fetch (match_info, match_num: i); |
1285 | g_match_info_fetch_pos (match_info, match_num: i, start_pos: &start, end_pos: &end); |
1286 | |
1287 | g_assert_cmpstr (exp->string, ==, matched_string); |
1288 | g_assert_cmpint (exp->start, ==, start); |
1289 | g_assert_cmpint (exp->end, ==, end); |
1290 | |
1291 | g_free (mem: matched_string); |
1292 | |
1293 | l_exp = g_slist_next (l_exp); |
1294 | } |
1295 | |
1296 | g_match_info_free (match_info); |
1297 | g_regex_unref (regex); |
1298 | } |
1299 | |
1300 | static void |
1301 | test_match_all (gconstpointer d) |
1302 | { |
1303 | const TestMatchAllData *data = d; |
1304 | GRegex *regex; |
1305 | GMatchInfo *match_info; |
1306 | GSList *l_exp; |
1307 | gboolean match_ok; |
1308 | guint i, match_count; |
1309 | |
1310 | regex = g_regex_new (pattern: data->pattern, compile_options: 0, match_options: 0, NULL); |
1311 | match_ok = g_regex_match_all (regex, string: data->string, match_options: 0, match_info: &match_info); |
1312 | |
1313 | if (g_slist_length (list: data->expected) == 0) |
1314 | g_assert (!match_ok); |
1315 | else |
1316 | g_assert (match_ok); |
1317 | |
1318 | match_count = g_match_info_get_match_count (match_info); |
1319 | g_assert_cmpint (match_count, >=, 0); |
1320 | |
1321 | if (match_count != g_slist_length (list: data->expected)) |
1322 | { |
1323 | g_message ("regex: %s" , data->pattern); |
1324 | g_message ("string: %s" , data->string); |
1325 | g_message ("matched strings:" ); |
1326 | |
1327 | for (i = 0; i < match_count; i++) |
1328 | { |
1329 | gint start, end; |
1330 | gchar *matched_string; |
1331 | |
1332 | matched_string = g_match_info_fetch (match_info, match_num: i); |
1333 | g_match_info_fetch_pos (match_info, match_num: i, start_pos: &start, end_pos: &end); |
1334 | g_message ("%u. %d-%d '%s'" , i, start, end, matched_string); |
1335 | g_free (mem: matched_string); |
1336 | } |
1337 | |
1338 | g_message ("expected strings:" ); |
1339 | i = 0; |
1340 | |
1341 | for (l_exp = data->expected; l_exp != NULL; l_exp = l_exp->next) |
1342 | { |
1343 | Match *exp = l_exp->data; |
1344 | |
1345 | g_message ("%u. %d-%d '%s'" , i, exp->start, exp->end, exp->string); |
1346 | i++; |
1347 | } |
1348 | |
1349 | g_error ("match_count not as expected: %u != %d" , |
1350 | match_count, g_slist_length (data->expected)); |
1351 | } |
1352 | |
1353 | l_exp = data->expected; |
1354 | for (i = 0; i < match_count; i++) |
1355 | { |
1356 | gint start, end; |
1357 | gchar *matched_string; |
1358 | Match *exp = l_exp->data; |
1359 | |
1360 | matched_string = g_match_info_fetch (match_info, match_num: i); |
1361 | g_match_info_fetch_pos (match_info, match_num: i, start_pos: &start, end_pos: &end); |
1362 | |
1363 | g_assert_cmpstr (exp->string, ==, matched_string); |
1364 | g_assert_cmpint (exp->start, ==, start); |
1365 | g_assert_cmpint (exp->end, ==, end); |
1366 | |
1367 | g_free (mem: matched_string); |
1368 | |
1369 | l_exp = g_slist_next (l_exp); |
1370 | } |
1371 | |
1372 | g_match_info_free (match_info); |
1373 | g_regex_unref (regex); |
1374 | } |
1375 | |
1376 | static void |
1377 | free_match_all_data (gpointer _data) |
1378 | { |
1379 | TestMatchAllData *data = _data; |
1380 | |
1381 | g_slist_free_full (list: data->expected, free_func: g_free); |
1382 | g_free (mem: data); |
1383 | } |
1384 | |
1385 | #define TEST_MATCH_ALL0(_pattern, _string, _string_len, _start_position) { \ |
1386 | TestMatchAllData *data; \ |
1387 | gchar *path; \ |
1388 | data = g_new0 (TestMatchAllData, 1); \ |
1389 | data->pattern = _pattern; \ |
1390 | data->string = _string; \ |
1391 | data->string_len = _string_len; \ |
1392 | data->start_position = _start_position; \ |
1393 | data->expected = NULL; \ |
1394 | if (_string_len == -1 && _start_position == 0) { \ |
1395 | path = g_strdup_printf ("/regex/match-all0/%d", ++total); \ |
1396 | g_test_add_data_func (path, data, test_match_all); \ |
1397 | g_free (path); \ |
1398 | } \ |
1399 | path = g_strdup_printf ("/regex/match-all-full0/%d", ++total); \ |
1400 | g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \ |
1401 | g_free (path); \ |
1402 | } |
1403 | |
1404 | #define TEST_MATCH_ALL1(_pattern, _string, _string_len, _start_position, \ |
1405 | t1, s1, e1) { \ |
1406 | TestMatchAllData *data; \ |
1407 | Match *match; \ |
1408 | gchar *path; \ |
1409 | data = g_new0 (TestMatchAllData, 1); \ |
1410 | data->pattern = _pattern; \ |
1411 | data->string = _string; \ |
1412 | data->string_len = _string_len; \ |
1413 | data->start_position = _start_position; \ |
1414 | data->expected = NULL; \ |
1415 | match = g_new0 (Match, 1); \ |
1416 | match->string = t1; \ |
1417 | match->start = s1; \ |
1418 | match->end = e1; \ |
1419 | data->expected = g_slist_append (data->expected, match); \ |
1420 | if (_string_len == -1 && _start_position == 0) { \ |
1421 | path = g_strdup_printf ("/regex/match-all1/%d", ++total); \ |
1422 | g_test_add_data_func (path, data, test_match_all); \ |
1423 | g_free (path); \ |
1424 | } \ |
1425 | path = g_strdup_printf ("/regex/match-all-full1/%d", ++total); \ |
1426 | g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \ |
1427 | g_free (path); \ |
1428 | } |
1429 | |
1430 | #define TEST_MATCH_ALL2(_pattern, _string, _string_len, _start_position, \ |
1431 | t1, s1, e1, t2, s2, e2) { \ |
1432 | TestMatchAllData *data; \ |
1433 | Match *match; \ |
1434 | gchar *path; \ |
1435 | data = g_new0 (TestMatchAllData, 1); \ |
1436 | data->pattern = _pattern; \ |
1437 | data->string = _string; \ |
1438 | data->string_len = _string_len; \ |
1439 | data->start_position = _start_position; \ |
1440 | data->expected = NULL; \ |
1441 | match = g_new0 (Match, 1); \ |
1442 | match->string = t1; \ |
1443 | match->start = s1; \ |
1444 | match->end = e1; \ |
1445 | data->expected = g_slist_append (data->expected, match); \ |
1446 | match = g_new0 (Match, 1); \ |
1447 | match->string = t2; \ |
1448 | match->start = s2; \ |
1449 | match->end = e2; \ |
1450 | data->expected = g_slist_append (data->expected, match); \ |
1451 | if (_string_len == -1 && _start_position == 0) { \ |
1452 | path = g_strdup_printf ("/regex/match-all2/%d", ++total); \ |
1453 | g_test_add_data_func (path, data, test_match_all); \ |
1454 | g_free (path); \ |
1455 | } \ |
1456 | path = g_strdup_printf ("/regex/match-all-full2/%d", ++total); \ |
1457 | g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \ |
1458 | g_free (path); \ |
1459 | } |
1460 | |
1461 | #define TEST_MATCH_ALL3(_pattern, _string, _string_len, _start_position, \ |
1462 | t1, s1, e1, t2, s2, e2, t3, s3, e3) { \ |
1463 | TestMatchAllData *data; \ |
1464 | Match *match; \ |
1465 | gchar *path; \ |
1466 | data = g_new0 (TestMatchAllData, 1); \ |
1467 | data->pattern = _pattern; \ |
1468 | data->string = _string; \ |
1469 | data->string_len = _string_len; \ |
1470 | data->start_position = _start_position; \ |
1471 | data->expected = NULL; \ |
1472 | match = g_new0 (Match, 1); \ |
1473 | match->string = t1; \ |
1474 | match->start = s1; \ |
1475 | match->end = e1; \ |
1476 | data->expected = g_slist_append (data->expected, match); \ |
1477 | match = g_new0 (Match, 1); \ |
1478 | match->string = t2; \ |
1479 | match->start = s2; \ |
1480 | match->end = e2; \ |
1481 | data->expected = g_slist_append (data->expected, match); \ |
1482 | match = g_new0 (Match, 1); \ |
1483 | match->string = t3; \ |
1484 | match->start = s3; \ |
1485 | match->end = e3; \ |
1486 | data->expected = g_slist_append (data->expected, match); \ |
1487 | if (_string_len == -1 && _start_position == 0) { \ |
1488 | path = g_strdup_printf ("/regex/match-all3/%d", ++total); \ |
1489 | g_test_add_data_func (path, data, test_match_all); \ |
1490 | g_free (path); \ |
1491 | } \ |
1492 | path = g_strdup_printf ("/regex/match-all-full3/%d", ++total); \ |
1493 | g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \ |
1494 | g_free (path); \ |
1495 | } |
1496 | |
1497 | static void |
1498 | test_properties (void) |
1499 | { |
1500 | GRegex *regex; |
1501 | GError *error; |
1502 | gboolean res; |
1503 | GMatchInfo *match; |
1504 | gchar *str; |
1505 | |
1506 | error = NULL; |
1507 | regex = g_regex_new (pattern: "\\p{L}\\p{Ll}\\p{Lu}\\p{L&}\\p{N}\\p{Nd}" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1508 | res = g_regex_match (regex, string: "ppPP01" , match_options: 0, match_info: &match); |
1509 | g_assert (res); |
1510 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1511 | g_assert_cmpstr (str, ==, "ppPP01" ); |
1512 | g_free (mem: str); |
1513 | |
1514 | g_match_info_free (match_info: match); |
1515 | g_regex_unref (regex); |
1516 | } |
1517 | |
1518 | static void |
1519 | test_class (void) |
1520 | { |
1521 | GRegex *regex; |
1522 | GError *error; |
1523 | GMatchInfo *match; |
1524 | gboolean res; |
1525 | gchar *str; |
1526 | |
1527 | error = NULL; |
1528 | regex = g_regex_new (pattern: "[abc\\x{0B1E}\\p{Mn}\\x{0391}-\\x{03A9}]" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1529 | res = g_regex_match (regex, string: "a:b:\340\254\236:\333\253:\316\240" , match_options: 0, match_info: &match); |
1530 | g_assert (res); |
1531 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1532 | g_assert_cmpstr (str, ==, "a" ); |
1533 | g_free (mem: str); |
1534 | res = g_match_info_next (match_info: match, NULL); |
1535 | g_assert (res); |
1536 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1537 | g_assert_cmpstr (str, ==, "b" ); |
1538 | g_free (mem: str); |
1539 | res = g_match_info_next (match_info: match, NULL); |
1540 | g_assert (res); |
1541 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1542 | g_assert_cmpstr (str, ==, "\340\254\236" ); |
1543 | g_free (mem: str); |
1544 | res = g_match_info_next (match_info: match, NULL); |
1545 | g_assert (res); |
1546 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1547 | g_assert_cmpstr (str, ==, "\333\253" ); |
1548 | g_free (mem: str); |
1549 | res = g_match_info_next (match_info: match, NULL); |
1550 | g_assert (res); |
1551 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1552 | g_assert_cmpstr (str, ==, "\316\240" ); |
1553 | g_free (mem: str); |
1554 | |
1555 | res = g_match_info_next (match_info: match, NULL); |
1556 | g_assert (!res); |
1557 | |
1558 | g_match_info_free (match_info: match); |
1559 | g_regex_unref (regex); |
1560 | } |
1561 | |
1562 | /* examples for lookahead assertions taken from pcrepattern(3) */ |
1563 | static void |
1564 | test_lookahead (void) |
1565 | { |
1566 | GRegex *regex; |
1567 | GError *error; |
1568 | GMatchInfo *match; |
1569 | gboolean res; |
1570 | gchar *str; |
1571 | gint start, end; |
1572 | |
1573 | error = NULL; |
1574 | regex = g_regex_new (pattern: "\\w+(?=;)" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1575 | g_assert (regex); |
1576 | g_assert_no_error (error); |
1577 | res = g_regex_match (regex, string: "word1 word2: word3;" , match_options: 0, match_info: &match); |
1578 | g_assert (res); |
1579 | g_assert (g_match_info_matches (match)); |
1580 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); |
1581 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1582 | g_assert_cmpstr (str, ==, "word3" ); |
1583 | g_free (mem: str); |
1584 | g_match_info_free (match_info: match); |
1585 | g_regex_unref (regex); |
1586 | |
1587 | error = NULL; |
1588 | regex = g_regex_new (pattern: "foo(?!bar)" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1589 | g_assert (regex); |
1590 | g_assert_no_error (error); |
1591 | res = g_regex_match (regex, string: "foobar foobaz" , match_options: 0, match_info: &match); |
1592 | g_assert (res); |
1593 | g_assert (g_match_info_matches (match)); |
1594 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); |
1595 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, end_pos: &end); |
1596 | g_assert (res); |
1597 | g_assert_cmpint (start, ==, 7); |
1598 | g_assert_cmpint (end, ==, 10); |
1599 | g_match_info_free (match_info: match); |
1600 | g_regex_unref (regex); |
1601 | |
1602 | error = NULL; |
1603 | regex = g_regex_new (pattern: "(?!bar)foo" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1604 | g_assert (regex); |
1605 | g_assert_no_error (error); |
1606 | res = g_regex_match (regex, string: "foobar foobaz" , match_options: 0, match_info: &match); |
1607 | g_assert (res); |
1608 | g_assert (g_match_info_matches (match)); |
1609 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); |
1610 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, end_pos: &end); |
1611 | g_assert (res); |
1612 | g_assert_cmpint (start, ==, 0); |
1613 | g_assert_cmpint (end, ==, 3); |
1614 | res = g_match_info_next (match_info: match, error: &error); |
1615 | g_assert (res); |
1616 | g_assert_no_error (error); |
1617 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, end_pos: &end); |
1618 | g_assert (res); |
1619 | g_assert_cmpint (start, ==, 7); |
1620 | g_assert_cmpint (end, ==, 10); |
1621 | g_match_info_free (match_info: match); |
1622 | g_regex_unref (regex); |
1623 | } |
1624 | |
1625 | /* examples for lookbehind assertions taken from pcrepattern(3) */ |
1626 | static void |
1627 | test_lookbehind (void) |
1628 | { |
1629 | GRegex *regex; |
1630 | GError *error; |
1631 | GMatchInfo *match; |
1632 | gboolean res; |
1633 | gint start, end; |
1634 | |
1635 | error = NULL; |
1636 | regex = g_regex_new (pattern: "(?<!foo)bar" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1637 | g_assert (regex); |
1638 | g_assert_no_error (error); |
1639 | res = g_regex_match (regex, string: "foobar boobar" , match_options: 0, match_info: &match); |
1640 | g_assert (res); |
1641 | g_assert (g_match_info_matches (match)); |
1642 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); |
1643 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, end_pos: &end); |
1644 | g_assert (res); |
1645 | g_assert_cmpint (start, ==, 10); |
1646 | g_assert_cmpint (end, ==, 13); |
1647 | g_match_info_free (match_info: match); |
1648 | g_regex_unref (regex); |
1649 | |
1650 | error = NULL; |
1651 | regex = g_regex_new (pattern: "(?<=bullock|donkey) poo" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1652 | g_assert (regex); |
1653 | g_assert_no_error (error); |
1654 | res = g_regex_match (regex, string: "don poo, and bullock poo" , match_options: 0, match_info: &match); |
1655 | g_assert (res); |
1656 | g_assert (g_match_info_matches (match)); |
1657 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 1); |
1658 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1659 | g_assert (res); |
1660 | g_assert_cmpint (start, ==, 20); |
1661 | g_match_info_free (match_info: match); |
1662 | g_regex_unref (regex); |
1663 | |
1664 | regex = g_regex_new (pattern: "(?<!dogs?|cats?) x" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1665 | g_assert (regex == NULL); |
1666 | g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND); |
1667 | g_clear_error (err: &error); |
1668 | |
1669 | regex = g_regex_new (pattern: "(?<=ab(c|de)) foo" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1670 | g_assert (regex == NULL); |
1671 | g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND); |
1672 | g_clear_error (err: &error); |
1673 | |
1674 | regex = g_regex_new (pattern: "(?<=abc|abde)foo" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1675 | g_assert (regex); |
1676 | g_assert_no_error (error); |
1677 | res = g_regex_match (regex, string: "abfoo, abdfoo, abcfoo" , match_options: 0, match_info: &match); |
1678 | g_assert (res); |
1679 | g_assert (g_match_info_matches (match)); |
1680 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1681 | g_assert (res); |
1682 | g_assert_cmpint (start, ==, 18); |
1683 | g_match_info_free (match_info: match); |
1684 | g_regex_unref (regex); |
1685 | |
1686 | regex = g_regex_new (pattern: "^.*+(?<=abcd)" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1687 | g_assert (regex); |
1688 | g_assert_no_error (error); |
1689 | res = g_regex_match (regex, string: "abcabcabcabcabcabcabcabcabcd" , match_options: 0, match_info: &match); |
1690 | g_assert (res); |
1691 | g_assert (g_match_info_matches (match)); |
1692 | g_match_info_free (match_info: match); |
1693 | g_regex_unref (regex); |
1694 | |
1695 | regex = g_regex_new (pattern: "(?<=\\d{3})(?<!999)foo" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1696 | g_assert (regex); |
1697 | g_assert_no_error (error); |
1698 | res = g_regex_match (regex, string: "999foo 123abcfoo 123foo" , match_options: 0, match_info: &match); |
1699 | g_assert (res); |
1700 | g_assert (g_match_info_matches (match)); |
1701 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1702 | g_assert (res); |
1703 | g_assert_cmpint (start, ==, 20); |
1704 | g_match_info_free (match_info: match); |
1705 | g_regex_unref (regex); |
1706 | |
1707 | regex = g_regex_new (pattern: "(?<=\\d{3}...)(?<!999)foo" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1708 | g_assert (regex); |
1709 | g_assert_no_error (error); |
1710 | res = g_regex_match (regex, string: "999foo 123abcfoo 123foo" , match_options: 0, match_info: &match); |
1711 | g_assert (res); |
1712 | g_assert (g_match_info_matches (match)); |
1713 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1714 | g_assert (res); |
1715 | g_assert_cmpint (start, ==, 13); |
1716 | g_match_info_free (match_info: match); |
1717 | g_regex_unref (regex); |
1718 | |
1719 | regex = g_regex_new (pattern: "(?<=\\d{3}(?!999)...)foo" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1720 | g_assert (regex); |
1721 | g_assert_no_error (error); |
1722 | res = g_regex_match (regex, string: "999foo 123abcfoo 123foo" , match_options: 0, match_info: &match); |
1723 | g_assert (res); |
1724 | g_assert (g_match_info_matches (match)); |
1725 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1726 | g_assert (res); |
1727 | g_assert_cmpint (start, ==, 13); |
1728 | g_match_info_free (match_info: match); |
1729 | g_regex_unref (regex); |
1730 | |
1731 | regex = g_regex_new (pattern: "(?<=(?<!foo)bar)baz" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1732 | g_assert (regex); |
1733 | g_assert_no_error (error); |
1734 | res = g_regex_match (regex, string: "foobarbaz barfoobaz barbarbaz" , match_options: 0, match_info: &match); |
1735 | g_assert (res); |
1736 | g_assert (g_match_info_matches (match)); |
1737 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1738 | g_assert (res); |
1739 | g_assert_cmpint (start, ==, 26); |
1740 | g_match_info_free (match_info: match); |
1741 | g_regex_unref (regex); |
1742 | } |
1743 | |
1744 | /* examples for subpatterns taken from pcrepattern(3) */ |
1745 | static void |
1746 | test_subpattern (void) |
1747 | { |
1748 | GRegex *regex; |
1749 | GError *error; |
1750 | GMatchInfo *match; |
1751 | gboolean res; |
1752 | gchar *str; |
1753 | gint start; |
1754 | |
1755 | error = NULL; |
1756 | regex = g_regex_new (pattern: "cat(aract|erpillar|)" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1757 | g_assert (regex); |
1758 | g_assert_no_error (error); |
1759 | g_assert_cmpint (g_regex_get_capture_count (regex), ==, 1); |
1760 | g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0); |
1761 | res = g_regex_match_all (regex, string: "caterpillar" , match_options: 0, match_info: &match); |
1762 | g_assert (res); |
1763 | g_assert (g_match_info_matches (match)); |
1764 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 2); |
1765 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1766 | g_assert_cmpstr (str, ==, "caterpillar" ); |
1767 | g_free (mem: str); |
1768 | str = g_match_info_fetch (match_info: match, match_num: 1); |
1769 | g_assert_cmpstr (str, ==, "cat" ); |
1770 | g_free (mem: str); |
1771 | g_match_info_free (match_info: match); |
1772 | g_regex_unref (regex); |
1773 | |
1774 | regex = g_regex_new (pattern: "the ((red|white) (king|queen))" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1775 | g_assert (regex); |
1776 | g_assert_no_error (error); |
1777 | g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3); |
1778 | g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0); |
1779 | res = g_regex_match (regex, string: "the red king" , match_options: 0, match_info: &match); |
1780 | g_assert (res); |
1781 | g_assert (g_match_info_matches (match)); |
1782 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 4); |
1783 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1784 | g_assert_cmpstr (str, ==, "the red king" ); |
1785 | g_free (mem: str); |
1786 | str = g_match_info_fetch (match_info: match, match_num: 1); |
1787 | g_assert_cmpstr (str, ==, "red king" ); |
1788 | g_free (mem: str); |
1789 | str = g_match_info_fetch (match_info: match, match_num: 2); |
1790 | g_assert_cmpstr (str, ==, "red" ); |
1791 | g_free (mem: str); |
1792 | str = g_match_info_fetch (match_info: match, match_num: 3); |
1793 | g_assert_cmpstr (str, ==, "king" ); |
1794 | g_free (mem: str); |
1795 | g_match_info_free (match_info: match); |
1796 | g_regex_unref (regex); |
1797 | |
1798 | regex = g_regex_new (pattern: "the ((?:red|white) (king|queen))" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1799 | g_assert (regex); |
1800 | g_assert_no_error (error); |
1801 | res = g_regex_match (regex, string: "the white queen" , match_options: 0, match_info: &match); |
1802 | g_assert (res); |
1803 | g_assert (g_match_info_matches (match)); |
1804 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 3); |
1805 | g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0); |
1806 | str = g_match_info_fetch (match_info: match, match_num: 0); |
1807 | g_assert_cmpstr (str, ==, "the white queen" ); |
1808 | g_free (mem: str); |
1809 | str = g_match_info_fetch (match_info: match, match_num: 1); |
1810 | g_assert_cmpstr (str, ==, "white queen" ); |
1811 | g_free (mem: str); |
1812 | str = g_match_info_fetch (match_info: match, match_num: 2); |
1813 | g_assert_cmpstr (str, ==, "queen" ); |
1814 | g_free (mem: str); |
1815 | g_match_info_free (match_info: match); |
1816 | g_regex_unref (regex); |
1817 | |
1818 | regex = g_regex_new (pattern: "(?|(Sat)(ur)|(Sun))day (morning|afternoon)" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1819 | g_assert (regex); |
1820 | g_assert_no_error (error); |
1821 | g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3); |
1822 | res = g_regex_match (regex, string: "Saturday morning" , match_options: 0, match_info: &match); |
1823 | g_assert (res); |
1824 | g_assert (g_match_info_matches (match)); |
1825 | g_assert_cmpint (g_match_info_get_match_count (match), ==, 4); |
1826 | str = g_match_info_fetch (match_info: match, match_num: 1); |
1827 | g_assert_cmpstr (str, ==, "Sat" ); |
1828 | g_free (mem: str); |
1829 | str = g_match_info_fetch (match_info: match, match_num: 2); |
1830 | g_assert_cmpstr (str, ==, "ur" ); |
1831 | g_free (mem: str); |
1832 | str = g_match_info_fetch (match_info: match, match_num: 3); |
1833 | g_assert_cmpstr (str, ==, "morning" ); |
1834 | g_free (mem: str); |
1835 | g_match_info_free (match_info: match); |
1836 | g_regex_unref (regex); |
1837 | |
1838 | regex = g_regex_new (pattern: "(?|(abc)|(def))\\1" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1839 | g_assert (regex); |
1840 | g_assert_no_error (error); |
1841 | g_assert_cmpint (g_regex_get_max_backref (regex), ==, 1); |
1842 | res = g_regex_match (regex, string: "abcabc abcdef defabc defdef" , match_options: 0, match_info: &match); |
1843 | g_assert (res); |
1844 | g_assert (g_match_info_matches (match)); |
1845 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1846 | g_assert (res); |
1847 | g_assert_cmpint (start, ==, 0); |
1848 | res = g_match_info_next (match_info: match, error: &error); |
1849 | g_assert (res); |
1850 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1851 | g_assert (res); |
1852 | g_assert_cmpint (start, ==, 21); |
1853 | g_match_info_free (match_info: match); |
1854 | g_regex_unref (regex); |
1855 | |
1856 | regex = g_regex_new (pattern: "(?|(abc)|(def))(?1)" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1857 | g_assert (regex); |
1858 | g_assert_no_error (error); |
1859 | res = g_regex_match (regex, string: "abcabc abcdef defabc defdef" , match_options: 0, match_info: &match); |
1860 | g_assert (res); |
1861 | g_assert (g_match_info_matches (match)); |
1862 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1863 | g_assert (res); |
1864 | g_assert_cmpint (start, ==, 0); |
1865 | res = g_match_info_next (match_info: match, error: &error); |
1866 | g_assert (res); |
1867 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
1868 | g_assert (res); |
1869 | g_assert_cmpint (start, ==, 14); |
1870 | g_match_info_free (match_info: match); |
1871 | g_regex_unref (regex); |
1872 | |
1873 | regex = g_regex_new (pattern: "(?<DN>Mon|Fri|Sun)(?:day)?|(?<DN>Tue)(?:sday)?|(?<DN>Wed)(?:nesday)?|(?<DN>Thu)(?:rsday)?|(?<DN>Sat)(?:urday)?" , compile_options: G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, match_options: 0, error: &error); |
1874 | g_assert (regex); |
1875 | g_assert_no_error (error); |
1876 | res = g_regex_match (regex, string: "Mon Tuesday Wed Saturday" , match_options: 0, match_info: &match); |
1877 | g_assert (res); |
1878 | g_assert (g_match_info_matches (match)); |
1879 | str = g_match_info_fetch_named (match_info: match, name: "DN" ); |
1880 | g_assert_cmpstr (str, ==, "Mon" ); |
1881 | g_free (mem: str); |
1882 | res = g_match_info_next (match_info: match, error: &error); |
1883 | g_assert (res); |
1884 | str = g_match_info_fetch_named (match_info: match, name: "DN" ); |
1885 | g_assert_cmpstr (str, ==, "Tue" ); |
1886 | g_free (mem: str); |
1887 | res = g_match_info_next (match_info: match, error: &error); |
1888 | g_assert (res); |
1889 | str = g_match_info_fetch_named (match_info: match, name: "DN" ); |
1890 | g_assert_cmpstr (str, ==, "Wed" ); |
1891 | g_free (mem: str); |
1892 | res = g_match_info_next (match_info: match, error: &error); |
1893 | g_assert (res); |
1894 | str = g_match_info_fetch_named (match_info: match, name: "DN" ); |
1895 | g_assert_cmpstr (str, ==, "Sat" ); |
1896 | g_free (mem: str); |
1897 | g_match_info_free (match_info: match); |
1898 | g_regex_unref (regex); |
1899 | |
1900 | regex = g_regex_new (pattern: "^(a|b\\1)+$" , compile_options: G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, match_options: 0, error: &error); |
1901 | g_assert (regex); |
1902 | g_assert_no_error (error); |
1903 | res = g_regex_match (regex, string: "aaaaaaaaaaaaaaaa" , match_options: 0, match_info: &match); |
1904 | g_assert (res); |
1905 | g_assert (g_match_info_matches (match)); |
1906 | g_match_info_free (match_info: match); |
1907 | res = g_regex_match (regex, string: "ababbaa" , match_options: 0, match_info: &match); |
1908 | g_assert (res); |
1909 | g_assert (g_match_info_matches (match)); |
1910 | g_match_info_free (match_info: match); |
1911 | g_regex_unref (regex); |
1912 | } |
1913 | |
1914 | /* examples for conditions taken from pcrepattern(3) */ |
1915 | static void |
1916 | test_condition (void) |
1917 | { |
1918 | GRegex *regex; |
1919 | GError *error; |
1920 | GMatchInfo *match; |
1921 | gboolean res; |
1922 | |
1923 | error = NULL; |
1924 | regex = g_regex_new (pattern: "^(a+)(\\()?[^()]+(?(-1)\\))(b+)$" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1925 | g_assert (regex); |
1926 | g_assert_no_error (error); |
1927 | res = g_regex_match (regex, string: "a(zzzzzz)b" , match_options: 0, match_info: &match); |
1928 | g_assert (res); |
1929 | g_assert (g_match_info_matches (match)); |
1930 | g_match_info_free (match_info: match); |
1931 | res = g_regex_match (regex, string: "aaazzzzzzbbb" , match_options: 0, match_info: &match); |
1932 | g_assert (res); |
1933 | g_assert (g_match_info_matches (match)); |
1934 | g_match_info_free (match_info: match); |
1935 | g_regex_unref (regex); |
1936 | |
1937 | error = NULL; |
1938 | regex = g_regex_new (pattern: "^(a+)(?<OPEN>\\()?[^()]+(?(<OPEN>)\\))(b+)$" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1939 | g_assert (regex); |
1940 | g_assert_no_error (error); |
1941 | res = g_regex_match (regex, string: "a(zzzzzz)b" , match_options: 0, match_info: &match); |
1942 | g_assert (res); |
1943 | g_assert (g_match_info_matches (match)); |
1944 | g_match_info_free (match_info: match); |
1945 | res = g_regex_match (regex, string: "aaazzzzzzbbb" , match_options: 0, match_info: &match); |
1946 | g_assert (res); |
1947 | g_assert (g_match_info_matches (match)); |
1948 | g_match_info_free (match_info: match); |
1949 | g_regex_unref (regex); |
1950 | |
1951 | regex = g_regex_new (pattern: "^(a+)(?(+1)\\[|\\<)?[^()]+(\\])?(b+)$" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
1952 | g_assert (regex); |
1953 | g_assert_no_error (error); |
1954 | res = g_regex_match (regex, string: "a[zzzzzz]b" , match_options: 0, match_info: &match); |
1955 | g_assert (res); |
1956 | g_assert (g_match_info_matches (match)); |
1957 | g_match_info_free (match_info: match); |
1958 | res = g_regex_match (regex, string: "aaa<zzzzzzbbb" , match_options: 0, match_info: &match); |
1959 | g_assert (res); |
1960 | g_assert (g_match_info_matches (match)); |
1961 | g_match_info_free (match_info: match); |
1962 | g_regex_unref (regex); |
1963 | |
1964 | regex = g_regex_new (pattern: "(?(DEFINE) (?<byte> 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d) )" |
1965 | "\\b (?&byte) (\\.(?&byte)){3} \\b" , |
1966 | compile_options: G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, match_options: 0, error: &error); |
1967 | g_assert (regex); |
1968 | g_assert_no_error (error); |
1969 | res = g_regex_match (regex, string: "128.0.0.1" , match_options: 0, match_info: &match); |
1970 | g_assert (res); |
1971 | g_assert (g_match_info_matches (match)); |
1972 | g_match_info_free (match_info: match); |
1973 | res = g_regex_match (regex, string: "192.168.1.1" , match_options: 0, match_info: &match); |
1974 | g_assert (res); |
1975 | g_assert (g_match_info_matches (match)); |
1976 | g_match_info_free (match_info: match); |
1977 | res = g_regex_match (regex, string: "209.132.180.167" , match_options: 0, match_info: &match); |
1978 | g_assert (res); |
1979 | g_assert (g_match_info_matches (match)); |
1980 | g_match_info_free (match_info: match); |
1981 | g_regex_unref (regex); |
1982 | |
1983 | regex = g_regex_new (pattern: "^(?(?=[^a-z]*[a-z])" |
1984 | "\\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )$" , |
1985 | compile_options: G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, match_options: 0, error: &error); |
1986 | g_assert (regex); |
1987 | g_assert_no_error (error); |
1988 | res = g_regex_match (regex, string: "01-abc-24" , match_options: 0, match_info: &match); |
1989 | g_assert (res); |
1990 | g_assert (g_match_info_matches (match)); |
1991 | g_match_info_free (match_info: match); |
1992 | res = g_regex_match (regex, string: "01-23-45" , match_options: 0, match_info: &match); |
1993 | g_assert (res); |
1994 | g_assert (g_match_info_matches (match)); |
1995 | g_match_info_free (match_info: match); |
1996 | res = g_regex_match (regex, string: "01-uv-45" , match_options: 0, match_info: &match); |
1997 | g_assert (!res); |
1998 | g_assert (!g_match_info_matches (match)); |
1999 | g_match_info_free (match_info: match); |
2000 | res = g_regex_match (regex, string: "01-234-45" , match_options: 0, match_info: &match); |
2001 | g_assert (!res); |
2002 | g_assert (!g_match_info_matches (match)); |
2003 | g_match_info_free (match_info: match); |
2004 | g_regex_unref (regex); |
2005 | } |
2006 | |
2007 | /* examples for recursion taken from pcrepattern(3) */ |
2008 | static void |
2009 | test_recursion (void) |
2010 | { |
2011 | GRegex *regex; |
2012 | GError *error; |
2013 | GMatchInfo *match; |
2014 | gboolean res; |
2015 | gint start; |
2016 | |
2017 | error = NULL; |
2018 | regex = g_regex_new (pattern: "\\( ( [^()]++ | (?R) )* \\)" , compile_options: G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, match_options: 0, error: &error); |
2019 | g_assert (regex); |
2020 | g_assert_no_error (error); |
2021 | res = g_regex_match (regex, string: "(middle)" , match_options: 0, match_info: &match); |
2022 | g_assert (res); |
2023 | g_assert (g_match_info_matches (match)); |
2024 | g_match_info_free (match_info: match); |
2025 | res = g_regex_match (regex, string: "((((((((((((((((middle))))))))))))))))" , match_options: 0, match_info: &match); |
2026 | g_assert (res); |
2027 | g_assert (g_match_info_matches (match)); |
2028 | g_match_info_free (match_info: match); |
2029 | res = g_regex_match (regex, string: "(((xxx(((" , match_options: 0, match_info: &match); |
2030 | g_assert (!res); |
2031 | g_assert (!g_match_info_matches (match)); |
2032 | g_match_info_free (match_info: match); |
2033 | g_regex_unref (regex); |
2034 | |
2035 | regex = g_regex_new (pattern: "^( \\( ( [^()]++ | (?1) )* \\) )$" , compile_options: G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, match_options: 0, error: &error); |
2036 | g_assert (regex); |
2037 | g_assert_no_error (error); |
2038 | res = g_regex_match (regex, string: "((((((((((((((((middle))))))))))))))))" , match_options: 0, match_info: &match); |
2039 | g_assert (res); |
2040 | g_assert (g_match_info_matches (match)); |
2041 | g_match_info_free (match_info: match); |
2042 | res = g_regex_match (regex, string: "(((xxx((()" , match_options: 0, match_info: &match); |
2043 | g_assert (!res); |
2044 | g_assert (!g_match_info_matches (match)); |
2045 | g_match_info_free (match_info: match); |
2046 | g_regex_unref (regex); |
2047 | |
2048 | regex = g_regex_new (pattern: "^(?<pn> \\( ( [^()]++ | (?&pn) )* \\) )$" , compile_options: G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, match_options: 0, error: &error); |
2049 | g_assert (regex); |
2050 | g_assert_no_error (error); |
2051 | g_regex_match (regex, string: "(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()" , match_options: 0, match_info: &match); |
2052 | g_assert (!res); |
2053 | g_assert (!g_match_info_matches (match)); |
2054 | g_match_info_free (match_info: match); |
2055 | g_regex_unref (regex); |
2056 | |
2057 | regex = g_regex_new (pattern: "< (?: (?(R) \\d++ | [^<>]*+) | (?R)) * >" , compile_options: G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, match_options: 0, error: &error); |
2058 | g_assert (regex); |
2059 | g_assert_no_error (error); |
2060 | res = g_regex_match (regex, string: "<ab<01<23<4>>>>" , match_options: 0, match_info: &match); |
2061 | g_assert (res); |
2062 | g_assert (g_match_info_matches (match)); |
2063 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
2064 | g_assert (res); |
2065 | g_assert_cmpint (start, ==, 0); |
2066 | g_match_info_free (match_info: match); |
2067 | res = g_regex_match (regex, string: "<ab<01<xx<x>>>>" , match_options: 0, match_info: &match); |
2068 | g_assert (res); |
2069 | g_assert (g_match_info_matches (match)); |
2070 | res = g_match_info_fetch_pos (match_info: match, match_num: 0, start_pos: &start, NULL); |
2071 | g_assert (res); |
2072 | g_assert_cmpint (start, >, 0); |
2073 | g_match_info_free (match_info: match); |
2074 | g_regex_unref (regex); |
2075 | |
2076 | regex = g_regex_new (pattern: "^((.)(?1)\\2|.)$" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
2077 | g_assert (regex); |
2078 | g_assert_no_error (error); |
2079 | res = g_regex_match (regex, string: "abcdcba" , match_options: 0, match_info: &match); |
2080 | g_assert (res); |
2081 | g_assert (g_match_info_matches (match)); |
2082 | g_match_info_free (match_info: match); |
2083 | res = g_regex_match (regex, string: "abcddcba" , match_options: 0, match_info: &match); |
2084 | g_assert (!res); |
2085 | g_assert (!g_match_info_matches (match)); |
2086 | g_match_info_free (match_info: match); |
2087 | g_regex_unref (regex); |
2088 | |
2089 | regex = g_regex_new (pattern: "^(?:((.)(?1)\\2|)|((.)(?3)\\4|.))$" , compile_options: G_REGEX_OPTIMIZE, match_options: 0, error: &error); |
2090 | g_assert (regex); |
2091 | g_assert_no_error (error); |
2092 | res = g_regex_match (regex, string: "abcdcba" , match_options: 0, match_info: &match); |
2093 | g_assert (res); |
2094 | g_assert (g_match_info_matches (match)); |
2095 | g_match_info_free (match_info: match); |
2096 | res = g_regex_match (regex, string: "abcddcba" , match_options: 0, match_info: &match); |
2097 | g_assert (res); |
2098 | g_assert (g_match_info_matches (match)); |
2099 | g_match_info_free (match_info: match); |
2100 | g_regex_unref (regex); |
2101 | |
2102 | regex = g_regex_new (pattern: "^\\W*+(?:((.)\\W*+(?1)\\W*+\\2|)|((.)\\W*+(?3)\\W*+\\4|\\W*+.\\W*+))\\W*+$" , compile_options: G_REGEX_OPTIMIZE|G_REGEX_CASELESS, match_options: 0, error: &error); |
2103 | g_assert (regex); |
2104 | g_assert_no_error (error); |
2105 | res = g_regex_match (regex, string: "abcdcba" , match_options: 0, match_info: &match); |
2106 | g_assert (res); |
2107 | g_assert (g_match_info_matches (match)); |
2108 | g_match_info_free (match_info: match); |
2109 | res = g_regex_match (regex, string: "A man, a plan, a canal: Panama!" , match_options: 0, match_info: &match); |
2110 | g_assert (res); |
2111 | g_assert (g_match_info_matches (match)); |
2112 | g_match_info_free (match_info: match); |
2113 | res = g_regex_match (regex, string: "Oozy rat in a sanitary zoo" , match_options: 0, match_info: &match); |
2114 | g_assert (res); |
2115 | g_assert (g_match_info_matches (match)); |
2116 | g_match_info_free (match_info: match); |
2117 | g_regex_unref (regex); |
2118 | } |
2119 | |
2120 | static void |
2121 | test_multiline (void) |
2122 | { |
2123 | GRegex *regex; |
2124 | GMatchInfo *info; |
2125 | gint count; |
2126 | |
2127 | g_test_bug (bug_uri_snippet: "640489" ); |
2128 | |
2129 | regex = g_regex_new (pattern: "^a$" , compile_options: G_REGEX_MULTILINE|G_REGEX_DOTALL, match_options: 0, NULL); |
2130 | |
2131 | count = 0; |
2132 | g_regex_match (regex, string: "a\nb\na" , match_options: 0, match_info: &info); |
2133 | while (g_match_info_matches (match_info: info)) |
2134 | { |
2135 | count++; |
2136 | g_match_info_next (match_info: info, NULL); |
2137 | } |
2138 | g_match_info_free (match_info: info); |
2139 | g_regex_unref (regex); |
2140 | |
2141 | g_assert_cmpint (count, ==, 2); |
2142 | } |
2143 | |
2144 | static void |
2145 | test_explicit_crlf (void) |
2146 | { |
2147 | GRegex *regex; |
2148 | |
2149 | regex = g_regex_new (pattern: "[\r\n]a" , compile_options: 0, match_options: 0, NULL); |
2150 | g_assert_cmpint (g_regex_get_has_cr_or_lf (regex), ==, TRUE); |
2151 | g_regex_unref (regex); |
2152 | } |
2153 | |
2154 | static void |
2155 | test_max_lookbehind (void) |
2156 | { |
2157 | GRegex *regex; |
2158 | |
2159 | regex = g_regex_new (pattern: "abc" , compile_options: 0, match_options: 0, NULL); |
2160 | g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 0); |
2161 | g_regex_unref (regex); |
2162 | |
2163 | regex = g_regex_new (pattern: "\\babc" , compile_options: 0, match_options: 0, NULL); |
2164 | g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 1); |
2165 | g_regex_unref (regex); |
2166 | |
2167 | regex = g_regex_new (pattern: "(?<=123)abc" , compile_options: 0, match_options: 0, NULL); |
2168 | g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 3); |
2169 | g_regex_unref (regex); |
2170 | } |
2171 | |
2172 | static gboolean |
2173 | pcre_ge (guint64 major, guint64 minor) |
2174 | { |
2175 | const char *version; |
2176 | gchar *ptr; |
2177 | guint64 pcre_major, pcre_minor; |
2178 | |
2179 | /* e.g. 8.35 2014-04-04 */ |
2180 | version = pcre_version (); |
2181 | |
2182 | pcre_major = g_ascii_strtoull (nptr: version, endptr: &ptr, base: 10); |
2183 | /* ptr points to ".MINOR (release date)" */ |
2184 | g_assert (ptr[0] == '.'); |
2185 | pcre_minor = g_ascii_strtoull (nptr: ptr + 1, NULL, base: 10); |
2186 | |
2187 | return (pcre_major > major) || (pcre_major == major && pcre_minor >= minor); |
2188 | } |
2189 | |
2190 | int |
2191 | main (int argc, char *argv[]) |
2192 | { |
2193 | setlocale (LC_ALL, locale: "" ); |
2194 | |
2195 | g_test_init (argc: &argc, argv: &argv, NULL); |
2196 | |
2197 | g_test_bug_base (uri_pattern: "http://bugzilla.gnome.org/" ); |
2198 | |
2199 | g_test_add_func (testpath: "/regex/properties" , test_func: test_properties); |
2200 | g_test_add_func (testpath: "/regex/class" , test_func: test_class); |
2201 | g_test_add_func (testpath: "/regex/lookahead" , test_func: test_lookahead); |
2202 | g_test_add_func (testpath: "/regex/lookbehind" , test_func: test_lookbehind); |
2203 | g_test_add_func (testpath: "/regex/subpattern" , test_func: test_subpattern); |
2204 | g_test_add_func (testpath: "/regex/condition" , test_func: test_condition); |
2205 | g_test_add_func (testpath: "/regex/recursion" , test_func: test_recursion); |
2206 | g_test_add_func (testpath: "/regex/multiline" , test_func: test_multiline); |
2207 | g_test_add_func (testpath: "/regex/explicit-crlf" , test_func: test_explicit_crlf); |
2208 | g_test_add_func (testpath: "/regex/max-lookbehind" , test_func: test_max_lookbehind); |
2209 | |
2210 | /* TEST_NEW(pattern, compile_opts, match_opts) */ |
2211 | TEST_NEW("[A-Z]+" , G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL); |
2212 | TEST_NEW("" , 0, 0); |
2213 | TEST_NEW(".*" , 0, 0); |
2214 | TEST_NEW(".*" , G_REGEX_OPTIMIZE, 0); |
2215 | TEST_NEW(".*" , G_REGEX_MULTILINE, 0); |
2216 | TEST_NEW(".*" , G_REGEX_DOTALL, 0); |
2217 | TEST_NEW(".*" , G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL); |
2218 | TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)" , 0, 0); |
2219 | TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)" , G_REGEX_CASELESS, 0); |
2220 | TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)" , G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0); |
2221 | TEST_NEW("(?P<A>x)|(?P<A>y)" , G_REGEX_DUPNAMES, 0); |
2222 | TEST_NEW("(?P<A>x)|(?P<A>y)" , G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0); |
2223 | /* This gives "internal error: code overflow" with pcre 6.0 */ |
2224 | TEST_NEW("(?i)(?-i)" , 0, 0); |
2225 | TEST_NEW ("(?i)a" , 0, 0); |
2226 | TEST_NEW ("(?m)a" , 0, 0); |
2227 | TEST_NEW ("(?s)a" , 0, 0); |
2228 | TEST_NEW ("(?x)a" , 0, 0); |
2229 | TEST_NEW ("(?J)a" , 0, 0); |
2230 | TEST_NEW ("(?U)[a-z]+" , 0, 0); |
2231 | |
2232 | /* TEST_NEW_CHECK_FLAGS(pattern, compile_opts, match_ops, real_compile_opts, real_match_opts) */ |
2233 | TEST_NEW_CHECK_FLAGS ("a" , G_REGEX_OPTIMIZE, 0, G_REGEX_OPTIMIZE, 0); |
2234 | TEST_NEW_CHECK_FLAGS ("a" , G_REGEX_RAW, 0, G_REGEX_RAW, 0); |
2235 | TEST_NEW_CHECK_FLAGS ("(?X)a" , 0, 0, 0 /* not exposed by GRegex */, 0); |
2236 | TEST_NEW_CHECK_FLAGS ("^.*" , 0, 0, G_REGEX_ANCHORED, 0); |
2237 | TEST_NEW_CHECK_FLAGS ("(*UTF8)a" , 0, 0, 0 /* this is the default in GRegex */, 0); |
2238 | TEST_NEW_CHECK_FLAGS ("(*UCP)a" , 0, 0, 0 /* this always on in GRegex */, 0); |
2239 | TEST_NEW_CHECK_FLAGS ("(*CR)a" , 0, 0, G_REGEX_NEWLINE_CR, 0); |
2240 | TEST_NEW_CHECK_FLAGS ("(*LF)a" , 0, 0, G_REGEX_NEWLINE_LF, 0); |
2241 | TEST_NEW_CHECK_FLAGS ("(*CRLF)a" , 0, 0, G_REGEX_NEWLINE_CRLF, 0); |
2242 | TEST_NEW_CHECK_FLAGS ("(*ANY)a" , 0, 0, 0 /* this is the default in GRegex */, 0); |
2243 | TEST_NEW_CHECK_FLAGS ("(*ANYCRLF)a" , 0, 0, G_REGEX_NEWLINE_ANYCRLF, 0); |
2244 | TEST_NEW_CHECK_FLAGS ("(*BSR_ANYCRLF)a" , 0, 0, G_REGEX_BSR_ANYCRLF, 0); |
2245 | TEST_NEW_CHECK_FLAGS ("(*BSR_UNICODE)a" , 0, 0, 0 /* this is the default in GRegex */, 0); |
2246 | TEST_NEW_CHECK_FLAGS ("(*NO_START_OPT)a" , 0, 0, 0 /* not exposed in GRegex */, 0); |
2247 | |
2248 | /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */ |
2249 | TEST_NEW_FAIL("(" , 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS); |
2250 | TEST_NEW_FAIL(")" , 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS); |
2251 | TEST_NEW_FAIL("[" , 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS); |
2252 | TEST_NEW_FAIL("*" , 0, G_REGEX_ERROR_NOTHING_TO_REPEAT); |
2253 | TEST_NEW_FAIL("?" , 0, G_REGEX_ERROR_NOTHING_TO_REPEAT); |
2254 | TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)" , 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME); |
2255 | |
2256 | /* Check all GRegexError codes */ |
2257 | TEST_NEW_FAIL ("a\\" , 0, G_REGEX_ERROR_STRAY_BACKSLASH); |
2258 | TEST_NEW_FAIL ("a\\c" , 0, G_REGEX_ERROR_MISSING_CONTROL_CHAR); |
2259 | TEST_NEW_FAIL ("a\\l" , 0, G_REGEX_ERROR_UNRECOGNIZED_ESCAPE); |
2260 | TEST_NEW_FAIL ("a{4,2}" , 0, G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER); |
2261 | TEST_NEW_FAIL ("a{999999,}" , 0, G_REGEX_ERROR_QUANTIFIER_TOO_BIG); |
2262 | TEST_NEW_FAIL ("[a-z" , 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS); |
2263 | TEST_NEW_FAIL ("(?X)[\\B]" , 0, G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS); |
2264 | TEST_NEW_FAIL ("[z-a]" , 0, G_REGEX_ERROR_RANGE_OUT_OF_ORDER); |
2265 | TEST_NEW_FAIL ("{2,4}" , 0, G_REGEX_ERROR_NOTHING_TO_REPEAT); |
2266 | TEST_NEW_FAIL ("a(?u)" , 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER); |
2267 | TEST_NEW_FAIL ("a(?<$foo)bar" , 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER); |
2268 | TEST_NEW_FAIL ("a[:alpha:]b" , 0, G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS); |
2269 | TEST_NEW_FAIL ("a(b" , 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS); |
2270 | TEST_NEW_FAIL ("a)b" , 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS); |
2271 | TEST_NEW_FAIL ("a(?R" , 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS); |
2272 | TEST_NEW_FAIL ("a(?-54" , 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS); |
2273 | TEST_NEW_FAIL ("(ab\\2)" , 0, G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE); |
2274 | TEST_NEW_FAIL ("a(?#abc" , 0, G_REGEX_ERROR_UNTERMINATED_COMMENT); |
2275 | TEST_NEW_FAIL ("(?<=a+)b" , 0, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND); |
2276 | TEST_NEW_FAIL ("(?(1?)a|b)" , 0, G_REGEX_ERROR_MALFORMED_CONDITION); |
2277 | TEST_NEW_FAIL ("(a)(?(1)a|b|c)" , 0, G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES); |
2278 | TEST_NEW_FAIL ("(?(?i))" , 0, G_REGEX_ERROR_ASSERTION_EXPECTED); |
2279 | TEST_NEW_FAIL ("a[[:fubar:]]b" , 0, G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME); |
2280 | TEST_NEW_FAIL ("[[.ch.]]" , 0, G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED); |
2281 | TEST_NEW_FAIL ("\\x{110000}" , 0, G_REGEX_ERROR_HEX_CODE_TOO_LARGE); |
2282 | TEST_NEW_FAIL ("^(?(0)f|b)oo" , 0, G_REGEX_ERROR_INVALID_CONDITION); |
2283 | TEST_NEW_FAIL ("(?<=\\C)X" , 0, G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND); |
2284 | TEST_NEW_FAIL ("(?!\\w)(?R)" , 0, G_REGEX_ERROR_INFINITE_LOOP); |
2285 | if (pcre_ge (major: 8, minor: 37)) |
2286 | { |
2287 | /* The expected errors changed here. */ |
2288 | TEST_NEW_FAIL ("(?(?<ab))" , 0, G_REGEX_ERROR_ASSERTION_EXPECTED); |
2289 | } |
2290 | else |
2291 | { |
2292 | TEST_NEW_FAIL ("(?(?<ab))" , 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR); |
2293 | } |
2294 | |
2295 | if (pcre_ge (major: 8, minor: 35)) |
2296 | { |
2297 | /* The expected errors changed here. */ |
2298 | TEST_NEW_FAIL ("(?P<sub>foo)\\g<sub" , 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR); |
2299 | } |
2300 | else |
2301 | { |
2302 | TEST_NEW_FAIL ("(?P<sub>foo)\\g<sub" , 0, G_REGEX_ERROR_MISSING_BACK_REFERENCE); |
2303 | } |
2304 | TEST_NEW_FAIL ("(?P<x>eks)(?P<x>eccs)" , 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME); |
2305 | #if 0 |
2306 | TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_MALFORMED_PROPERTY); |
2307 | TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_UNKNOWN_PROPERTY); |
2308 | #endif |
2309 | TEST_NEW_FAIL ("\\666" , G_REGEX_RAW, G_REGEX_ERROR_INVALID_OCTAL_VALUE); |
2310 | TEST_NEW_FAIL ("^(?(DEFINE) abc | xyz ) " , 0, G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE); |
2311 | TEST_NEW_FAIL ("a" , G_REGEX_NEWLINE_CRLF | G_REGEX_NEWLINE_ANYCRLF, G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS); |
2312 | TEST_NEW_FAIL ("^(a)\\g{3" , 0, G_REGEX_ERROR_MISSING_BACK_REFERENCE); |
2313 | TEST_NEW_FAIL ("^(a)\\g{0}" , 0, G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE); |
2314 | TEST_NEW_FAIL ("abc(*FAIL:123)xyz" , 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN); |
2315 | TEST_NEW_FAIL ("a(*FOOBAR)b" , 0, G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB); |
2316 | TEST_NEW_FAIL ("(?i:A{1,}\\6666666666)" , 0, G_REGEX_ERROR_NUMBER_TOO_BIG); |
2317 | TEST_NEW_FAIL ("(?<a>)(?&)" , 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME); |
2318 | TEST_NEW_FAIL ("(?+-a)" , 0, G_REGEX_ERROR_MISSING_DIGIT); |
2319 | TEST_NEW_FAIL ("TA]" , G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_INVALID_DATA_CHARACTER); |
2320 | TEST_NEW_FAIL ("(?|(?<a>A)|(?<b>B))" , 0, G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME); |
2321 | TEST_NEW_FAIL ("a(*MARK)b" , 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED); |
2322 | TEST_NEW_FAIL ("^\\c€" , 0, G_REGEX_ERROR_INVALID_CONTROL_CHAR); |
2323 | TEST_NEW_FAIL ("\\k" , 0, G_REGEX_ERROR_MISSING_NAME); |
2324 | TEST_NEW_FAIL ("a[\\NB]c" , 0, G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS); |
2325 | TEST_NEW_FAIL ("(*:0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFG)XX" , 0, G_REGEX_ERROR_NAME_TOO_LONG); |
2326 | TEST_NEW_FAIL ("\\u0100" , G_REGEX_RAW | G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_CHARACTER_VALUE_TOO_LARGE); |
2327 | |
2328 | /* These errors can't really be tested easily: |
2329 | * G_REGEX_ERROR_EXPRESSION_TOO_LARGE |
2330 | * G_REGEX_ERROR_MEMORY_ERROR |
2331 | * G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG |
2332 | * G_REGEX_ERROR_TOO_MANY_SUBPATTERNS |
2333 | * G_REGEX_ERROR_TOO_MANY_FORWARD_REFERENCES |
2334 | * |
2335 | * These errors are obsolete and never raised by PCRE: |
2336 | * G_REGEX_ERROR_DEFINE_REPETION |
2337 | */ |
2338 | |
2339 | /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */ |
2340 | TEST_MATCH_SIMPLE("a" , "" , 0, 0, FALSE); |
2341 | TEST_MATCH_SIMPLE("a" , "a" , 0, 0, TRUE); |
2342 | TEST_MATCH_SIMPLE("a" , "ba" , 0, 0, TRUE); |
2343 | TEST_MATCH_SIMPLE("^a" , "ba" , 0, 0, FALSE); |
2344 | TEST_MATCH_SIMPLE("a" , "ba" , G_REGEX_ANCHORED, 0, FALSE); |
2345 | TEST_MATCH_SIMPLE("a" , "ba" , 0, G_REGEX_MATCH_ANCHORED, FALSE); |
2346 | TEST_MATCH_SIMPLE("a" , "ab" , G_REGEX_ANCHORED, 0, TRUE); |
2347 | TEST_MATCH_SIMPLE("a" , "ab" , 0, G_REGEX_MATCH_ANCHORED, TRUE); |
2348 | TEST_MATCH_SIMPLE("a" , "a" , G_REGEX_CASELESS, 0, TRUE); |
2349 | TEST_MATCH_SIMPLE("a" , "A" , G_REGEX_CASELESS, 0, TRUE); |
2350 | /* These are needed to test extended properties. */ |
2351 | TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE); |
2352 | TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE); |
2353 | TEST_MATCH_SIMPLE("\\p{L}" , "a" , 0, 0, TRUE); |
2354 | TEST_MATCH_SIMPLE("\\p{L}" , "1" , 0, 0, FALSE); |
2355 | TEST_MATCH_SIMPLE("\\p{L}" , AGRAVE, 0, 0, TRUE); |
2356 | TEST_MATCH_SIMPLE("\\p{L}" , AGRAVE_UPPER, 0, 0, TRUE); |
2357 | TEST_MATCH_SIMPLE("\\p{L}" , SHEEN, 0, 0, TRUE); |
2358 | TEST_MATCH_SIMPLE("\\p{L}" , ETH30, 0, 0, FALSE); |
2359 | TEST_MATCH_SIMPLE("\\p{Ll}" , "a" , 0, 0, TRUE); |
2360 | TEST_MATCH_SIMPLE("\\p{Ll}" , AGRAVE, 0, 0, TRUE); |
2361 | TEST_MATCH_SIMPLE("\\p{Ll}" , AGRAVE_UPPER, 0, 0, FALSE); |
2362 | TEST_MATCH_SIMPLE("\\p{Ll}" , ETH30, 0, 0, FALSE); |
2363 | TEST_MATCH_SIMPLE("\\p{Sc}" , AGRAVE, 0, 0, FALSE); |
2364 | TEST_MATCH_SIMPLE("\\p{Sc}" , EURO, 0, 0, TRUE); |
2365 | TEST_MATCH_SIMPLE("\\p{Sc}" , ETH30, 0, 0, FALSE); |
2366 | TEST_MATCH_SIMPLE("\\p{N}" , "a" , 0, 0, FALSE); |
2367 | TEST_MATCH_SIMPLE("\\p{N}" , "1" , 0, 0, TRUE); |
2368 | TEST_MATCH_SIMPLE("\\p{N}" , AGRAVE, 0, 0, FALSE); |
2369 | TEST_MATCH_SIMPLE("\\p{N}" , AGRAVE_UPPER, 0, 0, FALSE); |
2370 | TEST_MATCH_SIMPLE("\\p{N}" , SHEEN, 0, 0, FALSE); |
2371 | TEST_MATCH_SIMPLE("\\p{N}" , ETH30, 0, 0, TRUE); |
2372 | TEST_MATCH_SIMPLE("\\p{Nd}" , "a" , 0, 0, FALSE); |
2373 | TEST_MATCH_SIMPLE("\\p{Nd}" , "1" , 0, 0, TRUE); |
2374 | TEST_MATCH_SIMPLE("\\p{Nd}" , AGRAVE, 0, 0, FALSE); |
2375 | TEST_MATCH_SIMPLE("\\p{Nd}" , AGRAVE_UPPER, 0, 0, FALSE); |
2376 | TEST_MATCH_SIMPLE("\\p{Nd}" , SHEEN, 0, 0, FALSE); |
2377 | TEST_MATCH_SIMPLE("\\p{Nd}" , ETH30, 0, 0, FALSE); |
2378 | TEST_MATCH_SIMPLE("\\p{Common}" , SHEEN, 0, 0, FALSE); |
2379 | TEST_MATCH_SIMPLE("\\p{Common}" , "a" , 0, 0, FALSE); |
2380 | TEST_MATCH_SIMPLE("\\p{Common}" , AGRAVE, 0, 0, FALSE); |
2381 | TEST_MATCH_SIMPLE("\\p{Common}" , AGRAVE_UPPER, 0, 0, FALSE); |
2382 | TEST_MATCH_SIMPLE("\\p{Common}" , ETH30, 0, 0, FALSE); |
2383 | TEST_MATCH_SIMPLE("\\p{Common}" , "%" , 0, 0, TRUE); |
2384 | TEST_MATCH_SIMPLE("\\p{Common}" , "1" , 0, 0, TRUE); |
2385 | TEST_MATCH_SIMPLE("\\p{Arabic}" , SHEEN, 0, 0, TRUE); |
2386 | TEST_MATCH_SIMPLE("\\p{Arabic}" , "a" , 0, 0, FALSE); |
2387 | TEST_MATCH_SIMPLE("\\p{Arabic}" , AGRAVE, 0, 0, FALSE); |
2388 | TEST_MATCH_SIMPLE("\\p{Arabic}" , AGRAVE_UPPER, 0, 0, FALSE); |
2389 | TEST_MATCH_SIMPLE("\\p{Arabic}" , ETH30, 0, 0, FALSE); |
2390 | TEST_MATCH_SIMPLE("\\p{Arabic}" , "%" , 0, 0, FALSE); |
2391 | TEST_MATCH_SIMPLE("\\p{Arabic}" , "1" , 0, 0, FALSE); |
2392 | TEST_MATCH_SIMPLE("\\p{Latin}" , SHEEN, 0, 0, FALSE); |
2393 | TEST_MATCH_SIMPLE("\\p{Latin}" , "a" , 0, 0, TRUE); |
2394 | TEST_MATCH_SIMPLE("\\p{Latin}" , AGRAVE, 0, 0, TRUE); |
2395 | TEST_MATCH_SIMPLE("\\p{Latin}" , AGRAVE_UPPER, 0, 0, TRUE); |
2396 | TEST_MATCH_SIMPLE("\\p{Latin}" , ETH30, 0, 0, FALSE); |
2397 | TEST_MATCH_SIMPLE("\\p{Latin}" , "%" , 0, 0, FALSE); |
2398 | TEST_MATCH_SIMPLE("\\p{Latin}" , "1" , 0, 0, FALSE); |
2399 | TEST_MATCH_SIMPLE("\\p{Ethiopic}" , SHEEN, 0, 0, FALSE); |
2400 | TEST_MATCH_SIMPLE("\\p{Ethiopic}" , "a" , 0, 0, FALSE); |
2401 | TEST_MATCH_SIMPLE("\\p{Ethiopic}" , AGRAVE, 0, 0, FALSE); |
2402 | TEST_MATCH_SIMPLE("\\p{Ethiopic}" , AGRAVE_UPPER, 0, 0, FALSE); |
2403 | TEST_MATCH_SIMPLE("\\p{Ethiopic}" , ETH30, 0, 0, TRUE); |
2404 | TEST_MATCH_SIMPLE("\\p{Ethiopic}" , "%" , 0, 0, FALSE); |
2405 | TEST_MATCH_SIMPLE("\\p{Ethiopic}" , "1" , 0, 0, FALSE); |
2406 | TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})" , SHEEN, 0, 0, TRUE); |
2407 | TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})" , SHEEN, 0, 0, FALSE); |
2408 | /* Invalid patterns. */ |
2409 | TEST_MATCH_SIMPLE("\\" , "a" , 0, 0, FALSE); |
2410 | TEST_MATCH_SIMPLE("[" , "" , 0, 0, FALSE); |
2411 | |
2412 | /* TEST_MATCH(pattern, compile_opts, match_opts, string, |
2413 | * string_len, start_position, match_opts2, expected) */ |
2414 | TEST_MATCH("a" , 0, 0, "a" , -1, 0, 0, TRUE); |
2415 | TEST_MATCH("a" , 0, 0, "A" , -1, 0, 0, FALSE); |
2416 | TEST_MATCH("a" , G_REGEX_CASELESS, 0, "A" , -1, 0, 0, TRUE); |
2417 | TEST_MATCH("a" , 0, 0, "ab" , -1, 1, 0, FALSE); |
2418 | TEST_MATCH("a" , 0, 0, "ba" , 1, 0, 0, FALSE); |
2419 | TEST_MATCH("a" , 0, 0, "bab" , -1, 0, 0, TRUE); |
2420 | TEST_MATCH("a" , 0, 0, "b" , -1, 0, 0, FALSE); |
2421 | TEST_MATCH("a" , 0, G_REGEX_MATCH_ANCHORED, "a" , -1, 0, 0, TRUE); |
2422 | TEST_MATCH("a" , 0, G_REGEX_MATCH_ANCHORED, "ab" , -1, 1, 0, FALSE); |
2423 | TEST_MATCH("a" , 0, G_REGEX_MATCH_ANCHORED, "ba" , 1, 0, 0, FALSE); |
2424 | TEST_MATCH("a" , 0, G_REGEX_MATCH_ANCHORED, "bab" , -1, 0, 0, FALSE); |
2425 | TEST_MATCH("a" , 0, G_REGEX_MATCH_ANCHORED, "b" , -1, 0, 0, FALSE); |
2426 | TEST_MATCH("a" , 0, 0, "a" , -1, 0, G_REGEX_MATCH_ANCHORED, TRUE); |
2427 | TEST_MATCH("a" , 0, 0, "ab" , -1, 1, G_REGEX_MATCH_ANCHORED, FALSE); |
2428 | TEST_MATCH("a" , 0, 0, "ba" , 1, 0, G_REGEX_MATCH_ANCHORED, FALSE); |
2429 | TEST_MATCH("a" , 0, 0, "bab" , -1, 0, G_REGEX_MATCH_ANCHORED, FALSE); |
2430 | TEST_MATCH("a" , 0, 0, "b" , -1, 0, G_REGEX_MATCH_ANCHORED, FALSE); |
2431 | TEST_MATCH("a|b" , 0, 0, "a" , -1, 0, 0, TRUE); |
2432 | TEST_MATCH("\\d" , 0, 0, EURO, -1, 0, 0, FALSE); |
2433 | TEST_MATCH("^.$" , 0, 0, EURO, -1, 0, 0, TRUE); |
2434 | TEST_MATCH("^.{3}$" , 0, 0, EURO, -1, 0, 0, FALSE); |
2435 | TEST_MATCH("^.$" , G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE); |
2436 | TEST_MATCH("^.{3}$" , G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE); |
2437 | TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE); |
2438 | |
2439 | /* New lines handling. */ |
2440 | TEST_MATCH("^a\\Rb$" , 0, 0, "a\r\nb" , -1, 0, 0, TRUE); |
2441 | TEST_MATCH("^a\\Rb$" , 0, 0, "a\nb" , -1, 0, 0, TRUE); |
2442 | TEST_MATCH("^a\\Rb$" , 0, 0, "a\rb" , -1, 0, 0, TRUE); |
2443 | TEST_MATCH("^a\\Rb$" , 0, 0, "a\n\rb" , -1, 0, 0, FALSE); |
2444 | TEST_MATCH("^a\\R\\Rb$" , 0, 0, "a\n\rb" , -1, 0, 0, TRUE); |
2445 | TEST_MATCH("^a\\nb$" , 0, 0, "a\r\nb" , -1, 0, 0, FALSE); |
2446 | TEST_MATCH("^a\\r\\nb$" , 0, 0, "a\r\nb" , -1, 0, 0, TRUE); |
2447 | |
2448 | TEST_MATCH("^b$" , 0, 0, "a\nb\nc" , -1, 0, 0, FALSE); |
2449 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, 0, "a\nb\nc" , -1, 0, 0, TRUE); |
2450 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, 0, "a\r\nb\r\nc" , -1, 0, 0, TRUE); |
2451 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, 0, "a\rb\rc" , -1, 0, 0, TRUE); |
2452 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc" , -1, 0, 0, FALSE); |
2453 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc" , -1, 0, 0, TRUE); |
2454 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc" , -1, 0, 0, FALSE); |
2455 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc" , -1, 0, 0, FALSE); |
2456 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc" , -1, 0, 0, FALSE); |
2457 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc" , -1, 0, 0, TRUE); |
2458 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc" , -1, 0, 0, TRUE); |
2459 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc" , -1, 0, 0, FALSE); |
2460 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc" , -1, 0, 0, FALSE); |
2461 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc" , -1, 0, 0, FALSE); |
2462 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc" , -1, 0, 0, TRUE); |
2463 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc" , -1, 0, 0, FALSE); |
2464 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc" , -1, 0, 0, FALSE); |
2465 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc" , -1, 0, 0, FALSE); |
2466 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc" , -1, 0, 0, TRUE); |
2467 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc" , -1, 0, 0, TRUE); |
2468 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc" , -1, 0, 0, FALSE); |
2469 | TEST_MATCH("^b$" , G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc" , -1, 0, 0, FALSE); |
2470 | |
2471 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc" , -1, 0, 0, TRUE); |
2472 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc" , -1, 0, 0, TRUE); |
2473 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc" , -1, 0, 0, TRUE); |
2474 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc" , -1, 0, 0, TRUE); |
2475 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc" , -1, 0, 0, FALSE); |
2476 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc" , -1, 0, 0, TRUE); |
2477 | TEST_MATCH("^b$" , G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc" , -1, 0, 0, FALSE); |
2478 | |
2479 | TEST_MATCH("a#\nb" , G_REGEX_EXTENDED, 0, "a" , -1, 0, 0, FALSE); |
2480 | TEST_MATCH("a#\r\nb" , G_REGEX_EXTENDED, 0, "a" , -1, 0, 0, FALSE); |
2481 | TEST_MATCH("a#\rb" , G_REGEX_EXTENDED, 0, "a" , -1, 0, 0, FALSE); |
2482 | TEST_MATCH("a#\nb" , G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a" , -1, 0, 0, FALSE); |
2483 | TEST_MATCH("a#\nb" , G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a" , -1, 0, 0, TRUE); |
2484 | |
2485 | TEST_MATCH("line\nbreak" , G_REGEX_MULTILINE, 0, "this is a line\nbreak" , -1, 0, 0, TRUE); |
2486 | TEST_MATCH("line\nbreak" , G_REGEX_MULTILINE | G_REGEX_FIRSTLINE, 0, "first line\na line\nbreak" , -1, 0, 0, FALSE); |
2487 | |
2488 | /* This failed with PCRE 7.2 (gnome bug #455640) */ |
2489 | TEST_MATCH(".*$" , 0, 0, "\xe1\xbb\x85" , -1, 0, 0, TRUE); |
2490 | |
2491 | /* Test that othercasing in our pcre/glib integration is bug-for-bug compatible |
2492 | * with pcre's internal tables. Bug #678273 */ |
2493 | TEST_MATCH("[Ç„]" , G_REGEX_CASELESS, 0, "Ç„" , -1, 0, 0, TRUE); |
2494 | TEST_MATCH("[DŽ]" , G_REGEX_CASELESS, 0, "dž" , -1, 0, 0, TRUE); |
2495 | #if PCRE_MAJOR > 8 || (PCRE_MAJOR == 8 && PCRE_MINOR >= 32) |
2496 | /* This would incorrectly fail to match in pcre < 8.32, so only assert |
2497 | * this for known-good pcre. */ |
2498 | TEST_MATCH("[Ç„]" , G_REGEX_CASELESS, 0, "Ç…" , -1, 0, 0, TRUE); |
2499 | #endif |
2500 | |
2501 | /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */ |
2502 | TEST_MATCH_NEXT0("a" , "x" , -1, 0); |
2503 | TEST_MATCH_NEXT0("a" , "ax" , -1, 1); |
2504 | TEST_MATCH_NEXT0("a" , "xa" , 1, 0); |
2505 | TEST_MATCH_NEXT0("a" , "axa" , 1, 2); |
2506 | TEST_MATCH_NEXT1("a" , "a" , -1, 0, "a" , 0, 1); |
2507 | TEST_MATCH_NEXT1("a" , "xax" , -1, 0, "a" , 1, 2); |
2508 | TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5); |
2509 | TEST_MATCH_NEXT1("a*" , "" , -1, 0, "" , 0, 0); |
2510 | TEST_MATCH_NEXT2("a*" , "aa" , -1, 0, "aa" , 0, 2, "" , 2, 2); |
2511 | TEST_MATCH_NEXT2(EURO "*" , EURO EURO, -1, 0, EURO EURO, 0, 6, "" , 6, 6); |
2512 | TEST_MATCH_NEXT2("a" , "axa" , -1, 0, "a" , 0, 1, "a" , 2, 3); |
2513 | TEST_MATCH_NEXT2("a+" , "aaxa" , -1, 0, "aa" , 0, 2, "a" , 3, 4); |
2514 | TEST_MATCH_NEXT2("a" , "aa" , -1, 0, "a" , 0, 1, "a" , 1, 2); |
2515 | TEST_MATCH_NEXT2("a" , "ababa" , -1, 2, "a" , 2, 3, "a" , 4, 5); |
2516 | TEST_MATCH_NEXT2(EURO "+" , EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7); |
2517 | TEST_MATCH_NEXT3("" , "ab" , -1, 0, "" , 0, 0, "" , 1, 1, "" , 2, 2); |
2518 | TEST_MATCH_NEXT3("" , AGRAVE "b" , -1, 0, "" , 0, 0, "" , 2, 2, "" , 3, 3); |
2519 | TEST_MATCH_NEXT3("a" , "aaxa" , -1, 0, "a" , 0, 1, "a" , 1, 2, "a" , 3, 4); |
2520 | TEST_MATCH_NEXT3("a" , "aa" OGRAVE "a" , -1, 0, "a" , 0, 1, "a" , 1, 2, "a" , 4, 5); |
2521 | TEST_MATCH_NEXT3("a*" , "aax" , -1, 0, "aa" , 0, 2, "" , 2, 2, "" , 3, 3); |
2522 | TEST_MATCH_NEXT3("(?=[A-Z0-9])" , "RegExTest" , -1, 0, "" , 0, 0, "" , 3, 3, "" , 5, 5); |
2523 | TEST_MATCH_NEXT4("a*" , "aaxa" , -1, 0, "aa" , 0, 2, "" , 2, 2, "a" , 3, 4, "" , 4, 4); |
2524 | |
2525 | /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */ |
2526 | TEST_MATCH_COUNT("a" , "" , 0, 0, 0); |
2527 | TEST_MATCH_COUNT("a" , "a" , 0, 0, 1); |
2528 | TEST_MATCH_COUNT("a" , "a" , 1, 0, 0); |
2529 | TEST_MATCH_COUNT("(.)" , "a" , 0, 0, 2); |
2530 | TEST_MATCH_COUNT("(.)" , EURO, 0, 0, 2); |
2531 | TEST_MATCH_COUNT("(?:.)" , "a" , 0, 0, 1); |
2532 | TEST_MATCH_COUNT("(?P<A>.)" , "a" , 0, 0, 2); |
2533 | TEST_MATCH_COUNT("a$" , "a" , 0, G_REGEX_MATCH_NOTEOL, 0); |
2534 | TEST_MATCH_COUNT("(a)?(b)" , "b" , 0, 0, 3); |
2535 | TEST_MATCH_COUNT("(a)?(b)" , "ab" , 0, 0, 3); |
2536 | |
2537 | /* TEST_PARTIAL(pattern, string, expected) */ |
2538 | TEST_PARTIAL("^ab" , "a" , TRUE); |
2539 | TEST_PARTIAL("^ab" , "xa" , FALSE); |
2540 | TEST_PARTIAL("ab" , "xa" , TRUE); |
2541 | TEST_PARTIAL("ab" , "ab" , FALSE); /* normal match. */ |
2542 | TEST_PARTIAL("a+b" , "aa" , TRUE); |
2543 | TEST_PARTIAL("(a)+b" , "aa" , TRUE); |
2544 | TEST_PARTIAL("a?b" , "a" , TRUE); |
2545 | |
2546 | /* Test soft vs. hard partial matching */ |
2547 | TEST_PARTIAL_FULL("cat(fish)?" , "cat" , G_REGEX_MATCH_PARTIAL_SOFT, FALSE); |
2548 | TEST_PARTIAL_FULL("cat(fish)?" , "cat" , G_REGEX_MATCH_PARTIAL_HARD, TRUE); |
2549 | |
2550 | /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub, |
2551 | * expected_start, expected_end) */ |
2552 | TEST_SUB_PATTERN("a" , "a" , 0, 0, "a" , 0, 1); |
2553 | TEST_SUB_PATTERN("a(.)" , "ab" , 0, 1, "b" , 1, 2); |
2554 | TEST_SUB_PATTERN("a(.)" , "a" EURO, 0, 1, EURO, 1, 4); |
2555 | TEST_SUB_PATTERN("(?:.*)(a)(.)" , "xxa" ENG, 0, 2, ENG, 3, 5); |
2556 | TEST_SUB_PATTERN("(" HSTROKE ")" , "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3); |
2557 | TEST_SUB_PATTERN("a" , "a" , 0, 1, NULL, UNTOUCHED, UNTOUCHED); |
2558 | TEST_SUB_PATTERN("a" , "a" , 0, 1, NULL, UNTOUCHED, UNTOUCHED); |
2559 | TEST_SUB_PATTERN("(a)?(b)" , "b" , 0, 0, "b" , 0, 1); |
2560 | TEST_SUB_PATTERN("(a)?(b)" , "b" , 0, 1, "" , -1, -1); |
2561 | TEST_SUB_PATTERN("(a)?(b)" , "b" , 0, 2, "b" , 0, 1); |
2562 | |
2563 | /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name, |
2564 | * expected_sub, expected_start, expected_end) */ |
2565 | TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?" , "ab" , 0, "A" , "b" , 1, 2); |
2566 | TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?" , "aab" , 1, "A" , "b" , 2, 3); |
2567 | TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?" , EURO "ab" , 0, "A" , "b" , 4, 5); |
2568 | TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?" , EURO "ab" , 0, "B" , NULL, UNTOUCHED, UNTOUCHED); |
2569 | TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?" , EURO "ab" , 0, "C" , NULL, UNTOUCHED, UNTOUCHED); |
2570 | TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?" , "a" EGRAVE "x" , 0, "A" , EGRAVE, 1, 3); |
2571 | TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?" , "a" EGRAVE "x" , 0, "B" , "x" , 3, 4); |
2572 | TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)" , "b" , 0, "A" , "" , -1, -1); |
2573 | TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)" , "b" , 0, "B" , "b" , 0, 1); |
2574 | |
2575 | /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name, |
2576 | * expected_sub, expected_start, expected_end) */ |
2577 | TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)" , "ab" , 0, "N" , "a" , 0, 1); |
2578 | TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)" , "aa" , 0, "N" , "aa" , 0, 2); |
2579 | TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)" , "aaa" , 0, "N" , "aa" , 0, 2); |
2580 | TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)" , "a" , 0, "N" , "a" , 0, 1); |
2581 | TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b" , "ab" , 0, "N" , "a" , 0, 1); |
2582 | |
2583 | /* DUPNAMES option inside the pattern */ |
2584 | TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)" , "ab" , 0, "N" , "a" , 0, 1); |
2585 | TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)" , "aa" , 0, "N" , "aa" , 0, 2); |
2586 | TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)" , "aaa" , 0, "N" , "aa" , 0, 2); |
2587 | TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)" , "a" , 0, "N" , "a" , 0, 1); |
2588 | TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b" , "ab" , 0, "N" , "a" , 0, 1); |
2589 | |
2590 | /* TEST_FETCH_ALL#(pattern, string, ...) */ |
2591 | TEST_FETCH_ALL0("a" , "" ); |
2592 | TEST_FETCH_ALL0("a" , "b" ); |
2593 | TEST_FETCH_ALL1("a" , "a" , "a" ); |
2594 | TEST_FETCH_ALL1("a+" , "aa" , "aa" ); |
2595 | TEST_FETCH_ALL1("(?:a)" , "a" , "a" ); |
2596 | TEST_FETCH_ALL2("(a)" , "a" , "a" , "a" ); |
2597 | TEST_FETCH_ALL2("a(.)" , "ab" , "ab" , "b" ); |
2598 | TEST_FETCH_ALL2("a(.)" , "a" HSTROKE, "a" HSTROKE, HSTROKE); |
2599 | TEST_FETCH_ALL3("(?:.*)(a)(.)" , "xyazk" , "xyaz" , "a" , "z" ); |
2600 | TEST_FETCH_ALL3("(?P<A>.)(a)" , "xa" , "xa" , "x" , "a" ); |
2601 | TEST_FETCH_ALL3("(?P<A>.)(a)" , ENG "a" , ENG "a" , ENG, "a" ); |
2602 | TEST_FETCH_ALL3("(a)?(b)" , "b" , "b" , "" , "b" ); |
2603 | TEST_FETCH_ALL3("(a)?(b)" , "ab" , "ab" , "a" , "b" ); |
2604 | |
2605 | /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */ |
2606 | TEST_SPLIT_SIMPLE0("" , "" ); |
2607 | TEST_SPLIT_SIMPLE0("a" , "" ); |
2608 | TEST_SPLIT_SIMPLE1("," , "a" , "a" ); |
2609 | TEST_SPLIT_SIMPLE1("(,)\\s*" , "a" , "a" ); |
2610 | TEST_SPLIT_SIMPLE2("," , "a,b" , "a" , "b" ); |
2611 | TEST_SPLIT_SIMPLE3("," , "a,b,c" , "a" , "b" , "c" ); |
2612 | TEST_SPLIT_SIMPLE3(",\\s*" , "a,b,c" , "a" , "b" , "c" ); |
2613 | TEST_SPLIT_SIMPLE3(",\\s*" , "a, b, c" , "a" , "b" , "c" ); |
2614 | TEST_SPLIT_SIMPLE3("(,)\\s*" , "a,b" , "a" , "," , "b" ); |
2615 | TEST_SPLIT_SIMPLE3("(,)\\s*" , "a, b" , "a" , "," , "b" ); |
2616 | TEST_SPLIT_SIMPLE2("\\s" , "ab c" , "ab" , "c" ); |
2617 | TEST_SPLIT_SIMPLE3("\\s*" , "ab c" , "a" , "b" , "c" ); |
2618 | /* Not matched sub-strings. */ |
2619 | TEST_SPLIT_SIMPLE2("a|(b)" , "xay" , "x" , "y" ); |
2620 | TEST_SPLIT_SIMPLE3("a|(b)" , "xby" , "x" , "b" , "y" ); |
2621 | /* Empty matches. */ |
2622 | TEST_SPLIT_SIMPLE3("" , "abc" , "a" , "b" , "c" ); |
2623 | TEST_SPLIT_SIMPLE3(" *" , "ab c" , "a" , "b" , "c" ); |
2624 | /* Invalid patterns. */ |
2625 | TEST_SPLIT_SIMPLE0("\\" , "" ); |
2626 | TEST_SPLIT_SIMPLE0("[" , "" ); |
2627 | |
2628 | /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */ |
2629 | TEST_SPLIT0("" , "" , 0, 0); |
2630 | TEST_SPLIT0("a" , "" , 0, 0); |
2631 | TEST_SPLIT0("a" , "" , 0, 1); |
2632 | TEST_SPLIT0("a" , "" , 0, 2); |
2633 | TEST_SPLIT0("a" , "a" , 1, 0); |
2634 | TEST_SPLIT1("," , "a" , 0, 0, "a" ); |
2635 | TEST_SPLIT1("," , "a,b" , 0, 1, "a,b" ); |
2636 | TEST_SPLIT1("(,)\\s*" , "a" , 0, 0, "a" ); |
2637 | TEST_SPLIT1("," , "a,b" , 2, 0, "b" ); |
2638 | TEST_SPLIT2("," , "a,b" , 0, 0, "a" , "b" ); |
2639 | TEST_SPLIT2("," , "a,b,c" , 0, 2, "a" , "b,c" ); |
2640 | TEST_SPLIT2("," , "a,b" , 1, 0, "" , "b" ); |
2641 | TEST_SPLIT2("," , "a," , 0, 0, "a" , "" ); |
2642 | TEST_SPLIT3("," , "a,b,c" , 0, 0, "a" , "b" , "c" ); |
2643 | TEST_SPLIT3(",\\s*" , "a,b,c" , 0, 0, "a" , "b" , "c" ); |
2644 | TEST_SPLIT3(",\\s*" , "a, b, c" , 0, 0, "a" , "b" , "c" ); |
2645 | TEST_SPLIT3("(,)\\s*" , "a,b" , 0, 0, "a" , "," , "b" ); |
2646 | TEST_SPLIT3("(,)\\s*" , "a, b" , 0, 0, "a" , "," , "b" ); |
2647 | /* Not matched sub-strings. */ |
2648 | TEST_SPLIT2("a|(b)" , "xay" , 0, 0, "x" , "y" ); |
2649 | TEST_SPLIT3("a|(b)" , "xby" , 0, -1, "x" , "b" , "y" ); |
2650 | /* Empty matches. */ |
2651 | TEST_SPLIT2(" *" , "ab c" , 1, 0, "b" , "c" ); |
2652 | TEST_SPLIT3("" , "abc" , 0, 0, "a" , "b" , "c" ); |
2653 | TEST_SPLIT3(" *" , "ab c" , 0, 0, "a" , "b" , "c" ); |
2654 | TEST_SPLIT1(" *" , "ab c" , 0, 1, "ab c" ); |
2655 | TEST_SPLIT2(" *" , "ab c" , 0, 2, "a" , "b c" ); |
2656 | TEST_SPLIT3(" *" , "ab c" , 0, 3, "a" , "b" , "c" ); |
2657 | TEST_SPLIT3(" *" , "ab c" , 0, 4, "a" , "b" , "c" ); |
2658 | |
2659 | /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */ |
2660 | TEST_CHECK_REPLACEMENT("" , TRUE, FALSE); |
2661 | TEST_CHECK_REPLACEMENT("a" , TRUE, FALSE); |
2662 | TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}" , TRUE, FALSE); |
2663 | TEST_CHECK_REPLACEMENT("\\0" , TRUE, TRUE); |
2664 | TEST_CHECK_REPLACEMENT("\\n\\2" , TRUE, TRUE); |
2665 | TEST_CHECK_REPLACEMENT("\\g<foo>" , TRUE, TRUE); |
2666 | /* Invalid strings */ |
2667 | TEST_CHECK_REPLACEMENT("\\Q" , FALSE, FALSE); |
2668 | TEST_CHECK_REPLACEMENT("x\\Ay" , FALSE, FALSE); |
2669 | |
2670 | /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */ |
2671 | TEST_EXPAND("a" , "a" , "" , FALSE, "" ); |
2672 | TEST_EXPAND("a" , "a" , "\\0" , FALSE, "a" ); |
2673 | TEST_EXPAND("a" , "a" , "\\1" , FALSE, "" ); |
2674 | TEST_EXPAND("(a)" , "ab" , "\\1" , FALSE, "a" ); |
2675 | TEST_EXPAND("(a)" , "a" , "\\1" , FALSE, "a" ); |
2676 | TEST_EXPAND("(a)" , "a" , "\\g<1>" , FALSE, "a" ); |
2677 | TEST_EXPAND("a" , "a" , "\\0130" , FALSE, "X" ); |
2678 | TEST_EXPAND("a" , "a" , "\\\\\\0" , FALSE, "\\a" ); |
2679 | TEST_EXPAND("a(?P<G>.)c" , "xabcy" , "X\\g<G>X" , FALSE, "XbX" ); |
2680 | #ifndef USE_SYSTEM_PCRE |
2681 | /* PCRE >= 8.34 no longer allows this usage. */ |
2682 | TEST_EXPAND("(.)(?P<1>.)" , "ab" , "\\1" , FALSE, "a" ); |
2683 | TEST_EXPAND("(.)(?P<1>.)" , "ab" , "\\g<1>" , FALSE, "a" ); |
2684 | #endif |
2685 | TEST_EXPAND("." , EURO, "\\0" , FALSE, EURO); |
2686 | TEST_EXPAND("(.)" , EURO, "\\1" , FALSE, EURO); |
2687 | TEST_EXPAND("(?P<G>.)" , EURO, "\\g<G>" , FALSE, EURO); |
2688 | TEST_EXPAND("." , "a" , EURO, FALSE, EURO); |
2689 | TEST_EXPAND("." , "a" , EURO "\\0" , FALSE, EURO "a" ); |
2690 | TEST_EXPAND("." , "" , "\\Lab\\Ec" , FALSE, "abc" ); |
2691 | TEST_EXPAND("." , "" , "\\LaB\\EC" , FALSE, "abC" ); |
2692 | TEST_EXPAND("." , "" , "\\Uab\\Ec" , FALSE, "ABc" ); |
2693 | TEST_EXPAND("." , "" , "a\\ubc" , FALSE, "aBc" ); |
2694 | TEST_EXPAND("." , "" , "a\\lbc" , FALSE, "abc" ); |
2695 | TEST_EXPAND("." , "" , "A\\uBC" , FALSE, "ABC" ); |
2696 | TEST_EXPAND("." , "" , "A\\lBC" , FALSE, "AbC" ); |
2697 | TEST_EXPAND("." , "" , "A\\l\\\\BC" , FALSE, "A\\BC" ); |
2698 | TEST_EXPAND("." , "" , "\\L" AGRAVE "\\E" , FALSE, AGRAVE); |
2699 | TEST_EXPAND("." , "" , "\\U" AGRAVE "\\E" , FALSE, AGRAVE_UPPER); |
2700 | TEST_EXPAND("." , "" , "\\u" AGRAVE "a" , FALSE, AGRAVE_UPPER "a" ); |
2701 | TEST_EXPAND("." , "ab" , "x\\U\\0y\\Ez" , FALSE, "xAYz" ); |
2702 | TEST_EXPAND(".(.)" , "AB" , "x\\L\\1y\\Ez" , FALSE, "xbyz" ); |
2703 | TEST_EXPAND("." , "ab" , "x\\u\\0y\\Ez" , FALSE, "xAyz" ); |
2704 | TEST_EXPAND(".(.)" , "AB" , "x\\l\\1y\\Ez" , FALSE, "xbyz" ); |
2705 | TEST_EXPAND(".(.)" , "a" AGRAVE_UPPER, "x\\l\\1y" , FALSE, "x" AGRAVE "y" ); |
2706 | TEST_EXPAND("a" , "bab" , "\\x{61}" , FALSE, "a" ); |
2707 | TEST_EXPAND("a" , "bab" , "\\x61" , FALSE, "a" ); |
2708 | TEST_EXPAND("a" , "bab" , "\\x5a" , FALSE, "Z" ); |
2709 | TEST_EXPAND("a" , "bab" , "\\0\\x5A" , FALSE, "aZ" ); |
2710 | TEST_EXPAND("a" , "bab" , "\\1\\x{5A}" , FALSE, "Z" ); |
2711 | TEST_EXPAND("a" , "bab" , "\\x{00E0}" , FALSE, AGRAVE); |
2712 | TEST_EXPAND("" , "bab" , "\\x{0634}" , FALSE, SHEEN); |
2713 | TEST_EXPAND("" , "bab" , "\\x{634}" , FALSE, SHEEN); |
2714 | TEST_EXPAND("" , "" , "\\t" , FALSE, "\t" ); |
2715 | TEST_EXPAND("" , "" , "\\v" , FALSE, "\v" ); |
2716 | TEST_EXPAND("" , "" , "\\r" , FALSE, "\r" ); |
2717 | TEST_EXPAND("" , "" , "\\n" , FALSE, "\n" ); |
2718 | TEST_EXPAND("" , "" , "\\f" , FALSE, "\f" ); |
2719 | TEST_EXPAND("" , "" , "\\a" , FALSE, "\a" ); |
2720 | TEST_EXPAND("" , "" , "\\b" , FALSE, "\b" ); |
2721 | TEST_EXPAND("a(.)" , "abc" , "\\0\\b\\1" , FALSE, "ab\bb" ); |
2722 | TEST_EXPAND("a(.)" , "abc" , "\\0141" , FALSE, "a" ); |
2723 | TEST_EXPAND("a(.)" , "abc" , "\\078" , FALSE, "\a8" ); |
2724 | TEST_EXPAND("a(.)" , "abc" , "\\077" , FALSE, "?" ); |
2725 | TEST_EXPAND("a(.)" , "abc" , "\\0778" , FALSE, "?8" ); |
2726 | TEST_EXPAND("a(.)" , "a" AGRAVE "b" , "\\1" , FALSE, AGRAVE); |
2727 | TEST_EXPAND("a(.)" , "a" AGRAVE "b" , "\\1" , TRUE, "\xc3" ); |
2728 | TEST_EXPAND("a(.)" , "a" AGRAVE "b" , "\\0" , TRUE, "a\xc3" ); |
2729 | /* Invalid strings. */ |
2730 | TEST_EXPAND("" , "" , "\\Q" , FALSE, NULL); |
2731 | TEST_EXPAND("" , "" , "x\\Ay" , FALSE, NULL); |
2732 | TEST_EXPAND("" , "" , "\\g<" , FALSE, NULL); |
2733 | TEST_EXPAND("" , "" , "\\g<>" , FALSE, NULL); |
2734 | TEST_EXPAND("" , "" , "\\g<1a>" , FALSE, NULL); |
2735 | TEST_EXPAND("" , "" , "\\g<a$>" , FALSE, NULL); |
2736 | TEST_EXPAND("" , "" , "\\" , FALSE, NULL); |
2737 | TEST_EXPAND("a" , "a" , "\\x{61" , FALSE, NULL); |
2738 | TEST_EXPAND("a" , "a" , "\\x6X" , FALSE, NULL); |
2739 | /* Pattern-less. */ |
2740 | TEST_EXPAND(NULL, NULL, "" , FALSE, "" ); |
2741 | TEST_EXPAND(NULL, NULL, "\\n" , FALSE, "\n" ); |
2742 | /* Invalid strings */ |
2743 | TEST_EXPAND(NULL, NULL, "\\Q" , FALSE, NULL); |
2744 | TEST_EXPAND(NULL, NULL, "x\\Ay" , FALSE, NULL); |
2745 | |
2746 | /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */ |
2747 | TEST_REPLACE("a" , "ababa" , 0, "A" , "AbAbA" ); |
2748 | TEST_REPLACE("a" , "ababa" , 1, "A" , "abAbA" ); |
2749 | TEST_REPLACE("a" , "ababa" , 2, "A" , "abAbA" ); |
2750 | TEST_REPLACE("a" , "ababa" , 3, "A" , "ababA" ); |
2751 | TEST_REPLACE("a" , "ababa" , 4, "A" , "ababA" ); |
2752 | TEST_REPLACE("a" , "ababa" , 5, "A" , "ababa" ); |
2753 | TEST_REPLACE("a" , "ababa" , 6, "A" , "ababa" ); |
2754 | TEST_REPLACE("a" , "abababa" , 2, "A" , "abAbAbA" ); |
2755 | TEST_REPLACE("a" , "abab" , 0, "A" , "AbAb" ); |
2756 | TEST_REPLACE("a" , "baba" , 0, "A" , "bAbA" ); |
2757 | TEST_REPLACE("a" , "bab" , 0, "A" , "bAb" ); |
2758 | TEST_REPLACE("$^" , "abc" , 0, "X" , "abc" ); |
2759 | TEST_REPLACE("(.)a" , "ciao" , 0, "a\\1" , "caio" ); |
2760 | TEST_REPLACE("a." , "abc" , 0, "\\0\\0" , "ababc" ); |
2761 | TEST_REPLACE("a" , "asd" , 0, "\\0101" , "Asd" ); |
2762 | TEST_REPLACE("(a).\\1" , "aba cda" , 0, "\\1\\n" , "a\n cda" ); |
2763 | TEST_REPLACE("a" AGRAVE "a" , "a" AGRAVE "a" , 0, "x" , "x" ); |
2764 | TEST_REPLACE("a" AGRAVE "a" , "a" AGRAVE "a" , 0, OGRAVE, OGRAVE); |
2765 | TEST_REPLACE("[^-]" , "-" EURO "-x-" HSTROKE, 0, "a" , "-a-a-a" ); |
2766 | TEST_REPLACE("[^-]" , "-" EURO "-" HSTROKE, 0, "a\\g<0>a" , "-a" EURO "a-a" HSTROKE "a" ); |
2767 | TEST_REPLACE("-" , "-" EURO "-" HSTROKE, 0, "" , EURO HSTROKE); |
2768 | TEST_REPLACE(".*" , "hello" , 0, "\\U\\0\\E" , "HELLO" ); |
2769 | TEST_REPLACE(".*" , "hello" , 0, "\\u\\0" , "Hello" ); |
2770 | TEST_REPLACE("\\S+" , "hello world" , 0, "\\U-\\0-" , "-HELLO- -WORLD-" ); |
2771 | TEST_REPLACE("." , "a" , 0, "\\A" , NULL); |
2772 | TEST_REPLACE("." , "a" , 0, "\\g" , NULL); |
2773 | |
2774 | /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */ |
2775 | TEST_REPLACE_LIT("a" , "ababa" , 0, "A" , "AbAbA" ); |
2776 | TEST_REPLACE_LIT("a" , "ababa" , 1, "A" , "abAbA" ); |
2777 | TEST_REPLACE_LIT("a" , "ababa" , 2, "A" , "abAbA" ); |
2778 | TEST_REPLACE_LIT("a" , "ababa" , 3, "A" , "ababA" ); |
2779 | TEST_REPLACE_LIT("a" , "ababa" , 4, "A" , "ababA" ); |
2780 | TEST_REPLACE_LIT("a" , "ababa" , 5, "A" , "ababa" ); |
2781 | TEST_REPLACE_LIT("a" , "ababa" , 6, "A" , "ababa" ); |
2782 | TEST_REPLACE_LIT("a" , "abababa" , 2, "A" , "abAbAbA" ); |
2783 | TEST_REPLACE_LIT("a" , "abcadaa" , 0, "A" , "AbcAdAA" ); |
2784 | TEST_REPLACE_LIT("$^" , "abc" , 0, "X" , "abc" ); |
2785 | TEST_REPLACE_LIT("(.)a" , "ciao" , 0, "a\\1" , "ca\\1o" ); |
2786 | TEST_REPLACE_LIT("a." , "abc" , 0, "\\0\\0\\n" , "\\0\\0\\nc" ); |
2787 | TEST_REPLACE_LIT("a" AGRAVE "a" , "a" AGRAVE "a" , 0, "x" , "x" ); |
2788 | TEST_REPLACE_LIT("a" AGRAVE "a" , "a" AGRAVE "a" , 0, OGRAVE, OGRAVE); |
2789 | TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a" , "-a" ENG "a-" HSTROKE); |
2790 | TEST_REPLACE_LIT("[^-]" , "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a" , "-a-a-a" ); |
2791 | TEST_REPLACE_LIT("[^-]" , "-" EURO "-" AGRAVE, 0, "a\\g<0>a" , "-a\\g<0>a-a\\g<0>a" ); |
2792 | TEST_REPLACE_LIT("-" , "-" EURO "-" AGRAVE "-" HSTROKE, 0, "" , EURO AGRAVE HSTROKE); |
2793 | TEST_REPLACE_LIT("(?=[A-Z0-9])" , "RegExTest" , 0, "_" , "_Reg_Ex_Test" ); |
2794 | TEST_REPLACE_LIT("(?=[A-Z0-9])" , "RegExTest" , 1, "_" , "Reg_Ex_Test" ); |
2795 | |
2796 | /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */ |
2797 | TEST_GET_STRING_NUMBER("" , "A" , -1); |
2798 | TEST_GET_STRING_NUMBER("(?P<A>.)" , "A" , 1); |
2799 | TEST_GET_STRING_NUMBER("(?P<A>.)" , "B" , -1); |
2800 | TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)" , "A" , 1); |
2801 | TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)" , "B" , 2); |
2802 | TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)" , "C" , -1); |
2803 | TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)" , "A" , 1); |
2804 | TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)" , "B" , 3); |
2805 | TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)" , "C" , -1); |
2806 | TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)" , "A" , 1); |
2807 | TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)" , "B" , -1); |
2808 | |
2809 | /* TEST_ESCAPE_NUL(string, length, expected) */ |
2810 | TEST_ESCAPE_NUL("hello world" , -1, "hello world" ); |
2811 | TEST_ESCAPE_NUL("hello\0world" , -1, "hello" ); |
2812 | TEST_ESCAPE_NUL("\0world" , -1, "" ); |
2813 | TEST_ESCAPE_NUL("hello world" , 5, "hello" ); |
2814 | TEST_ESCAPE_NUL("hello.world" , 11, "hello.world" ); |
2815 | TEST_ESCAPE_NUL("a(b\\b.$" , 7, "a(b\\b.$" ); |
2816 | TEST_ESCAPE_NUL("hello\0" , 6, "hello\\x00" ); |
2817 | TEST_ESCAPE_NUL("\0world" , 6, "\\x00world" ); |
2818 | TEST_ESCAPE_NUL("\0\0" , 2, "\\x00\\x00" ); |
2819 | TEST_ESCAPE_NUL("hello\0world" , 11, "hello\\x00world" ); |
2820 | TEST_ESCAPE_NUL("hello\0world\0" , 12, "hello\\x00world\\x00" ); |
2821 | TEST_ESCAPE_NUL("hello\\\0world" , 12, "hello\\x00world" ); |
2822 | TEST_ESCAPE_NUL("hello\\\\\0world" , 13, "hello\\\\\\x00world" ); |
2823 | TEST_ESCAPE_NUL("|()[]{}^$*+?." , 13, "|()[]{}^$*+?." ); |
2824 | TEST_ESCAPE_NUL("|()[]{}^$*+?.\\\\" , 15, "|()[]{}^$*+?.\\\\" ); |
2825 | |
2826 | /* TEST_ESCAPE(string, length, expected) */ |
2827 | TEST_ESCAPE("hello world" , -1, "hello world" ); |
2828 | TEST_ESCAPE("hello world" , 5, "hello" ); |
2829 | TEST_ESCAPE("hello.world" , -1, "hello\\.world" ); |
2830 | TEST_ESCAPE("a(b\\b.$" , -1, "a\\(b\\\\b\\.\\$" ); |
2831 | TEST_ESCAPE("hello\0world" , -1, "hello" ); |
2832 | TEST_ESCAPE("hello\0world" , 11, "hello\\0world" ); |
2833 | TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG); |
2834 | TEST_ESCAPE("a$" , -1, "a\\$" ); |
2835 | TEST_ESCAPE("$a" , -1, "\\$a" ); |
2836 | TEST_ESCAPE("a$a" , -1, "a\\$a" ); |
2837 | TEST_ESCAPE("$a$" , -1, "\\$a\\$" ); |
2838 | TEST_ESCAPE("$a$" , 0, "" ); |
2839 | TEST_ESCAPE("$a$" , 1, "\\$" ); |
2840 | TEST_ESCAPE("$a$" , 2, "\\$a" ); |
2841 | TEST_ESCAPE("$a$" , 3, "\\$a\\$" ); |
2842 | TEST_ESCAPE("$a$" , 4, "\\$a\\$\\0" ); |
2843 | TEST_ESCAPE("|()[]{}^$*+?." , -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\." ); |
2844 | TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a" , -1, |
2845 | "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a" ); |
2846 | |
2847 | /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */ |
2848 | TEST_MATCH_ALL0("<.*>" , "" , -1, 0); |
2849 | TEST_MATCH_ALL0("a+" , "" , -1, 0); |
2850 | TEST_MATCH_ALL0("a+" , "a" , 0, 0); |
2851 | TEST_MATCH_ALL0("a+" , "a" , -1, 1); |
2852 | TEST_MATCH_ALL1("<.*>" , "<a>" , -1, 0, "<a>" , 0, 3); |
2853 | TEST_MATCH_ALL1("a+" , "a" , -1, 0, "a" , 0, 1); |
2854 | TEST_MATCH_ALL1("a+" , "aa" , 1, 0, "a" , 0, 1); |
2855 | TEST_MATCH_ALL1("a+" , "aa" , -1, 1, "a" , 1, 2); |
2856 | TEST_MATCH_ALL1("a+" , "aa" , 2, 1, "a" , 1, 2); |
2857 | TEST_MATCH_ALL1(".+" , ENG, -1, 0, ENG, 0, 2); |
2858 | TEST_MATCH_ALL2("<.*>" , "<a><b>" , -1, 0, "<a><b>" , 0, 6, "<a>" , 0, 3); |
2859 | TEST_MATCH_ALL2("a+" , "aa" , -1, 0, "aa" , 0, 2, "a" , 0, 1); |
2860 | TEST_MATCH_ALL2(".+" , ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2); |
2861 | TEST_MATCH_ALL3("<.*>" , "<a><b><c>" , -1, 0, "<a><b><c>" , 0, 9, |
2862 | "<a><b>" , 0, 6, "<a>" , 0, 3); |
2863 | TEST_MATCH_ALL3("a+" , "aaa" , -1, 0, "aaa" , 0, 3, "aa" , 0, 2, "a" , 0, 1); |
2864 | |
2865 | /* NOTEMPTY matching */ |
2866 | TEST_MATCH_NOTEMPTY("a?b?" , "xyz" , FALSE); |
2867 | TEST_MATCH_NOTEMPTY_ATSTART("a?b?" , "xyz" , TRUE); |
2868 | |
2869 | return g_test_run (); |
2870 | } |
2871 | |