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
27 #include <glib/gi18n.h>
30 #ifdef USE_SYSTEM_PCRE
33 #include "pcre/pcre.h"
38 /* Mask of all the possible values for GRegexCompileFlags. */
39 #define G_REGEX_COMPILE_MASK (G_REGEX_CASELESS | \
44 G_REGEX_DOLLAR_ENDONLY | \
47 G_REGEX_NO_AUTO_CAPTURE | \
50 G_REGEX_NEWLINE_CR | \
51 G_REGEX_NEWLINE_LF | \
54 /* Mask of all the possible values for GRegexMatchFlags. */
55 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_ANCHORED | \
56 G_REGEX_MATCH_NOTBOL | \
57 G_REGEX_MATCH_NOTEOL | \
58 G_REGEX_MATCH_NOTEMPTY | \
59 G_REGEX_MATCH_PARTIAL | \
60 G_REGEX_MATCH_NEWLINE_CR | \
61 G_REGEX_MATCH_NEWLINE_LF | \
62 G_REGEX_MATCH_NEWLINE_CRLF | \
63 G_REGEX_MATCH_NEWLINE_ANY)
65 /* if the string is in UTF-8 use g_utf8_ functions, else use
67 #define NEXT_CHAR(re, s) (((re)->compile_opts & PCRE_UTF8) ? \
68 g_utf8_next_char (s) : \
70 #define PREV_CHAR(re, s) (((re)->compile_opts & PCRE_UTF8) ? \
71 g_utf8_prev_char (s) : \
76 GRegex *regex; /* the regex */
77 GRegexMatchFlags match_opts; /* options used at match time on the regex */
78 gint matches; /* number of matching sub patterns */
79 gint pos; /* position in the string where last match left off */
80 gint *offsets; /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
81 gint n_offsets; /* number of offsets */
82 gint *workspace; /* workspace for pcre_dfa_exec() */
83 gint n_workspace; /* number of workspace elements */
84 const gchar *string; /* string passed to the match function */
85 gssize string_len; /* length of string */
90 volatile guint ref_count; /* the ref count for the immutable part */
91 gchar *pattern; /* the pattern */
92 pcre *pcre_re; /* compiled form of the pattern */
93 GRegexCompileFlags compile_opts; /* options used at compile time on the pattern */
94 GRegexMatchFlags match_opts; /* options used at match time on the regex */
95 pcre_extra *extra; /* data stored when G_REGEX_OPTIMIZE is used */
98 /* TRUE if ret is an error code, FALSE otherwise. */
99 #define IS_PCRE_ERROR(ret) ((ret) < PCRE_ERROR_NOMATCH && (ret) != PCRE_ERROR_PARTIAL)
101 static GRegex *regex_ref (GRegex *regex);
102 static void regex_unref (GRegex *regex);
104 typedef struct _InterpolationData InterpolationData;
105 static gboolean interpolate_replacement (const GRegex *regex,
106 const GMatchInfo *match_info,
110 static GList *split_replacement (const gchar *replacement,
112 static void free_interpolation_data (InterpolationData *data);
116 match_error (gint errcode)
120 case PCRE_ERROR_NOMATCH:
123 case PCRE_ERROR_NULL:
124 /* NULL argument, this should not happen in GRegex */
125 g_warning ("A NULL argument was passed to PCRE");
127 case PCRE_ERROR_BADOPTION:
128 return "bad options";
129 case PCRE_ERROR_BADMAGIC:
130 return _("corrupted object");
131 case PCRE_ERROR_UNKNOWN_OPCODE:
132 return N_("internal error or corrupted object");
133 case PCRE_ERROR_NOMEMORY:
134 return _("out of memory");
135 case PCRE_ERROR_NOSUBSTRING:
136 /* not used by pcre_exec() */
138 case PCRE_ERROR_MATCHLIMIT:
139 return _("backtracking limit reached");
140 case PCRE_ERROR_CALLOUT:
141 /* callouts are not implemented */
143 case PCRE_ERROR_BADUTF8:
144 case PCRE_ERROR_BADUTF8_OFFSET:
145 /* we do not check if strings are valid */
147 case PCRE_ERROR_PARTIAL:
150 case PCRE_ERROR_BADPARTIAL:
151 return _("the pattern contains items not supported for partial matching");
152 case PCRE_ERROR_INTERNAL:
153 return _("internal error");
154 case PCRE_ERROR_BADCOUNT:
155 /* negative ovecsize, this should not happen in GRegex */
156 g_warning ("A negative ovecsize was passed to PCRE");
158 case PCRE_ERROR_DFA_UITEM:
159 return _("the pattern contains items not supported for partial matching");
160 case PCRE_ERROR_DFA_UCOND:
161 return _("back references as conditions are not supported for partial matching");
162 case PCRE_ERROR_DFA_UMLIMIT:
163 /* the match_field field is not used in GRegex */
165 case PCRE_ERROR_DFA_WSSIZE:
166 /* handled expanding the workspace */
168 case PCRE_ERROR_DFA_RECURSE:
169 case PCRE_ERROR_RECURSIONLIMIT:
170 return _("recursion limit reached");
171 case PCRE_ERROR_NULLWSLIMIT:
172 return _("workspace limit for empty substrings reached");
173 case PCRE_ERROR_BADNEWLINE:
174 return _("invalid combination of newline flags");
178 return _("unknown error");
185 match_info_new (const GRegex *regex,
192 GMatchInfo *match_info;
195 string_len = strlen (string);
197 match_info = g_new0 (GMatchInfo, 1);
198 match_info->regex = regex_ref ((GRegex *)regex);
199 match_info->string = string;
200 match_info->string_len = string_len;
201 match_info->matches = PCRE_ERROR_NOMATCH;
202 match_info->pos = start_position;
203 match_info->match_opts = match_options;
207 /* These values should be enough for most cases, if they are not
208 * enough g_regex_match_all_full() will expand them. */
209 match_info->n_offsets = 24;
210 match_info->n_workspace = 100;
211 match_info->workspace = g_new (gint, match_info->n_workspace);
216 pcre_fullinfo (regex->pcre_re, regex->extra,
217 PCRE_INFO_CAPTURECOUNT, &capture_count);
218 match_info->n_offsets = (capture_count + 1) * 3;
220 match_info->offsets = g_new0 (gint, match_info->n_offsets);
227 * @match_info: a #GMatchInfo
229 * Frees all the memory associated with the #GMatchInfo structure.
234 g_match_info_free (GMatchInfo *match_info)
236 regex_unref (match_info->regex);
237 g_free (match_info->offsets);
238 g_free (match_info->workspace);
244 * @match_info: a #GMatchInfo structure
245 * @error: location to store the error occuring, or NULL to ignore errors
247 * Scans for the next match using the same parameters of the previous
248 * call to g_regex_match_full() or g_regex_match() that returned
251 * The match is done on the string passed to the match function, so you
252 * cannot free it before calling this function.
254 * Returns: %TRUE is the string matched, %FALSE otherwise
259 g_match_info_next (GMatchInfo *match_info,
262 g_return_val_if_fail (match_info != NULL, FALSE);
263 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
264 g_return_val_if_fail (match_info->pos >= 0, FALSE);
266 match_info->matches = pcre_exec (match_info->regex->pcre_re,
267 match_info->regex->extra,
269 match_info->string_len,
271 match_info->regex->match_opts |
272 match_info->match_opts,
274 match_info->n_offsets);
275 if (IS_PCRE_ERROR (match_info->matches))
277 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
278 _("Error while matching regular expression %s: %s"),
279 match_info->regex->pattern, match_error (match_info->matches));
283 /* avoid infinite loops if the pattern is an empty string or something
285 if (match_info->pos == match_info->offsets[1])
287 if (match_info->pos > match_info->string_len)
289 /* we have reached the end of the string */
290 match_info->pos = -1;
291 match_info->matches = PCRE_ERROR_NOMATCH;
294 match_info->pos = NEXT_CHAR (match_info->regex,
295 &match_info->string[match_info->pos]) -
300 match_info->pos = match_info->offsets[1];
303 return match_info->matches >= 0;
307 * g_match_info_matches:
308 * @match_info: a #GMatchInfo structure
310 * Returns: %TRUE if the previous match operation succeeded, %FALSE
316 g_match_info_matches (const GMatchInfo *match_info)
318 g_return_val_if_fail (match_info != NULL, FALSE);
320 return match_info->matches >= 0;
324 * g_match_info_get_match_count:
325 * @match_info: a #GMatchInfo structure
327 * Retrieves the number of matched substrings (including substring 0, that
328 * is the whole matched text), so 1 is returned if the pattern has no
329 * substrings in it and 0 is returned if the match failed.
331 * If the last match was obtained using the DFA algorithm, that is using
332 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
333 * count is not that of the number of capturing parentheses but that of
334 * the number of matched substrings.
336 * Returns: Number of matched substrings, or -1 if an error occurred
341 g_match_info_get_match_count (const GMatchInfo *match_info)
343 g_return_val_if_fail (match_info, -1);
345 if (match_info->matches == PCRE_ERROR_NOMATCH)
348 else if (match_info->matches < PCRE_ERROR_NOMATCH)
353 return match_info->matches;
357 * g_match_info_is_partial_match:
358 * @match_info: a #GMatchInfo structure
360 * Usually if the string passed to g_regex_match*() matches as far as
361 * it goes, but is too short to match the entire pattern, %FALSE is
362 * returned. There are circumstances where it might be helpful to
363 * distinguish this case from other cases in which there is no match.
365 * Consider, for example, an application where a human is required to
366 * type in data for a field with specific formatting requirements. An
367 * example might be a date in the form ddmmmyy, defined by the pattern
368 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
369 * If the application sees the user’s keystrokes one by one, and can
370 * check that what has been typed so far is potentially valid, it is
371 * able to raise an error as soon as a mistake is made.
373 * GRegex supports the concept of partial matching by means of the
374 * #G_REGEX_MATCH_PARTIAL flag. When this is set the return code for
375 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
376 * for a complete match, %FALSE otherwise. But, when this functions
377 * returns %FALSE, you can check if the match was partial calling
378 * g_match_info_is_partial_match().
380 * When using partial matching you cannot use g_match_info_fetch*().
382 * Because of the way certain internal optimizations are implemented the
383 * partial matching algorithm cannot be used with all patterns. So repeated
384 * single characters such as "a{2,4}" and repeated single metasequences such
385 * as "\d+" are not permitted if the maximum number of occurrences is
386 * greater than one. Optional items such as "\d?" (where the maximum is one)
387 * are permitted. Quantifiers with any values are permitted after
388 * parentheses, so the invalid examples above can be coded thus "(a){2,4}"
389 * and "(\d)+". If #G_REGEX_MATCH_PARTIAL is set for a pattern that does
390 * not conform to the restrictions, matching functions return an error.
392 * Returns: %TRUE if the match was partial, %FALSE otherwise
397 g_match_info_is_partial_match (const GMatchInfo *match_info)
399 g_return_val_if_fail (match_info != NULL, FALSE);
401 return match_info->matches == PCRE_ERROR_PARTIAL;
405 * g_match_info_expand_references:
406 * @match_info: a #GMatchInfo
407 * @string_to_expand: the string to expand
408 * @error: location to store the error occuring, or %NULL to ignore errors
410 * Returns a new string containing the text in @string_to_expand with
411 * references expanded. References refer to the last match done with
412 * @string against @regex and have the same syntax used by g_regex_replace().
414 * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
415 * passed to g_regex_new().
417 * The backreferences are extracted from the string passed to the match
418 * function, so you cannot call this function after freeing the string.
420 * Returns: the expanded string, or %NULL if an error occurred
425 g_match_info_expand_references (const GMatchInfo *match_info,
426 const gchar *string_to_expand,
431 GError *tmp_error = NULL;
433 g_return_val_if_fail (match_info != NULL, NULL);
434 g_return_val_if_fail (string_to_expand != NULL, NULL);
435 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
437 list = split_replacement (string_to_expand, &tmp_error);
438 if (tmp_error != NULL)
440 g_propagate_error (error, tmp_error);
444 result = g_string_sized_new (strlen (string_to_expand));
445 interpolate_replacement (match_info->regex, match_info,
446 match_info->string, result, list);
448 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
451 return g_string_free (result, FALSE);
455 * g_match_info_fetch:
456 * @match_info: #GMatchInfo structure
457 * @match_num: number of the sub expression
459 * Retrieves the text matching the @match_num<!-- -->'th capturing parentheses.
460 * 0 is the full text of the match, 1 is the first paren set, 2 the second,
463 * If @match_num is a valid sub pattern but it didn't match anything (e.g.
464 * sub pattern 1, matching "b" against "(a)?b") then an empty string is
467 * If the match was obtained using the DFA algorithm, that is using
468 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
469 * string is not that of a set of parentheses but that of a matched
470 * substring. Substrings are matched in reverse order of length, so 0 is
473 * The string is fetched from the string passed to the match function,
474 * so you cannot call this function after freeing the string.
476 * Returns: The matched substring, or %NULL if an error occurred.
477 * You have to free the string yourself
482 g_match_info_fetch (const GMatchInfo *match_info,
485 /* we cannot use pcre_get_substring() because it allocates the
486 * string using pcre_malloc(). */
490 g_return_val_if_fail (match_info != NULL, NULL);
491 g_return_val_if_fail (match_num >= 0, NULL);
493 /* match_num does not exist or it didn't matched, i.e. matching "b"
494 * against "(a)?b" then group 0 is empty. */
495 if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
497 else if (start == -1)
498 match = g_strdup ("");
500 match = g_strndup (&match_info->string[start], end - start);
506 * g_match_info_fetch_pos:
507 * @match_info: #GMatchInfo structure
508 * @match_num: number of the sub expression
509 * @start_pos: pointer to location where to store the start position
510 * @end_pos: pointer to location where to store the end position
512 * Retrieves the position of the @match_num<!-- -->'th capturing parentheses.
513 * 0 is the full text of the match, 1 is the first paren set, 2 the second,
516 * If @match_num is a valid sub pattern but it didn't match anything (e.g.
517 * sub pattern 1, matching "b" against "(a)?b") then @start_pos and @end_pos
518 * are set to -1 and %TRUE is returned.
520 * If the match was obtained using the DFA algorithm, that is using
521 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
522 * position is not that of a set of parentheses but that of a matched
523 * substring. Substrings are matched in reverse order of length, so 0 is
526 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If the
527 * position cannot be fetched, @start_pos and @end_pos are left
533 g_match_info_fetch_pos (const GMatchInfo *match_info,
538 g_return_val_if_fail (match_info != NULL, FALSE);
539 g_return_val_if_fail (match_num >= 0, FALSE);
541 /* make sure the sub expression number they're requesting is less than
542 * the total number of sub expressions that were matched. */
543 if (match_num >= match_info->matches)
546 if (start_pos != NULL)
547 *start_pos = match_info->offsets[2 * match_num];
550 *end_pos = match_info->offsets[2 * match_num + 1];
556 * Returns number of first matched subpattern with name @name.
557 * There may be more than one in case when DUPNAMES is used,
558 * and not all subpatterns with that name match;
559 * pcre_get_stringnumber() does not work in that case.
562 get_matched_substring_number (const GMatchInfo *match_info,
569 if ((match_info->regex->compile_opts & G_REGEX_DUPNAMES) == 0)
570 return pcre_get_stringnumber (match_info->regex->pcre_re, name);
572 /* This code is copied from pcre_get.c: get_first_set() */
573 entrysize = pcre_get_stringtable_entries (match_info->regex->pcre_re,
581 for (entry = (guchar*) first; entry <= (guchar*) last; entry += entrysize)
583 gint n = (entry[0] << 8) + entry[1];
584 if (match_info->offsets[n*2] >= 0)
588 return (first[0] << 8) + first[1];
592 * g_match_info_fetch_named:
593 * @match_info: #GMatchInfo structure
594 * @name: name of the subexpression
596 * Retrieves the text matching the capturing parentheses named @name.
598 * If @name is a valid sub pattern name but it didn't match anything (e.g.
599 * sub pattern "X", matching "b" against "(?P<X>a)?b") then an empty
600 * string is returned.
602 * The string is fetched from the string passed to the match function,
603 * so you cannot call this function after freeing the string.
605 * Returns: The matched substring, or %NULL if an error occurred.
606 * You have to free the string yourself
611 g_match_info_fetch_named (const GMatchInfo *match_info,
614 /* we cannot use pcre_get_named_substring() because it allocates the
615 * string using pcre_malloc(). */
618 g_return_val_if_fail (match_info != NULL, NULL);
619 g_return_val_if_fail (name != NULL, NULL);
621 num = get_matched_substring_number (match_info, name);
625 return g_match_info_fetch (match_info, num);
629 * g_match_info_fetch_named_pos:
630 * @match_info: #GMatchInfo structure
631 * @name: name of the subexpression
632 * @start_pos: pointer to location where to store the start position
633 * @end_pos: pointer to location where to store the end position
635 * Retrieves the position of the capturing parentheses named @name.
637 * If @name is a valid sub pattern name but it didn't match anything (e.g.
638 * sub pattern "X", matching "b" against "(?P<X>a)?b") then @start_pos and
639 * @end_pos are set to -1 and %TRUE is returned.
641 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If the
642 * position cannot be fetched, @start_pos and @end_pos are left
648 g_match_info_fetch_named_pos (const GMatchInfo *match_info,
655 g_return_val_if_fail (match_info != NULL, FALSE);
656 g_return_val_if_fail (name != NULL, FALSE);
658 num = get_matched_substring_number (match_info, name);
662 return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
666 * g_match_info_fetch_all:
667 * @match_info: a #GMatchInfo structure
669 * Bundles up pointers to each of the matching substrings from a match
670 * and stores them in an array of gchar pointers. The first element in
671 * the returned array is the match number 0, i.e. the entire matched
674 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
675 * "b" against "(a)?b") then an empty string is inserted.
677 * If the last match was obtained using the DFA algorithm, that is using
678 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
679 * strings are not that matched by sets of parentheses but that of the
680 * matched substring. Substrings are matched in reverse order of length,
681 * so the first one is the longest match.
683 * The strings are fetched from the string passed to the match function,
684 * so you cannot call this function after freeing the string.
686 * Returns: a %NULL-terminated array of gchar * pointers. It must be freed
687 * using g_strfreev(). If the previous match failed %NULL is
693 g_match_info_fetch_all (const GMatchInfo *match_info)
695 /* we cannot use pcre_get_substring_list() because the returned value
696 * isn't suitable for g_strfreev(). */
700 g_return_val_if_fail (match_info != NULL, FALSE);
702 if (match_info->matches < 0)
705 result = g_new (gchar *, match_info->matches + 1);
706 for (i = 0; i < match_info->matches; i++)
707 result[i] = g_match_info_fetch (match_info, i);
717 g_regex_error_quark (void)
719 static GQuark error_quark = 0;
721 if (error_quark == 0)
722 error_quark = g_quark_from_static_string ("g-regex-error-quark");
728 regex_ref (GRegex *regex)
730 g_atomic_int_inc ((gint*) ®ex->ref_count);
735 regex_unref (GRegex *regex)
737 if (g_atomic_int_exchange_and_add ((gint *) ®ex->ref_count, -1) - 1 == 0)
739 g_free (regex->pattern);
740 if (regex->pcre_re != NULL)
741 pcre_free (regex->pcre_re);
742 if (regex->extra != NULL)
743 pcre_free (regex->extra);
750 * @pattern: the regular expression
751 * @compile_options: compile options for the regular expression
752 * @match_options: match options for the regular expression
753 * @error: return location for a #GError
755 * Compiles the regular expression to an internal form, and does the initial
756 * setup of the #GRegex structure.
758 * Returns: a #GRegex structure
763 g_regex_new (const gchar *pattern,
764 GRegexCompileFlags compile_options,
765 GRegexMatchFlags match_options,
772 gboolean optimize = FALSE;
773 static gboolean initialized = FALSE;
775 g_return_val_if_fail (pattern != NULL, NULL);
776 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
777 g_return_val_if_fail ((compile_options & ~G_REGEX_COMPILE_MASK) == 0, NULL);
778 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
785 pcre_config (PCRE_CONFIG_UTF8, &support);
788 msg = N_("PCRE library is compiled without UTF8 support");
790 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
794 pcre_config (PCRE_CONFIG_UNICODE_PROPERTIES, &support);
797 msg = N_("PCRE library is compiled without UTF8 properties support");
799 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
806 /* G_REGEX_OPTIMIZE has the same numeric value of PCRE_NO_UTF8_CHECK,
807 * as we do not need to wrap PCRE_NO_UTF8_CHECK. */
808 if (compile_options & G_REGEX_OPTIMIZE)
811 /* In GRegex the string are, by default, UTF-8 encoded. PCRE
812 * instead uses UTF-8 only if required with PCRE_UTF8. */
813 if (compile_options & G_REGEX_RAW)
816 compile_options &= ~G_REGEX_RAW;
821 compile_options |= PCRE_UTF8 | PCRE_NO_UTF8_CHECK;
822 match_options |= PCRE_NO_UTF8_CHECK;
825 /* PCRE_NEWLINE_ANY is the default for the internal PCRE but
826 * not for the system one. */
827 if (!(compile_options & G_REGEX_NEWLINE_CR) &&
828 !(compile_options & G_REGEX_NEWLINE_LF))
830 compile_options |= PCRE_NEWLINE_ANY;
833 /* compile the pattern */
834 re = pcre_compile (pattern, compile_options, &errmsg, &erroffset, NULL);
836 /* if the compilation failed, set the error member and return
840 GError *tmp_error = g_error_new (G_REGEX_ERROR,
841 G_REGEX_ERROR_COMPILE,
842 _("Error while compiling regular "
843 "expression %s at char %d: %s"),
844 pattern, erroffset, errmsg);
845 g_propagate_error (error, tmp_error);
850 regex = g_new0 (GRegex, 1);
851 regex->ref_count = 1;
852 regex->pattern = g_strdup (pattern);
854 regex->compile_opts = compile_options;
855 regex->match_opts = match_options;
859 regex->extra = pcre_study (regex->pcre_re, 0, &errmsg);
862 GError *tmp_error = g_error_new (G_REGEX_ERROR,
863 G_REGEX_ERROR_OPTIMIZE,
864 _("Error while optimizing "
865 "regular expression %s: %s"),
868 g_propagate_error (error, tmp_error);
880 * Frees all the memory associated with the regex structure.
885 g_regex_free (GRegex *regex)
894 * g_regex_get_pattern:
895 * @regex: a #GRegex structure
897 * Gets the pattern string associated with @regex, i.e. a copy of
898 * the string passed to g_regex_new().
900 * Returns: the pattern of @regex
905 g_regex_get_pattern (const GRegex *regex)
907 g_return_val_if_fail (regex != NULL, NULL);
909 return regex->pattern;
913 * g_regex_get_max_backref:
916 * Returns the number of the highest back reference
917 * in the pattern, or 0 if the pattern does not contain
920 * Returns: the number of the highest back reference.
925 g_regex_get_max_backref (const GRegex *regex)
929 pcre_fullinfo (regex->pcre_re, regex->extra,
930 PCRE_INFO_BACKREFMAX, &value);
936 * g_regex_get_capture_count:
939 * Returns the number of capturing subpatterns in the pattern.
941 * Returns: the number of capturing subpatterns.
946 g_regex_get_capture_count (const GRegex *regex)
950 pcre_fullinfo (regex->pcre_re, regex->extra,
951 PCRE_INFO_CAPTURECOUNT, &value);
957 * g_regex_match_simple:
958 * @pattern: the regular expression
959 * @string: the string to scan for matches
960 * @compile_options: compile options for the regular expression
961 * @match_options: match options
963 * Scans for a match in @string for @pattern.
965 * This function is equivalent to g_regex_match() but it does not
966 * require to compile the pattern with g_regex_new(), avoiding some
967 * lines of code when you need just to do a match without extracting
968 * substrings, capture counts, and so on.
970 * If this function is to be called on the same @pattern more than
971 * once, it's more efficient to compile the pattern once with
972 * g_regex_new() and then use g_regex_match().
974 * Returns: %TRUE is the string matched, %FALSE otherwise
979 g_regex_match_simple (const gchar *pattern,
981 GRegexCompileFlags compile_options,
982 GRegexMatchFlags match_options)
987 regex = g_regex_new (pattern, compile_options, 0, NULL);
990 result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
991 g_regex_free (regex);
997 * @regex: a #GRegex structure from g_regex_new()
998 * @string: the string to scan for matches
999 * @match_options: match options
1000 * @match_info: pointer to location where to store the #GMatchInfo, or
1001 * %NULL if you do not need it
1003 * Scans for a match in string for the pattern in @regex. The @match_options
1004 * are combined with the match options specified when the @regex structure
1005 * was created, letting you have more flexibility in reusing #GRegex
1008 * A #GMatchInfo structure, used to get information on the match, is stored
1009 * in @match_info if not %NULL.
1011 * To retrieve all the non-overlapping matches of the pattern in string you
1012 * can use g_match_info_next().
1014 * <informalexample><programlisting>
1016 * print_uppercase_words (const gchar *string)
1018 * /* Print all uppercase-only words. */
1020 * GMatchInfo *match_info;
1022 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1023 * g_regex_match (regex, string, 0, &match_info);
1024 * while (g_match_info_matches (match_info))
1026 * gchar *word = g_match_info_fetch (match_info, 0);
1027 * g_print ("Found: %s\n", word);
1029 * g_match_info_next (match_info, NULL);
1031 * g_match_info_free (match_info);
1032 * g_regex_free (regex);
1034 * </programlisting></informalexample>
1036 * Returns: %TRUE is the string matched, %FALSE otherwise
1041 g_regex_match (const GRegex *regex,
1042 const gchar *string,
1043 GRegexMatchFlags match_options,
1044 GMatchInfo **match_info)
1046 return g_regex_match_full (regex, string, -1, 0, match_options,
1051 * g_regex_match_full:
1052 * @regex: a #GRegex structure from g_regex_new()
1053 * @string: the string to scan for matches
1054 * @string_len: the length of @string, or -1 if @string is nul-terminated
1055 * @start_position: starting index of the string to match
1056 * @match_options: match options
1057 * @match_info: pointer to location where to store the #GMatchInfo, or
1058 * %NULL if you do not need it
1059 * @error: location to store the error occuring, or %NULL to ignore errors
1061 * Scans for a match in string for the pattern in @regex. The @match_options
1062 * are combined with the match options specified when the @regex structure
1063 * was created, letting you have more flexibility in reusing #GRegex
1066 * Setting @start_position differs from just passing over a shortened string
1067 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1068 * with any kind of lookbehind assertion, such as "\b".
1070 * A #GMatchInfo structure, used to get information on the match, is stored
1071 * in @match_info if not %NULL.
1073 * To retrieve all the non-overlapping matches of the pattern in string you
1074 * can use g_match_info_next().
1076 * <informalexample><programlisting>
1078 * print_uppercase_words (const gchar *string)
1080 * /* Print all uppercase-only words. */
1082 * GMatchInfo *match_info;
1083 * GError *error = NULL;
1085 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1086 * g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
1087 * while (g_match_info_matches (match_info))
1089 * gchar *word = g_match_info_fetch (match_info, 0);
1090 * g_print ("Found: %s\n", word);
1092 * g_match_info_next (match_info, &error);
1094 * g_match_info_free (match_info);
1095 * g_regex_free (regex);
1096 * if (error != NULL)
1098 * g_printerr ("Error while matching: %s\n", error->message);
1099 * g_error_free (error);
1102 * </programlisting></informalexample>
1104 * Returns: %TRUE is the string matched, %FALSE otherwise
1109 g_regex_match_full (const GRegex *regex,
1110 const gchar *string,
1112 gint start_position,
1113 GRegexMatchFlags match_options,
1114 GMatchInfo **match_info,
1120 g_return_val_if_fail (regex != NULL, FALSE);
1121 g_return_val_if_fail (string != NULL, FALSE);
1122 g_return_val_if_fail (start_position >= 0, FALSE);
1123 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1124 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1126 info = match_info_new (regex, string, string_len, start_position,
1127 match_options, FALSE);
1128 match_ok = g_match_info_next (info, error);
1129 if (match_info != NULL)
1132 g_match_info_free (info);
1138 * g_regex_match_all:
1139 * @regex: a #GRegex structure from g_regex_new()
1140 * @string: the string to scan for matches
1141 * @match_options: match options
1142 * @match_info: pointer to location where to store the #GMatchInfo, or
1143 * %NULL if you do not need it
1145 * Using the standard algorithm for regular expression matching only the
1146 * longest match in the string is retrieved. This function uses a
1147 * different algorithm so it can retrieve all the possible matches.
1148 * For more documentation see g_regex_match_all_full().
1150 * A #GMatchInfo structure, used to get information on the match, is stored
1151 * in @match_info if not %NULL.
1153 * Returns: %TRUE is the string matched, %FALSE otherwise
1158 g_regex_match_all (const GRegex *regex,
1159 const gchar *string,
1160 GRegexMatchFlags match_options,
1161 GMatchInfo **match_info)
1163 return g_regex_match_all_full (regex, string, -1, 0, match_options,
1168 * g_regex_match_all_full:
1169 * @regex: a #GRegex structure from g_regex_new()
1170 * @string: the string to scan for matches
1171 * @string_len: the length of @string, or -1 if @string is nul-terminated
1172 * @start_position: starting index of the string to match
1173 * @match_options: match options
1174 * @match_info: pointer to location where to store the #GMatchInfo, or
1175 * %NULL if you do not need it
1176 * @error: location to store the error occuring, or %NULL to ignore errors
1178 * Using the standard algorithm for regular expression matching only the
1179 * longest match in the string is retrieved, it is not possibile to obtain
1180 * all the available matches. For instance matching
1181 * "<a> <b> <c>" against the pattern "<.*>" you get
1182 * "<a> <b> <c>".
1184 * This function uses a different algorithm (called DFA, i.e. deterministic
1185 * finite automaton), so it can retrieve all the possible matches, all
1186 * starting at the same point in the string. For instance matching
1187 * "<a> <b> <c>" against the pattern "<.*>" you
1188 * would obtain three matches: "<a> <b> <c>",
1189 * "<a> <b>" and "<a>".
1191 * The number of matched strings is retrieved using
1192 * g_match_info_get_match_count().
1193 * To obtain the matched strings and their position you can use,
1194 * respectively, g_match_info_fetch() and g_match_info_fetch_pos(). Note that
1195 * the strings are returned in reverse order of length; that is, the longest
1196 * matching string is given first.
1198 * Note that the DFA algorithm is slower than the standard one and it is not
1199 * able to capture substrings, so backreferences do not work.
1201 * Setting @start_position differs from just passing over a shortened string
1202 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1203 * with any kind of lookbehind assertion, such as "\b".
1205 * A #GMatchInfo structure, used to get information on the match, is stored
1206 * in @match_info if not %NULL.
1208 * Returns: %TRUE is the string matched, %FALSE otherwise
1213 g_regex_match_all_full (const GRegex *regex,
1214 const gchar *string,
1216 gint start_position,
1217 GRegexMatchFlags match_options,
1218 GMatchInfo **match_info,
1224 g_return_val_if_fail (regex != NULL, FALSE);
1225 g_return_val_if_fail (string != NULL, FALSE);
1226 g_return_val_if_fail (start_position >= 0, FALSE);
1227 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1228 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1230 info = match_info_new (regex, string, string_len, start_position,
1231 match_options, TRUE);
1237 info->matches = pcre_dfa_exec (regex->pcre_re, regex->extra,
1238 info->string, info->string_len,
1240 regex->match_opts | match_options,
1241 info->offsets, info->n_offsets,
1242 info->workspace, info->n_workspace);
1243 if (info->matches == PCRE_ERROR_DFA_WSSIZE)
1245 /* info->workspace is too small. */
1246 info->n_workspace *= 2;
1247 info->workspace = g_realloc (info->workspace,
1248 info->n_workspace * sizeof (gint));
1251 else if (info->matches == 0)
1253 /* info->offsets is too small. */
1254 info->n_offsets *= 2;
1255 info->offsets = g_realloc (info->offsets,
1256 info->n_offsets * sizeof (gint));
1259 else if (IS_PCRE_ERROR (info->matches))
1261 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
1262 _("Error while matching regular expression %s: %s"),
1263 regex->pattern, match_error (info->matches));
1267 /* set info->pos to -1 so that a call to g_match_info_next() fails. */
1270 if (match_info != NULL)
1273 g_match_info_free (info);
1275 return info->matches >= 0;
1279 * g_regex_get_string_number:
1280 * @regex: #GRegex structure
1281 * @name: name of the subexpression
1283 * Retrieves the number of the subexpression named @name.
1285 * Returns: The number of the subexpression or -1 if @name does not exists
1290 g_regex_get_string_number (const GRegex *regex,
1295 g_return_val_if_fail (regex != NULL, -1);
1296 g_return_val_if_fail (name != NULL, -1);
1298 num = pcre_get_stringnumber (regex->pcre_re, name);
1299 if (num == PCRE_ERROR_NOSUBSTRING)
1306 * g_regex_split_simple:
1307 * @pattern: the regular expression
1308 * @string: the string to scan for matches
1309 * @compile_options: compile options for the regular expression
1310 * @match_options: match options
1312 * Breaks the string on the pattern, and returns an array of the tokens.
1313 * If the pattern contains capturing parentheses, then the text for each
1314 * of the substrings will also be returned. If the pattern does not match
1315 * anywhere in the string, then the whole string is returned as the first
1318 * This function is equivalent to g_regex_split() but it does not
1319 * require to compile the pattern with g_regex_new(), avoiding some
1320 * lines of code when you need just to do a split without extracting
1321 * substrings, capture counts, and so on.
1323 * If this function is to be called on the same @pattern more than
1324 * once, it's more efficient to compile the pattern once with
1325 * g_regex_new() and then use g_regex_split().
1327 * As a special case, the result of splitting the empty string "" is an
1328 * empty vector, not a vector containing a single string. The reason for
1329 * this special case is that being able to represent a empty vector is
1330 * typically more useful than consistent handling of empty elements. If
1331 * you do need to represent empty elements, you'll need to check for the
1332 * empty string before calling this function.
1334 * A pattern that can match empty strings splits @string into separate
1335 * characters wherever it matches the empty string between characters.
1336 * For example splitting "ab c" using as a separator "\s*", you will get
1339 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1344 g_regex_split_simple (const gchar *pattern,
1345 const gchar *string,
1346 GRegexCompileFlags compile_options,
1347 GRegexMatchFlags match_options)
1352 regex = g_regex_new (pattern, compile_options, 0, NULL);
1355 result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
1356 g_regex_free (regex);
1362 * @regex: a #GRegex structure
1363 * @string: the string to split with the pattern
1364 * @match_options: match time option flags
1366 * Breaks the string on the pattern, and returns an array of the tokens.
1367 * If the pattern contains capturing parentheses, then the text for each
1368 * of the substrings will also be returned. If the pattern does not match
1369 * anywhere in the string, then the whole string is returned as the first
1372 * As a special case, the result of splitting the empty string "" is an
1373 * empty vector, not a vector containing a single string. The reason for
1374 * this special case is that being able to represent a empty vector is
1375 * typically more useful than consistent handling of empty elements. If
1376 * you do need to represent empty elements, you'll need to check for the
1377 * empty string before calling this function.
1379 * A pattern that can match empty strings splits @string into separate
1380 * characters wherever it matches the empty string between characters.
1381 * For example splitting "ab c" using as a separator "\s*", you will get
1384 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1389 g_regex_split (const GRegex *regex,
1390 const gchar *string,
1391 GRegexMatchFlags match_options)
1393 return g_regex_split_full (regex, string, -1, 0,
1394 match_options, 0, NULL);
1398 * g_regex_split_full:
1399 * @regex: a #GRegex structure
1400 * @string: the string to split with the pattern
1401 * @string_len: the length of @string, or -1 if @string is nul-terminated
1402 * @start_position: starting index of the string to match
1403 * @match_options: match time option flags
1404 * @max_tokens: the maximum number of tokens to split @string into. If this
1405 * is less than 1, the string is split completely
1406 * @error: return location for a #GError
1408 * Breaks the string on the pattern, and returns an array of the tokens.
1409 * If the pattern contains capturing parentheses, then the text for each
1410 * of the substrings will also be returned. If the pattern does not match
1411 * anywhere in the string, then the whole string is returned as the first
1414 * As a special case, the result of splitting the empty string "" is an
1415 * empty vector, not a vector containing a single string. The reason for
1416 * this special case is that being able to represent a empty vector is
1417 * typically more useful than consistent handling of empty elements. If
1418 * you do need to represent empty elements, you'll need to check for the
1419 * empty string before calling this function.
1421 * A pattern that can match empty strings splits @string into separate
1422 * characters wherever it matches the empty string between characters.
1423 * For example splitting "ab c" using as a separator "\s*", you will get
1426 * Setting @start_position differs from just passing over a shortened string
1427 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1428 * with any kind of lookbehind assertion, such as "\b".
1430 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1435 g_regex_split_full (const GRegex *regex,
1436 const gchar *string,
1438 gint start_position,
1439 GRegexMatchFlags match_options,
1443 GError *tmp_error = NULL;
1444 GMatchInfo *match_info;
1449 /* position of the last separator. */
1450 gint last_separator_end;
1451 /* was the last match 0 bytes long? */
1452 gboolean last_match_is_empty;
1453 /* the returned array of char **s */
1454 gchar **string_list;
1456 g_return_val_if_fail (regex != NULL, NULL);
1457 g_return_val_if_fail (string != NULL, NULL);
1458 g_return_val_if_fail (start_position >= 0, NULL);
1459 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1460 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1462 if (max_tokens <= 0)
1463 max_tokens = G_MAXINT;
1466 string_len = strlen (string);
1468 /* zero-length string */
1469 if (string_len - start_position == 0)
1470 return g_new0 (gchar *, 1);
1472 if (max_tokens == 1)
1474 string_list = g_new0 (gchar *, 2);
1475 string_list[0] = g_strndup (&string[start_position],
1476 string_len - start_position);
1482 last_separator_end = start_position;
1483 last_match_is_empty = FALSE;
1485 match_ok = g_regex_match_full (regex, string, string_len, start_position,
1486 match_options, &match_info, &tmp_error);
1487 while (tmp_error == NULL)
1491 last_match_is_empty =
1492 (match_info->offsets[0] == match_info->offsets[1]);
1494 /* we need to skip empty separators at the same position of the end
1495 * of another separator. e.g. the string is "a b" and the separator
1496 * is " *", so from 1 to 2 we have a match and at position 2 we have
1497 * an empty match. */
1498 if (last_separator_end != match_info->offsets[1])
1503 token = g_strndup (string + last_separator_end,
1504 match_info->offsets[0] - last_separator_end);
1505 list = g_list_prepend (list, token);
1508 /* if there were substrings, these need to be added to
1510 match_count = g_match_info_get_match_count (match_info);
1511 if (match_count > 1)
1513 for (i = 1; i < match_count; i++)
1514 list = g_list_prepend (list, g_match_info_fetch (match_info, i));
1520 /* if there was no match, copy to end of string. */
1521 if (!last_match_is_empty)
1523 gchar *token = g_strndup (string + last_separator_end,
1524 match_info->string_len - last_separator_end);
1525 list = g_list_prepend (list, token);
1527 /* no more tokens, end the loop. */
1531 /* -1 to leave room for the last part. */
1532 if (token_count >= max_tokens - 1)
1534 /* we have reached the maximum number of tokens, so we copy
1535 * the remaining part of the string. */
1536 if (last_match_is_empty)
1538 /* the last match was empty, so we have moved one char
1539 * after the real position to avoid empty matches at the
1541 match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
1543 /* the if is needed in the case we have terminated the available
1544 * tokens, but we are at the end of the string, so there are no
1545 * characters left to copy. */
1546 if (string_len > match_info->pos)
1548 gchar *token = g_strndup (string + match_info->pos,
1549 string_len - match_info->pos);
1550 list = g_list_prepend (list, token);
1556 last_separator_end = match_info->pos;
1557 if (last_match_is_empty)
1558 /* if the last match was empty, g_match_info_next() has moved
1559 * forward to avoid infinite loops, but we still need to copy that
1561 last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
1563 match_ok = g_match_info_next (match_info, &tmp_error);
1565 g_match_info_free (match_info);
1566 if (tmp_error != NULL)
1568 g_propagate_error (error, tmp_error);
1569 g_list_foreach (list, (GFunc)g_free, NULL);
1571 match_info->pos = -1;
1575 string_list = g_new (gchar *, g_list_length (list) + 1);
1577 for (last = g_list_last (list); last; last = g_list_previous (last))
1578 string_list[i++] = last->data;
1588 REPL_TYPE_CHARACTER,
1589 REPL_TYPE_SYMBOLIC_REFERENCE,
1590 REPL_TYPE_NUMERIC_REFERENCE,
1591 REPL_TYPE_CHANGE_CASE
1596 CHANGE_CASE_NONE = 1 << 0,
1597 CHANGE_CASE_UPPER = 1 << 1,
1598 CHANGE_CASE_LOWER = 1 << 2,
1599 CHANGE_CASE_UPPER_SINGLE = 1 << 3,
1600 CHANGE_CASE_LOWER_SINGLE = 1 << 4,
1601 CHANGE_CASE_SINGLE_MASK = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
1602 CHANGE_CASE_LOWER_MASK = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
1603 CHANGE_CASE_UPPER_MASK = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
1606 struct _InterpolationData
1612 ChangeCase change_case;
1616 free_interpolation_data (InterpolationData *data)
1618 g_free (data->text);
1622 static const gchar *
1623 expand_escape (const gchar *replacement,
1625 InterpolationData *data,
1630 const gchar *error_detail;
1632 GError *tmp_error = NULL;
1640 data->type = REPL_TYPE_CHARACTER;
1645 data->type = REPL_TYPE_CHARACTER;
1650 data->type = REPL_TYPE_CHARACTER;
1655 data->type = REPL_TYPE_CHARACTER;
1660 data->type = REPL_TYPE_CHARACTER;
1665 data->type = REPL_TYPE_CHARACTER;
1670 data->type = REPL_TYPE_CHARACTER;
1675 data->type = REPL_TYPE_CHARACTER;
1685 h = g_ascii_xdigit_value (*p);
1688 error_detail = _("hexadecimal digit or '}' expected");
1699 for (i = 0; i < 2; i++)
1701 h = g_ascii_xdigit_value (*p);
1704 error_detail = _("hexadecimal digit expected");
1711 data->type = REPL_TYPE_STRING;
1712 data->text = g_new0 (gchar, 8);
1713 g_unichar_to_utf8 (x, data->text);
1717 data->type = REPL_TYPE_CHANGE_CASE;
1718 data->change_case = CHANGE_CASE_LOWER_SINGLE;
1722 data->type = REPL_TYPE_CHANGE_CASE;
1723 data->change_case = CHANGE_CASE_UPPER_SINGLE;
1727 data->type = REPL_TYPE_CHANGE_CASE;
1728 data->change_case = CHANGE_CASE_LOWER;
1732 data->type = REPL_TYPE_CHANGE_CASE;
1733 data->change_case = CHANGE_CASE_UPPER;
1737 data->type = REPL_TYPE_CHANGE_CASE;
1738 data->change_case = CHANGE_CASE_NONE;
1744 error_detail = _("missing '<' in symbolic reference");
1753 error_detail = _("unfinished symbolic reference");
1760 error_detail = _("zero-length symbolic reference");
1763 if (g_ascii_isdigit (*q))
1768 h = g_ascii_digit_value (*q);
1771 error_detail = _("digit expected");
1780 data->type = REPL_TYPE_NUMERIC_REFERENCE;
1787 if (!g_ascii_isalnum (*r))
1789 error_detail = _("illegal symbolic reference");
1796 data->text = g_strndup (q, p - q);
1797 data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
1802 /* if \0 is followed by a number is an octal number representing a
1803 * character, else it is a numeric reference. */
1804 if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
1807 p = g_utf8_next_char (p);
1820 for (i = 0; i < 3; i++)
1822 h = g_ascii_digit_value (*p);
1832 if (i == 2 && base == 10)
1838 if (base == 8 || i == 3)
1840 data->type = REPL_TYPE_STRING;
1841 data->text = g_new0 (gchar, 8);
1842 g_unichar_to_utf8 (x, data->text);
1846 data->type = REPL_TYPE_NUMERIC_REFERENCE;
1851 error_detail = _("stray final '\\'");
1855 error_detail = _("unknown escape sequence");
1862 /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
1863 tmp_error = g_error_new (G_REGEX_ERROR,
1864 G_REGEX_ERROR_REPLACE,
1865 _("Error while parsing replacement "
1866 "text \"%s\" at char %lu: %s"),
1868 (gulong)(p - replacement),
1870 g_propagate_error (error, tmp_error);
1876 split_replacement (const gchar *replacement,
1880 InterpolationData *data;
1881 const gchar *p, *start;
1883 start = p = replacement;
1888 data = g_new0 (InterpolationData, 1);
1889 start = p = expand_escape (replacement, p, data, error);
1892 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
1894 free_interpolation_data (data);
1898 list = g_list_prepend (list, data);
1903 if (*p == '\\' || *p == '\0')
1907 data = g_new0 (InterpolationData, 1);
1908 data->text = g_strndup (start, p - start);
1909 data->type = REPL_TYPE_STRING;
1910 list = g_list_prepend (list, data);
1916 return g_list_reverse (list);
1919 /* Change the case of c based on change_case. */
1920 #define CHANGE_CASE(c, change_case) \
1921 (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
1922 g_unichar_tolower (c) : \
1923 g_unichar_toupper (c))
1926 string_append (GString *string,
1928 ChangeCase *change_case)
1932 if (text[0] == '\0')
1935 if (*change_case == CHANGE_CASE_NONE)
1937 g_string_append (string, text);
1939 else if (*change_case & CHANGE_CASE_SINGLE_MASK)
1941 c = g_utf8_get_char (text);
1942 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
1943 g_string_append (string, g_utf8_next_char (text));
1944 *change_case = CHANGE_CASE_NONE;
1948 while (*text != '\0')
1950 c = g_utf8_get_char (text);
1951 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
1952 text = g_utf8_next_char (text);
1958 interpolate_replacement (const GRegex *regex,
1959 const GMatchInfo *match_info,
1960 const gchar *string,
1965 InterpolationData *idata;
1967 ChangeCase change_case = CHANGE_CASE_NONE;
1969 for (list = data; list; list = list->next)
1972 switch (idata->type)
1974 case REPL_TYPE_STRING:
1975 string_append (result, idata->text, &change_case);
1977 case REPL_TYPE_CHARACTER:
1978 g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
1979 if (change_case & CHANGE_CASE_SINGLE_MASK)
1980 change_case = CHANGE_CASE_NONE;
1982 case REPL_TYPE_NUMERIC_REFERENCE:
1983 match = g_match_info_fetch (match_info, idata->num);
1986 string_append (result, match, &change_case);
1990 case REPL_TYPE_SYMBOLIC_REFERENCE:
1991 match = g_match_info_fetch_named (match_info, idata->text);
1994 string_append (result, match, &change_case);
1998 case REPL_TYPE_CHANGE_CASE:
1999 change_case = idata->change_case;
2009 * @regex: a #GRegex structure
2010 * @string: the string to perform matches against
2011 * @string_len: the length of @string, or -1 if @string is nul-terminated
2012 * @start_position: starting index of the string to match
2013 * @replacement: text to replace each match with
2014 * @match_options: options for the match
2015 * @error: location to store the error occuring, or %NULL to ignore errors
2017 * Replaces all occurances of the pattern in @regex with the
2018 * replacement text. Backreferences of the form '\number' or '\g<number>'
2019 * in the replacement text are interpolated by the number-th captured
2020 * subexpression of the match, '\g<name>' refers to the captured subexpression
2021 * with the given name. '\0' refers to the complete match, but '\0' followed
2022 * by a number is the octal representation of a character. To include a
2023 * literal '\' in the replacement, write '\\'.
2024 * There are also escapes that changes the case of the following text:
2027 * <varlistentry><term>\l</term>
2029 * <para>Convert to lower case the next character</para>
2032 * <varlistentry><term>\u</term>
2034 * <para>Convert to upper case the next character</para>
2037 * <varlistentry><term>\L</term>
2039 * <para>Convert to lower case till \E</para>
2042 * <varlistentry><term>\U</term>
2044 * <para>Convert to upper case till \E</para>
2047 * <varlistentry><term>\E</term>
2049 * <para>End case modification</para>
2054 * If you do not need to use backreferences use g_regex_replace_literal().
2056 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was
2057 * passed to g_regex_new(). If you want to use not UTF-8 encoded stings
2058 * you can use g_regex_replace_literal().
2060 * Setting @start_position differs from just passing over a shortened string
2061 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2062 * with any kind of lookbehind assertion, such as "\b".
2064 * Returns: a newly allocated string containing the replacements
2069 g_regex_replace (const GRegex *regex,
2070 const gchar *string,
2072 gint start_position,
2073 const gchar *replacement,
2074 GRegexMatchFlags match_options,
2079 GError *tmp_error = NULL;
2081 g_return_val_if_fail (regex != NULL, NULL);
2082 g_return_val_if_fail (string != NULL, NULL);
2083 g_return_val_if_fail (start_position >= 0, NULL);
2084 g_return_val_if_fail (replacement != NULL, NULL);
2085 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2086 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2088 list = split_replacement (replacement, &tmp_error);
2089 if (tmp_error != NULL)
2091 g_propagate_error (error, tmp_error);
2095 result = g_regex_replace_eval (regex,
2096 string, string_len, start_position,
2098 interpolate_replacement,
2101 if (tmp_error != NULL)
2102 g_propagate_error (error, tmp_error);
2104 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
2111 literal_replacement (const GRegex *regex,
2112 const GMatchInfo *match_info,
2113 const gchar *string,
2117 g_string_append (result, data);
2122 * g_regex_replace_literal:
2123 * @regex: a #GRegex structure
2124 * @string: the string to perform matches against
2125 * @string_len: the length of @string, or -1 if @string is nul-terminated
2126 * @start_position: starting index of the string to match
2127 * @replacement: text to replace each match with
2128 * @match_options: options for the match
2129 * @error: location to store the error occuring, or %NULL to ignore errors
2131 * Replaces all occurances of the pattern in @regex with the
2132 * replacement text. @replacement is replaced literally, to
2133 * include backreferences use g_regex_replace().
2135 * Setting @start_position differs from just passing over a shortened string
2136 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2137 * with any kind of lookbehind assertion, such as "\b".
2139 * Returns: a newly allocated string containing the replacements
2144 g_regex_replace_literal (const GRegex *regex,
2145 const gchar *string,
2147 gint start_position,
2148 const gchar *replacement,
2149 GRegexMatchFlags match_options,
2152 g_return_val_if_fail (replacement != NULL, NULL);
2153 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2155 return g_regex_replace_eval (regex,
2156 string, string_len, start_position,
2158 literal_replacement,
2159 (gpointer)replacement,
2164 * g_regex_replace_eval:
2165 * @regex: a #GRegex structure from g_regex_new()
2166 * @string: string to perform matches against
2167 * @string_len: the length of @string, or -1 if @string is nul-terminated
2168 * @start_position: starting index of the string to match
2169 * @match_options: options for the match
2170 * @eval: a function to call for each match
2171 * @user_data: user data to pass to the function
2172 * @error: location to store the error occuring, or %NULL to ignore errors
2174 * Replaces occurances of the pattern in regex with the output of @eval
2175 * for that occurance.
2177 * Setting @start_position differs from just passing over a shortened string
2178 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2179 * with any kind of lookbehind assertion, such as "\b".
2181 * Returns: a newly allocated string containing the replacements
2186 g_regex_replace_eval (const GRegex *regex,
2187 const gchar *string,
2189 gint start_position,
2190 GRegexMatchFlags match_options,
2191 GRegexEvalCallback eval,
2195 GMatchInfo *match_info;
2198 gboolean done = FALSE;
2199 GError *tmp_error = NULL;
2201 g_return_val_if_fail (regex != NULL, NULL);
2202 g_return_val_if_fail (string != NULL, NULL);
2203 g_return_val_if_fail (start_position >= 0, NULL);
2204 g_return_val_if_fail (eval != NULL, NULL);
2205 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2208 string_len = strlen (string);
2210 result = g_string_sized_new (string_len);
2212 /* run down the string making matches. */
2213 g_regex_match_full (regex, string, string_len, start_position,
2214 match_options, &match_info, &tmp_error);
2215 while (!done && g_match_info_matches (match_info))
2217 g_string_append_len (result,
2219 match_info->offsets[0] - str_pos);
2220 done = (*eval) (regex, match_info, string, result, user_data);
2221 str_pos = match_info->offsets[1];
2222 g_match_info_next (match_info, &tmp_error);
2224 g_match_info_free (match_info);
2225 if (tmp_error != NULL)
2227 g_propagate_error (error, tmp_error);
2228 g_string_free (result, TRUE);
2232 g_string_append_len (result, string + str_pos, string_len - str_pos);
2233 return g_string_free (result, FALSE);
2237 * g_regex_escape_string:
2238 * @string: the string to escape
2239 * @length: the length of @string, or -1 if @string is nul-terminated
2241 * Escapes the special characters used for regular expressions in @string,
2242 * for instance "a.b*c" becomes "a\.b\*c". This function is useful to
2243 * dynamically generate regular expressions.
2245 * @string can contain nul characters that are replaced with "\0", in this
2246 * case remember to specify the correct length of @string in @length.
2248 * Returns: a newly-allocated escaped string
2253 g_regex_escape_string (const gchar *string,
2257 const char *p, *piece_start, *end;
2259 g_return_val_if_fail (string != NULL, NULL);
2262 length = strlen (string);
2264 end = string + length;
2265 p = piece_start = string;
2266 escaped = g_string_sized_new (length + 1);
2287 if (p != piece_start)
2288 /* copy the previous piece. */
2289 g_string_append_len (escaped, piece_start, p - piece_start);
2290 g_string_append_c (escaped, '\\');
2292 g_string_append_c (escaped, '0');
2294 g_string_append_c (escaped, *p);
2298 p = g_utf8_next_char (p);
2303 if (piece_start < end)
2304 g_string_append_len (escaped, piece_start, end - piece_start);
2306 return g_string_free (escaped, FALSE);
2309 #define __G_REGEX_C__
2310 #include "galiasdef.c"