1 /* GRegex -- regular expression API wrapper around PCRE.
3 * Copyright (C) 1999, 2000 Scott Wimer
4 * Copyright (C) 2004, Matthias Clasen <mclasen@redhat.com>
5 * Copyright (C) 2005 - 2007, Marco Barisione <marco@barisione.org>
6 * Copyright (C) 2022, Marco Trevisan <marco.trevisan@canonical.com>
8 * SPDX-License-Identifier: LGPL-2.1-or-later
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, see <http://www.gnu.org/licenses/>.
29 #define PCRE2_CODE_UNIT_WIDTH 8
36 #include "gmessages.h"
37 #include "gstrfuncs.h"
39 #include "gtestutils.h"
44 * @title: Perl-compatible regular expressions
45 * @short_description: matches strings against regular expressions
46 * @see_also: [Regular expression syntax][glib-regex-syntax]
48 * The g_regex_*() functions implement regular
49 * expression pattern matching using syntax and semantics similar to
50 * Perl regular expression.
52 * Some functions accept a @start_position argument, setting it differs
53 * from just passing over a shortened string and setting %G_REGEX_MATCH_NOTBOL
54 * in the case of a pattern that begins with any kind of lookbehind assertion.
55 * For example, consider the pattern "\Biss\B" which finds occurrences of "iss"
56 * in the middle of words. ("\B" matches only if the current position in the
57 * subject is not a word boundary.) When applied to the string "Mississipi"
58 * from the fourth byte, namely "issipi", it does not match, because "\B" is
59 * always false at the start of the subject, which is deemed to be a word
60 * boundary. However, if the entire string is passed , but with
61 * @start_position set to 4, it finds the second occurrence of "iss" because
62 * it is able to look behind the starting point to discover that it is
63 * preceded by a letter.
65 * Note that, unless you set the %G_REGEX_RAW flag, all the strings passed
66 * to these functions must be encoded in UTF-8. The lengths and the positions
67 * inside the strings are in bytes and not in characters, so, for instance,
68 * "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a
69 * single character. If you set %G_REGEX_RAW the strings can be non-valid
70 * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two
71 * bytes and two characters long.
73 * When matching a pattern, "\n" matches only against a "\n" character in
74 * the string, and "\r" matches only a "\r" character. To match any newline
75 * sequence use "\R". This particular group matches either the two-character
76 * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed,
77 * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"),
78 * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line
79 * separator, U+2028), or PS (paragraph separator, U+2029).
81 * The behaviour of the dot, circumflex, and dollar metacharacters are
82 * affected by newline characters, the default is to recognize any newline
83 * character (the same characters recognized by "\R"). This can be changed
84 * with %G_REGEX_NEWLINE_CR, %G_REGEX_NEWLINE_LF and %G_REGEX_NEWLINE_CRLF
85 * compile options, and with %G_REGEX_MATCH_NEWLINE_ANY,
86 * %G_REGEX_MATCH_NEWLINE_CR, %G_REGEX_MATCH_NEWLINE_LF and
87 * %G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also
88 * relevant when compiling a pattern if %G_REGEX_EXTENDED is set, and an
89 * unescaped "#" outside a character class is encountered. This indicates
90 * a comment that lasts until after the next newline.
92 * Creating and manipulating the same #GRegex structure from different
93 * threads is not a problem as #GRegex does not modify its internal
94 * state between creation and destruction, on the other hand #GMatchInfo
97 * The regular expressions low-level functionalities are obtained through
99 * [PCRE](http://www.pcre.org/)
100 * library written by Philip Hazel.
103 #define G_REGEX_PCRE_GENERIC_MASK (PCRE2_ANCHORED | \
104 PCRE2_NO_UTF_CHECK | \
107 /* Mask of all the possible values for GRegexCompileFlags. */
108 #define G_REGEX_COMPILE_MASK (G_REGEX_DEFAULT | \
110 G_REGEX_MULTILINE | \
114 G_REGEX_DOLLAR_ENDONLY | \
117 G_REGEX_NO_AUTO_CAPTURE | \
119 G_REGEX_FIRSTLINE | \
121 G_REGEX_NEWLINE_CR | \
122 G_REGEX_NEWLINE_LF | \
123 G_REGEX_NEWLINE_CRLF | \
124 G_REGEX_NEWLINE_ANYCRLF | \
127 #define G_REGEX_PCRE2_COMPILE_MASK (PCRE2_ALLOW_EMPTY_CLASS | \
129 PCRE2_AUTO_CALLOUT | \
131 PCRE2_DOLLAR_ENDONLY | \
136 PCRE2_MATCH_UNSET_BACKREF | \
140 PCRE2_NO_AUTO_CAPTURE | \
141 PCRE2_NO_AUTO_POSSESS | \
142 PCRE2_NO_DOTSTAR_ANCHOR | \
143 PCRE2_NO_START_OPTIMIZE | \
147 PCRE2_NEVER_BACKSLASH_C | \
148 PCRE2_ALT_CIRCUMFLEX | \
149 PCRE2_ALT_VERBNAMES | \
150 PCRE2_USE_OFFSET_LIMIT | \
151 PCRE2_EXTENDED_MORE | \
153 PCRE2_MATCH_INVALID_UTF | \
154 G_REGEX_PCRE_GENERIC_MASK)
156 #define G_REGEX_COMPILE_NONPCRE_MASK (PCRE2_UTF)
158 /* Mask of all the possible values for GRegexMatchFlags. */
159 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_DEFAULT | \
160 G_REGEX_MATCH_ANCHORED | \
161 G_REGEX_MATCH_NOTBOL | \
162 G_REGEX_MATCH_NOTEOL | \
163 G_REGEX_MATCH_NOTEMPTY | \
164 G_REGEX_MATCH_PARTIAL | \
165 G_REGEX_MATCH_NEWLINE_CR | \
166 G_REGEX_MATCH_NEWLINE_LF | \
167 G_REGEX_MATCH_NEWLINE_CRLF | \
168 G_REGEX_MATCH_NEWLINE_ANY | \
169 G_REGEX_MATCH_NEWLINE_ANYCRLF | \
170 G_REGEX_MATCH_BSR_ANYCRLF | \
171 G_REGEX_MATCH_BSR_ANY | \
172 G_REGEX_MATCH_PARTIAL_SOFT | \
173 G_REGEX_MATCH_PARTIAL_HARD | \
174 G_REGEX_MATCH_NOTEMPTY_ATSTART)
176 #define G_REGEX_PCRE2_MATCH_MASK (PCRE2_NOTBOL |\
179 PCRE2_NOTEMPTY_ATSTART |\
180 PCRE2_PARTIAL_SOFT |\
181 PCRE2_PARTIAL_HARD |\
183 PCRE2_COPY_MATCHED_SUBJECT |\
184 G_REGEX_PCRE_GENERIC_MASK)
186 /* TODO: Support PCRE2_NEWLINE_NUL */
187 #define G_REGEX_NEWLINE_MASK (PCRE2_NEWLINE_CR | \
189 PCRE2_NEWLINE_CRLF | \
190 PCRE2_NEWLINE_ANYCRLF)
192 /* Some match options are not supported when using JIT as stated in the
193 * pcre2jit man page under the «UNSUPPORTED OPTIONS AND PATTERN ITEMS» section:
194 * https://www.pcre.org/current/doc/html/pcre2jit.html#SEC5
196 #define G_REGEX_PCRE2_JIT_UNSUPPORTED_OPTIONS (PCRE2_ANCHORED | \
199 #define G_REGEX_COMPILE_NEWLINE_MASK (G_REGEX_NEWLINE_CR | \
200 G_REGEX_NEWLINE_LF | \
201 G_REGEX_NEWLINE_CRLF | \
202 G_REGEX_NEWLINE_ANYCRLF)
204 #define G_REGEX_MATCH_NEWLINE_MASK (G_REGEX_MATCH_NEWLINE_CR | \
205 G_REGEX_MATCH_NEWLINE_LF | \
206 G_REGEX_MATCH_NEWLINE_CRLF | \
207 G_REGEX_MATCH_NEWLINE_ANY | \
208 G_REGEX_MATCH_NEWLINE_ANYCRLF)
210 /* if the string is in UTF-8 use g_utf8_ functions, else use
212 #define NEXT_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
214 g_utf8_next_char (s))
215 #define PREV_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
217 g_utf8_prev_char (s))
221 gint ref_count; /* the ref count (atomic) */
222 GRegex *regex; /* the regex */
223 uint32_t match_opts; /* pcre match options used at match time on the regex */
224 gint matches; /* number of matching sub patterns, guaranteed to be <= (n_subpatterns + 1) if doing a single match (rather than matching all) */
225 uint32_t n_subpatterns; /* total number of sub patterns in the regex */
226 gint pos; /* position in the string where last match left off */
227 uint32_t n_offsets; /* number of offsets */
228 gint *offsets; /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
229 gint *workspace; /* workspace for pcre2_dfa_match() */
230 PCRE2_SIZE n_workspace; /* number of workspace elements */
231 const gchar *string; /* string passed to the match function */
232 gssize string_len; /* length of string, in bytes */
233 pcre2_match_context *match_context;
234 pcre2_match_data *match_data;
235 pcre2_jit_stack *jit_stack;
247 gint ref_count; /* the ref count for the immutable part (atomic) */
248 gchar *pattern; /* the pattern */
249 pcre2_code *pcre_re; /* compiled form of the pattern */
250 uint32_t compile_opts; /* options used at compile time on the pattern, pcre2 values */
251 GRegexCompileFlags orig_compile_opts; /* options used at compile time on the pattern, gregex values */
252 uint32_t match_opts; /* pcre2 options used at match time on the regex */
253 GRegexMatchFlags orig_match_opts; /* options used as default match options, gregex values */
254 uint32_t jit_options; /* options which were enabled for jit compiler */
255 JITStatus jit_status; /* indicates the status of jit compiler for this compiled regex */
256 /* The jit_status here does _not_ correspond to whether we used the JIT in the last invocation,
257 * which may be affected by match_options or a JIT_STACK_LIMIT error, but whether it was ever
258 * enabled for the current regex AND current set of jit_options.
259 * JIT_STATUS_DEFAULT means enablement was never tried,
260 * JIT_STATUS_ENABLED means it was tried and successful (even if we're not currently using it),
261 * and JIT_STATUS_DISABLED means it was tried and failed (so we shouldn't try again).
265 /* TRUE if ret is an error code, FALSE otherwise. */
266 #define IS_PCRE2_ERROR(ret) ((ret) < PCRE2_ERROR_NOMATCH && (ret) != PCRE2_ERROR_PARTIAL)
268 typedef struct _InterpolationData InterpolationData;
269 static gboolean interpolation_list_needs_match (GList *list);
270 static gboolean interpolate_replacement (const GMatchInfo *match_info,
273 static GList *split_replacement (const gchar *replacement,
275 static void free_interpolation_data (InterpolationData *data);
278 get_pcre2_compile_options (GRegexCompileFlags compile_flags)
280 /* Maps compile flags to pcre2 values */
281 uint32_t pcre2_flags = 0;
283 if (compile_flags & G_REGEX_CASELESS)
284 pcre2_flags |= PCRE2_CASELESS;
285 if (compile_flags & G_REGEX_MULTILINE)
286 pcre2_flags |= PCRE2_MULTILINE;
287 if (compile_flags & G_REGEX_DOTALL)
288 pcre2_flags |= PCRE2_DOTALL;
289 if (compile_flags & G_REGEX_EXTENDED)
290 pcre2_flags |= PCRE2_EXTENDED;
291 if (compile_flags & G_REGEX_ANCHORED)
292 pcre2_flags |= PCRE2_ANCHORED;
293 if (compile_flags & G_REGEX_DOLLAR_ENDONLY)
294 pcre2_flags |= PCRE2_DOLLAR_ENDONLY;
295 if (compile_flags & G_REGEX_UNGREEDY)
296 pcre2_flags |= PCRE2_UNGREEDY;
297 if (!(compile_flags & G_REGEX_RAW))
298 pcre2_flags |= PCRE2_UTF;
299 if (compile_flags & G_REGEX_NO_AUTO_CAPTURE)
300 pcre2_flags |= PCRE2_NO_AUTO_CAPTURE;
301 if (compile_flags & G_REGEX_FIRSTLINE)
302 pcre2_flags |= PCRE2_FIRSTLINE;
303 if (compile_flags & G_REGEX_DUPNAMES)
304 pcre2_flags |= PCRE2_DUPNAMES;
306 return pcre2_flags & G_REGEX_PCRE2_COMPILE_MASK;
310 get_pcre2_match_options (GRegexMatchFlags match_flags,
311 GRegexCompileFlags compile_flags)
313 /* Maps match flags to pcre2 values */
314 uint32_t pcre2_flags = 0;
316 if (match_flags & G_REGEX_MATCH_ANCHORED)
317 pcre2_flags |= PCRE2_ANCHORED;
318 if (match_flags & G_REGEX_MATCH_NOTBOL)
319 pcre2_flags |= PCRE2_NOTBOL;
320 if (match_flags & G_REGEX_MATCH_NOTEOL)
321 pcre2_flags |= PCRE2_NOTEOL;
322 if (match_flags & G_REGEX_MATCH_NOTEMPTY)
323 pcre2_flags |= PCRE2_NOTEMPTY;
324 if (match_flags & G_REGEX_MATCH_PARTIAL_SOFT)
325 pcre2_flags |= PCRE2_PARTIAL_SOFT;
326 if (match_flags & G_REGEX_MATCH_PARTIAL_HARD)
327 pcre2_flags |= PCRE2_PARTIAL_HARD;
328 if (match_flags & G_REGEX_MATCH_NOTEMPTY_ATSTART)
329 pcre2_flags |= PCRE2_NOTEMPTY_ATSTART;
331 if (compile_flags & G_REGEX_RAW)
332 pcre2_flags |= PCRE2_NO_UTF_CHECK;
334 return pcre2_flags & G_REGEX_PCRE2_MATCH_MASK;
337 static GRegexCompileFlags
338 g_regex_compile_flags_from_pcre2 (uint32_t pcre2_flags)
340 GRegexCompileFlags compile_flags = G_REGEX_DEFAULT;
342 if (pcre2_flags & PCRE2_CASELESS)
343 compile_flags |= G_REGEX_CASELESS;
344 if (pcre2_flags & PCRE2_MULTILINE)
345 compile_flags |= G_REGEX_MULTILINE;
346 if (pcre2_flags & PCRE2_DOTALL)
347 compile_flags |= G_REGEX_DOTALL;
348 if (pcre2_flags & PCRE2_EXTENDED)
349 compile_flags |= G_REGEX_EXTENDED;
350 if (pcre2_flags & PCRE2_ANCHORED)
351 compile_flags |= G_REGEX_ANCHORED;
352 if (pcre2_flags & PCRE2_DOLLAR_ENDONLY)
353 compile_flags |= G_REGEX_DOLLAR_ENDONLY;
354 if (pcre2_flags & PCRE2_UNGREEDY)
355 compile_flags |= G_REGEX_UNGREEDY;
356 if (!(pcre2_flags & PCRE2_UTF))
357 compile_flags |= G_REGEX_RAW;
358 if (pcre2_flags & PCRE2_NO_AUTO_CAPTURE)
359 compile_flags |= G_REGEX_NO_AUTO_CAPTURE;
360 if (pcre2_flags & PCRE2_FIRSTLINE)
361 compile_flags |= G_REGEX_FIRSTLINE;
362 if (pcre2_flags & PCRE2_DUPNAMES)
363 compile_flags |= G_REGEX_DUPNAMES;
365 return compile_flags & G_REGEX_COMPILE_MASK;
368 static GRegexMatchFlags
369 g_regex_match_flags_from_pcre2 (uint32_t pcre2_flags)
371 GRegexMatchFlags match_flags = G_REGEX_MATCH_DEFAULT;
373 if (pcre2_flags & PCRE2_ANCHORED)
374 match_flags |= G_REGEX_MATCH_ANCHORED;
375 if (pcre2_flags & PCRE2_NOTBOL)
376 match_flags |= G_REGEX_MATCH_NOTBOL;
377 if (pcre2_flags & PCRE2_NOTEOL)
378 match_flags |= G_REGEX_MATCH_NOTEOL;
379 if (pcre2_flags & PCRE2_NOTEMPTY)
380 match_flags |= G_REGEX_MATCH_NOTEMPTY;
381 if (pcre2_flags & PCRE2_PARTIAL_SOFT)
382 match_flags |= G_REGEX_MATCH_PARTIAL_SOFT;
383 if (pcre2_flags & PCRE2_PARTIAL_HARD)
384 match_flags |= G_REGEX_MATCH_PARTIAL_HARD;
385 if (pcre2_flags & PCRE2_NOTEMPTY_ATSTART)
386 match_flags |= G_REGEX_MATCH_NOTEMPTY_ATSTART;
388 return (match_flags & G_REGEX_MATCH_MASK);
392 get_pcre2_newline_compile_options (GRegexCompileFlags compile_flags)
394 compile_flags &= G_REGEX_COMPILE_NEWLINE_MASK;
396 switch (compile_flags)
398 case G_REGEX_NEWLINE_CR:
399 return PCRE2_NEWLINE_CR;
400 case G_REGEX_NEWLINE_LF:
401 return PCRE2_NEWLINE_LF;
402 case G_REGEX_NEWLINE_CRLF:
403 return PCRE2_NEWLINE_CRLF;
404 case G_REGEX_NEWLINE_ANYCRLF:
405 return PCRE2_NEWLINE_ANYCRLF;
407 if (compile_flags != 0)
410 return PCRE2_NEWLINE_ANY;
415 get_pcre2_newline_match_options (GRegexMatchFlags match_flags)
417 switch (match_flags & G_REGEX_MATCH_NEWLINE_MASK)
419 case G_REGEX_MATCH_NEWLINE_CR:
420 return PCRE2_NEWLINE_CR;
421 case G_REGEX_MATCH_NEWLINE_LF:
422 return PCRE2_NEWLINE_LF;
423 case G_REGEX_MATCH_NEWLINE_CRLF:
424 return PCRE2_NEWLINE_CRLF;
425 case G_REGEX_MATCH_NEWLINE_ANY:
426 return PCRE2_NEWLINE_ANY;
427 case G_REGEX_MATCH_NEWLINE_ANYCRLF:
428 return PCRE2_NEWLINE_ANYCRLF;
435 get_pcre2_bsr_compile_options (GRegexCompileFlags compile_flags)
437 if (compile_flags & G_REGEX_BSR_ANYCRLF)
438 return PCRE2_BSR_ANYCRLF;
440 return PCRE2_BSR_UNICODE;
444 get_pcre2_bsr_match_options (GRegexMatchFlags match_flags)
446 if (match_flags & G_REGEX_MATCH_BSR_ANYCRLF)
447 return PCRE2_BSR_ANYCRLF;
449 if (match_flags & G_REGEX_MATCH_BSR_ANY)
450 return PCRE2_BSR_UNICODE;
456 get_pcre2_error_string (int errcode)
458 PCRE2_UCHAR8 error_msg[2048];
461 err_length = pcre2_get_error_message (errcode, error_msg,
462 G_N_ELEMENTS (error_msg));
467 /* The array is always filled with a trailing zero */
468 g_assert ((size_t) err_length < G_N_ELEMENTS (error_msg));
469 return g_memdup2 (error_msg, err_length + 1);
473 translate_match_error (gint errcode)
477 case PCRE2_ERROR_NOMATCH:
480 case PCRE2_ERROR_NULL:
481 /* NULL argument, this should not happen in GRegex */
482 g_critical ("A NULL argument was passed to PCRE");
484 case PCRE2_ERROR_BADOPTION:
485 return "bad options";
486 case PCRE2_ERROR_BADMAGIC:
487 return _("corrupted object");
488 case PCRE2_ERROR_NOMEMORY:
489 return _("out of memory");
490 case PCRE2_ERROR_NOSUBSTRING:
491 /* not used by pcre2_match() */
493 case PCRE2_ERROR_MATCHLIMIT:
494 case PCRE2_ERROR_CALLOUT:
495 /* callouts are not implemented */
497 case PCRE2_ERROR_BADUTFOFFSET:
498 /* we do not check if strings are valid */
500 case PCRE2_ERROR_PARTIAL:
503 case PCRE2_ERROR_INTERNAL:
504 return _("internal error");
505 case PCRE2_ERROR_DFA_UITEM:
506 return _("the pattern contains items not supported for partial matching");
507 case PCRE2_ERROR_DFA_UCOND:
508 return _("back references as conditions are not supported for partial matching");
509 case PCRE2_ERROR_DFA_WSSIZE:
510 /* handled expanding the workspace */
512 case PCRE2_ERROR_DFA_RECURSE:
513 case PCRE2_ERROR_RECURSIONLIMIT:
514 return _("recursion limit reached");
515 case PCRE2_ERROR_BADOFFSET:
516 return _("bad offset");
517 case PCRE2_ERROR_RECURSELOOP:
518 return _("recursion loop");
519 case PCRE2_ERROR_JIT_BADOPTION:
520 /* should not happen in GRegex since we check modes before each match */
521 return _("matching mode is requested that was not compiled for JIT");
529 get_match_error_message (int errcode)
531 const char *msg = translate_match_error (errcode);
535 return g_strdup (msg);
537 error_string = get_pcre2_error_string (errcode);
542 return g_strdup (_("unknown error"));
546 translate_compile_error (gint *errcode, const gchar **errmsg)
548 /* If errcode is known we put the translatable error message in
549 * errmsg. If errcode is unknown we put the generic
550 * G_REGEX_ERROR_COMPILE error code in errcode.
551 * Note that there can be more PCRE errors with the same GRegexError
552 * and that some PCRE errors are useless for us.
554 gint original_errcode = *errcode;
559 switch (original_errcode)
561 case PCRE2_ERROR_END_BACKSLASH:
562 *errcode = G_REGEX_ERROR_STRAY_BACKSLASH;
563 *errmsg = _("\\ at end of pattern");
565 case PCRE2_ERROR_END_BACKSLASH_C:
566 *errcode = G_REGEX_ERROR_MISSING_CONTROL_CHAR;
567 *errmsg = _("\\c at end of pattern");
569 case PCRE2_ERROR_UNKNOWN_ESCAPE:
570 case PCRE2_ERROR_UNSUPPORTED_ESCAPE_SEQUENCE:
571 *errcode = G_REGEX_ERROR_UNRECOGNIZED_ESCAPE;
572 *errmsg = _("unrecognized character following \\");
574 case PCRE2_ERROR_QUANTIFIER_OUT_OF_ORDER:
575 *errcode = G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER;
576 *errmsg = _("numbers out of order in {} quantifier");
578 case PCRE2_ERROR_QUANTIFIER_TOO_BIG:
579 *errcode = G_REGEX_ERROR_QUANTIFIER_TOO_BIG;
580 *errmsg = _("number too big in {} quantifier");
582 case PCRE2_ERROR_MISSING_SQUARE_BRACKET:
583 *errcode = G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS;
584 *errmsg = _("missing terminating ] for character class");
586 case PCRE2_ERROR_ESCAPE_INVALID_IN_CLASS:
587 *errcode = G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS;
588 *errmsg = _("invalid escape sequence in character class");
590 case PCRE2_ERROR_CLASS_RANGE_ORDER:
591 *errcode = G_REGEX_ERROR_RANGE_OUT_OF_ORDER;
592 *errmsg = _("range out of order in character class");
594 case PCRE2_ERROR_QUANTIFIER_INVALID:
595 case PCRE2_ERROR_INTERNAL_UNEXPECTED_REPEAT:
596 *errcode = G_REGEX_ERROR_NOTHING_TO_REPEAT;
597 *errmsg = _("nothing to repeat");
599 case PCRE2_ERROR_INVALID_AFTER_PARENS_QUERY:
600 *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
601 *errmsg = _("unrecognized character after (? or (?-");
603 case PCRE2_ERROR_POSIX_CLASS_NOT_IN_CLASS:
604 *errcode = G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS;
605 *errmsg = _("POSIX named classes are supported only within a class");
607 case PCRE2_ERROR_POSIX_NO_SUPPORT_COLLATING:
608 *errcode = G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED;
609 *errmsg = _("POSIX collating elements are not supported");
611 case PCRE2_ERROR_MISSING_CLOSING_PARENTHESIS:
612 case PCRE2_ERROR_UNMATCHED_CLOSING_PARENTHESIS:
613 case PCRE2_ERROR_PARENS_QUERY_R_MISSING_CLOSING:
614 *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
615 *errmsg = _("missing terminating )");
617 case PCRE2_ERROR_BAD_SUBPATTERN_REFERENCE:
618 *errcode = G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE;
619 *errmsg = _("reference to non-existent subpattern");
621 case PCRE2_ERROR_MISSING_COMMENT_CLOSING:
622 *errcode = G_REGEX_ERROR_UNTERMINATED_COMMENT;
623 *errmsg = _("missing ) after comment");
625 case PCRE2_ERROR_PATTERN_TOO_LARGE:
626 *errcode = G_REGEX_ERROR_EXPRESSION_TOO_LARGE;
627 *errmsg = _("regular expression is too large");
629 case PCRE2_ERROR_MISSING_CONDITION_CLOSING:
630 *errcode = G_REGEX_ERROR_MALFORMED_CONDITION;
631 *errmsg = _("malformed number or name after (?(");
633 case PCRE2_ERROR_LOOKBEHIND_NOT_FIXED_LENGTH:
634 *errcode = G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND;
635 *errmsg = _("lookbehind assertion is not fixed length");
637 case PCRE2_ERROR_TOO_MANY_CONDITION_BRANCHES:
638 *errcode = G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES;
639 *errmsg = _("conditional group contains more than two branches");
641 case PCRE2_ERROR_CONDITION_ASSERTION_EXPECTED:
642 *errcode = G_REGEX_ERROR_ASSERTION_EXPECTED;
643 *errmsg = _("assertion expected after (?(");
645 case PCRE2_ERROR_BAD_RELATIVE_REFERENCE:
646 *errcode = G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE;
647 *errmsg = _("a numbered reference must not be zero");
649 case PCRE2_ERROR_UNKNOWN_POSIX_CLASS:
650 *errcode = G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME;
651 *errmsg = _("unknown POSIX class name");
653 case PCRE2_ERROR_CODE_POINT_TOO_BIG:
654 case PCRE2_ERROR_INVALID_HEXADECIMAL:
655 *errcode = G_REGEX_ERROR_HEX_CODE_TOO_LARGE;
656 *errmsg = _("character value in \\x{...} sequence is too large");
658 case PCRE2_ERROR_LOOKBEHIND_INVALID_BACKSLASH_C:
659 *errcode = G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND;
660 *errmsg = _("\\C not allowed in lookbehind assertion");
662 case PCRE2_ERROR_MISSING_NAME_TERMINATOR:
663 *errcode = G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR;
664 *errmsg = _("missing terminator in subpattern name");
666 case PCRE2_ERROR_DUPLICATE_SUBPATTERN_NAME:
667 *errcode = G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME;
668 *errmsg = _("two named subpatterns have the same name");
670 case PCRE2_ERROR_MALFORMED_UNICODE_PROPERTY:
671 *errcode = G_REGEX_ERROR_MALFORMED_PROPERTY;
672 *errmsg = _("malformed \\P or \\p sequence");
674 case PCRE2_ERROR_UNKNOWN_UNICODE_PROPERTY:
675 *errcode = G_REGEX_ERROR_UNKNOWN_PROPERTY;
676 *errmsg = _("unknown property name after \\P or \\p");
678 case PCRE2_ERROR_SUBPATTERN_NAME_TOO_LONG:
679 *errcode = G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG;
680 *errmsg = _("subpattern name is too long (maximum 32 characters)");
682 case PCRE2_ERROR_TOO_MANY_NAMED_SUBPATTERNS:
683 *errcode = G_REGEX_ERROR_TOO_MANY_SUBPATTERNS;
684 *errmsg = _("too many named subpatterns (maximum 10,000)");
686 case PCRE2_ERROR_OCTAL_BYTE_TOO_BIG:
687 *errcode = G_REGEX_ERROR_INVALID_OCTAL_VALUE;
688 *errmsg = _("octal value is greater than \\377");
690 case PCRE2_ERROR_DEFINE_TOO_MANY_BRANCHES:
691 *errcode = G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE;
692 *errmsg = _("DEFINE group contains more than one branch");
694 case PCRE2_ERROR_INTERNAL_UNKNOWN_NEWLINE:
695 *errcode = G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS;
696 *errmsg = _("inconsistent NEWLINE options");
698 case PCRE2_ERROR_BACKSLASH_G_SYNTAX:
699 *errcode = G_REGEX_ERROR_MISSING_BACK_REFERENCE;
700 *errmsg = _("\\g is not followed by a braced, angle-bracketed, or quoted name or "
701 "number, or by a plain number");
703 case PCRE2_ERROR_VERB_ARGUMENT_NOT_ALLOWED:
704 *errcode = G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN;
705 *errmsg = _("an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)");
707 case PCRE2_ERROR_VERB_UNKNOWN:
708 *errcode = G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB;
709 *errmsg = _("(*VERB) not recognized");
711 case PCRE2_ERROR_SUBPATTERN_NUMBER_TOO_BIG:
712 *errcode = G_REGEX_ERROR_NUMBER_TOO_BIG;
713 *errmsg = _("number is too big");
715 case PCRE2_ERROR_SUBPATTERN_NAME_EXPECTED:
716 *errcode = G_REGEX_ERROR_MISSING_SUBPATTERN_NAME;
717 *errmsg = _("missing subpattern name after (?&");
719 case PCRE2_ERROR_SUBPATTERN_NAMES_MISMATCH:
720 *errcode = G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME;
721 *errmsg = _("different names for subpatterns of the same number are not allowed");
723 case PCRE2_ERROR_MARK_MISSING_ARGUMENT:
724 *errcode = G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED;
725 *errmsg = _("(*MARK) must have an argument");
727 case PCRE2_ERROR_BACKSLASH_C_SYNTAX:
728 *errcode = G_REGEX_ERROR_INVALID_CONTROL_CHAR;
729 *errmsg = _( "\\c must be followed by an ASCII character");
731 case PCRE2_ERROR_BACKSLASH_K_SYNTAX:
732 *errcode = G_REGEX_ERROR_MISSING_NAME;
733 *errmsg = _("\\k is not followed by a braced, angle-bracketed, or quoted name");
735 case PCRE2_ERROR_BACKSLASH_N_IN_CLASS:
736 *errcode = G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS;
737 *errmsg = _("\\N is not supported in a class");
739 case PCRE2_ERROR_VERB_NAME_TOO_LONG:
740 *errcode = G_REGEX_ERROR_NAME_TOO_LONG;
741 *errmsg = _("name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)");
743 case PCRE2_ERROR_INTERNAL_CODE_OVERFLOW:
744 *errcode = G_REGEX_ERROR_INTERNAL;
745 *errmsg = _("code overflow");
747 case PCRE2_ERROR_UNRECOGNIZED_AFTER_QUERY_P:
748 *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
749 *errmsg = _("unrecognized character after (?P");
751 case PCRE2_ERROR_INTERNAL_OVERRAN_WORKSPACE:
752 *errcode = G_REGEX_ERROR_INTERNAL;
753 *errmsg = _("overran compiling workspace");
755 case PCRE2_ERROR_INTERNAL_MISSING_SUBPATTERN:
756 *errcode = G_REGEX_ERROR_INTERNAL;
757 *errmsg = _("previously-checked referenced subpattern not found");
759 case PCRE2_ERROR_HEAP_FAILED:
760 case PCRE2_ERROR_INTERNAL_PARSED_OVERFLOW:
761 case PCRE2_ERROR_UNICODE_NOT_SUPPORTED:
762 case PCRE2_ERROR_UNICODE_DISALLOWED_CODE_POINT:
763 case PCRE2_ERROR_NO_SURROGATES_IN_UTF16:
764 case PCRE2_ERROR_INTERNAL_BAD_CODE_LOOKBEHINDS:
765 case PCRE2_ERROR_UNICODE_PROPERTIES_UNAVAILABLE:
766 case PCRE2_ERROR_INTERNAL_STUDY_ERROR:
767 case PCRE2_ERROR_UTF_IS_DISABLED:
768 case PCRE2_ERROR_UCP_IS_DISABLED:
769 case PCRE2_ERROR_INTERNAL_BAD_CODE_AUTO_POSSESS:
770 case PCRE2_ERROR_BACKSLASH_C_LIBRARY_DISABLED:
771 case PCRE2_ERROR_INTERNAL_BAD_CODE:
772 case PCRE2_ERROR_INTERNAL_BAD_CODE_IN_SKIP:
773 *errcode = G_REGEX_ERROR_INTERNAL;
775 case PCRE2_ERROR_INVALID_SUBPATTERN_NAME:
776 case PCRE2_ERROR_CLASS_INVALID_RANGE:
777 case PCRE2_ERROR_ZERO_RELATIVE_REFERENCE:
778 case PCRE2_ERROR_PARENTHESES_STACK_CHECK:
779 case PCRE2_ERROR_LOOKBEHIND_TOO_COMPLICATED:
780 case PCRE2_ERROR_CALLOUT_NUMBER_TOO_BIG:
781 case PCRE2_ERROR_MISSING_CALLOUT_CLOSING:
782 case PCRE2_ERROR_ESCAPE_INVALID_IN_VERB:
783 case PCRE2_ERROR_NULL_PATTERN:
784 case PCRE2_ERROR_BAD_OPTIONS:
785 case PCRE2_ERROR_PARENTHESES_NEST_TOO_DEEP:
786 case PCRE2_ERROR_BACKSLASH_O_MISSING_BRACE:
787 case PCRE2_ERROR_INVALID_OCTAL:
788 case PCRE2_ERROR_CALLOUT_STRING_TOO_LONG:
789 case PCRE2_ERROR_BACKSLASH_U_CODE_POINT_TOO_BIG:
790 case PCRE2_ERROR_MISSING_OCTAL_OR_HEX_DIGITS:
791 case PCRE2_ERROR_VERSION_CONDITION_SYNTAX:
792 case PCRE2_ERROR_CALLOUT_NO_STRING_DELIMITER:
793 case PCRE2_ERROR_CALLOUT_BAD_STRING_DELIMITER:
794 case PCRE2_ERROR_BACKSLASH_C_CALLER_DISABLED:
795 case PCRE2_ERROR_QUERY_BARJX_NEST_TOO_DEEP:
796 case PCRE2_ERROR_PATTERN_TOO_COMPLICATED:
797 case PCRE2_ERROR_LOOKBEHIND_TOO_LONG:
798 case PCRE2_ERROR_PATTERN_STRING_TOO_LONG:
799 case PCRE2_ERROR_BAD_LITERAL_OPTIONS:
801 *errcode = G_REGEX_ERROR_COMPILE;
805 g_assert (*errcode != -1);
811 match_info_new (const GRegex *regex,
815 GRegexMatchFlags match_options,
818 GMatchInfo *match_info;
821 string_len = strlen (string);
823 match_info = g_new0 (GMatchInfo, 1);
824 match_info->ref_count = 1;
825 match_info->regex = g_regex_ref ((GRegex *)regex);
826 match_info->string = string;
827 match_info->string_len = string_len;
828 match_info->matches = PCRE2_ERROR_NOMATCH;
829 match_info->pos = start_position;
830 match_info->match_opts =
831 get_pcre2_match_options (match_options, regex->orig_compile_opts);
833 pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_CAPTURECOUNT,
834 &match_info->n_subpatterns);
836 match_info->match_context = pcre2_match_context_create (NULL);
840 /* These values should be enough for most cases, if they are not
841 * enough g_regex_match_all_full() will expand them. */
842 match_info->n_workspace = 100;
843 match_info->workspace = g_new (gint, match_info->n_workspace);
846 match_info->n_offsets = 2;
847 match_info->offsets = g_new0 (gint, match_info->n_offsets);
848 /* Set an invalid position for the previous match. */
849 match_info->offsets[0] = -1;
850 match_info->offsets[1] = -1;
852 match_info->match_data = pcre2_match_data_create_from_pattern (
853 match_info->regex->pcre_re,
860 recalc_match_offsets (GMatchInfo *match_info,
864 uint32_t ovector_size = 0;
865 uint32_t pre_n_offset;
868 g_assert (!IS_PCRE2_ERROR (match_info->matches));
870 if (match_info->matches == PCRE2_ERROR_PARTIAL)
872 else if (match_info->matches > 0)
873 ovector_size = match_info->matches;
875 g_assert (ovector_size != 0);
877 if (pcre2_get_ovector_count (match_info->match_data) < ovector_size)
879 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
880 _("Error while matching regular expression %s: %s"),
881 match_info->regex->pattern, _("code overflow"));
885 pre_n_offset = match_info->n_offsets;
886 match_info->n_offsets = ovector_size * 2;
887 ovector = pcre2_get_ovector_pointer (match_info->match_data);
889 if (match_info->n_offsets != pre_n_offset)
891 match_info->offsets = g_realloc_n (match_info->offsets,
892 match_info->n_offsets,
896 for (i = 0; i < match_info->n_offsets; i++)
898 match_info->offsets[i] = (int) ovector[i];
905 enable_jit_with_match_options (GMatchInfo *match_info,
906 uint32_t match_options)
909 uint32_t old_jit_options, new_jit_options;
911 if (!(match_info->regex->orig_compile_opts & G_REGEX_OPTIMIZE))
912 return JIT_STATUS_DISABLED;
914 if (match_info->regex->jit_status == JIT_STATUS_DISABLED)
915 return JIT_STATUS_DISABLED;
917 if (match_options & G_REGEX_PCRE2_JIT_UNSUPPORTED_OPTIONS)
918 return JIT_STATUS_DISABLED;
920 old_jit_options = match_info->regex->jit_options;
921 new_jit_options = old_jit_options | PCRE2_JIT_COMPLETE;
922 if (match_options & PCRE2_PARTIAL_HARD)
923 new_jit_options |= PCRE2_JIT_PARTIAL_HARD;
924 if (match_options & PCRE2_PARTIAL_SOFT)
925 new_jit_options |= PCRE2_JIT_PARTIAL_SOFT;
927 /* no new options enabled */
928 if (new_jit_options == old_jit_options)
930 g_assert (match_info->regex->jit_status != JIT_STATUS_DEFAULT);
931 return match_info->regex->jit_status;
934 retval = pcre2_jit_compile (match_info->regex->pcre_re, new_jit_options);
937 match_info->regex->jit_status = JIT_STATUS_ENABLED;
939 match_info->regex->jit_options = new_jit_options;
940 /* Set min stack size for JIT to 32KiB and max to 512KiB */
941 match_info->jit_stack = pcre2_jit_stack_create (1 << 15, 1 << 19, NULL);
942 pcre2_jit_stack_assign (match_info->match_context, NULL, match_info->jit_stack);
946 match_info->regex->jit_status = JIT_STATUS_DISABLED;
950 case PCRE2_ERROR_NOMEMORY:
951 g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
952 "but JIT was unable to allocate executable memory for the "
953 "compiler. Falling back to interpretive code.");
955 case PCRE2_ERROR_JIT_BADOPTION:
956 g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
957 "but JIT support is not available. Falling back to "
958 "interpretive code.");
961 g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
962 "but request for JIT support had unexpectedly failed (error %d). "
963 "Falling back to interpretive code.",
969 return match_info->regex->jit_status;
971 g_assert_not_reached ();
975 * g_match_info_get_regex:
976 * @match_info: a #GMatchInfo
978 * Returns #GRegex object used in @match_info. It belongs to Glib
979 * and must not be freed. Use g_regex_ref() if you need to keep it
980 * after you free @match_info object.
982 * Returns: (transfer none): #GRegex object used in @match_info
987 g_match_info_get_regex (const GMatchInfo *match_info)
989 g_return_val_if_fail (match_info != NULL, NULL);
990 return match_info->regex;
994 * g_match_info_get_string:
995 * @match_info: a #GMatchInfo
997 * Returns the string searched with @match_info. This is the
998 * string passed to g_regex_match() or g_regex_replace() so
999 * you may not free it before calling this function.
1001 * Returns: the string searched with @match_info
1006 g_match_info_get_string (const GMatchInfo *match_info)
1008 g_return_val_if_fail (match_info != NULL, NULL);
1009 return match_info->string;
1014 * @match_info: a #GMatchInfo
1016 * Increases reference count of @match_info by 1.
1018 * Returns: @match_info
1023 g_match_info_ref (GMatchInfo *match_info)
1025 g_return_val_if_fail (match_info != NULL, NULL);
1026 g_atomic_int_inc (&match_info->ref_count);
1031 * g_match_info_unref:
1032 * @match_info: a #GMatchInfo
1034 * Decreases reference count of @match_info by 1. When reference count drops
1035 * to zero, it frees all the memory associated with the match_info structure.
1040 g_match_info_unref (GMatchInfo *match_info)
1042 if (g_atomic_int_dec_and_test (&match_info->ref_count))
1044 g_regex_unref (match_info->regex);
1045 if (match_info->match_context)
1046 pcre2_match_context_free (match_info->match_context);
1047 if (match_info->jit_stack)
1048 pcre2_jit_stack_free (match_info->jit_stack);
1049 if (match_info->match_data)
1050 pcre2_match_data_free (match_info->match_data);
1051 g_free (match_info->offsets);
1052 g_free (match_info->workspace);
1053 g_free (match_info);
1058 * g_match_info_free:
1059 * @match_info: (nullable): a #GMatchInfo, or %NULL
1061 * If @match_info is not %NULL, calls g_match_info_unref(); otherwise does
1067 g_match_info_free (GMatchInfo *match_info)
1069 if (match_info == NULL)
1072 g_match_info_unref (match_info);
1076 * g_match_info_next:
1077 * @match_info: a #GMatchInfo structure
1078 * @error: location to store the error occurring, or %NULL to ignore errors
1080 * Scans for the next match using the same parameters of the previous
1081 * call to g_regex_match_full() or g_regex_match() that returned
1084 * The match is done on the string passed to the match function, so you
1085 * cannot free it before calling this function.
1087 * Returns: %TRUE is the string matched, %FALSE otherwise
1092 g_match_info_next (GMatchInfo *match_info,
1095 JITStatus jit_status;
1096 gint prev_match_start;
1097 gint prev_match_end;
1100 g_return_val_if_fail (match_info != NULL, FALSE);
1101 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1102 g_return_val_if_fail (match_info->pos >= 0, FALSE);
1104 prev_match_start = match_info->offsets[0];
1105 prev_match_end = match_info->offsets[1];
1107 if (match_info->pos > match_info->string_len)
1109 /* we have reached the end of the string */
1110 match_info->pos = -1;
1111 match_info->matches = PCRE2_ERROR_NOMATCH;
1115 opts = match_info->regex->match_opts | match_info->match_opts;
1117 jit_status = enable_jit_with_match_options (match_info, opts);
1118 if (jit_status == JIT_STATUS_ENABLED)
1120 match_info->matches = pcre2_jit_match (match_info->regex->pcre_re,
1121 (PCRE2_SPTR8) match_info->string,
1122 match_info->string_len,
1125 match_info->match_data,
1126 match_info->match_context);
1127 /* if the JIT stack limit was reached, fall back to non-JIT matching in
1128 * the next conditional statement */
1129 if (match_info->matches == PCRE2_ERROR_JIT_STACKLIMIT)
1131 g_debug ("PCRE2 JIT stack limit reached, falling back to "
1132 "non-optimized matching.");
1133 opts |= PCRE2_NO_JIT;
1134 jit_status = JIT_STATUS_DISABLED;
1138 if (jit_status != JIT_STATUS_ENABLED)
1140 match_info->matches = pcre2_match (match_info->regex->pcre_re,
1141 (PCRE2_SPTR8) match_info->string,
1142 match_info->string_len,
1145 match_info->match_data,
1146 match_info->match_context);
1149 if (IS_PCRE2_ERROR (match_info->matches))
1151 gchar *error_msg = get_match_error_message (match_info->matches);
1153 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
1154 _("Error while matching regular expression %s: %s"),
1155 match_info->regex->pattern, error_msg);
1156 g_clear_pointer (&error_msg, g_free);
1159 else if (match_info->matches == 0)
1161 /* info->offsets is too small. */
1162 match_info->n_offsets *= 2;
1163 match_info->offsets = g_realloc_n (match_info->offsets,
1164 match_info->n_offsets,
1167 pcre2_match_data_free (match_info->match_data);
1168 match_info->match_data = pcre2_match_data_create (match_info->n_offsets, NULL);
1170 return g_match_info_next (match_info, error);
1172 else if (match_info->matches == PCRE2_ERROR_NOMATCH)
1174 /* We're done with this match info */
1175 match_info->pos = -1;
1179 if (!recalc_match_offsets (match_info, error))
1182 /* avoid infinite loops if the pattern is an empty string or something
1184 if (match_info->pos == match_info->offsets[1])
1186 if (match_info->pos > match_info->string_len)
1188 /* we have reached the end of the string */
1189 match_info->pos = -1;
1190 match_info->matches = PCRE2_ERROR_NOMATCH;
1194 match_info->pos = NEXT_CHAR (match_info->regex,
1195 &match_info->string[match_info->pos]) -
1200 match_info->pos = match_info->offsets[1];
1203 g_assert (match_info->matches < 0 ||
1204 (uint32_t) match_info->matches <= match_info->n_subpatterns + 1);
1206 /* it's possible to get two identical matches when we are matching
1207 * empty strings, for instance if the pattern is "(?=[A-Z0-9])" and
1208 * the string is "RegExTest" we have:
1209 * - search at position 0: match from 0 to 0
1210 * - search at position 1: match from 3 to 3
1211 * - search at position 3: match from 3 to 3 (duplicate)
1212 * - search at position 4: match from 5 to 5
1213 * - search at position 5: match from 5 to 5 (duplicate)
1214 * - search at position 6: no match -> stop
1215 * so we have to ignore the duplicates.
1216 * see bug #515944: http://bugzilla.gnome.org/show_bug.cgi?id=515944 */
1217 if (match_info->matches >= 0 &&
1218 prev_match_start == match_info->offsets[0] &&
1219 prev_match_end == match_info->offsets[1])
1221 /* ignore this match and search the next one */
1222 return g_match_info_next (match_info, error);
1225 return match_info->matches >= 0;
1229 * g_match_info_matches:
1230 * @match_info: a #GMatchInfo structure
1232 * Returns whether the previous match operation succeeded.
1234 * Returns: %TRUE if the previous match operation succeeded,
1240 g_match_info_matches (const GMatchInfo *match_info)
1242 g_return_val_if_fail (match_info != NULL, FALSE);
1244 return match_info->matches >= 0;
1248 * g_match_info_get_match_count:
1249 * @match_info: a #GMatchInfo structure
1251 * Retrieves the number of matched substrings (including substring 0,
1252 * that is the whole matched text), so 1 is returned if the pattern
1253 * has no substrings in it and 0 is returned if the match failed.
1255 * If the last match was obtained using the DFA algorithm, that is
1256 * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
1257 * count is not that of the number of capturing parentheses but that of
1258 * the number of matched substrings.
1260 * Returns: Number of matched substrings, or -1 if an error occurred
1265 g_match_info_get_match_count (const GMatchInfo *match_info)
1267 g_return_val_if_fail (match_info, -1);
1269 if (match_info->matches == PCRE2_ERROR_NOMATCH)
1272 else if (match_info->matches < PCRE2_ERROR_NOMATCH)
1277 return match_info->matches;
1281 * g_match_info_is_partial_match:
1282 * @match_info: a #GMatchInfo structure
1284 * Usually if the string passed to g_regex_match*() matches as far as
1285 * it goes, but is too short to match the entire pattern, %FALSE is
1286 * returned. There are circumstances where it might be helpful to
1287 * distinguish this case from other cases in which there is no match.
1289 * Consider, for example, an application where a human is required to
1290 * type in data for a field with specific formatting requirements. An
1291 * example might be a date in the form ddmmmyy, defined by the pattern
1292 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
1293 * If the application sees the user’s keystrokes one by one, and can
1294 * check that what has been typed so far is potentially valid, it is
1295 * able to raise an error as soon as a mistake is made.
1297 * GRegex supports the concept of partial matching by means of the
1298 * %G_REGEX_MATCH_PARTIAL_SOFT and %G_REGEX_MATCH_PARTIAL_HARD flags.
1299 * When they are used, the return code for
1300 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
1301 * for a complete match, %FALSE otherwise. But, when these functions
1302 * return %FALSE, you can check if the match was partial calling
1303 * g_match_info_is_partial_match().
1305 * The difference between %G_REGEX_MATCH_PARTIAL_SOFT and
1306 * %G_REGEX_MATCH_PARTIAL_HARD is that when a partial match is encountered
1307 * with %G_REGEX_MATCH_PARTIAL_SOFT, matching continues to search for a
1308 * possible complete match, while with %G_REGEX_MATCH_PARTIAL_HARD matching
1309 * stops at the partial match.
1310 * When both %G_REGEX_MATCH_PARTIAL_SOFT and %G_REGEX_MATCH_PARTIAL_HARD
1311 * are set, the latter takes precedence.
1313 * There were formerly some restrictions on the pattern for partial matching.
1314 * The restrictions no longer apply.
1316 * See pcrepartial(3) for more information on partial matching.
1318 * Returns: %TRUE if the match was partial, %FALSE otherwise
1323 g_match_info_is_partial_match (const GMatchInfo *match_info)
1325 g_return_val_if_fail (match_info != NULL, FALSE);
1327 return match_info->matches == PCRE2_ERROR_PARTIAL;
1331 * g_match_info_expand_references:
1332 * @match_info: (nullable): a #GMatchInfo or %NULL
1333 * @string_to_expand: the string to expand
1334 * @error: location to store the error occurring, or %NULL to ignore errors
1336 * Returns a new string containing the text in @string_to_expand with
1337 * references and escape sequences expanded. References refer to the last
1338 * match done with @string against @regex and have the same syntax used by
1339 * g_regex_replace().
1341 * The @string_to_expand must be UTF-8 encoded even if %G_REGEX_RAW was
1342 * passed to g_regex_new().
1344 * The backreferences are extracted from the string passed to the match
1345 * function, so you cannot call this function after freeing the string.
1347 * @match_info may be %NULL in which case @string_to_expand must not
1348 * contain references. For instance "foo\n" does not refer to an actual
1349 * pattern and '\n' merely will be replaced with \n character,
1350 * while to expand "\0" (whole match) one needs the result of a match.
1351 * Use g_regex_check_replacement() to find out whether @string_to_expand
1352 * contains references.
1354 * Returns: (nullable): the expanded string, or %NULL if an error occurred
1359 g_match_info_expand_references (const GMatchInfo *match_info,
1360 const gchar *string_to_expand,
1365 GError *tmp_error = NULL;
1367 g_return_val_if_fail (string_to_expand != NULL, NULL);
1368 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1370 list = split_replacement (string_to_expand, &tmp_error);
1371 if (tmp_error != NULL)
1373 g_propagate_error (error, tmp_error);
1377 if (!match_info && interpolation_list_needs_match (list))
1379 g_critical ("String '%s' contains references to the match, can't "
1380 "expand references without GMatchInfo object",
1385 result = g_string_sized_new (strlen (string_to_expand));
1386 interpolate_replacement (match_info, result, list);
1388 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
1390 return g_string_free (result, FALSE);
1394 * g_match_info_fetch:
1395 * @match_info: #GMatchInfo structure
1396 * @match_num: number of the sub expression
1398 * Retrieves the text matching the @match_num'th capturing
1399 * parentheses. 0 is the full text of the match, 1 is the first paren
1400 * set, 2 the second, and so on.
1402 * If @match_num is a valid sub pattern but it didn't match anything
1403 * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
1404 * string is returned.
1406 * If the match was obtained using the DFA algorithm, that is using
1407 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1408 * string is not that of a set of parentheses but that of a matched
1409 * substring. Substrings are matched in reverse order of length, so
1410 * 0 is the longest match.
1412 * The string is fetched from the string passed to the match function,
1413 * so you cannot call this function after freeing the string.
1415 * Returns: (nullable): The matched substring, or %NULL if an error
1416 * occurred. You have to free the string yourself
1421 g_match_info_fetch (const GMatchInfo *match_info,
1424 gchar *match = NULL;
1427 g_return_val_if_fail (match_info != NULL, NULL);
1428 g_return_val_if_fail (match_num >= 0, NULL);
1430 /* match_num does not exist or it didn't matched, i.e. matching "b"
1431 * against "(a)?b" then group 0 is empty. */
1432 if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
1434 else if (start == -1)
1435 match = g_strdup ("");
1437 match = g_strndup (&match_info->string[start], end - start);
1443 * g_match_info_fetch_pos:
1444 * @match_info: #GMatchInfo structure
1445 * @match_num: number of the sub expression
1446 * @start_pos: (out) (optional): pointer to location where to store
1447 * the start position, or %NULL
1448 * @end_pos: (out) (optional): pointer to location where to store
1449 * the end position, or %NULL
1451 * Retrieves the position in bytes of the @match_num'th capturing
1452 * parentheses. 0 is the full text of the match, 1 is the first
1453 * paren set, 2 the second, and so on.
1455 * If @match_num is a valid sub pattern but it didn't match anything
1456 * (e.g. sub pattern 1, matching "b" against "(a)?b") then @start_pos
1457 * and @end_pos are set to -1 and %TRUE is returned.
1459 * If the match was obtained using the DFA algorithm, that is using
1460 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1461 * position is not that of a set of parentheses but that of a matched
1462 * substring. Substrings are matched in reverse order of length, so
1463 * 0 is the longest match.
1465 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
1466 * the position cannot be fetched, @start_pos and @end_pos are left
1472 g_match_info_fetch_pos (const GMatchInfo *match_info,
1477 g_return_val_if_fail (match_info != NULL, FALSE);
1478 g_return_val_if_fail (match_num >= 0, FALSE);
1480 /* check whether there was an error */
1481 if (match_info->matches < 0)
1484 /* make sure the sub expression number they're requesting is less than
1485 * the total number of sub expressions in the regex. When matching all
1486 * (g_regex_match_all()), also compare against the number of matches */
1487 if ((uint32_t) match_num >= MAX (match_info->n_subpatterns + 1, (uint32_t) match_info->matches))
1490 if (start_pos != NULL)
1491 *start_pos = (match_num < match_info->matches) ? match_info->offsets[2 * match_num] : -1;
1493 if (end_pos != NULL)
1494 *end_pos = (match_num < match_info->matches) ? match_info->offsets[2 * match_num + 1] : -1;
1500 * Returns number of first matched subpattern with name @name.
1501 * There may be more than one in case when DUPNAMES is used,
1502 * and not all subpatterns with that name match;
1503 * pcre2_substring_number_from_name() does not work in that case.
1506 get_matched_substring_number (const GMatchInfo *match_info,
1510 PCRE2_SPTR first, last;
1513 if (!(match_info->regex->compile_opts & PCRE2_DUPNAMES))
1514 return pcre2_substring_number_from_name (match_info->regex->pcre_re, (PCRE2_SPTR8) name);
1516 /* This code is analogous to code from pcre2_substring.c:
1517 * pcre2_substring_get_byname() */
1518 entrysize = pcre2_substring_nametable_scan (match_info->regex->pcre_re,
1526 for (entry = (guchar*) first; entry <= (guchar*) last; entry += entrysize)
1528 guint n = (entry[0] << 8) + entry[1];
1529 if (n * 2 < match_info->n_offsets && match_info->offsets[n * 2] >= 0)
1533 return (first[0] << 8) + first[1];
1537 * g_match_info_fetch_named:
1538 * @match_info: #GMatchInfo structure
1539 * @name: name of the subexpression
1541 * Retrieves the text matching the capturing parentheses named @name.
1543 * If @name is a valid sub pattern name but it didn't match anything
1544 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
1545 * then an empty string is returned.
1547 * The string is fetched from the string passed to the match function,
1548 * so you cannot call this function after freeing the string.
1550 * Returns: (nullable): The matched substring, or %NULL if an error
1551 * occurred. You have to free the string yourself
1556 g_match_info_fetch_named (const GMatchInfo *match_info,
1561 g_return_val_if_fail (match_info != NULL, NULL);
1562 g_return_val_if_fail (name != NULL, NULL);
1564 num = get_matched_substring_number (match_info, name);
1568 return g_match_info_fetch (match_info, num);
1572 * g_match_info_fetch_named_pos:
1573 * @match_info: #GMatchInfo structure
1574 * @name: name of the subexpression
1575 * @start_pos: (out) (optional): pointer to location where to store
1576 * the start position, or %NULL
1577 * @end_pos: (out) (optional): pointer to location where to store
1578 * the end position, or %NULL
1580 * Retrieves the position in bytes of the capturing parentheses named @name.
1582 * If @name is a valid sub pattern name but it didn't match anything
1583 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
1584 * then @start_pos and @end_pos are set to -1 and %TRUE is returned.
1586 * Returns: %TRUE if the position was fetched, %FALSE otherwise.
1587 * If the position cannot be fetched, @start_pos and @end_pos
1588 * are left unchanged.
1593 g_match_info_fetch_named_pos (const GMatchInfo *match_info,
1600 g_return_val_if_fail (match_info != NULL, FALSE);
1601 g_return_val_if_fail (name != NULL, FALSE);
1603 num = get_matched_substring_number (match_info, name);
1607 return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
1611 * g_match_info_fetch_all:
1612 * @match_info: a #GMatchInfo structure
1614 * Bundles up pointers to each of the matching substrings from a match
1615 * and stores them in an array of gchar pointers. The first element in
1616 * the returned array is the match number 0, i.e. the entire matched
1619 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
1620 * "b" against "(a)?b") then an empty string is inserted.
1622 * If the last match was obtained using the DFA algorithm, that is using
1623 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1624 * strings are not that matched by sets of parentheses but that of the
1625 * matched substring. Substrings are matched in reverse order of length,
1626 * so the first one is the longest match.
1628 * The strings are fetched from the string passed to the match function,
1629 * so you cannot call this function after freeing the string.
1631 * Returns: (transfer full): a %NULL-terminated array of gchar *
1632 * pointers. It must be freed using g_strfreev(). If the previous
1633 * match failed %NULL is returned
1638 g_match_info_fetch_all (const GMatchInfo *match_info)
1643 g_return_val_if_fail (match_info != NULL, NULL);
1645 if (match_info->matches < 0)
1648 result = g_new (gchar *, match_info->matches + 1);
1649 for (i = 0; i < match_info->matches; i++)
1650 result[i] = g_match_info_fetch (match_info, i);
1659 G_DEFINE_QUARK (g-regex-error-quark, g_regex_error)
1665 * Increases reference count of @regex by 1.
1672 g_regex_ref (GRegex *regex)
1674 g_return_val_if_fail (regex != NULL, NULL);
1675 g_atomic_int_inc (®ex->ref_count);
1683 * Decreases reference count of @regex by 1. When reference count drops
1684 * to zero, it frees all the memory associated with the regex structure.
1689 g_regex_unref (GRegex *regex)
1691 g_return_if_fail (regex != NULL);
1693 if (g_atomic_int_dec_and_test (®ex->ref_count))
1695 g_free (regex->pattern);
1696 if (regex->pcre_re != NULL)
1697 pcre2_code_free (regex->pcre_re);
1702 static pcre2_code * regex_compile (const gchar *pattern,
1703 uint32_t compile_options,
1704 uint32_t newline_options,
1705 uint32_t bsr_options,
1708 static uint32_t get_pcre2_inline_compile_options (pcre2_code *re,
1709 uint32_t compile_options);
1713 * @pattern: the regular expression
1714 * @compile_options: compile options for the regular expression, or 0
1715 * @match_options: match options for the regular expression, or 0
1716 * @error: return location for a #GError
1718 * Compiles the regular expression to an internal form, and does
1719 * the initial setup of the #GRegex structure.
1721 * Returns: (nullable): a #GRegex structure or %NULL if an error occurred. Call
1722 * g_regex_unref() when you are done with it
1727 g_regex_new (const gchar *pattern,
1728 GRegexCompileFlags compile_options,
1729 GRegexMatchFlags match_options,
1734 static gsize initialised = 0;
1735 uint32_t pcre_compile_options;
1736 uint32_t pcre_match_options;
1737 uint32_t newline_options;
1738 uint32_t bsr_options;
1740 g_return_val_if_fail (pattern != NULL, NULL);
1741 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1742 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1743 g_return_val_if_fail ((compile_options & ~(G_REGEX_COMPILE_MASK |
1744 G_REGEX_JAVASCRIPT_COMPAT)) == 0, NULL);
1745 G_GNUC_END_IGNORE_DEPRECATIONS
1746 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1748 if (g_once_init_enter (&initialised))
1752 pcre2_config (PCRE2_CONFIG_UNICODE, &supports_utf8);
1754 g_critical (_("PCRE library is compiled without UTF8 support"));
1756 g_once_init_leave (&initialised, supports_utf8 ? 1 : 2);
1759 if (G_UNLIKELY (initialised != 1))
1761 g_set_error_literal (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE,
1762 _("PCRE library is compiled with incompatible options"));
1766 pcre_compile_options = get_pcre2_compile_options (compile_options);
1767 pcre_match_options = get_pcre2_match_options (match_options, compile_options);
1769 newline_options = get_pcre2_newline_match_options (match_options);
1770 if (newline_options == 0)
1771 newline_options = get_pcre2_newline_compile_options (compile_options);
1773 if (newline_options == 0)
1775 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS,
1776 "Invalid newline flags");
1780 bsr_options = get_pcre2_bsr_match_options (match_options);
1782 bsr_options = get_pcre2_bsr_compile_options (compile_options);
1784 re = regex_compile (pattern, pcre_compile_options,
1785 newline_options, bsr_options, error);
1789 pcre_compile_options |=
1790 get_pcre2_inline_compile_options (re, pcre_compile_options);
1792 regex = g_new0 (GRegex, 1);
1793 regex->ref_count = 1;
1794 regex->pattern = g_strdup (pattern);
1795 regex->pcre_re = re;
1796 regex->compile_opts = pcre_compile_options;
1797 regex->orig_compile_opts = compile_options;
1798 regex->match_opts = pcre_match_options;
1799 regex->orig_match_opts = match_options;
1805 regex_compile (const gchar *pattern,
1806 uint32_t compile_options,
1807 uint32_t newline_options,
1808 uint32_t bsr_options,
1812 pcre2_compile_context *context;
1813 const gchar *errmsg;
1814 PCRE2_SIZE erroffset;
1817 context = pcre2_compile_context_create (NULL);
1819 /* set newline options */
1820 if (pcre2_set_newline (context, newline_options) != 0)
1822 g_set_error (error, G_REGEX_ERROR,
1823 G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS,
1824 "Invalid newline flags");
1825 pcre2_compile_context_free (context);
1829 /* set bsr options */
1830 if (pcre2_set_bsr (context, bsr_options) != 0)
1832 g_set_error (error, G_REGEX_ERROR,
1833 G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS,
1834 "Invalid BSR flags");
1835 pcre2_compile_context_free (context);
1839 /* In case UTF-8 mode is used, also set PCRE2_NO_UTF_CHECK */
1840 if (compile_options & PCRE2_UTF)
1841 compile_options |= PCRE2_NO_UTF_CHECK;
1843 compile_options |= PCRE2_UCP;
1845 /* compile the pattern */
1846 re = pcre2_compile ((PCRE2_SPTR8) pattern,
1847 PCRE2_ZERO_TERMINATED,
1852 pcre2_compile_context_free (context);
1854 /* if the compilation failed, set the error member and return
1860 gchar *pcre2_errmsg = NULL;
1861 int original_errcode;
1863 /* Translate the PCRE error code to GRegexError and use a translated
1864 * error message if possible */
1865 original_errcode = errcode;
1866 translate_compile_error (&errcode, &errmsg);
1870 errmsg = _("unknown error");
1871 pcre2_errmsg = get_pcre2_error_string (original_errcode);
1874 /* PCRE uses byte offsets but we want to show character offsets */
1875 erroffset = g_utf8_pointer_to_offset (pattern, &pattern[erroffset]);
1877 offset_str = g_strdup_printf ("%" G_GSIZE_FORMAT, erroffset);
1878 tmp_error = g_error_new (G_REGEX_ERROR, errcode,
1879 _("Error while compiling regular expression ‘%s’ "
1881 pattern, offset_str,
1882 pcre2_errmsg ? pcre2_errmsg : errmsg);
1883 g_propagate_error (error, tmp_error);
1884 g_free (offset_str);
1885 g_clear_pointer (&pcre2_errmsg, g_free);
1894 get_pcre2_inline_compile_options (pcre2_code *re,
1895 uint32_t compile_options)
1897 uint32_t pcre_compile_options;
1898 uint32_t nonpcre_compile_options;
1900 /* For options set at the beginning of the pattern, pcre puts them into
1901 * compile options, e.g. "(?i)foo" will make the pcre structure store
1902 * PCRE2_CASELESS even though it wasn't explicitly given for compilation. */
1903 nonpcre_compile_options = compile_options & G_REGEX_COMPILE_NONPCRE_MASK;
1904 pcre2_pattern_info (re, PCRE2_INFO_ALLOPTIONS, &pcre_compile_options);
1905 compile_options = pcre_compile_options & G_REGEX_PCRE2_COMPILE_MASK;
1906 compile_options |= nonpcre_compile_options;
1908 if (!(compile_options & PCRE2_DUPNAMES))
1910 uint32_t jchanged = 0;
1911 pcre2_pattern_info (re, PCRE2_INFO_JCHANGED, &jchanged);
1913 compile_options |= PCRE2_DUPNAMES;
1916 return compile_options;
1920 * g_regex_get_pattern:
1921 * @regex: a #GRegex structure
1923 * Gets the pattern string associated with @regex, i.e. a copy of
1924 * the string passed to g_regex_new().
1926 * Returns: the pattern of @regex
1931 g_regex_get_pattern (const GRegex *regex)
1933 g_return_val_if_fail (regex != NULL, NULL);
1935 return regex->pattern;
1939 * g_regex_get_max_backref:
1942 * Returns the number of the highest back reference
1943 * in the pattern, or 0 if the pattern does not contain
1946 * Returns: the number of the highest back reference
1951 g_regex_get_max_backref (const GRegex *regex)
1955 pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_BACKREFMAX, &value);
1961 * g_regex_get_capture_count:
1964 * Returns the number of capturing subpatterns in the pattern.
1966 * Returns: the number of capturing subpatterns
1971 g_regex_get_capture_count (const GRegex *regex)
1975 pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_CAPTURECOUNT, &value);
1981 * g_regex_get_has_cr_or_lf:
1982 * @regex: a #GRegex structure
1984 * Checks whether the pattern contains explicit CR or LF references.
1986 * Returns: %TRUE if the pattern contains explicit CR or LF references
1991 g_regex_get_has_cr_or_lf (const GRegex *regex)
1995 pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_HASCRORLF, &value);
2001 * g_regex_get_max_lookbehind:
2002 * @regex: a #GRegex structure
2004 * Gets the number of characters in the longest lookbehind assertion in the
2005 * pattern. This information is useful when doing multi-segment matching using
2006 * the partial matching facilities.
2008 * Returns: the number of characters in the longest lookbehind assertion.
2013 g_regex_get_max_lookbehind (const GRegex *regex)
2015 uint32_t max_lookbehind;
2017 pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_MAXLOOKBEHIND,
2020 return max_lookbehind;
2024 * g_regex_get_compile_flags:
2027 * Returns the compile options that @regex was created with.
2029 * Depending on the version of PCRE that is used, this may or may not
2030 * include flags set by option expressions such as `(?i)` found at the
2031 * top-level within the compiled pattern.
2033 * Returns: flags from #GRegexCompileFlags
2038 g_regex_get_compile_flags (const GRegex *regex)
2040 GRegexCompileFlags extra_flags;
2041 uint32_t info_value;
2043 g_return_val_if_fail (regex != NULL, 0);
2045 /* Preserve original G_REGEX_OPTIMIZE */
2046 extra_flags = (regex->orig_compile_opts & G_REGEX_OPTIMIZE);
2048 /* Also include the newline options */
2049 pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_NEWLINE, &info_value);
2052 case PCRE2_NEWLINE_ANYCRLF:
2053 extra_flags |= G_REGEX_NEWLINE_ANYCRLF;
2055 case PCRE2_NEWLINE_CRLF:
2056 extra_flags |= G_REGEX_NEWLINE_CRLF;
2058 case PCRE2_NEWLINE_LF:
2059 extra_flags |= G_REGEX_NEWLINE_LF;
2061 case PCRE2_NEWLINE_CR:
2062 extra_flags |= G_REGEX_NEWLINE_CR;
2068 /* Also include the bsr options */
2069 pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_BSR, &info_value);
2072 case PCRE2_BSR_ANYCRLF:
2073 extra_flags |= G_REGEX_BSR_ANYCRLF;
2079 return g_regex_compile_flags_from_pcre2 (regex->compile_opts) | extra_flags;
2083 * g_regex_get_match_flags:
2086 * Returns the match options that @regex was created with.
2088 * Returns: flags from #GRegexMatchFlags
2093 g_regex_get_match_flags (const GRegex *regex)
2097 g_return_val_if_fail (regex != NULL, 0);
2099 flags = g_regex_match_flags_from_pcre2 (regex->match_opts);
2100 flags |= (regex->orig_match_opts & G_REGEX_MATCH_NEWLINE_MASK);
2101 flags |= (regex->orig_match_opts & (G_REGEX_MATCH_BSR_ANY | G_REGEX_MATCH_BSR_ANYCRLF));
2107 * g_regex_match_simple:
2108 * @pattern: the regular expression
2109 * @string: the string to scan for matches
2110 * @compile_options: compile options for the regular expression, or 0
2111 * @match_options: match options, or 0
2113 * Scans for a match in @string for @pattern.
2115 * This function is equivalent to g_regex_match() but it does not
2116 * require to compile the pattern with g_regex_new(), avoiding some
2117 * lines of code when you need just to do a match without extracting
2118 * substrings, capture counts, and so on.
2120 * If this function is to be called on the same @pattern more than
2121 * once, it's more efficient to compile the pattern once with
2122 * g_regex_new() and then use g_regex_match().
2124 * Returns: %TRUE if the string matched, %FALSE otherwise
2129 g_regex_match_simple (const gchar *pattern,
2130 const gchar *string,
2131 GRegexCompileFlags compile_options,
2132 GRegexMatchFlags match_options)
2137 regex = g_regex_new (pattern, compile_options, G_REGEX_MATCH_DEFAULT, NULL);
2140 result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
2141 g_regex_unref (regex);
2147 * @regex: a #GRegex structure from g_regex_new()
2148 * @string: the string to scan for matches
2149 * @match_options: match options
2150 * @match_info: (out) (optional): pointer to location where to store
2151 * the #GMatchInfo, or %NULL if you do not need it
2153 * Scans for a match in @string for the pattern in @regex.
2154 * The @match_options are combined with the match options specified
2155 * when the @regex structure was created, letting you have more
2156 * flexibility in reusing #GRegex structures.
2158 * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8.
2160 * A #GMatchInfo structure, used to get information on the match,
2161 * is stored in @match_info if not %NULL. Note that if @match_info
2162 * is not %NULL then it is created even if the function returns %FALSE,
2163 * i.e. you must free it regardless if regular expression actually matched.
2165 * To retrieve all the non-overlapping matches of the pattern in
2166 * string you can use g_match_info_next().
2168 * |[<!-- language="C" -->
2170 * print_uppercase_words (const gchar *string)
2172 * // Print all uppercase-only words.
2174 * GMatchInfo *match_info;
2176 * regex = g_regex_new ("[A-Z]+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
2177 * g_regex_match (regex, string, 0, &match_info);
2178 * while (g_match_info_matches (match_info))
2180 * gchar *word = g_match_info_fetch (match_info, 0);
2181 * g_print ("Found: %s\n", word);
2183 * g_match_info_next (match_info, NULL);
2185 * g_match_info_free (match_info);
2186 * g_regex_unref (regex);
2190 * @string is not copied and is used in #GMatchInfo internally. If
2191 * you use any #GMatchInfo method (except g_match_info_free()) after
2192 * freeing or modifying @string then the behaviour is undefined.
2194 * Returns: %TRUE is the string matched, %FALSE otherwise
2199 g_regex_match (const GRegex *regex,
2200 const gchar *string,
2201 GRegexMatchFlags match_options,
2202 GMatchInfo **match_info)
2204 return g_regex_match_full (regex, string, -1, 0, match_options,
2209 * g_regex_match_full:
2210 * @regex: a #GRegex structure from g_regex_new()
2211 * @string: (array length=string_len): the string to scan for matches
2212 * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
2213 * @start_position: starting index of the string to match, in bytes
2214 * @match_options: match options
2215 * @match_info: (out) (optional): pointer to location where to store
2216 * the #GMatchInfo, or %NULL if you do not need it
2217 * @error: location to store the error occurring, or %NULL to ignore errors
2219 * Scans for a match in @string for the pattern in @regex.
2220 * The @match_options are combined with the match options specified
2221 * when the @regex structure was created, letting you have more
2222 * flexibility in reusing #GRegex structures.
2224 * Setting @start_position differs from just passing over a shortened
2225 * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
2226 * that begins with any kind of lookbehind assertion, such as "\b".
2228 * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8.
2230 * A #GMatchInfo structure, used to get information on the match, is
2231 * stored in @match_info if not %NULL. Note that if @match_info is
2232 * not %NULL then it is created even if the function returns %FALSE,
2233 * i.e. you must free it regardless if regular expression actually
2236 * @string is not copied and is used in #GMatchInfo internally. If
2237 * you use any #GMatchInfo method (except g_match_info_free()) after
2238 * freeing or modifying @string then the behaviour is undefined.
2240 * To retrieve all the non-overlapping matches of the pattern in
2241 * string you can use g_match_info_next().
2243 * |[<!-- language="C" -->
2245 * print_uppercase_words (const gchar *string)
2247 * // Print all uppercase-only words.
2249 * GMatchInfo *match_info;
2250 * GError *error = NULL;
2252 * regex = g_regex_new ("[A-Z]+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
2253 * g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
2254 * while (g_match_info_matches (match_info))
2256 * gchar *word = g_match_info_fetch (match_info, 0);
2257 * g_print ("Found: %s\n", word);
2259 * g_match_info_next (match_info, &error);
2261 * g_match_info_free (match_info);
2262 * g_regex_unref (regex);
2263 * if (error != NULL)
2265 * g_printerr ("Error while matching: %s\n", error->message);
2266 * g_error_free (error);
2271 * Returns: %TRUE is the string matched, %FALSE otherwise
2276 g_regex_match_full (const GRegex *regex,
2277 const gchar *string,
2279 gint start_position,
2280 GRegexMatchFlags match_options,
2281 GMatchInfo **match_info,
2287 g_return_val_if_fail (regex != NULL, FALSE);
2288 g_return_val_if_fail (string != NULL, FALSE);
2289 g_return_val_if_fail (start_position >= 0, FALSE);
2290 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2291 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
2293 info = match_info_new (regex, string, string_len, start_position,
2294 match_options, FALSE);
2295 match_ok = g_match_info_next (info, error);
2296 if (match_info != NULL)
2299 g_match_info_free (info);
2305 * g_regex_match_all:
2306 * @regex: a #GRegex structure from g_regex_new()
2307 * @string: the string to scan for matches
2308 * @match_options: match options
2309 * @match_info: (out) (optional): pointer to location where to store
2310 * the #GMatchInfo, or %NULL if you do not need it
2312 * Using the standard algorithm for regular expression matching only
2313 * the longest match in the string is retrieved. This function uses
2314 * a different algorithm so it can retrieve all the possible matches.
2315 * For more documentation see g_regex_match_all_full().
2317 * A #GMatchInfo structure, used to get information on the match, is
2318 * stored in @match_info if not %NULL. Note that if @match_info is
2319 * not %NULL then it is created even if the function returns %FALSE,
2320 * i.e. you must free it regardless if regular expression actually
2323 * @string is not copied and is used in #GMatchInfo internally. If
2324 * you use any #GMatchInfo method (except g_match_info_free()) after
2325 * freeing or modifying @string then the behaviour is undefined.
2327 * Returns: %TRUE is the string matched, %FALSE otherwise
2332 g_regex_match_all (const GRegex *regex,
2333 const gchar *string,
2334 GRegexMatchFlags match_options,
2335 GMatchInfo **match_info)
2337 return g_regex_match_all_full (regex, string, -1, 0, match_options,
2342 * g_regex_match_all_full:
2343 * @regex: a #GRegex structure from g_regex_new()
2344 * @string: (array length=string_len): the string to scan for matches
2345 * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
2346 * @start_position: starting index of the string to match, in bytes
2347 * @match_options: match options
2348 * @match_info: (out) (optional): pointer to location where to store
2349 * the #GMatchInfo, or %NULL if you do not need it
2350 * @error: location to store the error occurring, or %NULL to ignore errors
2352 * Using the standard algorithm for regular expression matching only
2353 * the longest match in the @string is retrieved, it is not possible
2354 * to obtain all the available matches. For instance matching
2355 * "<a> <b> <c>" against the pattern "<.*>"
2356 * you get "<a> <b> <c>".
2358 * This function uses a different algorithm (called DFA, i.e. deterministic
2359 * finite automaton), so it can retrieve all the possible matches, all
2360 * starting at the same point in the string. For instance matching
2361 * "<a> <b> <c>" against the pattern "<.*>;"
2362 * you would obtain three matches: "<a> <b> <c>",
2363 * "<a> <b>" and "<a>".
2365 * The number of matched strings is retrieved using
2366 * g_match_info_get_match_count(). To obtain the matched strings and
2367 * their position you can use, respectively, g_match_info_fetch() and
2368 * g_match_info_fetch_pos(). Note that the strings are returned in
2369 * reverse order of length; that is, the longest matching string is
2372 * Note that the DFA algorithm is slower than the standard one and it
2373 * is not able to capture substrings, so backreferences do not work.
2375 * Setting @start_position differs from just passing over a shortened
2376 * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
2377 * that begins with any kind of lookbehind assertion, such as "\b".
2379 * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8.
2381 * A #GMatchInfo structure, used to get information on the match, is
2382 * stored in @match_info if not %NULL. Note that if @match_info is
2383 * not %NULL then it is created even if the function returns %FALSE,
2384 * i.e. you must free it regardless if regular expression actually
2387 * @string is not copied and is used in #GMatchInfo internally. If
2388 * you use any #GMatchInfo method (except g_match_info_free()) after
2389 * freeing or modifying @string then the behaviour is undefined.
2391 * Returns: %TRUE is the string matched, %FALSE otherwise
2396 g_regex_match_all_full (const GRegex *regex,
2397 const gchar *string,
2399 gint start_position,
2400 GRegexMatchFlags match_options,
2401 GMatchInfo **match_info,
2406 pcre2_code *pcre_re;
2408 uint32_t newline_options;
2409 uint32_t bsr_options;
2411 g_return_val_if_fail (regex != NULL, FALSE);
2412 g_return_val_if_fail (string != NULL, FALSE);
2413 g_return_val_if_fail (start_position >= 0, FALSE);
2414 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2415 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
2417 newline_options = get_pcre2_newline_match_options (match_options);
2418 if (!newline_options)
2419 newline_options = get_pcre2_newline_compile_options (regex->orig_compile_opts);
2421 bsr_options = get_pcre2_bsr_match_options (match_options);
2423 bsr_options = get_pcre2_bsr_compile_options (regex->orig_compile_opts);
2425 /* For PCRE2 we need to turn off PCRE2_NO_AUTO_POSSESS, which is an
2426 * optimization for normal regex matching, but results in omitting some
2427 * shorter matches here, and an observable behaviour change.
2429 * DFA matching is rather niche, and very rarely used according to
2430 * codesearch.debian.net, so don't bother caching the recompiled RE. */
2431 pcre_re = regex_compile (regex->pattern,
2432 regex->compile_opts | PCRE2_NO_AUTO_POSSESS,
2433 newline_options, bsr_options, error);
2434 if (pcre_re == NULL)
2437 info = match_info_new (regex, string, string_len, start_position,
2438 match_options, TRUE);
2444 info->matches = pcre2_dfa_match (pcre_re,
2445 (PCRE2_SPTR8) info->string, info->string_len,
2447 (regex->match_opts | info->match_opts),
2449 info->match_context,
2450 info->workspace, info->n_workspace);
2451 if (info->matches == PCRE2_ERROR_DFA_WSSIZE)
2453 /* info->workspace is too small. */
2454 info->n_workspace *= 2;
2455 info->workspace = g_realloc_n (info->workspace,
2460 else if (info->matches == 0)
2462 /* info->offsets is too small. */
2463 info->n_offsets *= 2;
2464 info->offsets = g_realloc_n (info->offsets,
2467 pcre2_match_data_free (info->match_data);
2468 info->match_data = pcre2_match_data_create (info->n_offsets, NULL);
2471 else if (IS_PCRE2_ERROR (info->matches))
2473 gchar *error_msg = get_match_error_message (info->matches);
2475 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
2476 _("Error while matching regular expression %s: %s"),
2477 regex->pattern, error_msg);
2478 g_clear_pointer (&error_msg, g_free);
2480 else if (info->matches != PCRE2_ERROR_NOMATCH)
2482 if (!recalc_match_offsets (info, error))
2483 info->matches = PCRE2_ERROR_NOMATCH;
2487 pcre2_code_free (pcre_re);
2489 /* don’t assert that (info->matches <= info->n_subpatterns + 1) as that only
2490 * holds true for a single match, rather than matching all */
2492 /* set info->pos to -1 so that a call to g_match_info_next() fails. */
2494 retval = info->matches >= 0;
2496 if (match_info != NULL)
2499 g_match_info_free (info);
2505 * g_regex_get_string_number:
2506 * @regex: #GRegex structure
2507 * @name: name of the subexpression
2509 * Retrieves the number of the subexpression named @name.
2511 * Returns: The number of the subexpression or -1 if @name
2517 g_regex_get_string_number (const GRegex *regex,
2522 g_return_val_if_fail (regex != NULL, -1);
2523 g_return_val_if_fail (name != NULL, -1);
2525 num = pcre2_substring_number_from_name (regex->pcre_re, (PCRE2_SPTR8) name);
2526 if (num == PCRE2_ERROR_NOSUBSTRING)
2533 * g_regex_split_simple:
2534 * @pattern: the regular expression
2535 * @string: the string to scan for matches
2536 * @compile_options: compile options for the regular expression, or 0
2537 * @match_options: match options, or 0
2539 * Breaks the string on the pattern, and returns an array of
2540 * the tokens. If the pattern contains capturing parentheses,
2541 * then the text for each of the substrings will also be returned.
2542 * If the pattern does not match anywhere in the string, then the
2543 * whole string is returned as the first token.
2545 * This function is equivalent to g_regex_split() but it does
2546 * not require to compile the pattern with g_regex_new(), avoiding
2547 * some lines of code when you need just to do a split without
2548 * extracting substrings, capture counts, and so on.
2550 * If this function is to be called on the same @pattern more than
2551 * once, it's more efficient to compile the pattern once with
2552 * g_regex_new() and then use g_regex_split().
2554 * As a special case, the result of splitting the empty string ""
2555 * is an empty vector, not a vector containing a single string.
2556 * The reason for this special case is that being able to represent
2557 * an empty vector is typically more useful than consistent handling
2558 * of empty elements. If you do need to represent empty elements,
2559 * you'll need to check for the empty string before calling this
2562 * A pattern that can match empty strings splits @string into
2563 * separate characters wherever it matches the empty string between
2564 * characters. For example splitting "ab c" using as a separator
2565 * "\s*", you will get "a", "b" and "c".
2567 * Returns: (transfer full): a %NULL-terminated array of strings. Free
2568 * it using g_strfreev()
2573 g_regex_split_simple (const gchar *pattern,
2574 const gchar *string,
2575 GRegexCompileFlags compile_options,
2576 GRegexMatchFlags match_options)
2581 regex = g_regex_new (pattern, compile_options, 0, NULL);
2585 result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
2586 g_regex_unref (regex);
2592 * @regex: a #GRegex structure
2593 * @string: the string to split with the pattern
2594 * @match_options: match time option flags
2596 * Breaks the string on the pattern, and returns an array of the tokens.
2597 * If the pattern contains capturing parentheses, then the text for each
2598 * of the substrings will also be returned. If the pattern does not match
2599 * anywhere in the string, then the whole string is returned as the first
2602 * As a special case, the result of splitting the empty string "" is an
2603 * empty vector, not a vector containing a single string. The reason for
2604 * this special case is that being able to represent an empty vector is
2605 * typically more useful than consistent handling of empty elements. If
2606 * you do need to represent empty elements, you'll need to check for the
2607 * empty string before calling this function.
2609 * A pattern that can match empty strings splits @string into separate
2610 * characters wherever it matches the empty string between characters.
2611 * For example splitting "ab c" using as a separator "\s*", you will get
2614 * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
2615 * it using g_strfreev()
2620 g_regex_split (const GRegex *regex,
2621 const gchar *string,
2622 GRegexMatchFlags match_options)
2624 return g_regex_split_full (regex, string, -1, 0,
2625 match_options, 0, NULL);
2629 * g_regex_split_full:
2630 * @regex: a #GRegex structure
2631 * @string: (array length=string_len): the string to split with the pattern
2632 * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
2633 * @start_position: starting index of the string to match, in bytes
2634 * @match_options: match time option flags
2635 * @max_tokens: the maximum number of tokens to split @string into.
2636 * If this is less than 1, the string is split completely
2637 * @error: return location for a #GError
2639 * Breaks the string on the pattern, and returns an array of the tokens.
2640 * If the pattern contains capturing parentheses, then the text for each
2641 * of the substrings will also be returned. If the pattern does not match
2642 * anywhere in the string, then the whole string is returned as the first
2645 * As a special case, the result of splitting the empty string "" is an
2646 * empty vector, not a vector containing a single string. The reason for
2647 * this special case is that being able to represent an empty vector is
2648 * typically more useful than consistent handling of empty elements. If
2649 * you do need to represent empty elements, you'll need to check for the
2650 * empty string before calling this function.
2652 * A pattern that can match empty strings splits @string into separate
2653 * characters wherever it matches the empty string between characters.
2654 * For example splitting "ab c" using as a separator "\s*", you will get
2657 * Setting @start_position differs from just passing over a shortened
2658 * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
2659 * that begins with any kind of lookbehind assertion, such as "\b".
2661 * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
2662 * it using g_strfreev()
2667 g_regex_split_full (const GRegex *regex,
2668 const gchar *string,
2670 gint start_position,
2671 GRegexMatchFlags match_options,
2675 GError *tmp_error = NULL;
2676 GMatchInfo *match_info;
2681 /* position of the last separator. */
2682 gint last_separator_end;
2683 /* was the last match 0 bytes long? */
2684 gboolean last_match_is_empty;
2685 /* the returned array of char **s */
2686 gchar **string_list;
2688 g_return_val_if_fail (regex != NULL, NULL);
2689 g_return_val_if_fail (string != NULL, NULL);
2690 g_return_val_if_fail (start_position >= 0, NULL);
2691 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2692 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2694 if (max_tokens <= 0)
2695 max_tokens = G_MAXINT;
2698 string_len = strlen (string);
2700 /* zero-length string */
2701 if (string_len - start_position == 0)
2702 return g_new0 (gchar *, 1);
2704 if (max_tokens == 1)
2706 string_list = g_new0 (gchar *, 2);
2707 string_list[0] = g_strndup (&string[start_position],
2708 string_len - start_position);
2714 last_separator_end = start_position;
2715 last_match_is_empty = FALSE;
2717 match_ok = g_regex_match_full (regex, string, string_len, start_position,
2718 match_options, &match_info, &tmp_error);
2720 while (tmp_error == NULL)
2724 last_match_is_empty =
2725 (match_info->offsets[0] == match_info->offsets[1]);
2727 /* we need to skip empty separators at the same position of the end
2728 * of another separator. e.g. the string is "a b" and the separator
2729 * is " *", so from 1 to 2 we have a match and at position 2 we have
2730 * an empty match. */
2731 if (last_separator_end != match_info->offsets[1])
2736 token = g_strndup (string + last_separator_end,
2737 match_info->offsets[0] - last_separator_end);
2738 list = g_list_prepend (list, token);
2741 /* if there were substrings, these need to be added to
2743 match_count = g_match_info_get_match_count (match_info);
2744 if (match_count > 1)
2746 for (i = 1; i < match_count; i++)
2747 list = g_list_prepend (list, g_match_info_fetch (match_info, i));
2753 /* if there was no match, copy to end of string. */
2754 if (!last_match_is_empty)
2756 gchar *token = g_strndup (string + last_separator_end,
2757 match_info->string_len - last_separator_end);
2758 list = g_list_prepend (list, token);
2760 /* no more tokens, end the loop. */
2764 /* -1 to leave room for the last part. */
2765 if (token_count >= max_tokens - 1)
2767 /* we have reached the maximum number of tokens, so we copy
2768 * the remaining part of the string. */
2769 if (last_match_is_empty)
2771 /* the last match was empty, so we have moved one char
2772 * after the real position to avoid empty matches at the
2774 match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
2776 /* the if is needed in the case we have terminated the available
2777 * tokens, but we are at the end of the string, so there are no
2778 * characters left to copy. */
2779 if (string_len > match_info->pos)
2781 gchar *token = g_strndup (string + match_info->pos,
2782 string_len - match_info->pos);
2783 list = g_list_prepend (list, token);
2789 last_separator_end = match_info->pos;
2790 if (last_match_is_empty)
2791 /* if the last match was empty, g_match_info_next() has moved
2792 * forward to avoid infinite loops, but we still need to copy that
2794 last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
2796 match_ok = g_match_info_next (match_info, &tmp_error);
2798 g_match_info_free (match_info);
2799 if (tmp_error != NULL)
2801 g_propagate_error (error, tmp_error);
2802 g_list_free_full (list, g_free);
2806 string_list = g_new (gchar *, g_list_length (list) + 1);
2808 for (last = g_list_last (list); last; last = g_list_previous (last))
2809 string_list[i++] = last->data;
2810 string_list[i] = NULL;
2819 REPL_TYPE_CHARACTER,
2820 REPL_TYPE_SYMBOLIC_REFERENCE,
2821 REPL_TYPE_NUMERIC_REFERENCE,
2822 REPL_TYPE_CHANGE_CASE
2827 CHANGE_CASE_NONE = 1 << 0,
2828 CHANGE_CASE_UPPER = 1 << 1,
2829 CHANGE_CASE_LOWER = 1 << 2,
2830 CHANGE_CASE_UPPER_SINGLE = 1 << 3,
2831 CHANGE_CASE_LOWER_SINGLE = 1 << 4,
2832 CHANGE_CASE_SINGLE_MASK = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
2833 CHANGE_CASE_LOWER_MASK = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
2834 CHANGE_CASE_UPPER_MASK = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
2837 struct _InterpolationData
2843 ChangeCase change_case;
2847 free_interpolation_data (InterpolationData *data)
2849 g_free (data->text);
2853 static const gchar *
2854 expand_escape (const gchar *replacement,
2856 InterpolationData *data,
2861 const gchar *error_detail;
2863 GError *tmp_error = NULL;
2871 data->type = REPL_TYPE_CHARACTER;
2876 data->type = REPL_TYPE_CHARACTER;
2881 data->type = REPL_TYPE_CHARACTER;
2886 data->type = REPL_TYPE_CHARACTER;
2891 data->type = REPL_TYPE_CHARACTER;
2896 data->type = REPL_TYPE_CHARACTER;
2901 data->type = REPL_TYPE_CHARACTER;
2906 data->type = REPL_TYPE_CHARACTER;
2916 h = g_ascii_xdigit_value (*p);
2919 error_detail = _("hexadecimal digit or “}” expected");
2930 for (i = 0; i < 2; i++)
2932 h = g_ascii_xdigit_value (*p);
2935 error_detail = _("hexadecimal digit expected");
2942 data->type = REPL_TYPE_STRING;
2943 data->text = g_new0 (gchar, 8);
2944 g_unichar_to_utf8 (x, data->text);
2948 data->type = REPL_TYPE_CHANGE_CASE;
2949 data->change_case = CHANGE_CASE_LOWER_SINGLE;
2953 data->type = REPL_TYPE_CHANGE_CASE;
2954 data->change_case = CHANGE_CASE_UPPER_SINGLE;
2958 data->type = REPL_TYPE_CHANGE_CASE;
2959 data->change_case = CHANGE_CASE_LOWER;
2963 data->type = REPL_TYPE_CHANGE_CASE;
2964 data->change_case = CHANGE_CASE_UPPER;
2968 data->type = REPL_TYPE_CHANGE_CASE;
2969 data->change_case = CHANGE_CASE_NONE;
2975 error_detail = _("missing “<” in symbolic reference");
2984 error_detail = _("unfinished symbolic reference");
2991 error_detail = _("zero-length symbolic reference");
2994 if (g_ascii_isdigit (*q))
2999 h = g_ascii_digit_value (*q);
3002 error_detail = _("digit expected");
3011 data->type = REPL_TYPE_NUMERIC_REFERENCE;
3018 if (!g_ascii_isalnum (*r))
3020 error_detail = _("illegal symbolic reference");
3027 data->text = g_strndup (q, p - q);
3028 data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
3033 /* if \0 is followed by a number is an octal number representing a
3034 * character, else it is a numeric reference. */
3035 if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
3038 p = g_utf8_next_char (p);
3052 for (i = 0; i < 3; i++)
3054 h = g_ascii_digit_value (*p);
3064 if (i == 2 && base == 10)
3070 if (base == 8 || i == 3)
3072 data->type = REPL_TYPE_STRING;
3073 data->text = g_new0 (gchar, 8);
3074 g_unichar_to_utf8 (x, data->text);
3078 data->type = REPL_TYPE_NUMERIC_REFERENCE;
3083 error_detail = _("stray final “\\”");
3087 error_detail = _("unknown escape sequence");
3094 /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
3095 tmp_error = g_error_new (G_REGEX_ERROR,
3096 G_REGEX_ERROR_REPLACE,
3097 _("Error while parsing replacement "
3098 "text “%s” at char %lu: %s"),
3100 (gulong)(p - replacement),
3102 g_propagate_error (error, tmp_error);
3108 split_replacement (const gchar *replacement,
3112 InterpolationData *data;
3113 const gchar *p, *start;
3115 start = p = replacement;
3120 data = g_new0 (InterpolationData, 1);
3121 start = p = expand_escape (replacement, p, data, error);
3124 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
3125 free_interpolation_data (data);
3129 list = g_list_prepend (list, data);
3134 if (*p == '\\' || *p == '\0')
3138 data = g_new0 (InterpolationData, 1);
3139 data->text = g_strndup (start, p - start);
3140 data->type = REPL_TYPE_STRING;
3141 list = g_list_prepend (list, data);
3147 return g_list_reverse (list);
3150 /* Change the case of c based on change_case. */
3151 #define CHANGE_CASE(c, change_case) \
3152 (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
3153 g_unichar_tolower (c) : \
3154 g_unichar_toupper (c))
3157 string_append (GString *string,
3159 ChangeCase *change_case)
3163 if (text[0] == '\0')
3166 if (*change_case == CHANGE_CASE_NONE)
3168 g_string_append (string, text);
3170 else if (*change_case & CHANGE_CASE_SINGLE_MASK)
3172 c = g_utf8_get_char (text);
3173 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
3174 g_string_append (string, g_utf8_next_char (text));
3175 *change_case = CHANGE_CASE_NONE;
3179 while (*text != '\0')
3181 c = g_utf8_get_char (text);
3182 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
3183 text = g_utf8_next_char (text);
3189 interpolate_replacement (const GMatchInfo *match_info,
3194 InterpolationData *idata;
3196 ChangeCase change_case = CHANGE_CASE_NONE;
3198 for (list = data; list; list = list->next)
3201 switch (idata->type)
3203 case REPL_TYPE_STRING:
3204 string_append (result, idata->text, &change_case);
3206 case REPL_TYPE_CHARACTER:
3207 g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
3208 if (change_case & CHANGE_CASE_SINGLE_MASK)
3209 change_case = CHANGE_CASE_NONE;
3211 case REPL_TYPE_NUMERIC_REFERENCE:
3212 match = g_match_info_fetch (match_info, idata->num);
3215 string_append (result, match, &change_case);
3219 case REPL_TYPE_SYMBOLIC_REFERENCE:
3220 match = g_match_info_fetch_named (match_info, idata->text);
3223 string_append (result, match, &change_case);
3227 case REPL_TYPE_CHANGE_CASE:
3228 change_case = idata->change_case;
3236 /* whether actual match_info is needed for replacement, i.e.
3237 * whether there are references
3240 interpolation_list_needs_match (GList *list)
3242 while (list != NULL)
3244 InterpolationData *data = list->data;
3246 if (data->type == REPL_TYPE_SYMBOLIC_REFERENCE ||
3247 data->type == REPL_TYPE_NUMERIC_REFERENCE)
3260 * @regex: a #GRegex structure
3261 * @string: (array length=string_len): the string to perform matches against
3262 * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
3263 * @start_position: starting index of the string to match, in bytes
3264 * @replacement: text to replace each match with
3265 * @match_options: options for the match
3266 * @error: location to store the error occurring, or %NULL to ignore errors
3268 * Replaces all occurrences of the pattern in @regex with the
3269 * replacement text. Backreferences of the form '\number' or
3270 * '\g<number>' in the replacement text are interpolated by the
3271 * number-th captured subexpression of the match, '\g<name>' refers
3272 * to the captured subexpression with the given name. '\0' refers
3273 * to the complete match, but '\0' followed by a number is the octal
3274 * representation of a character. To include a literal '\' in the
3275 * replacement, write '\\\\'.
3277 * There are also escapes that changes the case of the following text:
3279 * - \l: Convert to lower case the next character
3280 * - \u: Convert to upper case the next character
3281 * - \L: Convert to lower case till \E
3282 * - \U: Convert to upper case till \E
3283 * - \E: End case modification
3285 * If you do not need to use backreferences use g_regex_replace_literal().
3287 * The @replacement string must be UTF-8 encoded even if %G_REGEX_RAW was
3288 * passed to g_regex_new(). If you want to use not UTF-8 encoded strings
3289 * you can use g_regex_replace_literal().
3291 * Setting @start_position differs from just passing over a shortened
3292 * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern that
3293 * begins with any kind of lookbehind assertion, such as "\b".
3295 * Returns: a newly allocated string containing the replacements
3300 g_regex_replace (const GRegex *regex,
3301 const gchar *string,
3303 gint start_position,
3304 const gchar *replacement,
3305 GRegexMatchFlags match_options,
3310 GError *tmp_error = NULL;
3312 g_return_val_if_fail (regex != NULL, NULL);
3313 g_return_val_if_fail (string != NULL, NULL);
3314 g_return_val_if_fail (start_position >= 0, NULL);
3315 g_return_val_if_fail (replacement != NULL, NULL);
3316 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3317 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
3319 list = split_replacement (replacement, &tmp_error);
3320 if (tmp_error != NULL)
3322 g_propagate_error (error, tmp_error);
3326 result = g_regex_replace_eval (regex,
3327 string, string_len, start_position,
3329 interpolate_replacement,
3332 if (tmp_error != NULL)
3333 g_propagate_error (error, tmp_error);
3335 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
3341 literal_replacement (const GMatchInfo *match_info,
3345 g_string_append (result, data);
3350 * g_regex_replace_literal:
3351 * @regex: a #GRegex structure
3352 * @string: (array length=string_len): the string to perform matches against
3353 * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
3354 * @start_position: starting index of the string to match, in bytes
3355 * @replacement: text to replace each match with
3356 * @match_options: options for the match
3357 * @error: location to store the error occurring, or %NULL to ignore errors
3359 * Replaces all occurrences of the pattern in @regex with the
3360 * replacement text. @replacement is replaced literally, to
3361 * include backreferences use g_regex_replace().
3363 * Setting @start_position differs from just passing over a
3364 * shortened string and setting %G_REGEX_MATCH_NOTBOL in the
3365 * case of a pattern that begins with any kind of lookbehind
3366 * assertion, such as "\b".
3368 * Returns: a newly allocated string containing the replacements
3373 g_regex_replace_literal (const GRegex *regex,
3374 const gchar *string,
3376 gint start_position,
3377 const gchar *replacement,
3378 GRegexMatchFlags match_options,
3381 g_return_val_if_fail (replacement != NULL, NULL);
3382 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
3384 return g_regex_replace_eval (regex,
3385 string, string_len, start_position,
3387 literal_replacement,
3388 (gpointer)replacement,
3393 * g_regex_replace_eval:
3394 * @regex: a #GRegex structure from g_regex_new()
3395 * @string: (array length=string_len): string to perform matches against
3396 * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
3397 * @start_position: starting index of the string to match, in bytes
3398 * @match_options: options for the match
3399 * @eval: a function to call for each match
3400 * @user_data: user data to pass to the function
3401 * @error: location to store the error occurring, or %NULL to ignore errors
3403 * Replaces occurrences of the pattern in regex with the output of
3404 * @eval for that occurrence.
3406 * Setting @start_position differs from just passing over a shortened
3407 * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
3408 * that begins with any kind of lookbehind assertion, such as "\b".
3410 * The following example uses g_regex_replace_eval() to replace multiple
3412 * |[<!-- language="C" -->
3414 * eval_cb (const GMatchInfo *info,
3421 * match = g_match_info_fetch (info, 0);
3422 * r = g_hash_table_lookup ((GHashTable *)data, match);
3423 * g_string_append (res, r);
3435 * h = g_hash_table_new (g_str_hash, g_str_equal);
3437 * g_hash_table_insert (h, "1", "ONE");
3438 * g_hash_table_insert (h, "2", "TWO");
3439 * g_hash_table_insert (h, "3", "THREE");
3440 * g_hash_table_insert (h, "4", "FOUR");
3442 * reg = g_regex_new ("1|2|3|4", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
3443 * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL);
3444 * g_hash_table_destroy (h);
3449 * Returns: a newly allocated string containing the replacements
3454 g_regex_replace_eval (const GRegex *regex,
3455 const gchar *string,
3457 gint start_position,
3458 GRegexMatchFlags match_options,
3459 GRegexEvalCallback eval,
3463 GMatchInfo *match_info;
3466 gboolean done = FALSE;
3467 GError *tmp_error = NULL;
3469 g_return_val_if_fail (regex != NULL, NULL);
3470 g_return_val_if_fail (string != NULL, NULL);
3471 g_return_val_if_fail (start_position >= 0, NULL);
3472 g_return_val_if_fail (eval != NULL, NULL);
3473 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
3476 string_len = strlen (string);
3478 result = g_string_sized_new (string_len);
3480 /* run down the string making matches. */
3481 g_regex_match_full (regex, string, string_len, start_position,
3482 match_options, &match_info, &tmp_error);
3483 while (!done && g_match_info_matches (match_info))
3485 g_string_append_len (result,
3487 match_info->offsets[0] - str_pos);
3488 done = (*eval) (match_info, result, user_data);
3489 str_pos = match_info->offsets[1];
3490 g_match_info_next (match_info, &tmp_error);
3492 g_match_info_free (match_info);
3493 if (tmp_error != NULL)
3495 g_propagate_error (error, tmp_error);
3496 g_string_free (result, TRUE);
3500 g_string_append_len (result, string + str_pos, string_len - str_pos);
3501 return g_string_free (result, FALSE);
3505 * g_regex_check_replacement:
3506 * @replacement: the replacement string
3507 * @has_references: (out) (optional): location to store information about
3508 * references in @replacement or %NULL
3509 * @error: location to store error
3511 * Checks whether @replacement is a valid replacement string
3512 * (see g_regex_replace()), i.e. that all escape sequences in
3515 * If @has_references is not %NULL then @replacement is checked
3516 * for pattern references. For instance, replacement text 'foo\n'
3517 * does not contain references and may be evaluated without information
3518 * about actual match, but '\0\1' (whole match followed by first
3519 * subpattern) requires valid #GMatchInfo object.
3521 * Returns: whether @replacement is a valid replacement string
3526 g_regex_check_replacement (const gchar *replacement,
3527 gboolean *has_references,
3533 list = split_replacement (replacement, &tmp);
3537 g_propagate_error (error, tmp);
3542 *has_references = interpolation_list_needs_match (list);
3544 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
3550 * g_regex_escape_nul:
3551 * @string: the string to escape
3552 * @length: the length of @string
3554 * Escapes the nul characters in @string to "\x00". It can be used
3555 * to compile a regex with embedded nul characters.
3557 * For completeness, @length can be -1 for a nul-terminated string.
3558 * In this case the output string will be of course equal to @string.
3560 * Returns: a newly-allocated escaped string
3565 g_regex_escape_nul (const gchar *string,
3569 const gchar *p, *piece_start, *end;
3572 g_return_val_if_fail (string != NULL, NULL);
3575 return g_strdup (string);
3577 end = string + length;
3578 p = piece_start = string;
3579 escaped = g_string_sized_new (length + 1);
3587 if (p != piece_start)
3589 /* copy the previous piece. */
3590 g_string_append_len (escaped, piece_start, p - piece_start);
3592 if ((backslashes & 1) == 0)
3593 g_string_append_c (escaped, '\\');
3594 g_string_append_c (escaped, 'x');
3595 g_string_append_c (escaped, '0');
3596 g_string_append_c (escaped, '0');
3606 p = g_utf8_next_char (p);
3611 if (piece_start < end)
3612 g_string_append_len (escaped, piece_start, end - piece_start);
3614 return g_string_free (escaped, FALSE);
3618 * g_regex_escape_string:
3619 * @string: the string to escape
3620 * @length: the length of @string, in bytes, or -1 if @string is nul-terminated
3622 * Escapes the special characters used for regular expressions
3623 * in @string, for instance "a.b*c" becomes "a\.b\*c". This
3624 * function is useful to dynamically generate regular expressions.
3626 * @string can contain nul characters that are replaced with "\0",
3627 * in this case remember to specify the correct length of @string
3630 * Returns: a newly-allocated escaped string
3635 g_regex_escape_string (const gchar *string,
3639 const char *p, *piece_start, *end;
3641 g_return_val_if_fail (string != NULL, NULL);
3644 length = strlen (string);
3646 end = string + length;
3647 p = piece_start = string;
3648 escaped = g_string_sized_new (length + 1);
3669 if (p != piece_start)
3670 /* copy the previous piece. */
3671 g_string_append_len (escaped, piece_start, p - piece_start);
3672 g_string_append_c (escaped, '\\');
3674 g_string_append_c (escaped, '0');
3676 g_string_append_c (escaped, *p);
3680 p = g_utf8_next_char (p);
3685 if (piece_start < end)
3686 g_string_append_len (escaped, piece_start, end - piece_start);
3688 return g_string_free (escaped, FALSE);