Add some more cases to the app-id unit tests
[glib.git] / glib / gregex.c
bloba9f6814e266f56cda7434b7959da363e2675aecb
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>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <string.h>
25 #ifdef USE_SYSTEM_PCRE
26 #include <pcre.h>
27 #else
28 #include "pcre/pcre.h"
29 #endif
31 #include "gtypes.h"
32 #include "gregex.h"
33 #include "glibintl.h"
34 #include "glist.h"
35 #include "gmessages.h"
36 #include "gstrfuncs.h"
37 #include "gatomic.h"
38 #include "gthread.h"
40 /**
41 * SECTION:gregex
42 * @title: Perl-compatible regular expressions
43 * @short_description: matches strings against regular expressions
44 * @see_also: [Regular expression syntax][glib-regex-syntax]
46 * The g_regex_*() functions implement regular
47 * expression pattern matching using syntax and semantics similar to
48 * Perl regular expression.
50 * Some functions accept a @start_position argument, setting it differs
51 * from just passing over a shortened string and setting #G_REGEX_MATCH_NOTBOL
52 * in the case of a pattern that begins with any kind of lookbehind assertion.
53 * For example, consider the pattern "\Biss\B" which finds occurrences of "iss"
54 * in the middle of words. ("\B" matches only if the current position in the
55 * subject is not a word boundary.) When applied to the string "Mississipi"
56 * from the fourth byte, namely "issipi", it does not match, because "\B" is
57 * always false at the start of the subject, which is deemed to be a word
58 * boundary. However, if the entire string is passed , but with
59 * @start_position set to 4, it finds the second occurrence of "iss" because
60 * it is able to look behind the starting point to discover that it is
61 * preceded by a letter.
63 * Note that, unless you set the #G_REGEX_RAW flag, all the strings passed
64 * to these functions must be encoded in UTF-8. The lengths and the positions
65 * inside the strings are in bytes and not in characters, so, for instance,
66 * "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a
67 * single character. If you set #G_REGEX_RAW the strings can be non-valid
68 * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two
69 * bytes and two characters long.
71 * When matching a pattern, "\n" matches only against a "\n" character in
72 * the string, and "\r" matches only a "\r" character. To match any newline
73 * sequence use "\R". This particular group matches either the two-character
74 * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed,
75 * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"),
76 * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line
77 * separator, U+2028), or PS (paragraph separator, U+2029).
79 * The behaviour of the dot, circumflex, and dollar metacharacters are
80 * affected by newline characters, the default is to recognize any newline
81 * character (the same characters recognized by "\R"). This can be changed
82 * with #G_REGEX_NEWLINE_CR, #G_REGEX_NEWLINE_LF and #G_REGEX_NEWLINE_CRLF
83 * compile options, and with #G_REGEX_MATCH_NEWLINE_ANY,
84 * #G_REGEX_MATCH_NEWLINE_CR, #G_REGEX_MATCH_NEWLINE_LF and
85 * #G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also
86 * relevant when compiling a pattern if #G_REGEX_EXTENDED is set, and an
87 * unescaped "#" outside a character class is encountered. This indicates
88 * a comment that lasts until after the next newline.
90 * When setting the %G_REGEX_JAVASCRIPT_COMPAT flag, pattern syntax and pattern
91 * matching is changed to be compatible with the way that regular expressions
92 * work in JavaScript. More precisely, a lonely ']' character in the pattern
93 * is a syntax error; the '\x' escape only allows 0 to 2 hexadecimal digits, and
94 * you must use the '\u' escape sequence with 4 hex digits to specify a unicode
95 * codepoint instead of '\x' or 'x{....}'. If '\x' or '\u' are not followed by
96 * the specified number of hex digits, they match 'x' and 'u' literally; also
97 * '\U' always matches 'U' instead of being an error in the pattern. Finally,
98 * pattern matching is modified so that back references to an unset subpattern
99 * group produces a match with the empty string instead of an error. See
100 * pcreapi(3) for more information.
102 * Creating and manipulating the same #GRegex structure from different
103 * threads is not a problem as #GRegex does not modify its internal
104 * state between creation and destruction, on the other hand #GMatchInfo
105 * is not threadsafe.
107 * The regular expressions low-level functionalities are obtained through
108 * the excellent
109 * [PCRE](http://www.pcre.org/)
110 * library written by Philip Hazel.
113 /* Mask of all the possible values for GRegexCompileFlags. */
114 #define G_REGEX_COMPILE_MASK (G_REGEX_CASELESS | \
115 G_REGEX_MULTILINE | \
116 G_REGEX_DOTALL | \
117 G_REGEX_EXTENDED | \
118 G_REGEX_ANCHORED | \
119 G_REGEX_DOLLAR_ENDONLY | \
120 G_REGEX_UNGREEDY | \
121 G_REGEX_RAW | \
122 G_REGEX_NO_AUTO_CAPTURE | \
123 G_REGEX_OPTIMIZE | \
124 G_REGEX_FIRSTLINE | \
125 G_REGEX_DUPNAMES | \
126 G_REGEX_NEWLINE_CR | \
127 G_REGEX_NEWLINE_LF | \
128 G_REGEX_NEWLINE_CRLF | \
129 G_REGEX_NEWLINE_ANYCRLF | \
130 G_REGEX_BSR_ANYCRLF | \
131 G_REGEX_JAVASCRIPT_COMPAT)
133 /* Mask of all GRegexCompileFlags values that are (not) passed trough to PCRE */
134 #define G_REGEX_COMPILE_PCRE_MASK (G_REGEX_COMPILE_MASK & ~G_REGEX_COMPILE_NONPCRE_MASK)
135 #define G_REGEX_COMPILE_NONPCRE_MASK (G_REGEX_RAW | \
136 G_REGEX_OPTIMIZE)
138 /* Mask of all the possible values for GRegexMatchFlags. */
139 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_ANCHORED | \
140 G_REGEX_MATCH_NOTBOL | \
141 G_REGEX_MATCH_NOTEOL | \
142 G_REGEX_MATCH_NOTEMPTY | \
143 G_REGEX_MATCH_PARTIAL | \
144 G_REGEX_MATCH_NEWLINE_CR | \
145 G_REGEX_MATCH_NEWLINE_LF | \
146 G_REGEX_MATCH_NEWLINE_CRLF | \
147 G_REGEX_MATCH_NEWLINE_ANY | \
148 G_REGEX_MATCH_NEWLINE_ANYCRLF | \
149 G_REGEX_MATCH_BSR_ANYCRLF | \
150 G_REGEX_MATCH_BSR_ANY | \
151 G_REGEX_MATCH_PARTIAL_SOFT | \
152 G_REGEX_MATCH_PARTIAL_HARD | \
153 G_REGEX_MATCH_NOTEMPTY_ATSTART)
155 /* we rely on these flags having the same values */
156 G_STATIC_ASSERT (G_REGEX_CASELESS == PCRE_CASELESS);
157 G_STATIC_ASSERT (G_REGEX_MULTILINE == PCRE_MULTILINE);
158 G_STATIC_ASSERT (G_REGEX_DOTALL == PCRE_DOTALL);
159 G_STATIC_ASSERT (G_REGEX_EXTENDED == PCRE_EXTENDED);
160 G_STATIC_ASSERT (G_REGEX_ANCHORED == PCRE_ANCHORED);
161 G_STATIC_ASSERT (G_REGEX_DOLLAR_ENDONLY == PCRE_DOLLAR_ENDONLY);
162 G_STATIC_ASSERT (G_REGEX_UNGREEDY == PCRE_UNGREEDY);
163 G_STATIC_ASSERT (G_REGEX_NO_AUTO_CAPTURE == PCRE_NO_AUTO_CAPTURE);
164 G_STATIC_ASSERT (G_REGEX_FIRSTLINE == PCRE_FIRSTLINE);
165 G_STATIC_ASSERT (G_REGEX_DUPNAMES == PCRE_DUPNAMES);
166 G_STATIC_ASSERT (G_REGEX_NEWLINE_CR == PCRE_NEWLINE_CR);
167 G_STATIC_ASSERT (G_REGEX_NEWLINE_LF == PCRE_NEWLINE_LF);
168 G_STATIC_ASSERT (G_REGEX_NEWLINE_CRLF == PCRE_NEWLINE_CRLF);
169 G_STATIC_ASSERT (G_REGEX_NEWLINE_ANYCRLF == PCRE_NEWLINE_ANYCRLF);
170 G_STATIC_ASSERT (G_REGEX_BSR_ANYCRLF == PCRE_BSR_ANYCRLF);
171 G_STATIC_ASSERT (G_REGEX_JAVASCRIPT_COMPAT == PCRE_JAVASCRIPT_COMPAT);
173 G_STATIC_ASSERT (G_REGEX_MATCH_ANCHORED == PCRE_ANCHORED);
174 G_STATIC_ASSERT (G_REGEX_MATCH_NOTBOL == PCRE_NOTBOL);
175 G_STATIC_ASSERT (G_REGEX_MATCH_NOTEOL == PCRE_NOTEOL);
176 G_STATIC_ASSERT (G_REGEX_MATCH_NOTEMPTY == PCRE_NOTEMPTY);
177 G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL == PCRE_PARTIAL);
178 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_CR == PCRE_NEWLINE_CR);
179 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_LF == PCRE_NEWLINE_LF);
180 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_CRLF == PCRE_NEWLINE_CRLF);
181 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_ANY == PCRE_NEWLINE_ANY);
182 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_ANYCRLF == PCRE_NEWLINE_ANYCRLF);
183 G_STATIC_ASSERT (G_REGEX_MATCH_BSR_ANYCRLF == PCRE_BSR_ANYCRLF);
184 G_STATIC_ASSERT (G_REGEX_MATCH_BSR_ANY == PCRE_BSR_UNICODE);
185 G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL_SOFT == PCRE_PARTIAL_SOFT);
186 G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL_HARD == PCRE_PARTIAL_HARD);
187 G_STATIC_ASSERT (G_REGEX_MATCH_NOTEMPTY_ATSTART == PCRE_NOTEMPTY_ATSTART);
189 /* These PCRE flags are unused or not exposed publically in GRegexFlags, so
190 * it should be ok to reuse them for different things.
192 G_STATIC_ASSERT (G_REGEX_OPTIMIZE == PCRE_NO_UTF8_CHECK);
193 G_STATIC_ASSERT (G_REGEX_RAW == PCRE_UTF8);
195 /* if the string is in UTF-8 use g_utf8_ functions, else use
196 * use just +/- 1. */
197 #define NEXT_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
198 ((s) + 1) : \
199 g_utf8_next_char (s))
200 #define PREV_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
201 ((s) - 1) : \
202 g_utf8_prev_char (s))
204 struct _GMatchInfo
206 volatile gint ref_count; /* the ref count */
207 GRegex *regex; /* the regex */
208 GRegexMatchFlags match_opts; /* options used at match time on the regex */
209 gint matches; /* number of matching sub patterns */
210 gint pos; /* position in the string where last match left off */
211 gint n_offsets; /* number of offsets */
212 gint *offsets; /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
213 gint *workspace; /* workspace for pcre_dfa_exec() */
214 gint n_workspace; /* number of workspace elements */
215 const gchar *string; /* string passed to the match function */
216 gssize string_len; /* length of string */
219 struct _GRegex
221 volatile gint ref_count; /* the ref count for the immutable part */
222 gchar *pattern; /* the pattern */
223 pcre *pcre_re; /* compiled form of the pattern */
224 GRegexCompileFlags compile_opts; /* options used at compile time on the pattern */
225 GRegexMatchFlags match_opts; /* options used at match time on the regex */
226 pcre_extra *extra; /* data stored when G_REGEX_OPTIMIZE is used */
229 /* TRUE if ret is an error code, FALSE otherwise. */
230 #define IS_PCRE_ERROR(ret) ((ret) < PCRE_ERROR_NOMATCH && (ret) != PCRE_ERROR_PARTIAL)
232 typedef struct _InterpolationData InterpolationData;
233 static gboolean interpolation_list_needs_match (GList *list);
234 static gboolean interpolate_replacement (const GMatchInfo *match_info,
235 GString *result,
236 gpointer data);
237 static GList *split_replacement (const gchar *replacement,
238 GError **error);
239 static void free_interpolation_data (InterpolationData *data);
242 static const gchar *
243 match_error (gint errcode)
245 switch (errcode)
247 case PCRE_ERROR_NOMATCH:
248 /* not an error */
249 break;
250 case PCRE_ERROR_NULL:
251 /* NULL argument, this should not happen in GRegex */
252 g_warning ("A NULL argument was passed to PCRE");
253 break;
254 case PCRE_ERROR_BADOPTION:
255 return "bad options";
256 case PCRE_ERROR_BADMAGIC:
257 return _("corrupted object");
258 case PCRE_ERROR_UNKNOWN_OPCODE:
259 return N_("internal error or corrupted object");
260 case PCRE_ERROR_NOMEMORY:
261 return _("out of memory");
262 case PCRE_ERROR_NOSUBSTRING:
263 /* not used by pcre_exec() */
264 break;
265 case PCRE_ERROR_MATCHLIMIT:
266 return _("backtracking limit reached");
267 case PCRE_ERROR_CALLOUT:
268 /* callouts are not implemented */
269 break;
270 case PCRE_ERROR_BADUTF8:
271 case PCRE_ERROR_BADUTF8_OFFSET:
272 /* we do not check if strings are valid */
273 break;
274 case PCRE_ERROR_PARTIAL:
275 /* not an error */
276 break;
277 case PCRE_ERROR_BADPARTIAL:
278 return _("the pattern contains items not supported for partial matching");
279 case PCRE_ERROR_INTERNAL:
280 return _("internal error");
281 case PCRE_ERROR_BADCOUNT:
282 /* negative ovecsize, this should not happen in GRegex */
283 g_warning ("A negative ovecsize was passed to PCRE");
284 break;
285 case PCRE_ERROR_DFA_UITEM:
286 return _("the pattern contains items not supported for partial matching");
287 case PCRE_ERROR_DFA_UCOND:
288 return _("back references as conditions are not supported for partial matching");
289 case PCRE_ERROR_DFA_UMLIMIT:
290 /* the match_field field is not used in GRegex */
291 break;
292 case PCRE_ERROR_DFA_WSSIZE:
293 /* handled expanding the workspace */
294 break;
295 case PCRE_ERROR_DFA_RECURSE:
296 case PCRE_ERROR_RECURSIONLIMIT:
297 return _("recursion limit reached");
298 case PCRE_ERROR_BADNEWLINE:
299 return _("invalid combination of newline flags");
300 case PCRE_ERROR_BADOFFSET:
301 return _("bad offset");
302 case PCRE_ERROR_SHORTUTF8:
303 return _("short utf8");
304 case PCRE_ERROR_RECURSELOOP:
305 return _("recursion loop");
306 default:
307 break;
309 return _("unknown error");
312 static void
313 translate_compile_error (gint *errcode, const gchar **errmsg)
315 /* Compile errors are created adding 100 to the error code returned
316 * by PCRE.
317 * If errcode is known we put the translatable error message in
318 * erromsg. If errcode is unknown we put the generic
319 * G_REGEX_ERROR_COMPILE error code in errcode and keep the
320 * untranslated error message returned by PCRE.
321 * Note that there can be more PCRE errors with the same GRegexError
322 * and that some PCRE errors are useless for us.
324 *errcode += 100;
326 switch (*errcode)
328 case G_REGEX_ERROR_STRAY_BACKSLASH:
329 *errmsg = _("\\ at end of pattern");
330 break;
331 case G_REGEX_ERROR_MISSING_CONTROL_CHAR:
332 *errmsg = _("\\c at end of pattern");
333 break;
334 case G_REGEX_ERROR_UNRECOGNIZED_ESCAPE:
335 *errmsg = _("unrecognized character following \\");
336 break;
337 case G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER:
338 *errmsg = _("numbers out of order in {} quantifier");
339 break;
340 case G_REGEX_ERROR_QUANTIFIER_TOO_BIG:
341 *errmsg = _("number too big in {} quantifier");
342 break;
343 case G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS:
344 *errmsg = _("missing terminating ] for character class");
345 break;
346 case G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS:
347 *errmsg = _("invalid escape sequence in character class");
348 break;
349 case G_REGEX_ERROR_RANGE_OUT_OF_ORDER:
350 *errmsg = _("range out of order in character class");
351 break;
352 case G_REGEX_ERROR_NOTHING_TO_REPEAT:
353 *errmsg = _("nothing to repeat");
354 break;
355 case 111: /* internal error: unexpected repeat */
356 *errcode = G_REGEX_ERROR_INTERNAL;
357 *errmsg = _("unexpected repeat");
358 break;
359 case G_REGEX_ERROR_UNRECOGNIZED_CHARACTER:
360 *errmsg = _("unrecognized character after (? or (?-");
361 break;
362 case G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS:
363 *errmsg = _("POSIX named classes are supported only within a class");
364 break;
365 case G_REGEX_ERROR_UNMATCHED_PARENTHESIS:
366 *errmsg = _("missing terminating )");
367 break;
368 case G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE:
369 *errmsg = _("reference to non-existent subpattern");
370 break;
371 case G_REGEX_ERROR_UNTERMINATED_COMMENT:
372 *errmsg = _("missing ) after comment");
373 break;
374 case G_REGEX_ERROR_EXPRESSION_TOO_LARGE:
375 *errmsg = _("regular expression is too large");
376 break;
377 case G_REGEX_ERROR_MEMORY_ERROR:
378 *errmsg = _("failed to get memory");
379 break;
380 case 122: /* unmatched parentheses */
381 *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
382 *errmsg = _(") without opening (");
383 break;
384 case 123: /* internal error: code overflow */
385 *errcode = G_REGEX_ERROR_INTERNAL;
386 *errmsg = _("code overflow");
387 break;
388 case 124: /* "unrecognized character after (?<\0 */
389 *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
390 *errmsg = _("unrecognized character after (?<");
391 break;
392 case G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND:
393 *errmsg = _("lookbehind assertion is not fixed length");
394 break;
395 case G_REGEX_ERROR_MALFORMED_CONDITION:
396 *errmsg = _("malformed number or name after (?(");
397 break;
398 case G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES:
399 *errmsg = _("conditional group contains more than two branches");
400 break;
401 case G_REGEX_ERROR_ASSERTION_EXPECTED:
402 *errmsg = _("assertion expected after (?(");
403 break;
404 case 129:
405 *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
406 /* translators: '(?R' and '(?[+-]digits' are both meant as (groups of)
407 * sequences here, '(?-54' would be an example for the second group.
409 *errmsg = _("(?R or (?[+-]digits must be followed by )");
410 break;
411 case G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME:
412 *errmsg = _("unknown POSIX class name");
413 break;
414 case G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED:
415 *errmsg = _("POSIX collating elements are not supported");
416 break;
417 case G_REGEX_ERROR_HEX_CODE_TOO_LARGE:
418 *errmsg = _("character value in \\x{...} sequence is too large");
419 break;
420 case G_REGEX_ERROR_INVALID_CONDITION:
421 *errmsg = _("invalid condition (?(0)");
422 break;
423 case G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND:
424 *errmsg = _("\\C not allowed in lookbehind assertion");
425 break;
426 case 137: /* PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0 */
427 /* A number of Perl escapes are not handled by PCRE.
428 * Therefore it explicitly raises ERR37.
430 *errcode = G_REGEX_ERROR_UNRECOGNIZED_ESCAPE;
431 *errmsg = _("escapes \\L, \\l, \\N{name}, \\U, and \\u are not supported");
432 break;
433 case G_REGEX_ERROR_INFINITE_LOOP:
434 *errmsg = _("recursive call could loop indefinitely");
435 break;
436 case 141: /* unrecognized character after (?P\0 */
437 *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
438 *errmsg = _("unrecognized character after (?P");
439 break;
440 case G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR:
441 *errmsg = _("missing terminator in subpattern name");
442 break;
443 case G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME:
444 *errmsg = _("two named subpatterns have the same name");
445 break;
446 case G_REGEX_ERROR_MALFORMED_PROPERTY:
447 *errmsg = _("malformed \\P or \\p sequence");
448 break;
449 case G_REGEX_ERROR_UNKNOWN_PROPERTY:
450 *errmsg = _("unknown property name after \\P or \\p");
451 break;
452 case G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG:
453 *errmsg = _("subpattern name is too long (maximum 32 characters)");
454 break;
455 case G_REGEX_ERROR_TOO_MANY_SUBPATTERNS:
456 *errmsg = _("too many named subpatterns (maximum 10,000)");
457 break;
458 case G_REGEX_ERROR_INVALID_OCTAL_VALUE:
459 *errmsg = _("octal value is greater than \\377");
460 break;
461 case 152: /* internal error: overran compiling workspace */
462 *errcode = G_REGEX_ERROR_INTERNAL;
463 *errmsg = _("overran compiling workspace");
464 break;
465 case 153: /* internal error: previously-checked referenced subpattern not found */
466 *errcode = G_REGEX_ERROR_INTERNAL;
467 *errmsg = _("previously-checked referenced subpattern not found");
468 break;
469 case G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE:
470 *errmsg = _("DEFINE group contains more than one branch");
471 break;
472 case G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS:
473 *errmsg = _("inconsistent NEWLINE options");
474 break;
475 case G_REGEX_ERROR_MISSING_BACK_REFERENCE:
476 *errmsg = _("\\g is not followed by a braced, angle-bracketed, or quoted name or "
477 "number, or by a plain number");
478 break;
479 case G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE:
480 *errmsg = _("a numbered reference must not be zero");
481 break;
482 case G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN:
483 *errmsg = _("an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)");
484 break;
485 case G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB:
486 *errmsg = _("(*VERB) not recognized");
487 break;
488 case G_REGEX_ERROR_NUMBER_TOO_BIG:
489 *errmsg = _("number is too big");
490 break;
491 case G_REGEX_ERROR_MISSING_SUBPATTERN_NAME:
492 *errmsg = _("missing subpattern name after (?&");
493 break;
494 case G_REGEX_ERROR_MISSING_DIGIT:
495 *errmsg = _("digit expected after (?+");
496 break;
497 case G_REGEX_ERROR_INVALID_DATA_CHARACTER:
498 *errmsg = _("] is an invalid data character in JavaScript compatibility mode");
499 break;
500 case G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME:
501 *errmsg = _("different names for subpatterns of the same number are not allowed");
502 break;
503 case G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED:
504 *errmsg = _("(*MARK) must have an argument");
505 break;
506 case G_REGEX_ERROR_INVALID_CONTROL_CHAR:
507 *errmsg = _( "\\c must be followed by an ASCII character");
508 break;
509 case G_REGEX_ERROR_MISSING_NAME:
510 *errmsg = _("\\k is not followed by a braced, angle-bracketed, or quoted name");
511 break;
512 case G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS:
513 *errmsg = _("\\N is not supported in a class");
514 break;
515 case G_REGEX_ERROR_TOO_MANY_FORWARD_REFERENCES:
516 *errmsg = _("too many forward references");
517 break;
518 case G_REGEX_ERROR_NAME_TOO_LONG:
519 *errmsg = _("name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)");
520 break;
521 case G_REGEX_ERROR_CHARACTER_VALUE_TOO_LARGE:
522 *errmsg = _("character value in \\u.... sequence is too large");
523 break;
525 case 116: /* erroffset passed as NULL */
526 /* This should not happen as we never pass a NULL erroffset */
527 g_warning ("erroffset passed as NULL");
528 *errcode = G_REGEX_ERROR_COMPILE;
529 break;
530 case 117: /* unknown option bit(s) set */
531 /* This should not happen as we check options before passing them
532 * to pcre_compile2() */
533 g_warning ("unknown option bit(s) set");
534 *errcode = G_REGEX_ERROR_COMPILE;
535 break;
536 case 132: /* this version of PCRE is compiled without UTF support */
537 case 144: /* invalid UTF-8 string */
538 case 145: /* support for \\P, \\p, and \\X has not been compiled */
539 case 167: /* this version of PCRE is not compiled with Unicode property support */
540 case 173: /* disallowed Unicode code point (>= 0xd800 && <= 0xdfff) */
541 case 174: /* invalid UTF-16 string */
542 /* These errors should not happen as we are using an UTF-8 and UCP-enabled PCRE
543 * and we do not check if strings are valid */
544 case 170: /* internal error: unknown opcode in find_fixedlength() */
545 *errcode = G_REGEX_ERROR_INTERNAL;
546 break;
548 default:
549 *errcode = G_REGEX_ERROR_COMPILE;
553 /* GMatchInfo */
555 static GMatchInfo *
556 match_info_new (const GRegex *regex,
557 const gchar *string,
558 gint string_len,
559 gint start_position,
560 gint match_options,
561 gboolean is_dfa)
563 GMatchInfo *match_info;
565 if (string_len < 0)
566 string_len = strlen (string);
568 match_info = g_new0 (GMatchInfo, 1);
569 match_info->ref_count = 1;
570 match_info->regex = g_regex_ref ((GRegex *)regex);
571 match_info->string = string;
572 match_info->string_len = string_len;
573 match_info->matches = PCRE_ERROR_NOMATCH;
574 match_info->pos = start_position;
575 match_info->match_opts = match_options;
577 if (is_dfa)
579 /* These values should be enough for most cases, if they are not
580 * enough g_regex_match_all_full() will expand them. */
581 match_info->n_offsets = 24;
582 match_info->n_workspace = 100;
583 match_info->workspace = g_new (gint, match_info->n_workspace);
585 else
587 gint capture_count;
588 pcre_fullinfo (regex->pcre_re, regex->extra,
589 PCRE_INFO_CAPTURECOUNT, &capture_count);
590 match_info->n_offsets = (capture_count + 1) * 3;
593 match_info->offsets = g_new0 (gint, match_info->n_offsets);
594 /* Set an invalid position for the previous match. */
595 match_info->offsets[0] = -1;
596 match_info->offsets[1] = -1;
598 return match_info;
602 * g_match_info_get_regex:
603 * @match_info: a #GMatchInfo
605 * Returns #GRegex object used in @match_info. It belongs to Glib
606 * and must not be freed. Use g_regex_ref() if you need to keep it
607 * after you free @match_info object.
609 * Returns: #GRegex object used in @match_info
611 * Since: 2.14
613 GRegex *
614 g_match_info_get_regex (const GMatchInfo *match_info)
616 g_return_val_if_fail (match_info != NULL, NULL);
617 return match_info->regex;
621 * g_match_info_get_string:
622 * @match_info: a #GMatchInfo
624 * Returns the string searched with @match_info. This is the
625 * string passed to g_regex_match() or g_regex_replace() so
626 * you may not free it before calling this function.
628 * Returns: the string searched with @match_info
630 * Since: 2.14
632 const gchar *
633 g_match_info_get_string (const GMatchInfo *match_info)
635 g_return_val_if_fail (match_info != NULL, NULL);
636 return match_info->string;
640 * g_match_info_ref:
641 * @match_info: a #GMatchInfo
643 * Increases reference count of @match_info by 1.
645 * Returns: @match_info
647 * Since: 2.30
649 GMatchInfo *
650 g_match_info_ref (GMatchInfo *match_info)
652 g_return_val_if_fail (match_info != NULL, NULL);
653 g_atomic_int_inc (&match_info->ref_count);
654 return match_info;
658 * g_match_info_unref:
659 * @match_info: a #GMatchInfo
661 * Decreases reference count of @match_info by 1. When reference count drops
662 * to zero, it frees all the memory associated with the match_info structure.
664 * Since: 2.30
666 void
667 g_match_info_unref (GMatchInfo *match_info)
669 if (g_atomic_int_dec_and_test (&match_info->ref_count))
671 g_regex_unref (match_info->regex);
672 g_free (match_info->offsets);
673 g_free (match_info->workspace);
674 g_free (match_info);
679 * g_match_info_free:
680 * @match_info: (nullable): a #GMatchInfo, or %NULL
682 * If @match_info is not %NULL, calls g_match_info_unref(); otherwise does
683 * nothing.
685 * Since: 2.14
687 void
688 g_match_info_free (GMatchInfo *match_info)
690 if (match_info == NULL)
691 return;
693 g_match_info_unref (match_info);
697 * g_match_info_next:
698 * @match_info: a #GMatchInfo structure
699 * @error: location to store the error occurring, or %NULL to ignore errors
701 * Scans for the next match using the same parameters of the previous
702 * call to g_regex_match_full() or g_regex_match() that returned
703 * @match_info.
705 * The match is done on the string passed to the match function, so you
706 * cannot free it before calling this function.
708 * Returns: %TRUE is the string matched, %FALSE otherwise
710 * Since: 2.14
712 gboolean
713 g_match_info_next (GMatchInfo *match_info,
714 GError **error)
716 gint prev_match_start;
717 gint prev_match_end;
719 g_return_val_if_fail (match_info != NULL, FALSE);
720 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
721 g_return_val_if_fail (match_info->pos >= 0, FALSE);
723 prev_match_start = match_info->offsets[0];
724 prev_match_end = match_info->offsets[1];
726 if (match_info->pos > match_info->string_len)
728 /* we have reached the end of the string */
729 match_info->pos = -1;
730 match_info->matches = PCRE_ERROR_NOMATCH;
731 return FALSE;
734 match_info->matches = pcre_exec (match_info->regex->pcre_re,
735 match_info->regex->extra,
736 match_info->string,
737 match_info->string_len,
738 match_info->pos,
739 match_info->regex->match_opts | match_info->match_opts,
740 match_info->offsets,
741 match_info->n_offsets);
742 if (IS_PCRE_ERROR (match_info->matches))
744 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
745 _("Error while matching regular expression %s: %s"),
746 match_info->regex->pattern, match_error (match_info->matches));
747 return FALSE;
750 /* avoid infinite loops if the pattern is an empty string or something
751 * equivalent */
752 if (match_info->pos == match_info->offsets[1])
754 if (match_info->pos > match_info->string_len)
756 /* we have reached the end of the string */
757 match_info->pos = -1;
758 match_info->matches = PCRE_ERROR_NOMATCH;
759 return FALSE;
762 match_info->pos = NEXT_CHAR (match_info->regex,
763 &match_info->string[match_info->pos]) -
764 match_info->string;
766 else
768 match_info->pos = match_info->offsets[1];
771 /* it's possible to get two identical matches when we are matching
772 * empty strings, for instance if the pattern is "(?=[A-Z0-9])" and
773 * the string is "RegExTest" we have:
774 * - search at position 0: match from 0 to 0
775 * - search at position 1: match from 3 to 3
776 * - search at position 3: match from 3 to 3 (duplicate)
777 * - search at position 4: match from 5 to 5
778 * - search at position 5: match from 5 to 5 (duplicate)
779 * - search at position 6: no match -> stop
780 * so we have to ignore the duplicates.
781 * see bug #515944: http://bugzilla.gnome.org/show_bug.cgi?id=515944 */
782 if (match_info->matches >= 0 &&
783 prev_match_start == match_info->offsets[0] &&
784 prev_match_end == match_info->offsets[1])
786 /* ignore this match and search the next one */
787 return g_match_info_next (match_info, error);
790 return match_info->matches >= 0;
794 * g_match_info_matches:
795 * @match_info: a #GMatchInfo structure
797 * Returns whether the previous match operation succeeded.
799 * Returns: %TRUE if the previous match operation succeeded,
800 * %FALSE otherwise
802 * Since: 2.14
804 gboolean
805 g_match_info_matches (const GMatchInfo *match_info)
807 g_return_val_if_fail (match_info != NULL, FALSE);
809 return match_info->matches >= 0;
813 * g_match_info_get_match_count:
814 * @match_info: a #GMatchInfo structure
816 * Retrieves the number of matched substrings (including substring 0,
817 * that is the whole matched text), so 1 is returned if the pattern
818 * has no substrings in it and 0 is returned if the match failed.
820 * If the last match was obtained using the DFA algorithm, that is
821 * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
822 * count is not that of the number of capturing parentheses but that of
823 * the number of matched substrings.
825 * Returns: Number of matched substrings, or -1 if an error occurred
827 * Since: 2.14
829 gint
830 g_match_info_get_match_count (const GMatchInfo *match_info)
832 g_return_val_if_fail (match_info, -1);
834 if (match_info->matches == PCRE_ERROR_NOMATCH)
835 /* no match */
836 return 0;
837 else if (match_info->matches < PCRE_ERROR_NOMATCH)
838 /* error */
839 return -1;
840 else
841 /* match */
842 return match_info->matches;
846 * g_match_info_is_partial_match:
847 * @match_info: a #GMatchInfo structure
849 * Usually if the string passed to g_regex_match*() matches as far as
850 * it goes, but is too short to match the entire pattern, %FALSE is
851 * returned. There are circumstances where it might be helpful to
852 * distinguish this case from other cases in which there is no match.
854 * Consider, for example, an application where a human is required to
855 * type in data for a field with specific formatting requirements. An
856 * example might be a date in the form ddmmmyy, defined by the pattern
857 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
858 * If the application sees the user’s keystrokes one by one, and can
859 * check that what has been typed so far is potentially valid, it is
860 * able to raise an error as soon as a mistake is made.
862 * GRegex supports the concept of partial matching by means of the
863 * #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD flags.
864 * When they are used, the return code for
865 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
866 * for a complete match, %FALSE otherwise. But, when these functions
867 * return %FALSE, you can check if the match was partial calling
868 * g_match_info_is_partial_match().
870 * The difference between #G_REGEX_MATCH_PARTIAL_SOFT and
871 * #G_REGEX_MATCH_PARTIAL_HARD is that when a partial match is encountered
872 * with #G_REGEX_MATCH_PARTIAL_SOFT, matching continues to search for a
873 * possible complete match, while with #G_REGEX_MATCH_PARTIAL_HARD matching
874 * stops at the partial match.
875 * When both #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD
876 * are set, the latter takes precedence.
878 * There were formerly some restrictions on the pattern for partial matching.
879 * The restrictions no longer apply.
881 * See pcrepartial(3) for more information on partial matching.
883 * Returns: %TRUE if the match was partial, %FALSE otherwise
885 * Since: 2.14
887 gboolean
888 g_match_info_is_partial_match (const GMatchInfo *match_info)
890 g_return_val_if_fail (match_info != NULL, FALSE);
892 return match_info->matches == PCRE_ERROR_PARTIAL;
896 * g_match_info_expand_references:
897 * @match_info: (nullable): a #GMatchInfo or %NULL
898 * @string_to_expand: the string to expand
899 * @error: location to store the error occurring, or %NULL to ignore errors
901 * Returns a new string containing the text in @string_to_expand with
902 * references and escape sequences expanded. References refer to the last
903 * match done with @string against @regex and have the same syntax used by
904 * g_regex_replace().
906 * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
907 * passed to g_regex_new().
909 * The backreferences are extracted from the string passed to the match
910 * function, so you cannot call this function after freeing the string.
912 * @match_info may be %NULL in which case @string_to_expand must not
913 * contain references. For instance "foo\n" does not refer to an actual
914 * pattern and '\n' merely will be replaced with \n character,
915 * while to expand "\0" (whole match) one needs the result of a match.
916 * Use g_regex_check_replacement() to find out whether @string_to_expand
917 * contains references.
919 * Returns: (nullable): the expanded string, or %NULL if an error occurred
921 * Since: 2.14
923 gchar *
924 g_match_info_expand_references (const GMatchInfo *match_info,
925 const gchar *string_to_expand,
926 GError **error)
928 GString *result;
929 GList *list;
930 GError *tmp_error = NULL;
932 g_return_val_if_fail (string_to_expand != NULL, NULL);
933 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
935 list = split_replacement (string_to_expand, &tmp_error);
936 if (tmp_error != NULL)
938 g_propagate_error (error, tmp_error);
939 return NULL;
942 if (!match_info && interpolation_list_needs_match (list))
944 g_critical ("String '%s' contains references to the match, can't "
945 "expand references without GMatchInfo object",
946 string_to_expand);
947 return NULL;
950 result = g_string_sized_new (strlen (string_to_expand));
951 interpolate_replacement (match_info, result, list);
953 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
955 return g_string_free (result, FALSE);
959 * g_match_info_fetch:
960 * @match_info: #GMatchInfo structure
961 * @match_num: number of the sub expression
963 * Retrieves the text matching the @match_num'th capturing
964 * parentheses. 0 is the full text of the match, 1 is the first paren
965 * set, 2 the second, and so on.
967 * If @match_num is a valid sub pattern but it didn't match anything
968 * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
969 * string is returned.
971 * If the match was obtained using the DFA algorithm, that is using
972 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
973 * string is not that of a set of parentheses but that of a matched
974 * substring. Substrings are matched in reverse order of length, so
975 * 0 is the longest match.
977 * The string is fetched from the string passed to the match function,
978 * so you cannot call this function after freeing the string.
980 * Returns: (nullable): The matched substring, or %NULL if an error
981 * occurred. You have to free the string yourself
983 * Since: 2.14
985 gchar *
986 g_match_info_fetch (const GMatchInfo *match_info,
987 gint match_num)
989 /* we cannot use pcre_get_substring() because it allocates the
990 * string using pcre_malloc(). */
991 gchar *match = NULL;
992 gint start, end;
994 g_return_val_if_fail (match_info != NULL, NULL);
995 g_return_val_if_fail (match_num >= 0, NULL);
997 /* match_num does not exist or it didn't matched, i.e. matching "b"
998 * against "(a)?b" then group 0 is empty. */
999 if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
1000 match = NULL;
1001 else if (start == -1)
1002 match = g_strdup ("");
1003 else
1004 match = g_strndup (&match_info->string[start], end - start);
1006 return match;
1010 * g_match_info_fetch_pos:
1011 * @match_info: #GMatchInfo structure
1012 * @match_num: number of the sub expression
1013 * @start_pos: (out) (optional): pointer to location where to store
1014 * the start position, or %NULL
1015 * @end_pos: (out) (optional): pointer to location where to store
1016 * the end position, or %NULL
1018 * Retrieves the position in bytes of the @match_num'th capturing
1019 * parentheses. 0 is the full text of the match, 1 is the first
1020 * paren set, 2 the second, and so on.
1022 * If @match_num is a valid sub pattern but it didn't match anything
1023 * (e.g. sub pattern 1, matching "b" against "(a)?b") then @start_pos
1024 * and @end_pos are set to -1 and %TRUE is returned.
1026 * If the match was obtained using the DFA algorithm, that is using
1027 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1028 * position is not that of a set of parentheses but that of a matched
1029 * substring. Substrings are matched in reverse order of length, so
1030 * 0 is the longest match.
1032 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
1033 * the position cannot be fetched, @start_pos and @end_pos are left
1034 * unchanged
1036 * Since: 2.14
1038 gboolean
1039 g_match_info_fetch_pos (const GMatchInfo *match_info,
1040 gint match_num,
1041 gint *start_pos,
1042 gint *end_pos)
1044 g_return_val_if_fail (match_info != NULL, FALSE);
1045 g_return_val_if_fail (match_num >= 0, FALSE);
1047 /* make sure the sub expression number they're requesting is less than
1048 * the total number of sub expressions that were matched. */
1049 if (match_num >= match_info->matches)
1050 return FALSE;
1052 if (start_pos != NULL)
1053 *start_pos = match_info->offsets[2 * match_num];
1055 if (end_pos != NULL)
1056 *end_pos = match_info->offsets[2 * match_num + 1];
1058 return TRUE;
1062 * Returns number of first matched subpattern with name @name.
1063 * There may be more than one in case when DUPNAMES is used,
1064 * and not all subpatterns with that name match;
1065 * pcre_get_stringnumber() does not work in that case.
1067 static gint
1068 get_matched_substring_number (const GMatchInfo *match_info,
1069 const gchar *name)
1071 gint entrysize;
1072 gchar *first, *last;
1073 guchar *entry;
1075 if (!(match_info->regex->compile_opts & G_REGEX_DUPNAMES))
1076 return pcre_get_stringnumber (match_info->regex->pcre_re, name);
1078 /* This code is copied from pcre_get.c: get_first_set() */
1079 entrysize = pcre_get_stringtable_entries (match_info->regex->pcre_re,
1080 name,
1081 &first,
1082 &last);
1084 if (entrysize <= 0)
1085 return entrysize;
1087 for (entry = (guchar*) first; entry <= (guchar*) last; entry += entrysize)
1089 gint n = (entry[0] << 8) + entry[1];
1090 if (match_info->offsets[n*2] >= 0)
1091 return n;
1094 return (first[0] << 8) + first[1];
1098 * g_match_info_fetch_named:
1099 * @match_info: #GMatchInfo structure
1100 * @name: name of the subexpression
1102 * Retrieves the text matching the capturing parentheses named @name.
1104 * If @name is a valid sub pattern name but it didn't match anything
1105 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
1106 * then an empty string is returned.
1108 * The string is fetched from the string passed to the match function,
1109 * so you cannot call this function after freeing the string.
1111 * Returns: (nullable): The matched substring, or %NULL if an error
1112 * occurred. You have to free the string yourself
1114 * Since: 2.14
1116 gchar *
1117 g_match_info_fetch_named (const GMatchInfo *match_info,
1118 const gchar *name)
1120 /* we cannot use pcre_get_named_substring() because it allocates the
1121 * string using pcre_malloc(). */
1122 gint num;
1124 g_return_val_if_fail (match_info != NULL, NULL);
1125 g_return_val_if_fail (name != NULL, NULL);
1127 num = get_matched_substring_number (match_info, name);
1128 if (num < 0)
1129 return NULL;
1130 else
1131 return g_match_info_fetch (match_info, num);
1135 * g_match_info_fetch_named_pos:
1136 * @match_info: #GMatchInfo structure
1137 * @name: name of the subexpression
1138 * @start_pos: (out) (optional): pointer to location where to store
1139 * the start position, or %NULL
1140 * @end_pos: (out) (optional): pointer to location where to store
1141 * the end position, or %NULL
1143 * Retrieves the position in bytes of the capturing parentheses named @name.
1145 * If @name is a valid sub pattern name but it didn't match anything
1146 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
1147 * then @start_pos and @end_pos are set to -1 and %TRUE is returned.
1149 * Returns: %TRUE if the position was fetched, %FALSE otherwise.
1150 * If the position cannot be fetched, @start_pos and @end_pos
1151 * are left unchanged.
1153 * Since: 2.14
1155 gboolean
1156 g_match_info_fetch_named_pos (const GMatchInfo *match_info,
1157 const gchar *name,
1158 gint *start_pos,
1159 gint *end_pos)
1161 gint num;
1163 g_return_val_if_fail (match_info != NULL, FALSE);
1164 g_return_val_if_fail (name != NULL, FALSE);
1166 num = get_matched_substring_number (match_info, name);
1167 if (num < 0)
1168 return FALSE;
1170 return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
1174 * g_match_info_fetch_all:
1175 * @match_info: a #GMatchInfo structure
1177 * Bundles up pointers to each of the matching substrings from a match
1178 * and stores them in an array of gchar pointers. The first element in
1179 * the returned array is the match number 0, i.e. the entire matched
1180 * text.
1182 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
1183 * "b" against "(a)?b") then an empty string is inserted.
1185 * If the last match was obtained using the DFA algorithm, that is using
1186 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1187 * strings are not that matched by sets of parentheses but that of the
1188 * matched substring. Substrings are matched in reverse order of length,
1189 * so the first one is the longest match.
1191 * The strings are fetched from the string passed to the match function,
1192 * so you cannot call this function after freeing the string.
1194 * Returns: (transfer full): a %NULL-terminated array of gchar *
1195 * pointers. It must be freed using g_strfreev(). If the previous
1196 * match failed %NULL is returned
1198 * Since: 2.14
1200 gchar **
1201 g_match_info_fetch_all (const GMatchInfo *match_info)
1203 /* we cannot use pcre_get_substring_list() because the returned value
1204 * isn't suitable for g_strfreev(). */
1205 gchar **result;
1206 gint i;
1208 g_return_val_if_fail (match_info != NULL, NULL);
1210 if (match_info->matches < 0)
1211 return NULL;
1213 result = g_new (gchar *, match_info->matches + 1);
1214 for (i = 0; i < match_info->matches; i++)
1215 result[i] = g_match_info_fetch (match_info, i);
1216 result[i] = NULL;
1218 return result;
1222 /* GRegex */
1224 G_DEFINE_QUARK (g-regex-error-quark, g_regex_error)
1227 * g_regex_ref:
1228 * @regex: a #GRegex
1230 * Increases reference count of @regex by 1.
1232 * Returns: @regex
1234 * Since: 2.14
1236 GRegex *
1237 g_regex_ref (GRegex *regex)
1239 g_return_val_if_fail (regex != NULL, NULL);
1240 g_atomic_int_inc (&regex->ref_count);
1241 return regex;
1245 * g_regex_unref:
1246 * @regex: a #GRegex
1248 * Decreases reference count of @regex by 1. When reference count drops
1249 * to zero, it frees all the memory associated with the regex structure.
1251 * Since: 2.14
1253 void
1254 g_regex_unref (GRegex *regex)
1256 g_return_if_fail (regex != NULL);
1258 if (g_atomic_int_dec_and_test (&regex->ref_count))
1260 g_free (regex->pattern);
1261 if (regex->pcre_re != NULL)
1262 pcre_free (regex->pcre_re);
1263 if (regex->extra != NULL)
1264 pcre_free (regex->extra);
1265 g_free (regex);
1270 * @match_options: (inout) (optional):
1272 static pcre *regex_compile (const gchar *pattern,
1273 GRegexCompileFlags compile_options,
1274 GRegexCompileFlags *compile_options_out,
1275 GRegexMatchFlags *match_options,
1276 GError **error);
1279 * g_regex_new:
1280 * @pattern: the regular expression
1281 * @compile_options: compile options for the regular expression, or 0
1282 * @match_options: match options for the regular expression, or 0
1283 * @error: return location for a #GError
1285 * Compiles the regular expression to an internal form, and does
1286 * the initial setup of the #GRegex structure.
1288 * Returns: (nullable): a #GRegex structure or %NULL if an error occured. Call
1289 * g_regex_unref() when you are done with it
1291 * Since: 2.14
1293 GRegex *
1294 g_regex_new (const gchar *pattern,
1295 GRegexCompileFlags compile_options,
1296 GRegexMatchFlags match_options,
1297 GError **error)
1299 GRegex *regex;
1300 pcre *re;
1301 const gchar *errmsg;
1302 gboolean optimize = FALSE;
1303 static volatile gsize initialised = 0;
1305 g_return_val_if_fail (pattern != NULL, NULL);
1306 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1307 g_return_val_if_fail ((compile_options & ~G_REGEX_COMPILE_MASK) == 0, NULL);
1308 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1310 if (g_once_init_enter (&initialised))
1312 int supports_utf8, supports_ucp;
1314 pcre_config (PCRE_CONFIG_UTF8, &supports_utf8);
1315 if (!supports_utf8)
1316 g_critical (_("PCRE library is compiled without UTF8 support"));
1318 pcre_config (PCRE_CONFIG_UNICODE_PROPERTIES, &supports_ucp);
1319 if (!supports_ucp)
1320 g_critical (_("PCRE library is compiled without UTF8 properties support"));
1322 g_once_init_leave (&initialised, supports_utf8 && supports_ucp ? 1 : 2);
1325 if (G_UNLIKELY (initialised != 1))
1327 g_set_error_literal (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE,
1328 _("PCRE library is compiled with incompatible options"));
1329 return NULL;
1332 /* G_REGEX_OPTIMIZE has the same numeric value of PCRE_NO_UTF8_CHECK,
1333 * as we do not need to wrap PCRE_NO_UTF8_CHECK. */
1334 if (compile_options & G_REGEX_OPTIMIZE)
1335 optimize = TRUE;
1337 re = regex_compile (pattern, compile_options, &compile_options,
1338 &match_options, error);
1340 if (re == NULL)
1341 return NULL;
1343 regex = g_new0 (GRegex, 1);
1344 regex->ref_count = 1;
1345 regex->pattern = g_strdup (pattern);
1346 regex->pcre_re = re;
1347 regex->compile_opts = compile_options;
1348 regex->match_opts = match_options;
1350 if (optimize)
1352 regex->extra = pcre_study (regex->pcre_re, 0, &errmsg);
1353 if (errmsg != NULL)
1355 GError *tmp_error = g_error_new (G_REGEX_ERROR,
1356 G_REGEX_ERROR_OPTIMIZE,
1357 _("Error while optimizing "
1358 "regular expression %s: %s"),
1359 regex->pattern,
1360 errmsg);
1361 g_propagate_error (error, tmp_error);
1363 g_regex_unref (regex);
1364 return NULL;
1368 return regex;
1371 static pcre *
1372 regex_compile (const gchar *pattern,
1373 GRegexCompileFlags compile_options,
1374 GRegexCompileFlags *compile_options_out,
1375 GRegexMatchFlags *match_options,
1376 GError **error)
1378 pcre *re;
1379 const gchar *errmsg;
1380 gint erroffset;
1381 gint errcode;
1382 GRegexCompileFlags nonpcre_compile_options;
1383 unsigned long int pcre_compile_options;
1385 nonpcre_compile_options = compile_options & G_REGEX_COMPILE_NONPCRE_MASK;
1387 /* In GRegex the string are, by default, UTF-8 encoded. PCRE
1388 * instead uses UTF-8 only if required with PCRE_UTF8. */
1389 if (compile_options & G_REGEX_RAW)
1391 /* disable utf-8 */
1392 compile_options &= ~G_REGEX_RAW;
1394 else
1396 /* enable utf-8 */
1397 compile_options |= PCRE_UTF8 | PCRE_NO_UTF8_CHECK;
1399 if (match_options != NULL)
1400 *match_options |= PCRE_NO_UTF8_CHECK;
1403 /* PCRE_NEWLINE_ANY is the default for the internal PCRE but
1404 * not for the system one. */
1405 if (!(compile_options & G_REGEX_NEWLINE_CR) &&
1406 !(compile_options & G_REGEX_NEWLINE_LF))
1408 compile_options |= PCRE_NEWLINE_ANY;
1411 compile_options |= PCRE_UCP;
1413 /* PCRE_BSR_UNICODE is the default for the internal PCRE but
1414 * possibly not for the system one.
1416 if (~compile_options & G_REGEX_BSR_ANYCRLF)
1417 compile_options |= PCRE_BSR_UNICODE;
1419 /* compile the pattern */
1420 re = pcre_compile2 (pattern, compile_options, &errcode,
1421 &errmsg, &erroffset, NULL);
1423 /* if the compilation failed, set the error member and return
1424 * immediately */
1425 if (re == NULL)
1427 GError *tmp_error;
1429 /* Translate the PCRE error code to GRegexError and use a translated
1430 * error message if possible */
1431 translate_compile_error (&errcode, &errmsg);
1433 /* PCRE uses byte offsets but we want to show character offsets */
1434 erroffset = g_utf8_pointer_to_offset (pattern, &pattern[erroffset]);
1436 tmp_error = g_error_new (G_REGEX_ERROR, errcode,
1437 _("Error while compiling regular "
1438 "expression %s at char %d: %s"),
1439 pattern, erroffset, errmsg);
1440 g_propagate_error (error, tmp_error);
1442 return NULL;
1445 /* For options set at the beginning of the pattern, pcre puts them into
1446 * compile options, e.g. "(?i)foo" will make the pcre structure store
1447 * PCRE_CASELESS even though it wasn't explicitly given for compilation. */
1448 pcre_fullinfo (re, NULL, PCRE_INFO_OPTIONS, &pcre_compile_options);
1449 compile_options = pcre_compile_options & G_REGEX_COMPILE_PCRE_MASK;
1451 /* Don't leak PCRE_NEWLINE_ANY, which is part of PCRE_NEWLINE_ANYCRLF */
1452 if ((pcre_compile_options & PCRE_NEWLINE_ANYCRLF) != PCRE_NEWLINE_ANYCRLF)
1453 compile_options &= ~PCRE_NEWLINE_ANY;
1455 compile_options |= nonpcre_compile_options;
1457 if (!(compile_options & G_REGEX_DUPNAMES))
1459 gboolean jchanged = FALSE;
1460 pcre_fullinfo (re, NULL, PCRE_INFO_JCHANGED, &jchanged);
1461 if (jchanged)
1462 compile_options |= G_REGEX_DUPNAMES;
1465 if (compile_options_out != 0)
1466 *compile_options_out = compile_options;
1468 return re;
1472 * g_regex_get_pattern:
1473 * @regex: a #GRegex structure
1475 * Gets the pattern string associated with @regex, i.e. a copy of
1476 * the string passed to g_regex_new().
1478 * Returns: the pattern of @regex
1480 * Since: 2.14
1482 const gchar *
1483 g_regex_get_pattern (const GRegex *regex)
1485 g_return_val_if_fail (regex != NULL, NULL);
1487 return regex->pattern;
1491 * g_regex_get_max_backref:
1492 * @regex: a #GRegex
1494 * Returns the number of the highest back reference
1495 * in the pattern, or 0 if the pattern does not contain
1496 * back references.
1498 * Returns: the number of the highest back reference
1500 * Since: 2.14
1502 gint
1503 g_regex_get_max_backref (const GRegex *regex)
1505 gint value;
1507 pcre_fullinfo (regex->pcre_re, regex->extra,
1508 PCRE_INFO_BACKREFMAX, &value);
1510 return value;
1514 * g_regex_get_capture_count:
1515 * @regex: a #GRegex
1517 * Returns the number of capturing subpatterns in the pattern.
1519 * Returns: the number of capturing subpatterns
1521 * Since: 2.14
1523 gint
1524 g_regex_get_capture_count (const GRegex *regex)
1526 gint value;
1528 pcre_fullinfo (regex->pcre_re, regex->extra,
1529 PCRE_INFO_CAPTURECOUNT, &value);
1531 return value;
1535 * g_regex_get_has_cr_or_lf:
1536 * @regex: a #GRegex structure
1538 * Checks whether the pattern contains explicit CR or LF references.
1540 * Returns: %TRUE if the pattern contains explicit CR or LF references
1542 * Since: 2.34
1544 gboolean
1545 g_regex_get_has_cr_or_lf (const GRegex *regex)
1547 gint value;
1549 pcre_fullinfo (regex->pcre_re, regex->extra,
1550 PCRE_INFO_HASCRORLF, &value);
1552 return !!value;
1556 * g_regex_get_max_lookbehind:
1557 * @regex: a #GRegex structure
1559 * Gets the number of characters in the longest lookbehind assertion in the
1560 * pattern. This information is useful when doing multi-segment matching using
1561 * the partial matching facilities.
1563 * Returns: the number of characters in the longest lookbehind assertion.
1565 * Since: 2.38
1567 gint
1568 g_regex_get_max_lookbehind (const GRegex *regex)
1570 gint max_lookbehind;
1572 pcre_fullinfo (regex->pcre_re, regex->extra,
1573 PCRE_INFO_MAXLOOKBEHIND, &max_lookbehind);
1575 return max_lookbehind;
1579 * g_regex_get_compile_flags:
1580 * @regex: a #GRegex
1582 * Returns the compile options that @regex was created with.
1584 * Depending on the version of PCRE that is used, this may or may not
1585 * include flags set by option expressions such as `(?i)` found at the
1586 * top-level within the compiled pattern.
1588 * Returns: flags from #GRegexCompileFlags
1590 * Since: 2.26
1592 GRegexCompileFlags
1593 g_regex_get_compile_flags (const GRegex *regex)
1595 g_return_val_if_fail (regex != NULL, 0);
1597 return regex->compile_opts;
1601 * g_regex_get_match_flags:
1602 * @regex: a #GRegex
1604 * Returns the match options that @regex was created with.
1606 * Returns: flags from #GRegexMatchFlags
1608 * Since: 2.26
1610 GRegexMatchFlags
1611 g_regex_get_match_flags (const GRegex *regex)
1613 g_return_val_if_fail (regex != NULL, 0);
1615 return regex->match_opts & G_REGEX_MATCH_MASK;
1619 * g_regex_match_simple:
1620 * @pattern: the regular expression
1621 * @string: the string to scan for matches
1622 * @compile_options: compile options for the regular expression, or 0
1623 * @match_options: match options, or 0
1625 * Scans for a match in @string for @pattern.
1627 * This function is equivalent to g_regex_match() but it does not
1628 * require to compile the pattern with g_regex_new(), avoiding some
1629 * lines of code when you need just to do a match without extracting
1630 * substrings, capture counts, and so on.
1632 * If this function is to be called on the same @pattern more than
1633 * once, it's more efficient to compile the pattern once with
1634 * g_regex_new() and then use g_regex_match().
1636 * Returns: %TRUE if the string matched, %FALSE otherwise
1638 * Since: 2.14
1640 gboolean
1641 g_regex_match_simple (const gchar *pattern,
1642 const gchar *string,
1643 GRegexCompileFlags compile_options,
1644 GRegexMatchFlags match_options)
1646 GRegex *regex;
1647 gboolean result;
1649 regex = g_regex_new (pattern, compile_options, 0, NULL);
1650 if (!regex)
1651 return FALSE;
1652 result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
1653 g_regex_unref (regex);
1654 return result;
1658 * g_regex_match:
1659 * @regex: a #GRegex structure from g_regex_new()
1660 * @string: the string to scan for matches
1661 * @match_options: match options
1662 * @match_info: (out) (optional): pointer to location where to store
1663 * the #GMatchInfo, or %NULL if you do not need it
1665 * Scans for a match in string for the pattern in @regex.
1666 * The @match_options are combined with the match options specified
1667 * when the @regex structure was created, letting you have more
1668 * flexibility in reusing #GRegex structures.
1670 * A #GMatchInfo structure, used to get information on the match,
1671 * is stored in @match_info if not %NULL. Note that if @match_info
1672 * is not %NULL then it is created even if the function returns %FALSE,
1673 * i.e. you must free it regardless if regular expression actually matched.
1675 * To retrieve all the non-overlapping matches of the pattern in
1676 * string you can use g_match_info_next().
1678 * |[<!-- language="C" -->
1679 * static void
1680 * print_uppercase_words (const gchar *string)
1682 * // Print all uppercase-only words.
1683 * GRegex *regex;
1684 * GMatchInfo *match_info;
1686 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1687 * g_regex_match (regex, string, 0, &match_info);
1688 * while (g_match_info_matches (match_info))
1690 * gchar *word = g_match_info_fetch (match_info, 0);
1691 * g_print ("Found: %s\n", word);
1692 * g_free (word);
1693 * g_match_info_next (match_info, NULL);
1695 * g_match_info_free (match_info);
1696 * g_regex_unref (regex);
1698 * ]|
1700 * @string is not copied and is used in #GMatchInfo internally. If
1701 * you use any #GMatchInfo method (except g_match_info_free()) after
1702 * freeing or modifying @string then the behaviour is undefined.
1704 * Returns: %TRUE is the string matched, %FALSE otherwise
1706 * Since: 2.14
1708 gboolean
1709 g_regex_match (const GRegex *regex,
1710 const gchar *string,
1711 GRegexMatchFlags match_options,
1712 GMatchInfo **match_info)
1714 return g_regex_match_full (regex, string, -1, 0, match_options,
1715 match_info, NULL);
1719 * g_regex_match_full:
1720 * @regex: a #GRegex structure from g_regex_new()
1721 * @string: (array length=string_len): the string to scan for matches
1722 * @string_len: the length of @string, or -1 if @string is nul-terminated
1723 * @start_position: starting index of the string to match, in bytes
1724 * @match_options: match options
1725 * @match_info: (out) (optional): pointer to location where to store
1726 * the #GMatchInfo, or %NULL if you do not need it
1727 * @error: location to store the error occurring, or %NULL to ignore errors
1729 * Scans for a match in string for the pattern in @regex.
1730 * The @match_options are combined with the match options specified
1731 * when the @regex structure was created, letting you have more
1732 * flexibility in reusing #GRegex structures.
1734 * Setting @start_position differs from just passing over a shortened
1735 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
1736 * that begins with any kind of lookbehind assertion, such as "\b".
1738 * A #GMatchInfo structure, used to get information on the match, is
1739 * stored in @match_info if not %NULL. Note that if @match_info is
1740 * not %NULL then it is created even if the function returns %FALSE,
1741 * i.e. you must free it regardless if regular expression actually
1742 * matched.
1744 * @string is not copied and is used in #GMatchInfo internally. If
1745 * you use any #GMatchInfo method (except g_match_info_free()) after
1746 * freeing or modifying @string then the behaviour is undefined.
1748 * To retrieve all the non-overlapping matches of the pattern in
1749 * string you can use g_match_info_next().
1751 * |[<!-- language="C" -->
1752 * static void
1753 * print_uppercase_words (const gchar *string)
1755 * // Print all uppercase-only words.
1756 * GRegex *regex;
1757 * GMatchInfo *match_info;
1758 * GError *error = NULL;
1760 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1761 * g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
1762 * while (g_match_info_matches (match_info))
1764 * gchar *word = g_match_info_fetch (match_info, 0);
1765 * g_print ("Found: %s\n", word);
1766 * g_free (word);
1767 * g_match_info_next (match_info, &error);
1769 * g_match_info_free (match_info);
1770 * g_regex_unref (regex);
1771 * if (error != NULL)
1773 * g_printerr ("Error while matching: %s\n", error->message);
1774 * g_error_free (error);
1777 * ]|
1779 * Returns: %TRUE is the string matched, %FALSE otherwise
1781 * Since: 2.14
1783 gboolean
1784 g_regex_match_full (const GRegex *regex,
1785 const gchar *string,
1786 gssize string_len,
1787 gint start_position,
1788 GRegexMatchFlags match_options,
1789 GMatchInfo **match_info,
1790 GError **error)
1792 GMatchInfo *info;
1793 gboolean match_ok;
1795 g_return_val_if_fail (regex != NULL, FALSE);
1796 g_return_val_if_fail (string != NULL, FALSE);
1797 g_return_val_if_fail (start_position >= 0, FALSE);
1798 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1799 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1801 info = match_info_new (regex, string, string_len, start_position,
1802 match_options, FALSE);
1803 match_ok = g_match_info_next (info, error);
1804 if (match_info != NULL)
1805 *match_info = info;
1806 else
1807 g_match_info_free (info);
1809 return match_ok;
1813 * g_regex_match_all:
1814 * @regex: a #GRegex structure from g_regex_new()
1815 * @string: the string to scan for matches
1816 * @match_options: match options
1817 * @match_info: (out) (optional): pointer to location where to store
1818 * the #GMatchInfo, or %NULL if you do not need it
1820 * Using the standard algorithm for regular expression matching only
1821 * the longest match in the string is retrieved. This function uses
1822 * a different algorithm so it can retrieve all the possible matches.
1823 * For more documentation see g_regex_match_all_full().
1825 * A #GMatchInfo structure, used to get information on the match, is
1826 * stored in @match_info if not %NULL. Note that if @match_info is
1827 * not %NULL then it is created even if the function returns %FALSE,
1828 * i.e. you must free it regardless if regular expression actually
1829 * matched.
1831 * @string is not copied and is used in #GMatchInfo internally. If
1832 * you use any #GMatchInfo method (except g_match_info_free()) after
1833 * freeing or modifying @string then the behaviour is undefined.
1835 * Returns: %TRUE is the string matched, %FALSE otherwise
1837 * Since: 2.14
1839 gboolean
1840 g_regex_match_all (const GRegex *regex,
1841 const gchar *string,
1842 GRegexMatchFlags match_options,
1843 GMatchInfo **match_info)
1845 return g_regex_match_all_full (regex, string, -1, 0, match_options,
1846 match_info, NULL);
1850 * g_regex_match_all_full:
1851 * @regex: a #GRegex structure from g_regex_new()
1852 * @string: (array length=string_len): the string to scan for matches
1853 * @string_len: the length of @string, or -1 if @string is nul-terminated
1854 * @start_position: starting index of the string to match, in bytes
1855 * @match_options: match options
1856 * @match_info: (out) (optional): pointer to location where to store
1857 * the #GMatchInfo, or %NULL if you do not need it
1858 * @error: location to store the error occurring, or %NULL to ignore errors
1860 * Using the standard algorithm for regular expression matching only
1861 * the longest match in the string is retrieved, it is not possible
1862 * to obtain all the available matches. For instance matching
1863 * "<a> <b> <c>" against the pattern "<.*>"
1864 * you get "<a> <b> <c>".
1866 * This function uses a different algorithm (called DFA, i.e. deterministic
1867 * finite automaton), so it can retrieve all the possible matches, all
1868 * starting at the same point in the string. For instance matching
1869 * "<a> <b> <c>" against the pattern "<.*>;"
1870 * you would obtain three matches: "<a> <b> <c>",
1871 * "<a> <b>" and "<a>".
1873 * The number of matched strings is retrieved using
1874 * g_match_info_get_match_count(). To obtain the matched strings and
1875 * their position you can use, respectively, g_match_info_fetch() and
1876 * g_match_info_fetch_pos(). Note that the strings are returned in
1877 * reverse order of length; that is, the longest matching string is
1878 * given first.
1880 * Note that the DFA algorithm is slower than the standard one and it
1881 * is not able to capture substrings, so backreferences do not work.
1883 * Setting @start_position differs from just passing over a shortened
1884 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
1885 * that begins with any kind of lookbehind assertion, such as "\b".
1887 * A #GMatchInfo structure, used to get information on the match, is
1888 * stored in @match_info if not %NULL. Note that if @match_info is
1889 * not %NULL then it is created even if the function returns %FALSE,
1890 * i.e. you must free it regardless if regular expression actually
1891 * matched.
1893 * @string is not copied and is used in #GMatchInfo internally. If
1894 * you use any #GMatchInfo method (except g_match_info_free()) after
1895 * freeing or modifying @string then the behaviour is undefined.
1897 * Returns: %TRUE is the string matched, %FALSE otherwise
1899 * Since: 2.14
1901 gboolean
1902 g_regex_match_all_full (const GRegex *regex,
1903 const gchar *string,
1904 gssize string_len,
1905 gint start_position,
1906 GRegexMatchFlags match_options,
1907 GMatchInfo **match_info,
1908 GError **error)
1910 GMatchInfo *info;
1911 gboolean done;
1912 pcre *pcre_re;
1913 pcre_extra *extra;
1914 gboolean retval;
1916 g_return_val_if_fail (regex != NULL, FALSE);
1917 g_return_val_if_fail (string != NULL, FALSE);
1918 g_return_val_if_fail (start_position >= 0, FALSE);
1919 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1920 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1922 #ifdef PCRE_NO_AUTO_POSSESS
1923 /* For PCRE >= 8.34 we need to turn off PCRE_NO_AUTO_POSSESS, which
1924 * is an optimization for normal regex matching, but results in omitting
1925 * some shorter matches here, and an observable behaviour change.
1927 * DFA matching is rather niche, and very rarely used according to
1928 * codesearch.debian.net, so don't bother caching the recompiled RE. */
1929 pcre_re = regex_compile (regex->pattern,
1930 regex->compile_opts | PCRE_NO_AUTO_POSSESS,
1931 NULL, NULL, error);
1933 if (pcre_re == NULL)
1934 return FALSE;
1936 /* Not bothering to cache the optimization data either, with similar
1937 * reasoning */
1938 extra = NULL;
1939 #else
1940 /* For PCRE < 8.33 the precompiled regex is fine. */
1941 pcre_re = regex->pcre_re;
1942 extra = regex->extra;
1943 #endif
1945 info = match_info_new (regex, string, string_len, start_position,
1946 match_options, TRUE);
1948 done = FALSE;
1949 while (!done)
1951 done = TRUE;
1952 info->matches = pcre_dfa_exec (pcre_re, extra,
1953 info->string, info->string_len,
1954 info->pos,
1955 regex->match_opts | match_options,
1956 info->offsets, info->n_offsets,
1957 info->workspace, info->n_workspace);
1958 if (info->matches == PCRE_ERROR_DFA_WSSIZE)
1960 /* info->workspace is too small. */
1961 info->n_workspace *= 2;
1962 info->workspace = g_realloc (info->workspace,
1963 info->n_workspace * sizeof (gint));
1964 done = FALSE;
1966 else if (info->matches == 0)
1968 /* info->offsets is too small. */
1969 info->n_offsets *= 2;
1970 info->offsets = g_realloc (info->offsets,
1971 info->n_offsets * sizeof (gint));
1972 done = FALSE;
1974 else if (IS_PCRE_ERROR (info->matches))
1976 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
1977 _("Error while matching regular expression %s: %s"),
1978 regex->pattern, match_error (info->matches));
1982 #ifdef PCRE_NO_AUTO_POSSESS
1983 pcre_free (pcre_re);
1984 #endif
1986 /* set info->pos to -1 so that a call to g_match_info_next() fails. */
1987 info->pos = -1;
1988 retval = info->matches >= 0;
1990 if (match_info != NULL)
1991 *match_info = info;
1992 else
1993 g_match_info_free (info);
1995 return retval;
1999 * g_regex_get_string_number:
2000 * @regex: #GRegex structure
2001 * @name: name of the subexpression
2003 * Retrieves the number of the subexpression named @name.
2005 * Returns: The number of the subexpression or -1 if @name
2006 * does not exists
2008 * Since: 2.14
2010 gint
2011 g_regex_get_string_number (const GRegex *regex,
2012 const gchar *name)
2014 gint num;
2016 g_return_val_if_fail (regex != NULL, -1);
2017 g_return_val_if_fail (name != NULL, -1);
2019 num = pcre_get_stringnumber (regex->pcre_re, name);
2020 if (num == PCRE_ERROR_NOSUBSTRING)
2021 num = -1;
2023 return num;
2027 * g_regex_split_simple:
2028 * @pattern: the regular expression
2029 * @string: the string to scan for matches
2030 * @compile_options: compile options for the regular expression, or 0
2031 * @match_options: match options, or 0
2033 * Breaks the string on the pattern, and returns an array of
2034 * the tokens. If the pattern contains capturing parentheses,
2035 * then the text for each of the substrings will also be returned.
2036 * If the pattern does not match anywhere in the string, then the
2037 * whole string is returned as the first token.
2039 * This function is equivalent to g_regex_split() but it does
2040 * not require to compile the pattern with g_regex_new(), avoiding
2041 * some lines of code when you need just to do a split without
2042 * extracting substrings, capture counts, and so on.
2044 * If this function is to be called on the same @pattern more than
2045 * once, it's more efficient to compile the pattern once with
2046 * g_regex_new() and then use g_regex_split().
2048 * As a special case, the result of splitting the empty string ""
2049 * is an empty vector, not a vector containing a single string.
2050 * The reason for this special case is that being able to represent
2051 * a empty vector is typically more useful than consistent handling
2052 * of empty elements. If you do need to represent empty elements,
2053 * you'll need to check for the empty string before calling this
2054 * function.
2056 * A pattern that can match empty strings splits @string into
2057 * separate characters wherever it matches the empty string between
2058 * characters. For example splitting "ab c" using as a separator
2059 * "\s*", you will get "a", "b" and "c".
2061 * Returns: (transfer full): a %NULL-terminated array of strings. Free
2062 * it using g_strfreev()
2064 * Since: 2.14
2066 gchar **
2067 g_regex_split_simple (const gchar *pattern,
2068 const gchar *string,
2069 GRegexCompileFlags compile_options,
2070 GRegexMatchFlags match_options)
2072 GRegex *regex;
2073 gchar **result;
2075 regex = g_regex_new (pattern, compile_options, 0, NULL);
2076 if (!regex)
2077 return NULL;
2079 result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
2080 g_regex_unref (regex);
2081 return result;
2085 * g_regex_split:
2086 * @regex: a #GRegex structure
2087 * @string: the string to split with the pattern
2088 * @match_options: match time option flags
2090 * Breaks the string on the pattern, and returns an array of the tokens.
2091 * If the pattern contains capturing parentheses, then the text for each
2092 * of the substrings will also be returned. If the pattern does not match
2093 * anywhere in the string, then the whole string is returned as the first
2094 * token.
2096 * As a special case, the result of splitting the empty string "" is an
2097 * empty vector, not a vector containing a single string. The reason for
2098 * this special case is that being able to represent a empty vector is
2099 * typically more useful than consistent handling of empty elements. If
2100 * you do need to represent empty elements, you'll need to check for the
2101 * empty string before calling this function.
2103 * A pattern that can match empty strings splits @string into separate
2104 * characters wherever it matches the empty string between characters.
2105 * For example splitting "ab c" using as a separator "\s*", you will get
2106 * "a", "b" and "c".
2108 * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
2109 * it using g_strfreev()
2111 * Since: 2.14
2113 gchar **
2114 g_regex_split (const GRegex *regex,
2115 const gchar *string,
2116 GRegexMatchFlags match_options)
2118 return g_regex_split_full (regex, string, -1, 0,
2119 match_options, 0, NULL);
2123 * g_regex_split_full:
2124 * @regex: a #GRegex structure
2125 * @string: (array length=string_len): the string to split with the pattern
2126 * @string_len: the length of @string, or -1 if @string is nul-terminated
2127 * @start_position: starting index of the string to match, in bytes
2128 * @match_options: match time option flags
2129 * @max_tokens: the maximum number of tokens to split @string into.
2130 * If this is less than 1, the string is split completely
2131 * @error: return location for a #GError
2133 * Breaks the string on the pattern, and returns an array of the tokens.
2134 * If the pattern contains capturing parentheses, then the text for each
2135 * of the substrings will also be returned. If the pattern does not match
2136 * anywhere in the string, then the whole string is returned as the first
2137 * token.
2139 * As a special case, the result of splitting the empty string "" is an
2140 * empty vector, not a vector containing a single string. The reason for
2141 * this special case is that being able to represent a empty vector is
2142 * typically more useful than consistent handling of empty elements. If
2143 * you do need to represent empty elements, you'll need to check for the
2144 * empty string before calling this function.
2146 * A pattern that can match empty strings splits @string into separate
2147 * characters wherever it matches the empty string between characters.
2148 * For example splitting "ab c" using as a separator "\s*", you will get
2149 * "a", "b" and "c".
2151 * Setting @start_position differs from just passing over a shortened
2152 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
2153 * that begins with any kind of lookbehind assertion, such as "\b".
2155 * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
2156 * it using g_strfreev()
2158 * Since: 2.14
2160 gchar **
2161 g_regex_split_full (const GRegex *regex,
2162 const gchar *string,
2163 gssize string_len,
2164 gint start_position,
2165 GRegexMatchFlags match_options,
2166 gint max_tokens,
2167 GError **error)
2169 GError *tmp_error = NULL;
2170 GMatchInfo *match_info;
2171 GList *list, *last;
2172 gint i;
2173 gint token_count;
2174 gboolean match_ok;
2175 /* position of the last separator. */
2176 gint last_separator_end;
2177 /* was the last match 0 bytes long? */
2178 gboolean last_match_is_empty;
2179 /* the returned array of char **s */
2180 gchar **string_list;
2182 g_return_val_if_fail (regex != NULL, NULL);
2183 g_return_val_if_fail (string != NULL, NULL);
2184 g_return_val_if_fail (start_position >= 0, NULL);
2185 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2186 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2188 if (max_tokens <= 0)
2189 max_tokens = G_MAXINT;
2191 if (string_len < 0)
2192 string_len = strlen (string);
2194 /* zero-length string */
2195 if (string_len - start_position == 0)
2196 return g_new0 (gchar *, 1);
2198 if (max_tokens == 1)
2200 string_list = g_new0 (gchar *, 2);
2201 string_list[0] = g_strndup (&string[start_position],
2202 string_len - start_position);
2203 return string_list;
2206 list = NULL;
2207 token_count = 0;
2208 last_separator_end = start_position;
2209 last_match_is_empty = FALSE;
2211 match_ok = g_regex_match_full (regex, string, string_len, start_position,
2212 match_options, &match_info, &tmp_error);
2214 while (tmp_error == NULL)
2216 if (match_ok)
2218 last_match_is_empty =
2219 (match_info->offsets[0] == match_info->offsets[1]);
2221 /* we need to skip empty separators at the same position of the end
2222 * of another separator. e.g. the string is "a b" and the separator
2223 * is " *", so from 1 to 2 we have a match and at position 2 we have
2224 * an empty match. */
2225 if (last_separator_end != match_info->offsets[1])
2227 gchar *token;
2228 gint match_count;
2230 token = g_strndup (string + last_separator_end,
2231 match_info->offsets[0] - last_separator_end);
2232 list = g_list_prepend (list, token);
2233 token_count++;
2235 /* if there were substrings, these need to be added to
2236 * the list. */
2237 match_count = g_match_info_get_match_count (match_info);
2238 if (match_count > 1)
2240 for (i = 1; i < match_count; i++)
2241 list = g_list_prepend (list, g_match_info_fetch (match_info, i));
2245 else
2247 /* if there was no match, copy to end of string. */
2248 if (!last_match_is_empty)
2250 gchar *token = g_strndup (string + last_separator_end,
2251 match_info->string_len - last_separator_end);
2252 list = g_list_prepend (list, token);
2254 /* no more tokens, end the loop. */
2255 break;
2258 /* -1 to leave room for the last part. */
2259 if (token_count >= max_tokens - 1)
2261 /* we have reached the maximum number of tokens, so we copy
2262 * the remaining part of the string. */
2263 if (last_match_is_empty)
2265 /* the last match was empty, so we have moved one char
2266 * after the real position to avoid empty matches at the
2267 * same position. */
2268 match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
2270 /* the if is needed in the case we have terminated the available
2271 * tokens, but we are at the end of the string, so there are no
2272 * characters left to copy. */
2273 if (string_len > match_info->pos)
2275 gchar *token = g_strndup (string + match_info->pos,
2276 string_len - match_info->pos);
2277 list = g_list_prepend (list, token);
2279 /* end the loop. */
2280 break;
2283 last_separator_end = match_info->pos;
2284 if (last_match_is_empty)
2285 /* if the last match was empty, g_match_info_next() has moved
2286 * forward to avoid infinite loops, but we still need to copy that
2287 * character. */
2288 last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
2290 match_ok = g_match_info_next (match_info, &tmp_error);
2292 g_match_info_free (match_info);
2293 if (tmp_error != NULL)
2295 g_propagate_error (error, tmp_error);
2296 g_list_free_full (list, g_free);
2297 return NULL;
2300 string_list = g_new (gchar *, g_list_length (list) + 1);
2301 i = 0;
2302 for (last = g_list_last (list); last; last = g_list_previous (last))
2303 string_list[i++] = last->data;
2304 string_list[i] = NULL;
2305 g_list_free (list);
2307 return string_list;
2310 enum
2312 REPL_TYPE_STRING,
2313 REPL_TYPE_CHARACTER,
2314 REPL_TYPE_SYMBOLIC_REFERENCE,
2315 REPL_TYPE_NUMERIC_REFERENCE,
2316 REPL_TYPE_CHANGE_CASE
2319 typedef enum
2321 CHANGE_CASE_NONE = 1 << 0,
2322 CHANGE_CASE_UPPER = 1 << 1,
2323 CHANGE_CASE_LOWER = 1 << 2,
2324 CHANGE_CASE_UPPER_SINGLE = 1 << 3,
2325 CHANGE_CASE_LOWER_SINGLE = 1 << 4,
2326 CHANGE_CASE_SINGLE_MASK = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
2327 CHANGE_CASE_LOWER_MASK = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
2328 CHANGE_CASE_UPPER_MASK = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
2329 } ChangeCase;
2331 struct _InterpolationData
2333 gchar *text;
2334 gint type;
2335 gint num;
2336 gchar c;
2337 ChangeCase change_case;
2340 static void
2341 free_interpolation_data (InterpolationData *data)
2343 g_free (data->text);
2344 g_free (data);
2347 static const gchar *
2348 expand_escape (const gchar *replacement,
2349 const gchar *p,
2350 InterpolationData *data,
2351 GError **error)
2353 const gchar *q, *r;
2354 gint x, d, h, i;
2355 const gchar *error_detail;
2356 gint base = 0;
2357 GError *tmp_error = NULL;
2359 p++;
2360 switch (*p)
2362 case 't':
2363 p++;
2364 data->c = '\t';
2365 data->type = REPL_TYPE_CHARACTER;
2366 break;
2367 case 'n':
2368 p++;
2369 data->c = '\n';
2370 data->type = REPL_TYPE_CHARACTER;
2371 break;
2372 case 'v':
2373 p++;
2374 data->c = '\v';
2375 data->type = REPL_TYPE_CHARACTER;
2376 break;
2377 case 'r':
2378 p++;
2379 data->c = '\r';
2380 data->type = REPL_TYPE_CHARACTER;
2381 break;
2382 case 'f':
2383 p++;
2384 data->c = '\f';
2385 data->type = REPL_TYPE_CHARACTER;
2386 break;
2387 case 'a':
2388 p++;
2389 data->c = '\a';
2390 data->type = REPL_TYPE_CHARACTER;
2391 break;
2392 case 'b':
2393 p++;
2394 data->c = '\b';
2395 data->type = REPL_TYPE_CHARACTER;
2396 break;
2397 case '\\':
2398 p++;
2399 data->c = '\\';
2400 data->type = REPL_TYPE_CHARACTER;
2401 break;
2402 case 'x':
2403 p++;
2404 x = 0;
2405 if (*p == '{')
2407 p++;
2410 h = g_ascii_xdigit_value (*p);
2411 if (h < 0)
2413 error_detail = _("hexadecimal digit or “}” expected");
2414 goto error;
2416 x = x * 16 + h;
2417 p++;
2419 while (*p != '}');
2420 p++;
2422 else
2424 for (i = 0; i < 2; i++)
2426 h = g_ascii_xdigit_value (*p);
2427 if (h < 0)
2429 error_detail = _("hexadecimal digit expected");
2430 goto error;
2432 x = x * 16 + h;
2433 p++;
2436 data->type = REPL_TYPE_STRING;
2437 data->text = g_new0 (gchar, 8);
2438 g_unichar_to_utf8 (x, data->text);
2439 break;
2440 case 'l':
2441 p++;
2442 data->type = REPL_TYPE_CHANGE_CASE;
2443 data->change_case = CHANGE_CASE_LOWER_SINGLE;
2444 break;
2445 case 'u':
2446 p++;
2447 data->type = REPL_TYPE_CHANGE_CASE;
2448 data->change_case = CHANGE_CASE_UPPER_SINGLE;
2449 break;
2450 case 'L':
2451 p++;
2452 data->type = REPL_TYPE_CHANGE_CASE;
2453 data->change_case = CHANGE_CASE_LOWER;
2454 break;
2455 case 'U':
2456 p++;
2457 data->type = REPL_TYPE_CHANGE_CASE;
2458 data->change_case = CHANGE_CASE_UPPER;
2459 break;
2460 case 'E':
2461 p++;
2462 data->type = REPL_TYPE_CHANGE_CASE;
2463 data->change_case = CHANGE_CASE_NONE;
2464 break;
2465 case 'g':
2466 p++;
2467 if (*p != '<')
2469 error_detail = _("missing “<” in symbolic reference");
2470 goto error;
2472 q = p + 1;
2475 p++;
2476 if (!*p)
2478 error_detail = _("unfinished symbolic reference");
2479 goto error;
2482 while (*p != '>');
2483 if (p - q == 0)
2485 error_detail = _("zero-length symbolic reference");
2486 goto error;
2488 if (g_ascii_isdigit (*q))
2490 x = 0;
2493 h = g_ascii_digit_value (*q);
2494 if (h < 0)
2496 error_detail = _("digit expected");
2497 p = q;
2498 goto error;
2500 x = x * 10 + h;
2501 q++;
2503 while (q != p);
2504 data->num = x;
2505 data->type = REPL_TYPE_NUMERIC_REFERENCE;
2507 else
2509 r = q;
2512 if (!g_ascii_isalnum (*r))
2514 error_detail = _("illegal symbolic reference");
2515 p = r;
2516 goto error;
2518 r++;
2520 while (r != p);
2521 data->text = g_strndup (q, p - q);
2522 data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
2524 p++;
2525 break;
2526 case '0':
2527 /* if \0 is followed by a number is an octal number representing a
2528 * character, else it is a numeric reference. */
2529 if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
2531 base = 8;
2532 p = g_utf8_next_char (p);
2534 case '1':
2535 case '2':
2536 case '3':
2537 case '4':
2538 case '5':
2539 case '6':
2540 case '7':
2541 case '8':
2542 case '9':
2543 x = 0;
2544 d = 0;
2545 for (i = 0; i < 3; i++)
2547 h = g_ascii_digit_value (*p);
2548 if (h < 0)
2549 break;
2550 if (h > 7)
2552 if (base == 8)
2553 break;
2554 else
2555 base = 10;
2557 if (i == 2 && base == 10)
2558 break;
2559 x = x * 8 + h;
2560 d = d * 10 + h;
2561 p++;
2563 if (base == 8 || i == 3)
2565 data->type = REPL_TYPE_STRING;
2566 data->text = g_new0 (gchar, 8);
2567 g_unichar_to_utf8 (x, data->text);
2569 else
2571 data->type = REPL_TYPE_NUMERIC_REFERENCE;
2572 data->num = d;
2574 break;
2575 case 0:
2576 error_detail = _("stray final “\\”");
2577 goto error;
2578 break;
2579 default:
2580 error_detail = _("unknown escape sequence");
2581 goto error;
2584 return p;
2586 error:
2587 /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
2588 tmp_error = g_error_new (G_REGEX_ERROR,
2589 G_REGEX_ERROR_REPLACE,
2590 _("Error while parsing replacement "
2591 "text “%s” at char %lu: %s"),
2592 replacement,
2593 (gulong)(p - replacement),
2594 error_detail);
2595 g_propagate_error (error, tmp_error);
2597 return NULL;
2600 static GList *
2601 split_replacement (const gchar *replacement,
2602 GError **error)
2604 GList *list = NULL;
2605 InterpolationData *data;
2606 const gchar *p, *start;
2608 start = p = replacement;
2609 while (*p)
2611 if (*p == '\\')
2613 data = g_new0 (InterpolationData, 1);
2614 start = p = expand_escape (replacement, p, data, error);
2615 if (p == NULL)
2617 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
2618 free_interpolation_data (data);
2620 return NULL;
2622 list = g_list_prepend (list, data);
2624 else
2626 p++;
2627 if (*p == '\\' || *p == '\0')
2629 if (p - start > 0)
2631 data = g_new0 (InterpolationData, 1);
2632 data->text = g_strndup (start, p - start);
2633 data->type = REPL_TYPE_STRING;
2634 list = g_list_prepend (list, data);
2640 return g_list_reverse (list);
2643 /* Change the case of c based on change_case. */
2644 #define CHANGE_CASE(c, change_case) \
2645 (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
2646 g_unichar_tolower (c) : \
2647 g_unichar_toupper (c))
2649 static void
2650 string_append (GString *string,
2651 const gchar *text,
2652 ChangeCase *change_case)
2654 gunichar c;
2656 if (text[0] == '\0')
2657 return;
2659 if (*change_case == CHANGE_CASE_NONE)
2661 g_string_append (string, text);
2663 else if (*change_case & CHANGE_CASE_SINGLE_MASK)
2665 c = g_utf8_get_char (text);
2666 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2667 g_string_append (string, g_utf8_next_char (text));
2668 *change_case = CHANGE_CASE_NONE;
2670 else
2672 while (*text != '\0')
2674 c = g_utf8_get_char (text);
2675 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2676 text = g_utf8_next_char (text);
2681 static gboolean
2682 interpolate_replacement (const GMatchInfo *match_info,
2683 GString *result,
2684 gpointer data)
2686 GList *list;
2687 InterpolationData *idata;
2688 gchar *match;
2689 ChangeCase change_case = CHANGE_CASE_NONE;
2691 for (list = data; list; list = list->next)
2693 idata = list->data;
2694 switch (idata->type)
2696 case REPL_TYPE_STRING:
2697 string_append (result, idata->text, &change_case);
2698 break;
2699 case REPL_TYPE_CHARACTER:
2700 g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
2701 if (change_case & CHANGE_CASE_SINGLE_MASK)
2702 change_case = CHANGE_CASE_NONE;
2703 break;
2704 case REPL_TYPE_NUMERIC_REFERENCE:
2705 match = g_match_info_fetch (match_info, idata->num);
2706 if (match)
2708 string_append (result, match, &change_case);
2709 g_free (match);
2711 break;
2712 case REPL_TYPE_SYMBOLIC_REFERENCE:
2713 match = g_match_info_fetch_named (match_info, idata->text);
2714 if (match)
2716 string_append (result, match, &change_case);
2717 g_free (match);
2719 break;
2720 case REPL_TYPE_CHANGE_CASE:
2721 change_case = idata->change_case;
2722 break;
2726 return FALSE;
2729 /* whether actual match_info is needed for replacement, i.e.
2730 * whether there are references
2732 static gboolean
2733 interpolation_list_needs_match (GList *list)
2735 while (list != NULL)
2737 InterpolationData *data = list->data;
2739 if (data->type == REPL_TYPE_SYMBOLIC_REFERENCE ||
2740 data->type == REPL_TYPE_NUMERIC_REFERENCE)
2742 return TRUE;
2745 list = list->next;
2748 return FALSE;
2752 * g_regex_replace:
2753 * @regex: a #GRegex structure
2754 * @string: (array length=string_len): the string to perform matches against
2755 * @string_len: the length of @string, or -1 if @string is nul-terminated
2756 * @start_position: starting index of the string to match, in bytes
2757 * @replacement: text to replace each match with
2758 * @match_options: options for the match
2759 * @error: location to store the error occurring, or %NULL to ignore errors
2761 * Replaces all occurrences of the pattern in @regex with the
2762 * replacement text. Backreferences of the form '\number' or
2763 * '\g<number>' in the replacement text are interpolated by the
2764 * number-th captured subexpression of the match, '\g<name>' refers
2765 * to the captured subexpression with the given name. '\0' refers
2766 * to the complete match, but '\0' followed by a number is the octal
2767 * representation of a character. To include a literal '\' in the
2768 * replacement, write '\\'.
2770 * There are also escapes that changes the case of the following text:
2772 * - \l: Convert to lower case the next character
2773 * - \u: Convert to upper case the next character
2774 * - \L: Convert to lower case till \E
2775 * - \U: Convert to upper case till \E
2776 * - \E: End case modification
2778 * If you do not need to use backreferences use g_regex_replace_literal().
2780 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was
2781 * passed to g_regex_new(). If you want to use not UTF-8 encoded stings
2782 * you can use g_regex_replace_literal().
2784 * Setting @start_position differs from just passing over a shortened
2785 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that
2786 * begins with any kind of lookbehind assertion, such as "\b".
2788 * Returns: a newly allocated string containing the replacements
2790 * Since: 2.14
2792 gchar *
2793 g_regex_replace (const GRegex *regex,
2794 const gchar *string,
2795 gssize string_len,
2796 gint start_position,
2797 const gchar *replacement,
2798 GRegexMatchFlags match_options,
2799 GError **error)
2801 gchar *result;
2802 GList *list;
2803 GError *tmp_error = NULL;
2805 g_return_val_if_fail (regex != NULL, NULL);
2806 g_return_val_if_fail (string != NULL, NULL);
2807 g_return_val_if_fail (start_position >= 0, NULL);
2808 g_return_val_if_fail (replacement != NULL, NULL);
2809 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2810 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2812 list = split_replacement (replacement, &tmp_error);
2813 if (tmp_error != NULL)
2815 g_propagate_error (error, tmp_error);
2816 return NULL;
2819 result = g_regex_replace_eval (regex,
2820 string, string_len, start_position,
2821 match_options,
2822 interpolate_replacement,
2823 (gpointer)list,
2824 &tmp_error);
2825 if (tmp_error != NULL)
2826 g_propagate_error (error, tmp_error);
2828 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
2830 return result;
2833 static gboolean
2834 literal_replacement (const GMatchInfo *match_info,
2835 GString *result,
2836 gpointer data)
2838 g_string_append (result, data);
2839 return FALSE;
2843 * g_regex_replace_literal:
2844 * @regex: a #GRegex structure
2845 * @string: (array length=string_len): the string to perform matches against
2846 * @string_len: the length of @string, or -1 if @string is nul-terminated
2847 * @start_position: starting index of the string to match, in bytes
2848 * @replacement: text to replace each match with
2849 * @match_options: options for the match
2850 * @error: location to store the error occurring, or %NULL to ignore errors
2852 * Replaces all occurrences of the pattern in @regex with the
2853 * replacement text. @replacement is replaced literally, to
2854 * include backreferences use g_regex_replace().
2856 * Setting @start_position differs from just passing over a
2857 * shortened string and setting #G_REGEX_MATCH_NOTBOL in the
2858 * case of a pattern that begins with any kind of lookbehind
2859 * assertion, such as "\b".
2861 * Returns: a newly allocated string containing the replacements
2863 * Since: 2.14
2865 gchar *
2866 g_regex_replace_literal (const GRegex *regex,
2867 const gchar *string,
2868 gssize string_len,
2869 gint start_position,
2870 const gchar *replacement,
2871 GRegexMatchFlags match_options,
2872 GError **error)
2874 g_return_val_if_fail (replacement != NULL, NULL);
2875 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2877 return g_regex_replace_eval (regex,
2878 string, string_len, start_position,
2879 match_options,
2880 literal_replacement,
2881 (gpointer)replacement,
2882 error);
2886 * g_regex_replace_eval:
2887 * @regex: a #GRegex structure from g_regex_new()
2888 * @string: (array length=string_len): string to perform matches against
2889 * @string_len: the length of @string, or -1 if @string is nul-terminated
2890 * @start_position: starting index of the string to match, in bytes
2891 * @match_options: options for the match
2892 * @eval: a function to call for each match
2893 * @user_data: user data to pass to the function
2894 * @error: location to store the error occurring, or %NULL to ignore errors
2896 * Replaces occurrences of the pattern in regex with the output of
2897 * @eval for that occurrence.
2899 * Setting @start_position differs from just passing over a shortened
2900 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
2901 * that begins with any kind of lookbehind assertion, such as "\b".
2903 * The following example uses g_regex_replace_eval() to replace multiple
2904 * strings at once:
2905 * |[<!-- language="C" -->
2906 * static gboolean
2907 * eval_cb (const GMatchInfo *info,
2908 * GString *res,
2909 * gpointer data)
2911 * gchar *match;
2912 * gchar *r;
2914 * match = g_match_info_fetch (info, 0);
2915 * r = g_hash_table_lookup ((GHashTable *)data, match);
2916 * g_string_append (res, r);
2917 * g_free (match);
2919 * return FALSE;
2922 * ...
2924 * GRegex *reg;
2925 * GHashTable *h;
2926 * gchar *res;
2928 * h = g_hash_table_new (g_str_hash, g_str_equal);
2930 * g_hash_table_insert (h, "1", "ONE");
2931 * g_hash_table_insert (h, "2", "TWO");
2932 * g_hash_table_insert (h, "3", "THREE");
2933 * g_hash_table_insert (h, "4", "FOUR");
2935 * reg = g_regex_new ("1|2|3|4", 0, 0, NULL);
2936 * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL);
2937 * g_hash_table_destroy (h);
2939 * ...
2940 * ]|
2942 * Returns: a newly allocated string containing the replacements
2944 * Since: 2.14
2946 gchar *
2947 g_regex_replace_eval (const GRegex *regex,
2948 const gchar *string,
2949 gssize string_len,
2950 gint start_position,
2951 GRegexMatchFlags match_options,
2952 GRegexEvalCallback eval,
2953 gpointer user_data,
2954 GError **error)
2956 GMatchInfo *match_info;
2957 GString *result;
2958 gint str_pos = 0;
2959 gboolean done = FALSE;
2960 GError *tmp_error = NULL;
2962 g_return_val_if_fail (regex != NULL, NULL);
2963 g_return_val_if_fail (string != NULL, NULL);
2964 g_return_val_if_fail (start_position >= 0, NULL);
2965 g_return_val_if_fail (eval != NULL, NULL);
2966 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2968 if (string_len < 0)
2969 string_len = strlen (string);
2971 result = g_string_sized_new (string_len);
2973 /* run down the string making matches. */
2974 g_regex_match_full (regex, string, string_len, start_position,
2975 match_options, &match_info, &tmp_error);
2976 while (!done && g_match_info_matches (match_info))
2978 g_string_append_len (result,
2979 string + str_pos,
2980 match_info->offsets[0] - str_pos);
2981 done = (*eval) (match_info, result, user_data);
2982 str_pos = match_info->offsets[1];
2983 g_match_info_next (match_info, &tmp_error);
2985 g_match_info_free (match_info);
2986 if (tmp_error != NULL)
2988 g_propagate_error (error, tmp_error);
2989 g_string_free (result, TRUE);
2990 return NULL;
2993 g_string_append_len (result, string + str_pos, string_len - str_pos);
2994 return g_string_free (result, FALSE);
2998 * g_regex_check_replacement:
2999 * @replacement: the replacement string
3000 * @has_references: (out) (optional): location to store information about
3001 * references in @replacement or %NULL
3002 * @error: location to store error
3004 * Checks whether @replacement is a valid replacement string
3005 * (see g_regex_replace()), i.e. that all escape sequences in
3006 * it are valid.
3008 * If @has_references is not %NULL then @replacement is checked
3009 * for pattern references. For instance, replacement text 'foo\n'
3010 * does not contain references and may be evaluated without information
3011 * about actual match, but '\0\1' (whole match followed by first
3012 * subpattern) requires valid #GMatchInfo object.
3014 * Returns: whether @replacement is a valid replacement string
3016 * Since: 2.14
3018 gboolean
3019 g_regex_check_replacement (const gchar *replacement,
3020 gboolean *has_references,
3021 GError **error)
3023 GList *list;
3024 GError *tmp = NULL;
3026 list = split_replacement (replacement, &tmp);
3028 if (tmp)
3030 g_propagate_error (error, tmp);
3031 return FALSE;
3034 if (has_references)
3035 *has_references = interpolation_list_needs_match (list);
3037 g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
3039 return TRUE;
3043 * g_regex_escape_nul:
3044 * @string: the string to escape
3045 * @length: the length of @string
3047 * Escapes the nul characters in @string to "\x00". It can be used
3048 * to compile a regex with embedded nul characters.
3050 * For completeness, @length can be -1 for a nul-terminated string.
3051 * In this case the output string will be of course equal to @string.
3053 * Returns: a newly-allocated escaped string
3055 * Since: 2.30
3057 gchar *
3058 g_regex_escape_nul (const gchar *string,
3059 gint length)
3061 GString *escaped;
3062 const gchar *p, *piece_start, *end;
3063 gint backslashes;
3065 g_return_val_if_fail (string != NULL, NULL);
3067 if (length < 0)
3068 return g_strdup (string);
3070 end = string + length;
3071 p = piece_start = string;
3072 escaped = g_string_sized_new (length + 1);
3074 backslashes = 0;
3075 while (p < end)
3077 switch (*p)
3079 case '\0':
3080 if (p != piece_start)
3082 /* copy the previous piece. */
3083 g_string_append_len (escaped, piece_start, p - piece_start);
3085 if ((backslashes & 1) == 0)
3086 g_string_append_c (escaped, '\\');
3087 g_string_append_c (escaped, 'x');
3088 g_string_append_c (escaped, '0');
3089 g_string_append_c (escaped, '0');
3090 piece_start = ++p;
3091 backslashes = 0;
3092 break;
3093 case '\\':
3094 backslashes++;
3095 ++p;
3096 break;
3097 default:
3098 backslashes = 0;
3099 p = g_utf8_next_char (p);
3100 break;
3104 if (piece_start < end)
3105 g_string_append_len (escaped, piece_start, end - piece_start);
3107 return g_string_free (escaped, FALSE);
3111 * g_regex_escape_string:
3112 * @string: (array length=length): the string to escape
3113 * @length: the length of @string, or -1 if @string is nul-terminated
3115 * Escapes the special characters used for regular expressions
3116 * in @string, for instance "a.b*c" becomes "a\.b\*c". This
3117 * function is useful to dynamically generate regular expressions.
3119 * @string can contain nul characters that are replaced with "\0",
3120 * in this case remember to specify the correct length of @string
3121 * in @length.
3123 * Returns: a newly-allocated escaped string
3125 * Since: 2.14
3127 gchar *
3128 g_regex_escape_string (const gchar *string,
3129 gint length)
3131 GString *escaped;
3132 const char *p, *piece_start, *end;
3134 g_return_val_if_fail (string != NULL, NULL);
3136 if (length < 0)
3137 length = strlen (string);
3139 end = string + length;
3140 p = piece_start = string;
3141 escaped = g_string_sized_new (length + 1);
3143 while (p < end)
3145 switch (*p)
3147 case '\0':
3148 case '\\':
3149 case '|':
3150 case '(':
3151 case ')':
3152 case '[':
3153 case ']':
3154 case '{':
3155 case '}':
3156 case '^':
3157 case '$':
3158 case '*':
3159 case '+':
3160 case '?':
3161 case '.':
3162 if (p != piece_start)
3163 /* copy the previous piece. */
3164 g_string_append_len (escaped, piece_start, p - piece_start);
3165 g_string_append_c (escaped, '\\');
3166 if (*p == '\0')
3167 g_string_append_c (escaped, '0');
3168 else
3169 g_string_append_c (escaped, *p);
3170 piece_start = ++p;
3171 break;
3172 default:
3173 p = g_utf8_next_char (p);
3174 break;
3178 if (piece_start < end)
3179 g_string_append_len (escaped, piece_start, end - piece_start);
3181 return g_string_free (escaped, FALSE);