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
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #ifdef USE_SYSTEM_PCRE
33 #include "pcre/pcre.h"
36 /* PCRE 7.3 does not contain the definition of PCRE_ERROR_NULLWSLIMIT */
37 #ifndef PCRE_ERROR_NULLWSLIMIT
38 #define PCRE_ERROR_NULLWSLIMIT (-22)
43 /* Mask of all the possible values for GRegexCompileFlags. */
44 #define G_REGEX_COMPILE_MASK (G_REGEX_CASELESS | \
49 G_REGEX_DOLLAR_ENDONLY | \
52 G_REGEX_NO_AUTO_CAPTURE | \
55 G_REGEX_NEWLINE_CR | \
56 G_REGEX_NEWLINE_LF | \
59 /* Mask of all the possible values for GRegexMatchFlags. */
60 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_ANCHORED | \
61 G_REGEX_MATCH_NOTBOL | \
62 G_REGEX_MATCH_NOTEOL | \
63 G_REGEX_MATCH_NOTEMPTY | \
64 G_REGEX_MATCH_PARTIAL | \
65 G_REGEX_MATCH_NEWLINE_CR | \
66 G_REGEX_MATCH_NEWLINE_LF | \
67 G_REGEX_MATCH_NEWLINE_CRLF | \
68 G_REGEX_MATCH_NEWLINE_ANY)
70 /* if the string is in UTF-8 use g_utf8_ functions, else use
72 #define NEXT_CHAR(re, s) (((re)->compile_opts & PCRE_UTF8) ? \
73 g_utf8_next_char (s) : \
75 #define PREV_CHAR(re, s) (((re)->compile_opts & PCRE_UTF8) ? \
76 g_utf8_prev_char (s) : \
81 GRegex *regex; /* the regex */
82 GRegexMatchFlags match_opts; /* options used at match time on the regex */
83 gint matches; /* number of matching sub patterns */
84 gint pos; /* position in the string where last match left off */
85 gint *offsets; /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
86 gint n_offsets; /* number of offsets */
87 gint *workspace; /* workspace for pcre_dfa_exec() */
88 gint n_workspace; /* number of workspace elements */
89 const gchar *string; /* string passed to the match function */
90 gssize string_len; /* length of string */
95 volatile gint ref_count; /* the ref count for the immutable part */
96 gchar *pattern; /* the pattern */
97 pcre *pcre_re; /* compiled form of the pattern */
98 GRegexCompileFlags compile_opts; /* options used at compile time on the pattern */
99 GRegexMatchFlags match_opts; /* options used at match time on the regex */
100 pcre_extra *extra; /* data stored when G_REGEX_OPTIMIZE is used */
103 /* TRUE if ret is an error code, FALSE otherwise. */
104 #define IS_PCRE_ERROR(ret) ((ret) < PCRE_ERROR_NOMATCH && (ret) != PCRE_ERROR_PARTIAL)
106 typedef struct _InterpolationData InterpolationData;
107 static gboolean interpolation_list_needs_match (GList *list);
108 static gboolean interpolate_replacement (const GMatchInfo *match_info,
111 static GList *split_replacement (const gchar *replacement,
113 static void free_interpolation_data (InterpolationData *data);
117 match_error (gint errcode)
121 case PCRE_ERROR_NOMATCH:
124 case PCRE_ERROR_NULL:
125 /* NULL argument, this should not happen in GRegex */
126 g_warning ("A NULL argument was passed to PCRE");
128 case PCRE_ERROR_BADOPTION:
129 return "bad options";
130 case PCRE_ERROR_BADMAGIC:
131 return _("corrupted object");
132 case PCRE_ERROR_UNKNOWN_OPCODE:
133 return N_("internal error or corrupted object");
134 case PCRE_ERROR_NOMEMORY:
135 return _("out of memory");
136 case PCRE_ERROR_NOSUBSTRING:
137 /* not used by pcre_exec() */
139 case PCRE_ERROR_MATCHLIMIT:
140 return _("backtracking limit reached");
141 case PCRE_ERROR_CALLOUT:
142 /* callouts are not implemented */
144 case PCRE_ERROR_BADUTF8:
145 case PCRE_ERROR_BADUTF8_OFFSET:
146 /* we do not check if strings are valid */
148 case PCRE_ERROR_PARTIAL:
151 case PCRE_ERROR_BADPARTIAL:
152 return _("the pattern contains items not supported for partial matching");
153 case PCRE_ERROR_INTERNAL:
154 return _("internal error");
155 case PCRE_ERROR_BADCOUNT:
156 /* negative ovecsize, this should not happen in GRegex */
157 g_warning ("A negative ovecsize was passed to PCRE");
159 case PCRE_ERROR_DFA_UITEM:
160 return _("the pattern contains items not supported for partial matching");
161 case PCRE_ERROR_DFA_UCOND:
162 return _("back references as conditions are not supported for partial matching");
163 case PCRE_ERROR_DFA_UMLIMIT:
164 /* the match_field field is not used in GRegex */
166 case PCRE_ERROR_DFA_WSSIZE:
167 /* handled expanding the workspace */
169 case PCRE_ERROR_DFA_RECURSE:
170 case PCRE_ERROR_RECURSIONLIMIT:
171 return _("recursion limit reached");
172 case PCRE_ERROR_NULLWSLIMIT:
173 return _("workspace limit for empty substrings reached");
174 case PCRE_ERROR_BADNEWLINE:
175 return _("invalid combination of newline flags");
179 return _("unknown error");
183 translate_compile_error (gint *errcode, const gchar **errmsg)
185 /* Compile errors are created adding 100 to the error code returned
187 * If errcode is known we put the translatable error message in
188 * erromsg. If errcode is unknown we put the generic
189 * G_REGEX_ERROR_COMPILE error code in errcode and keep the
190 * untranslated error message returned by PCRE.
191 * Note that there can be more PCRE errors with the same GRegexError
192 * and that some PCRE errors are useless for us.
198 case G_REGEX_ERROR_STRAY_BACKSLASH:
199 *errmsg = _("\\ at end of pattern");
201 case G_REGEX_ERROR_MISSING_CONTROL_CHAR:
202 *errmsg = _("\\c at end of pattern");
204 case G_REGEX_ERROR_UNRECOGNIZED_ESCAPE:
205 *errmsg = _("unrecognized character follows \\");
208 /* A number of Perl escapes are not handled by PCRE.
209 * Therefore it explicitly raises ERR37.
211 *errcode = G_REGEX_ERROR_UNRECOGNIZED_ESCAPE;
212 *errmsg = _("case-changing escapes (\\l, \\L, \\u, \\U) are not allowed here");
214 case G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER:
215 *errmsg = _("numbers out of order in {} quantifier");
217 case G_REGEX_ERROR_QUANTIFIER_TOO_BIG:
218 *errmsg = _("number too big in {} quantifier");
220 case G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS:
221 *errmsg = _("missing terminating ] for character class");
223 case G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS:
224 *errmsg = _("invalid escape sequence in character class");
226 case G_REGEX_ERROR_RANGE_OUT_OF_ORDER:
227 *errmsg = _("range out of order in character class");
229 case G_REGEX_ERROR_NOTHING_TO_REPEAT:
230 *errmsg = _("nothing to repeat");
232 case G_REGEX_ERROR_UNRECOGNIZED_CHARACTER:
233 *errmsg = _("unrecognized character after (?");
236 *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
237 *errmsg = _("unrecognized character after (?<");
240 *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
241 *errmsg = _("unrecognized character after (?P");
243 case G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS:
244 *errmsg = _("POSIX named classes are supported only within a class");
246 case G_REGEX_ERROR_UNMATCHED_PARENTHESIS:
247 *errmsg = _("missing terminating )");
250 *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
251 *errmsg = _(") without opening (");
254 *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
255 /* translators: '(?R' and '(?[+-]digits' are both meant as (groups of)
256 * sequences here, '(?-54' would be an example for the second group.
258 *errmsg = _("(?R or (?[+-]digits must be followed by )");
260 case G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE:
261 *errmsg = _("reference to non-existent subpattern");
263 case G_REGEX_ERROR_UNTERMINATED_COMMENT:
264 *errmsg = _("missing ) after comment");
266 case G_REGEX_ERROR_EXPRESSION_TOO_LARGE:
267 *errmsg = _("regular expression too large");
269 case G_REGEX_ERROR_MEMORY_ERROR:
270 *errmsg = _("failed to get memory");
272 case G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND:
273 *errmsg = _("lookbehind assertion is not fixed length");
275 case G_REGEX_ERROR_MALFORMED_CONDITION:
276 *errmsg = _("malformed number or name after (?(");
278 case G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES:
279 *errmsg = _("conditional group contains more than two branches");
281 case G_REGEX_ERROR_ASSERTION_EXPECTED:
282 *errmsg = _("assertion expected after (?(");
284 case G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME:
285 *errmsg = _("unknown POSIX class name");
287 case G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED:
288 *errmsg = _("POSIX collating elements are not supported");
290 case G_REGEX_ERROR_HEX_CODE_TOO_LARGE:
291 *errmsg = _("character value in \\x{...} sequence is too large");
293 case G_REGEX_ERROR_INVALID_CONDITION:
294 *errmsg = _("invalid condition (?(0)");
296 case G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND:
297 *errmsg = _("\\C not allowed in lookbehind assertion");
299 case G_REGEX_ERROR_INFINITE_LOOP:
300 *errmsg = _("recursive call could loop indefinitely");
302 case G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR:
303 *errmsg = _("missing terminator in subpattern name");
305 case G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME:
306 *errmsg = _("two named subpatterns have the same name");
308 case G_REGEX_ERROR_MALFORMED_PROPERTY:
309 *errmsg = _("malformed \\P or \\p sequence");
311 case G_REGEX_ERROR_UNKNOWN_PROPERTY:
312 *errmsg = _("unknown property name after \\P or \\p");
314 case G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG:
315 *errmsg = _("subpattern name is too long (maximum 32 characters)");
317 case G_REGEX_ERROR_TOO_MANY_SUBPATTERNS:
318 *errmsg = _("too many named subpatterns (maximum 10,000)");
320 case G_REGEX_ERROR_INVALID_OCTAL_VALUE:
321 *errmsg = _("octal value is greater than \\377");
323 case G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE:
324 *errmsg = _("DEFINE group contains more than one branch");
326 case G_REGEX_ERROR_DEFINE_REPETION:
327 *errmsg = _("repeating a DEFINE group is not allowed");
329 case G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS:
330 *errmsg = _("inconsistent NEWLINE options");
332 case G_REGEX_ERROR_MISSING_BACK_REFERENCE:
333 *errmsg = _("\\g is not followed by a braced name or an optionally "
334 "braced non-zero number");
337 *errcode = G_REGEX_ERROR_INTERNAL;
338 *errmsg = _("unexpected repeat");
341 *errcode = G_REGEX_ERROR_INTERNAL;
342 *errmsg = _("code overflow");
345 *errcode = G_REGEX_ERROR_INTERNAL;
346 *errmsg = _("overran compiling workspace");
349 *errcode = G_REGEX_ERROR_INTERNAL;
350 *errmsg = _("previously-checked referenced subpattern not found");
353 /* This should not happen as we never pass a NULL erroffset */
354 g_warning ("erroffset passed as NULL");
355 *errcode = G_REGEX_ERROR_COMPILE;
358 /* This should not happen as we check options before passing them
359 * to pcre_compile2() */
360 g_warning ("unknown option bit(s) set");
361 *errcode = G_REGEX_ERROR_COMPILE;
366 /* These errors should not happen as we are using an UTF8-enabled PCRE
367 * and we do not check if strings are valid */
368 g_warning ("%s", *errmsg);
369 *errcode = G_REGEX_ERROR_COMPILE;
372 *errcode = G_REGEX_ERROR_COMPILE;
379 match_info_new (const GRegex *regex,
386 GMatchInfo *match_info;
389 string_len = strlen (string);
391 match_info = g_new0 (GMatchInfo, 1);
392 match_info->regex = g_regex_ref ((GRegex *)regex);
393 match_info->string = string;
394 match_info->string_len = string_len;
395 match_info->matches = PCRE_ERROR_NOMATCH;
396 match_info->pos = start_position;
397 match_info->match_opts = match_options;
401 /* These values should be enough for most cases, if they are not
402 * enough g_regex_match_all_full() will expand them. */
403 match_info->n_offsets = 24;
404 match_info->n_workspace = 100;
405 match_info->workspace = g_new (gint, match_info->n_workspace);
410 pcre_fullinfo (regex->pcre_re, regex->extra,
411 PCRE_INFO_CAPTURECOUNT, &capture_count);
412 match_info->n_offsets = (capture_count + 1) * 3;
415 match_info->offsets = g_new0 (gint, match_info->n_offsets);
416 /* Set an invalid position for the previous match. */
417 match_info->offsets[0] = -1;
418 match_info->offsets[1] = -1;
424 * g_match_info_get_regex:
425 * @match_info: a #GMatchInfo
427 * Returns #GRegex object used in @match_info. It belongs to Glib
428 * and must not be freed. Use g_regex_ref() if you need to keep it
429 * after you free @match_info object.
431 * Returns: #GRegex object used in @match_info
436 g_match_info_get_regex (const GMatchInfo *match_info)
438 g_return_val_if_fail (match_info != NULL, NULL);
439 return match_info->regex;
443 * g_match_info_get_string:
444 * @match_info: a #GMatchInfo
446 * Returns the string searched with @match_info. This is the
447 * string passed to g_regex_match() or g_regex_replace() so
448 * you may not free it before calling this function.
450 * Returns: the string searched with @match_info
455 g_match_info_get_string (const GMatchInfo *match_info)
457 g_return_val_if_fail (match_info != NULL, NULL);
458 return match_info->string;
463 * @match_info: a #GMatchInfo
465 * Frees all the memory associated with the #GMatchInfo structure.
470 g_match_info_free (GMatchInfo *match_info)
474 g_regex_unref (match_info->regex);
475 g_free (match_info->offsets);
476 g_free (match_info->workspace);
483 * @match_info: a #GMatchInfo structure
484 * @error: location to store the error occuring, or %NULL to ignore errors
486 * Scans for the next match using the same parameters of the previous
487 * call to g_regex_match_full() or g_regex_match() that returned
490 * The match is done on the string passed to the match function, so you
491 * cannot free it before calling this function.
493 * Returns: %TRUE is the string matched, %FALSE otherwise
498 g_match_info_next (GMatchInfo *match_info,
502 gint prev_match_start;
505 g_return_val_if_fail (match_info != NULL, FALSE);
506 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
507 g_return_val_if_fail (match_info->pos >= 0, FALSE);
509 opts = match_info->regex->match_opts | match_info->match_opts;
511 prev_match_start = match_info->offsets[0];
512 prev_match_end = match_info->offsets[1];
514 match_info->matches = pcre_exec (match_info->regex->pcre_re,
515 match_info->regex->extra,
517 match_info->string_len,
519 match_info->regex->match_opts |
520 match_info->match_opts,
522 match_info->n_offsets);
523 if (IS_PCRE_ERROR (match_info->matches))
525 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
526 _("Error while matching regular expression %s: %s"),
527 match_info->regex->pattern, match_error (match_info->matches));
531 /* avoid infinite loops if the pattern is an empty string or something
533 if (match_info->pos == match_info->offsets[1])
535 if (match_info->pos > match_info->string_len)
537 /* we have reached the end of the string */
538 match_info->pos = -1;
539 match_info->matches = PCRE_ERROR_NOMATCH;
543 match_info->pos = NEXT_CHAR (match_info->regex,
544 &match_info->string[match_info->pos]) -
549 match_info->pos = match_info->offsets[1];
552 /* it's possibile to get two identical matches when we are matching
553 * empty strings, for instance if the pattern is "(?=[A-Z0-9])" and
554 * the string is "RegExTest" we have:
555 * - search at position 0: match from 0 to 0
556 * - search at position 1: match from 3 to 3
557 * - search at position 3: match from 3 to 3 (duplicate)
558 * - search at position 4: match from 5 to 5
559 * - search at position 5: match from 5 to 5 (duplicate)
560 * - search at position 6: no match -> stop
561 * so we have to ignore the duplicates.
562 * see bug #515944: http://bugzilla.gnome.org/show_bug.cgi?id=515944 */
563 if (match_info->matches >= 0 &&
564 prev_match_start == match_info->offsets[0] &&
565 prev_match_end == match_info->offsets[1])
567 /* ignore this match and search the next one */
568 return g_match_info_next (match_info, error);
571 return match_info->matches >= 0;
575 * g_match_info_matches:
576 * @match_info: a #GMatchInfo structure
578 * Returns whether the previous match operation succeeded.
580 * Returns: %TRUE if the previous match operation succeeded,
586 g_match_info_matches (const GMatchInfo *match_info)
588 g_return_val_if_fail (match_info != NULL, FALSE);
590 return match_info->matches >= 0;
594 * g_match_info_get_match_count:
595 * @match_info: a #GMatchInfo structure
597 * Retrieves the number of matched substrings (including substring 0,
598 * that is the whole matched text), so 1 is returned if the pattern
599 * has no substrings in it and 0 is returned if the match failed.
601 * If the last match was obtained using the DFA algorithm, that is
602 * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
603 * count is not that of the number of capturing parentheses but that of
604 * the number of matched substrings.
606 * Returns: Number of matched substrings, or -1 if an error occurred
611 g_match_info_get_match_count (const GMatchInfo *match_info)
613 g_return_val_if_fail (match_info, -1);
615 if (match_info->matches == PCRE_ERROR_NOMATCH)
618 else if (match_info->matches < PCRE_ERROR_NOMATCH)
623 return match_info->matches;
627 * g_match_info_is_partial_match:
628 * @match_info: a #GMatchInfo structure
630 * Usually if the string passed to g_regex_match*() matches as far as
631 * it goes, but is too short to match the entire pattern, %FALSE is
632 * returned. There are circumstances where it might be helpful to
633 * distinguish this case from other cases in which there is no match.
635 * Consider, for example, an application where a human is required to
636 * type in data for a field with specific formatting requirements. An
637 * example might be a date in the form ddmmmyy, defined by the pattern
638 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
639 * If the application sees the user’s keystrokes one by one, and can
640 * check that what has been typed so far is potentially valid, it is
641 * able to raise an error as soon as a mistake is made.
643 * GRegex supports the concept of partial matching by means of the
644 * #G_REGEX_MATCH_PARTIAL flag. When this is set the return code for
645 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
646 * for a complete match, %FALSE otherwise. But, when these functions
647 * return %FALSE, you can check if the match was partial calling
648 * g_match_info_is_partial_match().
650 * When using partial matching you cannot use g_match_info_fetch*().
652 * Because of the way certain internal optimizations are implemented
653 * the partial matching algorithm cannot be used with all patterns.
654 * So repeated single characters such as "a{2,4}" and repeated single
655 * meta-sequences such as "\d+" are not permitted if the maximum number
656 * of occurrences is greater than one. Optional items such as "\d?"
657 * (where the maximum is one) are permitted. Quantifiers with any values
658 * are permitted after parentheses, so the invalid examples above can be
659 * coded thus "(a){2,4}" and "(\d)+". If #G_REGEX_MATCH_PARTIAL is set
660 * for a pattern that does not conform to the restrictions, matching
661 * functions return an error.
663 * Returns: %TRUE if the match was partial, %FALSE otherwise
668 g_match_info_is_partial_match (const GMatchInfo *match_info)
670 g_return_val_if_fail (match_info != NULL, FALSE);
672 return match_info->matches == PCRE_ERROR_PARTIAL;
676 * g_match_info_expand_references:
677 * @match_info: a #GMatchInfo or %NULL
678 * @string_to_expand: the string to expand
679 * @error: location to store the error occuring, or %NULL to ignore errors
681 * Returns a new string containing the text in @string_to_expand with
682 * references and escape sequences expanded. References refer to the last
683 * match done with @string against @regex and have the same syntax used by
686 * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
687 * passed to g_regex_new().
689 * The backreferences are extracted from the string passed to the match
690 * function, so you cannot call this function after freeing the string.
692 * @match_info may be %NULL in which case @string_to_expand must not
693 * contain references. For instance "foo\n" does not refer to an actual
694 * pattern and '\n' merely will be replaced with \n character,
695 * while to expand "\0" (whole match) one needs the result of a match.
696 * Use g_regex_check_replacement() to find out whether @string_to_expand
697 * contains references.
699 * Returns: the expanded string, or %NULL if an error occurred
704 g_match_info_expand_references (const GMatchInfo *match_info,
705 const gchar *string_to_expand,
710 GError *tmp_error = NULL;
712 g_return_val_if_fail (string_to_expand != NULL, NULL);
713 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
715 list = split_replacement (string_to_expand, &tmp_error);
716 if (tmp_error != NULL)
718 g_propagate_error (error, tmp_error);
722 if (!match_info && interpolation_list_needs_match (list))
724 g_critical ("String '%s' contains references to the match, can't "
725 "expand references without GMatchInfo object",
730 result = g_string_sized_new (strlen (string_to_expand));
731 interpolate_replacement (match_info, result, list);
733 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
736 return g_string_free (result, FALSE);
740 * g_match_info_fetch:
741 * @match_info: #GMatchInfo structure
742 * @match_num: number of the sub expression
744 * Retrieves the text matching the @match_num<!-- -->'th capturing
745 * parentheses. 0 is the full text of the match, 1 is the first paren
746 * set, 2 the second, and so on.
748 * If @match_num is a valid sub pattern but it didn't match anything
749 * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
750 * string is returned.
752 * If the match was obtained using the DFA algorithm, that is using
753 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
754 * string is not that of a set of parentheses but that of a matched
755 * substring. Substrings are matched in reverse order of length, so
756 * 0 is the longest match.
758 * The string is fetched from the string passed to the match function,
759 * so you cannot call this function after freeing the string.
761 * Returns: The matched substring, or %NULL if an error occurred.
762 * You have to free the string yourself
767 g_match_info_fetch (const GMatchInfo *match_info,
770 /* we cannot use pcre_get_substring() because it allocates the
771 * string using pcre_malloc(). */
775 g_return_val_if_fail (match_info != NULL, NULL);
776 g_return_val_if_fail (match_num >= 0, NULL);
778 /* match_num does not exist or it didn't matched, i.e. matching "b"
779 * against "(a)?b" then group 0 is empty. */
780 if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
782 else if (start == -1)
783 match = g_strdup ("");
785 match = g_strndup (&match_info->string[start], end - start);
791 * g_match_info_fetch_pos:
792 * @match_info: #GMatchInfo structure
793 * @match_num: number of the sub expression
794 * @start_pos: pointer to location where to store the start position
795 * @end_pos: pointer to location where to store the end position
797 * Retrieves the position in bytes of the @match_num<!-- -->'th capturing
798 * parentheses. 0 is the full text of the match, 1 is the first
799 * paren set, 2 the second, and so on.
801 * If @match_num is a valid sub pattern but it didn't match anything
802 * (e.g. sub pattern 1, matching "b" against "(a)?b") then @start_pos
803 * and @end_pos are set to -1 and %TRUE is returned.
805 * If the match was obtained using the DFA algorithm, that is using
806 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
807 * position is not that of a set of parentheses but that of a matched
808 * substring. Substrings are matched in reverse order of length, so
809 * 0 is the longest match.
811 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
812 * the position cannot be fetched, @start_pos and @end_pos are left
818 g_match_info_fetch_pos (const GMatchInfo *match_info,
823 g_return_val_if_fail (match_info != NULL, FALSE);
824 g_return_val_if_fail (match_num >= 0, FALSE);
826 /* make sure the sub expression number they're requesting is less than
827 * the total number of sub expressions that were matched. */
828 if (match_num >= match_info->matches)
831 if (start_pos != NULL)
832 *start_pos = match_info->offsets[2 * match_num];
835 *end_pos = match_info->offsets[2 * match_num + 1];
841 * Returns number of first matched subpattern with name @name.
842 * There may be more than one in case when DUPNAMES is used,
843 * and not all subpatterns with that name match;
844 * pcre_get_stringnumber() does not work in that case.
847 get_matched_substring_number (const GMatchInfo *match_info,
854 if (!(match_info->regex->compile_opts & G_REGEX_DUPNAMES))
855 return pcre_get_stringnumber (match_info->regex->pcre_re, name);
857 /* This code is copied from pcre_get.c: get_first_set() */
858 entrysize = pcre_get_stringtable_entries (match_info->regex->pcre_re,
866 for (entry = (guchar*) first; entry <= (guchar*) last; entry += entrysize)
868 gint n = (entry[0] << 8) + entry[1];
869 if (match_info->offsets[n*2] >= 0)
873 return (first[0] << 8) + first[1];
877 * g_match_info_fetch_named:
878 * @match_info: #GMatchInfo structure
879 * @name: name of the subexpression
881 * Retrieves the text matching the capturing parentheses named @name.
883 * If @name is a valid sub pattern name but it didn't match anything
884 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
885 * then an empty string is returned.
887 * The string is fetched from the string passed to the match function,
888 * so you cannot call this function after freeing the string.
890 * Returns: The matched substring, or %NULL if an error occurred.
891 * You have to free the string yourself
896 g_match_info_fetch_named (const GMatchInfo *match_info,
899 /* we cannot use pcre_get_named_substring() because it allocates the
900 * string using pcre_malloc(). */
903 g_return_val_if_fail (match_info != NULL, NULL);
904 g_return_val_if_fail (name != NULL, NULL);
906 num = get_matched_substring_number (match_info, name);
910 return g_match_info_fetch (match_info, num);
914 * g_match_info_fetch_named_pos:
915 * @match_info: #GMatchInfo structure
916 * @name: name of the subexpression
917 * @start_pos: pointer to location where to store the start position
918 * @end_pos: pointer to location where to store the end position
920 * Retrieves the position in bytes of the capturing parentheses named @name.
922 * If @name is a valid sub pattern name but it didn't match anything
923 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
924 * then @start_pos and @end_pos are set to -1 and %TRUE is returned.
926 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
927 * the position cannot be fetched, @start_pos and @end_pos are left
933 g_match_info_fetch_named_pos (const GMatchInfo *match_info,
940 g_return_val_if_fail (match_info != NULL, FALSE);
941 g_return_val_if_fail (name != NULL, FALSE);
943 num = get_matched_substring_number (match_info, name);
947 return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
951 * g_match_info_fetch_all:
952 * @match_info: a #GMatchInfo structure
954 * Bundles up pointers to each of the matching substrings from a match
955 * and stores them in an array of gchar pointers. The first element in
956 * the returned array is the match number 0, i.e. the entire matched
959 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
960 * "b" against "(a)?b") then an empty string is inserted.
962 * If the last match was obtained using the DFA algorithm, that is using
963 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
964 * strings are not that matched by sets of parentheses but that of the
965 * matched substring. Substrings are matched in reverse order of length,
966 * so the first one is the longest match.
968 * The strings are fetched from the string passed to the match function,
969 * so you cannot call this function after freeing the string.
971 * Returns: a %NULL-terminated array of gchar * pointers. It must be
972 * freed using g_strfreev(). If the previous match failed %NULL is
978 g_match_info_fetch_all (const GMatchInfo *match_info)
980 /* we cannot use pcre_get_substring_list() because the returned value
981 * isn't suitable for g_strfreev(). */
985 g_return_val_if_fail (match_info != NULL, NULL);
987 if (match_info->matches < 0)
990 result = g_new (gchar *, match_info->matches + 1);
991 for (i = 0; i < match_info->matches; i++)
992 result[i] = g_match_info_fetch (match_info, i);
1002 g_regex_error_quark (void)
1004 static GQuark error_quark = 0;
1006 if (error_quark == 0)
1007 error_quark = g_quark_from_static_string ("g-regex-error-quark");
1016 * Increases reference count of @regex by 1.
1023 g_regex_ref (GRegex *regex)
1025 g_return_val_if_fail (regex != NULL, NULL);
1026 g_atomic_int_inc (®ex->ref_count);
1034 * Decreases reference count of @regex by 1. When reference count drops
1035 * to zero, it frees all the memory associated with the regex structure.
1040 g_regex_unref (GRegex *regex)
1042 g_return_if_fail (regex != NULL);
1044 if (g_atomic_int_exchange_and_add (®ex->ref_count, -1) - 1 == 0)
1046 g_free (regex->pattern);
1047 if (regex->pcre_re != NULL)
1048 pcre_free (regex->pcre_re);
1049 if (regex->extra != NULL)
1050 pcre_free (regex->extra);
1057 * @pattern: the regular expression
1058 * @compile_options: compile options for the regular expression, or 0
1059 * @match_options: match options for the regular expression, or 0
1060 * @error: return location for a #GError
1062 * Compiles the regular expression to an internal form, and does
1063 * the initial setup of the #GRegex structure.
1065 * Returns: a #GRegex structure. Call g_regex_unref() when you
1071 g_regex_new (const gchar *pattern,
1072 GRegexCompileFlags compile_options,
1073 GRegexMatchFlags match_options,
1078 const gchar *errmsg;
1081 gboolean optimize = FALSE;
1082 static gboolean initialized = FALSE;
1083 unsigned long int pcre_compile_options;
1085 g_return_val_if_fail (pattern != NULL, NULL);
1086 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1087 g_return_val_if_fail ((compile_options & ~G_REGEX_COMPILE_MASK) == 0, NULL);
1088 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1095 pcre_config (PCRE_CONFIG_UTF8, &support);
1098 msg = N_("PCRE library is compiled without UTF8 support");
1099 g_critical ("%s", msg);
1100 g_set_error_literal (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
1104 pcre_config (PCRE_CONFIG_UNICODE_PROPERTIES, &support);
1107 msg = N_("PCRE library is compiled without UTF8 properties support");
1108 g_critical ("%s", msg);
1109 g_set_error_literal (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
1116 /* G_REGEX_OPTIMIZE has the same numeric value of PCRE_NO_UTF8_CHECK,
1117 * as we do not need to wrap PCRE_NO_UTF8_CHECK. */
1118 if (compile_options & G_REGEX_OPTIMIZE)
1121 /* In GRegex the string are, by default, UTF-8 encoded. PCRE
1122 * instead uses UTF-8 only if required with PCRE_UTF8. */
1123 if (compile_options & G_REGEX_RAW)
1126 compile_options &= ~G_REGEX_RAW;
1131 compile_options |= PCRE_UTF8 | PCRE_NO_UTF8_CHECK;
1132 match_options |= PCRE_NO_UTF8_CHECK;
1135 /* PCRE_NEWLINE_ANY is the default for the internal PCRE but
1136 * not for the system one. */
1137 if (!(compile_options & G_REGEX_NEWLINE_CR) &&
1138 !(compile_options & G_REGEX_NEWLINE_LF))
1140 compile_options |= PCRE_NEWLINE_ANY;
1143 /* compile the pattern */
1144 re = pcre_compile2 (pattern, compile_options, &errcode,
1145 &errmsg, &erroffset, NULL);
1147 /* if the compilation failed, set the error member and return
1153 /* Translate the PCRE error code to GRegexError and use a translated
1154 * error message if possible */
1155 translate_compile_error (&errcode, &errmsg);
1157 /* PCRE uses byte offsets but we want to show character offsets */
1158 erroffset = g_utf8_pointer_to_offset (pattern, &pattern[erroffset]);
1160 tmp_error = g_error_new (G_REGEX_ERROR, errcode,
1161 _("Error while compiling regular "
1162 "expression %s at char %d: %s"),
1163 pattern, erroffset, errmsg);
1164 g_propagate_error (error, tmp_error);
1169 /* For options set at the beginning of the pattern, pcre puts them into
1170 * compile options, e.g. "(?i)foo" will make the pcre structure store
1171 * PCRE_CASELESS even though it wasn't explicitly given for compilation. */
1172 pcre_fullinfo (re, NULL, PCRE_INFO_OPTIONS, &pcre_compile_options);
1173 compile_options = pcre_compile_options;
1175 if (!(compile_options & G_REGEX_DUPNAMES))
1177 gboolean jchanged = FALSE;
1178 pcre_fullinfo (re, NULL, PCRE_INFO_JCHANGED, &jchanged);
1180 compile_options |= G_REGEX_DUPNAMES;
1183 regex = g_new0 (GRegex, 1);
1184 regex->ref_count = 1;
1185 regex->pattern = g_strdup (pattern);
1186 regex->pcre_re = re;
1187 regex->compile_opts = compile_options;
1188 regex->match_opts = match_options;
1192 regex->extra = pcre_study (regex->pcre_re, 0, &errmsg);
1195 GError *tmp_error = g_error_new (G_REGEX_ERROR,
1196 G_REGEX_ERROR_OPTIMIZE,
1197 _("Error while optimizing "
1198 "regular expression %s: %s"),
1201 g_propagate_error (error, tmp_error);
1203 g_regex_unref (regex);
1212 * g_regex_get_pattern:
1213 * @regex: a #GRegex structure
1215 * Gets the pattern string associated with @regex, i.e. a copy of
1216 * the string passed to g_regex_new().
1218 * Returns: the pattern of @regex
1223 g_regex_get_pattern (const GRegex *regex)
1225 g_return_val_if_fail (regex != NULL, NULL);
1227 return regex->pattern;
1231 * g_regex_get_max_backref:
1234 * Returns the number of the highest back reference
1235 * in the pattern, or 0 if the pattern does not contain
1238 * Returns: the number of the highest back reference
1243 g_regex_get_max_backref (const GRegex *regex)
1247 pcre_fullinfo (regex->pcre_re, regex->extra,
1248 PCRE_INFO_BACKREFMAX, &value);
1254 * g_regex_get_capture_count:
1257 * Returns the number of capturing subpatterns in the pattern.
1259 * Returns: the number of capturing subpatterns
1264 g_regex_get_capture_count (const GRegex *regex)
1268 pcre_fullinfo (regex->pcre_re, regex->extra,
1269 PCRE_INFO_CAPTURECOUNT, &value);
1275 * g_regex_match_simple:
1276 * @pattern: the regular expression
1277 * @string: the string to scan for matches
1278 * @compile_options: compile options for the regular expression, or 0
1279 * @match_options: match options, or 0
1281 * Scans for a match in @string for @pattern.
1283 * This function is equivalent to g_regex_match() but it does not
1284 * require to compile the pattern with g_regex_new(), avoiding some
1285 * lines of code when you need just to do a match without extracting
1286 * substrings, capture counts, and so on.
1288 * If this function is to be called on the same @pattern more than
1289 * once, it's more efficient to compile the pattern once with
1290 * g_regex_new() and then use g_regex_match().
1292 * Returns: %TRUE if the string matched, %FALSE otherwise
1297 g_regex_match_simple (const gchar *pattern,
1298 const gchar *string,
1299 GRegexCompileFlags compile_options,
1300 GRegexMatchFlags match_options)
1305 regex = g_regex_new (pattern, compile_options, 0, NULL);
1308 result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
1309 g_regex_unref (regex);
1315 * @regex: a #GRegex structure from g_regex_new()
1316 * @string: the string to scan for matches
1317 * @match_options: match options
1318 * @match_info: pointer to location where to store the #GMatchInfo,
1319 * or %NULL if you do not need it
1321 * Scans for a match in string for the pattern in @regex.
1322 * The @match_options are combined with the match options specified
1323 * when the @regex structure was created, letting you have more
1324 * flexibility in reusing #GRegex structures.
1326 * A #GMatchInfo structure, used to get information on the match,
1327 * is stored in @match_info if not %NULL. Note that if @match_info
1328 * is not %NULL then it is created even if the function returns %FALSE,
1329 * i.e. you must free it regardless if regular expression actually matched.
1331 * To retrieve all the non-overlapping matches of the pattern in
1332 * string you can use g_match_info_next().
1336 * print_uppercase_words (const gchar *string)
1338 * /* Print all uppercase-only words. */
1340 * GMatchInfo *match_info;
1342 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1343 * g_regex_match (regex, string, 0, &match_info);
1344 * while (g_match_info_matches (match_info))
1346 * gchar *word = g_match_info_fetch (match_info, 0);
1347 * g_print ("Found: %s\n", word);
1349 * g_match_info_next (match_info, NULL);
1351 * g_match_info_free (match_info);
1352 * g_regex_unref (regex);
1356 * @string is not copied and is used in #GMatchInfo internally. If
1357 * you use any #GMatchInfo method (except g_match_info_free()) after
1358 * freeing or modifying @string then the behaviour is undefined.
1360 * Returns: %TRUE is the string matched, %FALSE otherwise
1365 g_regex_match (const GRegex *regex,
1366 const gchar *string,
1367 GRegexMatchFlags match_options,
1368 GMatchInfo **match_info)
1370 return g_regex_match_full (regex, string, -1, 0, match_options,
1375 * g_regex_match_full:
1376 * @regex: a #GRegex structure from g_regex_new()
1377 * @string: the string to scan for matches
1378 * @string_len: the length of @string, or -1 if @string is nul-terminated
1379 * @start_position: starting index of the string to match
1380 * @match_options: match options
1381 * @match_info: pointer to location where to store the #GMatchInfo,
1382 * or %NULL if you do not need it
1383 * @error: location to store the error occuring, or %NULL to ignore errors
1385 * Scans for a match in string for the pattern in @regex.
1386 * The @match_options are combined with the match options specified
1387 * when the @regex structure was created, letting you have more
1388 * flexibility in reusing #GRegex structures.
1390 * Setting @start_position differs from just passing over a shortened
1391 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
1392 * that begins with any kind of lookbehind assertion, such as "\b".
1394 * A #GMatchInfo structure, used to get information on the match, is
1395 * stored in @match_info if not %NULL. Note that if @match_info is
1396 * not %NULL then it is created even if the function returns %FALSE,
1397 * i.e. you must free it regardless if regular expression actually
1400 * @string is not copied and is used in #GMatchInfo internally. If
1401 * you use any #GMatchInfo method (except g_match_info_free()) after
1402 * freeing or modifying @string then the behaviour is undefined.
1404 * To retrieve all the non-overlapping matches of the pattern in
1405 * string you can use g_match_info_next().
1409 * print_uppercase_words (const gchar *string)
1411 * /* Print all uppercase-only words. */
1413 * GMatchInfo *match_info;
1414 * GError *error = NULL;
1416 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1417 * g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
1418 * while (g_match_info_matches (match_info))
1420 * gchar *word = g_match_info_fetch (match_info, 0);
1421 * g_print ("Found: %s\n", word);
1423 * g_match_info_next (match_info, &error);
1425 * g_match_info_free (match_info);
1426 * g_regex_unref (regex);
1427 * if (error != NULL)
1429 * g_printerr ("Error while matching: %s\n", error->message);
1430 * g_error_free (error);
1435 * Returns: %TRUE is the string matched, %FALSE otherwise
1440 g_regex_match_full (const GRegex *regex,
1441 const gchar *string,
1443 gint start_position,
1444 GRegexMatchFlags match_options,
1445 GMatchInfo **match_info,
1451 g_return_val_if_fail (regex != NULL, FALSE);
1452 g_return_val_if_fail (string != NULL, FALSE);
1453 g_return_val_if_fail (start_position >= 0, FALSE);
1454 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1455 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1457 info = match_info_new (regex, string, string_len, start_position,
1458 match_options, FALSE);
1459 match_ok = g_match_info_next (info, error);
1460 if (match_info != NULL)
1463 g_match_info_free (info);
1469 * g_regex_match_all:
1470 * @regex: a #GRegex structure from g_regex_new()
1471 * @string: the string to scan for matches
1472 * @match_options: match options
1473 * @match_info: pointer to location where to store the #GMatchInfo,
1474 * or %NULL if you do not need it
1476 * Using the standard algorithm for regular expression matching only
1477 * the longest match in the string is retrieved. This function uses
1478 * a different algorithm so it can retrieve all the possible matches.
1479 * For more documentation see g_regex_match_all_full().
1481 * A #GMatchInfo structure, used to get information on the match, is
1482 * stored in @match_info if not %NULL. Note that if @match_info is
1483 * not %NULL then it is created even if the function returns %FALSE,
1484 * i.e. you must free it regardless if regular expression actually
1487 * @string is not copied and is used in #GMatchInfo internally. If
1488 * you use any #GMatchInfo method (except g_match_info_free()) after
1489 * freeing or modifying @string then the behaviour is undefined.
1491 * Returns: %TRUE is the string matched, %FALSE otherwise
1496 g_regex_match_all (const GRegex *regex,
1497 const gchar *string,
1498 GRegexMatchFlags match_options,
1499 GMatchInfo **match_info)
1501 return g_regex_match_all_full (regex, string, -1, 0, match_options,
1506 * g_regex_match_all_full:
1507 * @regex: a #GRegex structure from g_regex_new()
1508 * @string: the string to scan for matches
1509 * @string_len: the length of @string, or -1 if @string is nul-terminated
1510 * @start_position: starting index of the string to match
1511 * @match_options: match options
1512 * @match_info: pointer to location where to store the #GMatchInfo,
1513 * or %NULL if you do not need it
1514 * @error: location to store the error occuring, or %NULL to ignore errors
1516 * Using the standard algorithm for regular expression matching only
1517 * the longest match in the string is retrieved, it is not possibile
1518 * to obtain all the available matches. For instance matching
1519 * "<a> <b> <c>" against the pattern "<.*>"
1520 * you get "<a> <b> <c>".
1522 * This function uses a different algorithm (called DFA, i.e. deterministic
1523 * finite automaton), so it can retrieve all the possible matches, all
1524 * starting at the same point in the string. For instance matching
1525 * "<a> <b> <c>" against the pattern "<.*>"
1526 * you would obtain three matches: "<a> <b> <c>",
1527 * "<a> <b>" and "<a>".
1529 * The number of matched strings is retrieved using
1530 * g_match_info_get_match_count(). To obtain the matched strings and
1531 * their position you can use, respectively, g_match_info_fetch() and
1532 * g_match_info_fetch_pos(). Note that the strings are returned in
1533 * reverse order of length; that is, the longest matching string is
1536 * Note that the DFA algorithm is slower than the standard one and it
1537 * is not able to capture substrings, so backreferences do not work.
1539 * Setting @start_position differs from just passing over a shortened
1540 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
1541 * that begins with any kind of lookbehind assertion, such as "\b".
1543 * A #GMatchInfo structure, used to get information on the match, is
1544 * stored in @match_info if not %NULL. Note that if @match_info is
1545 * not %NULL then it is created even if the function returns %FALSE,
1546 * i.e. you must free it regardless if regular expression actually
1549 * @string is not copied and is used in #GMatchInfo internally. If
1550 * you use any #GMatchInfo method (except g_match_info_free()) after
1551 * freeing or modifying @string then the behaviour is undefined.
1553 * Returns: %TRUE is the string matched, %FALSE otherwise
1558 g_regex_match_all_full (const GRegex *regex,
1559 const gchar *string,
1561 gint start_position,
1562 GRegexMatchFlags match_options,
1563 GMatchInfo **match_info,
1569 g_return_val_if_fail (regex != NULL, FALSE);
1570 g_return_val_if_fail (string != NULL, FALSE);
1571 g_return_val_if_fail (start_position >= 0, FALSE);
1572 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1573 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1575 info = match_info_new (regex, string, string_len, start_position,
1576 match_options, TRUE);
1582 info->matches = pcre_dfa_exec (regex->pcre_re, regex->extra,
1583 info->string, info->string_len,
1585 regex->match_opts | match_options,
1586 info->offsets, info->n_offsets,
1587 info->workspace, info->n_workspace);
1588 if (info->matches == PCRE_ERROR_DFA_WSSIZE)
1590 /* info->workspace is too small. */
1591 info->n_workspace *= 2;
1592 info->workspace = g_realloc (info->workspace,
1593 info->n_workspace * sizeof (gint));
1596 else if (info->matches == 0)
1598 /* info->offsets is too small. */
1599 info->n_offsets *= 2;
1600 info->offsets = g_realloc (info->offsets,
1601 info->n_offsets * sizeof (gint));
1604 else if (IS_PCRE_ERROR (info->matches))
1606 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
1607 _("Error while matching regular expression %s: %s"),
1608 regex->pattern, match_error (info->matches));
1612 /* set info->pos to -1 so that a call to g_match_info_next() fails. */
1615 if (match_info != NULL)
1618 g_match_info_free (info);
1620 return info->matches >= 0;
1624 * g_regex_get_string_number:
1625 * @regex: #GRegex structure
1626 * @name: name of the subexpression
1628 * Retrieves the number of the subexpression named @name.
1630 * Returns: The number of the subexpression or -1 if @name
1636 g_regex_get_string_number (const GRegex *regex,
1641 g_return_val_if_fail (regex != NULL, -1);
1642 g_return_val_if_fail (name != NULL, -1);
1644 num = pcre_get_stringnumber (regex->pcre_re, name);
1645 if (num == PCRE_ERROR_NOSUBSTRING)
1652 * g_regex_split_simple:
1653 * @pattern: the regular expression
1654 * @string: the string to scan for matches
1655 * @compile_options: compile options for the regular expression, or 0
1656 * @match_options: match options, or 0
1658 * Breaks the string on the pattern, and returns an array of
1659 * the tokens. If the pattern contains capturing parentheses,
1660 * then the text for each of the substrings will also be returned.
1661 * If the pattern does not match anywhere in the string, then the
1662 * whole string is returned as the first token.
1664 * This function is equivalent to g_regex_split() but it does
1665 * not require to compile the pattern with g_regex_new(), avoiding
1666 * some lines of code when you need just to do a split without
1667 * extracting substrings, capture counts, and so on.
1669 * If this function is to be called on the same @pattern more than
1670 * once, it's more efficient to compile the pattern once with
1671 * g_regex_new() and then use g_regex_split().
1673 * As a special case, the result of splitting the empty string ""
1674 * is an empty vector, not a vector containing a single string.
1675 * The reason for this special case is that being able to represent
1676 * a empty vector is typically more useful than consistent handling
1677 * of empty elements. If you do need to represent empty elements,
1678 * you'll need to check for the empty string before calling this
1681 * A pattern that can match empty strings splits @string into
1682 * separate characters wherever it matches the empty string between
1683 * characters. For example splitting "ab c" using as a separator
1684 * "\s*", you will get "a", "b" and "c".
1686 * Returns: a %NULL-terminated array of strings. Free it using g_strfreev()
1691 g_regex_split_simple (const gchar *pattern,
1692 const gchar *string,
1693 GRegexCompileFlags compile_options,
1694 GRegexMatchFlags match_options)
1699 regex = g_regex_new (pattern, compile_options, 0, NULL);
1702 result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
1703 g_regex_unref (regex);
1709 * @regex: a #GRegex structure
1710 * @string: the string to split with the pattern
1711 * @match_options: match time option flags
1713 * Breaks the string on the pattern, and returns an array of the tokens.
1714 * If the pattern contains capturing parentheses, then the text for each
1715 * of the substrings will also be returned. If the pattern does not match
1716 * anywhere in the string, then the whole string is returned as the first
1719 * As a special case, the result of splitting the empty string "" is an
1720 * empty vector, not a vector containing a single string. The reason for
1721 * this special case is that being able to represent a empty vector is
1722 * typically more useful than consistent handling of empty elements. If
1723 * you do need to represent empty elements, you'll need to check for the
1724 * empty string before calling this function.
1726 * A pattern that can match empty strings splits @string into separate
1727 * characters wherever it matches the empty string between characters.
1728 * For example splitting "ab c" using as a separator "\s*", you will get
1731 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1736 g_regex_split (const GRegex *regex,
1737 const gchar *string,
1738 GRegexMatchFlags match_options)
1740 return g_regex_split_full (regex, string, -1, 0,
1741 match_options, 0, NULL);
1745 * g_regex_split_full:
1746 * @regex: a #GRegex structure
1747 * @string: the string to split with the pattern
1748 * @string_len: the length of @string, or -1 if @string is nul-terminated
1749 * @start_position: starting index of the string to match
1750 * @match_options: match time option flags
1751 * @max_tokens: the maximum number of tokens to split @string into.
1752 * If this is less than 1, the string is split completely
1753 * @error: return location for a #GError
1755 * Breaks the string on the pattern, and returns an array of the tokens.
1756 * If the pattern contains capturing parentheses, then the text for each
1757 * of the substrings will also be returned. If the pattern does not match
1758 * anywhere in the string, then the whole string is returned as the first
1761 * As a special case, the result of splitting the empty string "" is an
1762 * empty vector, not a vector containing a single string. The reason for
1763 * this special case is that being able to represent a empty vector is
1764 * typically more useful than consistent handling of empty elements. If
1765 * you do need to represent empty elements, you'll need to check for the
1766 * empty string before calling this function.
1768 * A pattern that can match empty strings splits @string into separate
1769 * characters wherever it matches the empty string between characters.
1770 * For example splitting "ab c" using as a separator "\s*", you will get
1773 * Setting @start_position differs from just passing over a shortened
1774 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
1775 * that begins with any kind of lookbehind assertion, such as "\b".
1777 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1782 g_regex_split_full (const GRegex *regex,
1783 const gchar *string,
1785 gint start_position,
1786 GRegexMatchFlags match_options,
1790 GError *tmp_error = NULL;
1791 GMatchInfo *match_info;
1796 /* position of the last separator. */
1797 gint last_separator_end;
1798 /* was the last match 0 bytes long? */
1799 gboolean last_match_is_empty;
1800 /* the returned array of char **s */
1801 gchar **string_list;
1803 g_return_val_if_fail (regex != NULL, NULL);
1804 g_return_val_if_fail (string != NULL, NULL);
1805 g_return_val_if_fail (start_position >= 0, NULL);
1806 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1807 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1809 if (max_tokens <= 0)
1810 max_tokens = G_MAXINT;
1813 string_len = strlen (string);
1815 /* zero-length string */
1816 if (string_len - start_position == 0)
1817 return g_new0 (gchar *, 1);
1819 if (max_tokens == 1)
1821 string_list = g_new0 (gchar *, 2);
1822 string_list[0] = g_strndup (&string[start_position],
1823 string_len - start_position);
1829 last_separator_end = start_position;
1830 last_match_is_empty = FALSE;
1832 match_ok = g_regex_match_full (regex, string, string_len, start_position,
1833 match_options, &match_info, &tmp_error);
1834 while (tmp_error == NULL)
1838 last_match_is_empty =
1839 (match_info->offsets[0] == match_info->offsets[1]);
1841 /* we need to skip empty separators at the same position of the end
1842 * of another separator. e.g. the string is "a b" and the separator
1843 * is " *", so from 1 to 2 we have a match and at position 2 we have
1844 * an empty match. */
1845 if (last_separator_end != match_info->offsets[1])
1850 token = g_strndup (string + last_separator_end,
1851 match_info->offsets[0] - last_separator_end);
1852 list = g_list_prepend (list, token);
1855 /* if there were substrings, these need to be added to
1857 match_count = g_match_info_get_match_count (match_info);
1858 if (match_count > 1)
1860 for (i = 1; i < match_count; i++)
1861 list = g_list_prepend (list, g_match_info_fetch (match_info, i));
1867 /* if there was no match, copy to end of string. */
1868 if (!last_match_is_empty)
1870 gchar *token = g_strndup (string + last_separator_end,
1871 match_info->string_len - last_separator_end);
1872 list = g_list_prepend (list, token);
1874 /* no more tokens, end the loop. */
1878 /* -1 to leave room for the last part. */
1879 if (token_count >= max_tokens - 1)
1881 /* we have reached the maximum number of tokens, so we copy
1882 * the remaining part of the string. */
1883 if (last_match_is_empty)
1885 /* the last match was empty, so we have moved one char
1886 * after the real position to avoid empty matches at the
1888 match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
1890 /* the if is needed in the case we have terminated the available
1891 * tokens, but we are at the end of the string, so there are no
1892 * characters left to copy. */
1893 if (string_len > match_info->pos)
1895 gchar *token = g_strndup (string + match_info->pos,
1896 string_len - match_info->pos);
1897 list = g_list_prepend (list, token);
1903 last_separator_end = match_info->pos;
1904 if (last_match_is_empty)
1905 /* if the last match was empty, g_match_info_next() has moved
1906 * forward to avoid infinite loops, but we still need to copy that
1908 last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
1910 match_ok = g_match_info_next (match_info, &tmp_error);
1912 g_match_info_free (match_info);
1913 if (tmp_error != NULL)
1915 g_propagate_error (error, tmp_error);
1916 g_list_foreach (list, (GFunc)g_free, NULL);
1918 match_info->pos = -1;
1922 string_list = g_new (gchar *, g_list_length (list) + 1);
1924 for (last = g_list_last (list); last; last = g_list_previous (last))
1925 string_list[i++] = last->data;
1926 string_list[i] = NULL;
1935 REPL_TYPE_CHARACTER,
1936 REPL_TYPE_SYMBOLIC_REFERENCE,
1937 REPL_TYPE_NUMERIC_REFERENCE,
1938 REPL_TYPE_CHANGE_CASE
1943 CHANGE_CASE_NONE = 1 << 0,
1944 CHANGE_CASE_UPPER = 1 << 1,
1945 CHANGE_CASE_LOWER = 1 << 2,
1946 CHANGE_CASE_UPPER_SINGLE = 1 << 3,
1947 CHANGE_CASE_LOWER_SINGLE = 1 << 4,
1948 CHANGE_CASE_SINGLE_MASK = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
1949 CHANGE_CASE_LOWER_MASK = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
1950 CHANGE_CASE_UPPER_MASK = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
1953 struct _InterpolationData
1959 ChangeCase change_case;
1963 free_interpolation_data (InterpolationData *data)
1965 g_free (data->text);
1969 static const gchar *
1970 expand_escape (const gchar *replacement,
1972 InterpolationData *data,
1977 const gchar *error_detail;
1979 GError *tmp_error = NULL;
1987 data->type = REPL_TYPE_CHARACTER;
1992 data->type = REPL_TYPE_CHARACTER;
1997 data->type = REPL_TYPE_CHARACTER;
2002 data->type = REPL_TYPE_CHARACTER;
2007 data->type = REPL_TYPE_CHARACTER;
2012 data->type = REPL_TYPE_CHARACTER;
2017 data->type = REPL_TYPE_CHARACTER;
2022 data->type = REPL_TYPE_CHARACTER;
2032 h = g_ascii_xdigit_value (*p);
2035 error_detail = _("hexadecimal digit or '}' expected");
2046 for (i = 0; i < 2; i++)
2048 h = g_ascii_xdigit_value (*p);
2051 error_detail = _("hexadecimal digit expected");
2058 data->type = REPL_TYPE_STRING;
2059 data->text = g_new0 (gchar, 8);
2060 g_unichar_to_utf8 (x, data->text);
2064 data->type = REPL_TYPE_CHANGE_CASE;
2065 data->change_case = CHANGE_CASE_LOWER_SINGLE;
2069 data->type = REPL_TYPE_CHANGE_CASE;
2070 data->change_case = CHANGE_CASE_UPPER_SINGLE;
2074 data->type = REPL_TYPE_CHANGE_CASE;
2075 data->change_case = CHANGE_CASE_LOWER;
2079 data->type = REPL_TYPE_CHANGE_CASE;
2080 data->change_case = CHANGE_CASE_UPPER;
2084 data->type = REPL_TYPE_CHANGE_CASE;
2085 data->change_case = CHANGE_CASE_NONE;
2091 error_detail = _("missing '<' in symbolic reference");
2100 error_detail = _("unfinished symbolic reference");
2107 error_detail = _("zero-length symbolic reference");
2110 if (g_ascii_isdigit (*q))
2115 h = g_ascii_digit_value (*q);
2118 error_detail = _("digit expected");
2127 data->type = REPL_TYPE_NUMERIC_REFERENCE;
2134 if (!g_ascii_isalnum (*r))
2136 error_detail = _("illegal symbolic reference");
2143 data->text = g_strndup (q, p - q);
2144 data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
2149 /* if \0 is followed by a number is an octal number representing a
2150 * character, else it is a numeric reference. */
2151 if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
2154 p = g_utf8_next_char (p);
2167 for (i = 0; i < 3; i++)
2169 h = g_ascii_digit_value (*p);
2179 if (i == 2 && base == 10)
2185 if (base == 8 || i == 3)
2187 data->type = REPL_TYPE_STRING;
2188 data->text = g_new0 (gchar, 8);
2189 g_unichar_to_utf8 (x, data->text);
2193 data->type = REPL_TYPE_NUMERIC_REFERENCE;
2198 error_detail = _("stray final '\\'");
2202 error_detail = _("unknown escape sequence");
2209 /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
2210 tmp_error = g_error_new (G_REGEX_ERROR,
2211 G_REGEX_ERROR_REPLACE,
2212 _("Error while parsing replacement "
2213 "text \"%s\" at char %lu: %s"),
2215 (gulong)(p - replacement),
2217 g_propagate_error (error, tmp_error);
2223 split_replacement (const gchar *replacement,
2227 InterpolationData *data;
2228 const gchar *p, *start;
2230 start = p = replacement;
2235 data = g_new0 (InterpolationData, 1);
2236 start = p = expand_escape (replacement, p, data, error);
2239 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
2241 free_interpolation_data (data);
2245 list = g_list_prepend (list, data);
2250 if (*p == '\\' || *p == '\0')
2254 data = g_new0 (InterpolationData, 1);
2255 data->text = g_strndup (start, p - start);
2256 data->type = REPL_TYPE_STRING;
2257 list = g_list_prepend (list, data);
2263 return g_list_reverse (list);
2266 /* Change the case of c based on change_case. */
2267 #define CHANGE_CASE(c, change_case) \
2268 (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
2269 g_unichar_tolower (c) : \
2270 g_unichar_toupper (c))
2273 string_append (GString *string,
2275 ChangeCase *change_case)
2279 if (text[0] == '\0')
2282 if (*change_case == CHANGE_CASE_NONE)
2284 g_string_append (string, text);
2286 else if (*change_case & CHANGE_CASE_SINGLE_MASK)
2288 c = g_utf8_get_char (text);
2289 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2290 g_string_append (string, g_utf8_next_char (text));
2291 *change_case = CHANGE_CASE_NONE;
2295 while (*text != '\0')
2297 c = g_utf8_get_char (text);
2298 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2299 text = g_utf8_next_char (text);
2305 interpolate_replacement (const GMatchInfo *match_info,
2310 InterpolationData *idata;
2312 ChangeCase change_case = CHANGE_CASE_NONE;
2314 for (list = data; list; list = list->next)
2317 switch (idata->type)
2319 case REPL_TYPE_STRING:
2320 string_append (result, idata->text, &change_case);
2322 case REPL_TYPE_CHARACTER:
2323 g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
2324 if (change_case & CHANGE_CASE_SINGLE_MASK)
2325 change_case = CHANGE_CASE_NONE;
2327 case REPL_TYPE_NUMERIC_REFERENCE:
2328 match = g_match_info_fetch (match_info, idata->num);
2331 string_append (result, match, &change_case);
2335 case REPL_TYPE_SYMBOLIC_REFERENCE:
2336 match = g_match_info_fetch_named (match_info, idata->text);
2339 string_append (result, match, &change_case);
2343 case REPL_TYPE_CHANGE_CASE:
2344 change_case = idata->change_case;
2352 /* whether actual match_info is needed for replacement, i.e.
2353 * whether there are references
2356 interpolation_list_needs_match (GList *list)
2358 while (list != NULL)
2360 InterpolationData *data = list->data;
2362 if (data->type == REPL_TYPE_SYMBOLIC_REFERENCE ||
2363 data->type == REPL_TYPE_NUMERIC_REFERENCE)
2376 * @regex: a #GRegex structure
2377 * @string: the string to perform matches against
2378 * @string_len: the length of @string, or -1 if @string is nul-terminated
2379 * @start_position: starting index of the string to match
2380 * @replacement: text to replace each match with
2381 * @match_options: options for the match
2382 * @error: location to store the error occuring, or %NULL to ignore errors
2384 * Replaces all occurrences of the pattern in @regex with the
2385 * replacement text. Backreferences of the form '\number' or
2386 * '\g<number>' in the replacement text are interpolated by the
2387 * number-th captured subexpression of the match, '\g<name>' refers
2388 * to the captured subexpression with the given name. '\0' refers to the
2389 * complete match, but '\0' followed by a number is the octal representation
2390 * of a character. To include a literal '\' in the replacement, write '\\'.
2391 * There are also escapes that changes the case of the following text:
2394 * <varlistentry><term>\l</term>
2396 * <para>Convert to lower case the next character</para>
2399 * <varlistentry><term>\u</term>
2401 * <para>Convert to upper case the next character</para>
2404 * <varlistentry><term>\L</term>
2406 * <para>Convert to lower case till \E</para>
2409 * <varlistentry><term>\U</term>
2411 * <para>Convert to upper case till \E</para>
2414 * <varlistentry><term>\E</term>
2416 * <para>End case modification</para>
2421 * If you do not need to use backreferences use g_regex_replace_literal().
2423 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was
2424 * passed to g_regex_new(). If you want to use not UTF-8 encoded stings
2425 * you can use g_regex_replace_literal().
2427 * Setting @start_position differs from just passing over a shortened
2428 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that
2429 * begins with any kind of lookbehind assertion, such as "\b".
2431 * Returns: a newly allocated string containing the replacements
2436 g_regex_replace (const GRegex *regex,
2437 const gchar *string,
2439 gint start_position,
2440 const gchar *replacement,
2441 GRegexMatchFlags match_options,
2446 GError *tmp_error = NULL;
2448 g_return_val_if_fail (regex != NULL, NULL);
2449 g_return_val_if_fail (string != NULL, NULL);
2450 g_return_val_if_fail (start_position >= 0, NULL);
2451 g_return_val_if_fail (replacement != NULL, NULL);
2452 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2453 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2455 list = split_replacement (replacement, &tmp_error);
2456 if (tmp_error != NULL)
2458 g_propagate_error (error, tmp_error);
2462 result = g_regex_replace_eval (regex,
2463 string, string_len, start_position,
2465 interpolate_replacement,
2468 if (tmp_error != NULL)
2469 g_propagate_error (error, tmp_error);
2471 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
2478 literal_replacement (const GMatchInfo *match_info,
2482 g_string_append (result, data);
2487 * g_regex_replace_literal:
2488 * @regex: a #GRegex structure
2489 * @string: the string to perform matches against
2490 * @string_len: the length of @string, or -1 if @string is nul-terminated
2491 * @start_position: starting index of the string to match
2492 * @replacement: text to replace each match with
2493 * @match_options: options for the match
2494 * @error: location to store the error occuring, or %NULL to ignore errors
2496 * Replaces all occurrences of the pattern in @regex with the
2497 * replacement text. @replacement is replaced literally, to
2498 * include backreferences use g_regex_replace().
2500 * Setting @start_position differs from just passing over a
2501 * shortened string and setting #G_REGEX_MATCH_NOTBOL in the
2502 * case of a pattern that begins with any kind of lookbehind
2503 * assertion, such as "\b".
2505 * Returns: a newly allocated string containing the replacements
2510 g_regex_replace_literal (const GRegex *regex,
2511 const gchar *string,
2513 gint start_position,
2514 const gchar *replacement,
2515 GRegexMatchFlags match_options,
2518 g_return_val_if_fail (replacement != NULL, NULL);
2519 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2521 return g_regex_replace_eval (regex,
2522 string, string_len, start_position,
2524 literal_replacement,
2525 (gpointer)replacement,
2530 * g_regex_replace_eval:
2531 * @regex: a #GRegex structure from g_regex_new()
2532 * @string: string to perform matches against
2533 * @string_len: the length of @string, or -1 if @string is nul-terminated
2534 * @start_position: starting index of the string to match
2535 * @match_options: options for the match
2536 * @eval: a function to call for each match
2537 * @user_data: user data to pass to the function
2538 * @error: location to store the error occuring, or %NULL to ignore errors
2540 * Replaces occurrences of the pattern in regex with the output of
2541 * @eval for that occurrence.
2543 * Setting @start_position differs from just passing over a shortened
2544 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
2545 * that begins with any kind of lookbehind assertion, such as "\b".
2547 * The following example uses g_regex_replace_eval() to replace multiple
2551 * eval_cb (const GMatchInfo *info,
2558 * match = g_match_info_fetch (info, 0);
2559 * r = g_hash_table_lookup ((GHashTable *)data, match);
2560 * g_string_append (res, r);
2572 * h = g_hash_table_new (g_str_hash, g_str_equal);
2574 * g_hash_table_insert (h, "1", "ONE");
2575 * g_hash_table_insert (h, "2", "TWO");
2576 * g_hash_table_insert (h, "3", "THREE");
2577 * g_hash_table_insert (h, "4", "FOUR");
2579 * reg = g_regex_new ("1|2|3|4", 0, 0, NULL);
2580 * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL);
2581 * g_hash_table_destroy (h);
2586 * Returns: a newly allocated string containing the replacements
2591 g_regex_replace_eval (const GRegex *regex,
2592 const gchar *string,
2594 gint start_position,
2595 GRegexMatchFlags match_options,
2596 GRegexEvalCallback eval,
2600 GMatchInfo *match_info;
2603 gboolean done = FALSE;
2604 GError *tmp_error = NULL;
2606 g_return_val_if_fail (regex != NULL, NULL);
2607 g_return_val_if_fail (string != NULL, NULL);
2608 g_return_val_if_fail (start_position >= 0, NULL);
2609 g_return_val_if_fail (eval != NULL, NULL);
2610 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2613 string_len = strlen (string);
2615 result = g_string_sized_new (string_len);
2617 /* run down the string making matches. */
2618 g_regex_match_full (regex, string, string_len, start_position,
2619 match_options, &match_info, &tmp_error);
2620 while (!done && g_match_info_matches (match_info))
2622 g_string_append_len (result,
2624 match_info->offsets[0] - str_pos);
2625 done = (*eval) (match_info, result, user_data);
2626 str_pos = match_info->offsets[1];
2627 g_match_info_next (match_info, &tmp_error);
2629 g_match_info_free (match_info);
2630 if (tmp_error != NULL)
2632 g_propagate_error (error, tmp_error);
2633 g_string_free (result, TRUE);
2637 g_string_append_len (result, string + str_pos, string_len - str_pos);
2638 return g_string_free (result, FALSE);
2642 * g_regex_check_replacement:
2643 * @replacement: the replacement string
2644 * @has_references: location to store information about
2645 * references in @replacement or %NULL
2646 * @error: location to store error
2648 * Checks whether @replacement is a valid replacement string
2649 * (see g_regex_replace()), i.e. that all escape sequences in
2652 * If @has_references is not %NULL then @replacement is checked
2653 * for pattern references. For instance, replacement text 'foo\n'
2654 * does not contain references and may be evaluated without information
2655 * about actual match, but '\0\1' (whole match followed by first
2656 * subpattern) requires valid #GMatchInfo object.
2658 * Returns: whether @replacement is a valid replacement string
2663 g_regex_check_replacement (const gchar *replacement,
2664 gboolean *has_references,
2670 list = split_replacement (replacement, &tmp);
2674 g_propagate_error (error, tmp);
2679 *has_references = interpolation_list_needs_match (list);
2681 g_list_foreach (list, (GFunc) free_interpolation_data, NULL);
2688 * g_regex_escape_string:
2689 * @string: the string to escape
2690 * @length: the length of @string, or -1 if @string is nul-terminated
2692 * Escapes the special characters used for regular expressions
2693 * in @string, for instance "a.b*c" becomes "a\.b\*c". This
2694 * function is useful to dynamically generate regular expressions.
2696 * @string can contain nul characters that are replaced with "\0",
2697 * in this case remember to specify the correct length of @string
2700 * Returns: a newly-allocated escaped string
2705 g_regex_escape_string (const gchar *string,
2709 const char *p, *piece_start, *end;
2711 g_return_val_if_fail (string != NULL, NULL);
2714 length = strlen (string);
2716 end = string + length;
2717 p = piece_start = string;
2718 escaped = g_string_sized_new (length + 1);
2739 if (p != piece_start)
2740 /* copy the previous piece. */
2741 g_string_append_len (escaped, piece_start, p - piece_start);
2742 g_string_append_c (escaped, '\\');
2744 g_string_append_c (escaped, '0');
2746 g_string_append_c (escaped, *p);
2750 p = g_utf8_next_char (p);
2755 if (piece_start < end)
2756 g_string_append_len (escaped, piece_start, end - piece_start);
2758 return g_string_free (escaped, FALSE);
2761 #define __G_REGEX_C__
2762 #include "galiasdef.c"