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 | \
49 G_REGEX_NEWLINE_CR | \
50 G_REGEX_NEWLINE_LF | \
53 /* Mask of all the possible values for GRegexMatchFlags. */
54 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_ANCHORED | \
55 G_REGEX_MATCH_NOTBOL | \
56 G_REGEX_MATCH_NOTEOL | \
57 G_REGEX_MATCH_NOTEMPTY | \
58 G_REGEX_MATCH_PARTIAL | \
59 G_REGEX_MATCH_NEWLINE_CR | \
60 G_REGEX_MATCH_NEWLINE_LF | \
61 G_REGEX_MATCH_NEWLINE_CRLF | \
62 G_REGEX_MATCH_NEWLINE_ANY)
64 /* if the string is in UTF-8 use g_utf8_ functions, else use
66 #define NEXT_CHAR(re, s) (((re)->pattern->compile_opts & PCRE_UTF8) ? \
67 g_utf8_next_char (s) : \
69 #define PREV_CHAR(re, s) (((re)->pattern->compile_opts & PCRE_UTF8) ? \
70 g_utf8_prev_char (s) : \
73 #define WORKSPACE_INITIAL 1000
74 #define OFFSETS_DFA_MIN_SIZE 21
76 /* atomically returns the pcre_extra struct in the regex. */
77 #define REGEX_GET_EXTRA(re) ((pcre_extra *)g_atomic_pointer_get (&(re)->pattern->extra))
79 /* this struct can be shared by more regexes */
82 volatile guint ref_count; /* the ref count for the immutable part */
83 gchar *pattern; /* the pattern */
84 pcre *pcre_re; /* compiled form of the pattern */
85 GRegexCompileFlags compile_opts; /* options used at compile time on the pattern */
86 GRegexMatchFlags match_opts; /* options used at match time on the regex */
87 pcre_extra *extra; /* data stored when g_regex_optimize() is used */
90 /* this struct is used only by a single regex */
93 gint matches; /* number of matching sub patterns */
94 gint pos; /* position in the string where last match left off */
95 gint *offsets; /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
96 gint n_offsets; /* number of offsets */
97 gint *workspace; /* workspace for pcre_dfa_exec() */
98 gint n_workspace; /* number of workspace elements */
99 gssize string_len; /* length of the string last used against */
100 GSList *delims; /* delimiter sub strings from split next */
101 gint last_separator_end; /* position of the last separator for split_next_full() */
102 gboolean last_match_is_empty; /* was the last match in split_next_full() 0 bytes long? */
107 GRegexPattern *pattern; /* immutable part, shared */
108 GRegexMatch *match; /* mutable part, not shared */
111 /* TRUE if ret is an error code, FALSE otherwise. */
112 #define IS_PCRE_ERROR(ret) ((ret) < PCRE_ERROR_NOMATCH && (ret) != PCRE_ERROR_PARTIAL)
115 match_error (gint errcode)
119 case PCRE_ERROR_NOMATCH:
122 case PCRE_ERROR_NULL:
123 /* NULL argument, this should not happen in GRegex */
124 g_warning ("A NULL argument was passed to PCRE");
126 case PCRE_ERROR_BADOPTION:
127 return "bad options";
128 case PCRE_ERROR_BADMAGIC:
129 return _("corrupted object");
130 case PCRE_ERROR_UNKNOWN_OPCODE:
131 return N_("internal error or corrupted object");
132 case PCRE_ERROR_NOMEMORY:
133 return _("out of memory");
134 case PCRE_ERROR_NOSUBSTRING:
135 /* not used by pcre_exec() */
137 case PCRE_ERROR_MATCHLIMIT:
138 return _("backtracking limit reached");
139 case PCRE_ERROR_CALLOUT:
140 /* callouts are not implemented */
142 case PCRE_ERROR_BADUTF8:
143 case PCRE_ERROR_BADUTF8_OFFSET:
144 /* we do not check if strings are valid */
146 case PCRE_ERROR_PARTIAL:
149 case PCRE_ERROR_BADPARTIAL:
150 return _("the pattern contains items not supported for partial matching");
151 case PCRE_ERROR_INTERNAL:
152 return _("internal error");
153 case PCRE_ERROR_BADCOUNT:
154 /* negative ovecsize, this should not happen in GRegex */
155 g_warning ("A negative ovecsize was passed to PCRE");
157 case PCRE_ERROR_DFA_UITEM:
158 return _("the pattern contains items not supported for partial matching");
159 case PCRE_ERROR_DFA_UCOND:
160 return _("back references as conditions are not supported for partial matching");
161 case PCRE_ERROR_DFA_UMLIMIT:
162 /* the match_field field is not udes in GRegex */
164 case PCRE_ERROR_DFA_WSSIZE:
165 /* handled expanding the workspace */
167 case PCRE_ERROR_DFA_RECURSE:
168 case PCRE_ERROR_RECURSIONLIMIT:
169 return _("recursion limit reached");
170 case PCRE_ERROR_NULLWSLIMIT:
171 return _("workspace limit for empty substrings reached");
172 case PCRE_ERROR_BADNEWLINE:
173 return _("invalid combination of newline flags");
177 return _("unknown error");
181 g_regex_error_quark (void)
183 static GQuark error_quark = 0;
185 if (error_quark == 0)
186 error_quark = g_quark_from_static_string ("g-regex-error-quark");
191 static GRegexPattern *
192 regex_pattern_new (pcre *re,
193 const gchar *pattern,
194 GRegexCompileFlags compile_options,
195 GRegexMatchFlags match_options)
197 GRegexPattern *rp = g_new0 (GRegexPattern, 1);
200 rp->pattern = g_strdup (pattern);
201 rp->compile_opts = compile_options;
202 rp->match_opts = match_options;
206 static GRegexPattern *
207 regex_pattern_ref (GRegexPattern *rp)
209 /* increases the ref count of the immutable part of the GRegex */
210 g_atomic_int_inc ((gint*) &rp->ref_count);
215 regex_pattern_unref (GRegexPattern *rp)
217 /* decreases the ref count of the immutable part of the GRegex
218 * and deletes it if the ref count went to 0 */
219 if (g_atomic_int_exchange_and_add ((gint *) &rp->ref_count, -1) - 1 == 0)
221 g_free (rp->pattern);
222 if (rp->pcre_re != NULL)
223 pcre_free (rp->pcre_re);
224 if (rp->extra != NULL)
225 pcre_free (rp->extra);
231 regex_match_free (GRegexMatch *rm)
236 g_slist_foreach (rm->delims, (GFunc) g_free, NULL);
237 g_slist_free (rm->delims);
238 g_free (rm->offsets);
239 g_free (rm->workspace);
244 regex_lazy_init_match (GRegex *regex,
249 if (regex->match != NULL)
252 pcre_fullinfo (regex->pattern->pcre_re,
253 REGEX_GET_EXTRA (regex),
254 PCRE_INFO_CAPTURECOUNT, &n_offsets);
255 n_offsets = (MAX (n_offsets, min_offsets) + 1) * 3;
257 regex->match = g_new0 (GRegexMatch, 1);
258 regex->match->string_len = -1;
259 regex->match->matches = -1000;
260 regex->match->n_offsets = n_offsets;
261 regex->match->offsets = g_new0 (gint, n_offsets);
266 * @pattern: the regular expression
267 * @compile_options: compile options for the regular expression
268 * @match_options: match options for the regular expression
269 * @error: return location for a #GError
271 * Compiles the regular expression to an internal form, and does the initial
272 * setup of the #GRegex structure.
274 * Returns: a #GRegex structure
279 g_regex_new (const gchar *pattern,
280 GRegexCompileFlags compile_options,
281 GRegexMatchFlags match_options,
287 static gboolean initialized = FALSE;
289 g_return_val_if_fail (pattern != NULL, NULL);
290 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
291 g_return_val_if_fail ((compile_options & ~G_REGEX_COMPILE_MASK) == 0, NULL);
292 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
299 pcre_config (PCRE_CONFIG_UTF8, &support);
302 msg = N_("PCRE library is compiled without UTF8 support");
304 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
308 pcre_config (PCRE_CONFIG_UNICODE_PROPERTIES, &support);
311 msg = N_("PCRE library is compiled without UTF8 properties support");
313 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
320 /* In GRegex the string are, by default, UTF-8 encoded. PCRE
321 * instead uses UTF-8 only if required with PCRE_UTF8. */
322 if (compile_options & G_REGEX_RAW)
325 compile_options &= ~G_REGEX_RAW;
330 compile_options |= PCRE_UTF8 | PCRE_NO_UTF8_CHECK;
331 match_options |= PCRE_NO_UTF8_CHECK;
334 /* compile the pattern */
335 re = pcre_compile (pattern, compile_options, &errmsg, &erroffset, NULL);
337 /* if the compilation failed, set the error member and return
341 GError *tmp_error = g_error_new (G_REGEX_ERROR,
342 G_REGEX_ERROR_COMPILE,
343 _("Error while compiling regular "
344 "expression %s at char %d: %s"),
345 pattern, erroffset, errmsg);
346 g_propagate_error (error, tmp_error);
352 GRegex *regex = g_new0 (GRegex, 1);
353 regex->pattern = regex_pattern_new (re, pattern,
354 compile_options, match_options);
363 * Frees all the memory associated with the regex structure.
368 g_regex_free (GRegex *regex)
373 regex_pattern_unref (regex->pattern);
374 regex_match_free (regex->match);
380 * @regex: a #GRegex structure from g_regex_new()
382 * Copies a #GRegex. The returned #Gregex is in the same state as after
383 * a call to g_regex_clear(), so it does not contain information on the
384 * last match. If @regex is %NULL it returns %NULL.
386 * The returned copy shares some of its internal state with the original
387 * @regex, and the other internal variables are created only when needed,
388 * so the copy is a lightweight operation.
390 * Returns: a newly-allocated copy of @regex, or %NULL if an error
396 g_regex_copy (const GRegex *regex)
403 copy = g_new0 (GRegex, 1);
404 copy->pattern = regex_pattern_ref (regex->pattern);
410 * g_regex_get_pattern:
411 * @regex: a #GRegex structure
413 * Gets the pattern string associated with @regex, i.e. a copy of
414 * the string passed to g_regex_new().
416 * Returns: the pattern of @regex
421 g_regex_get_pattern (const GRegex *regex)
423 g_return_val_if_fail (regex != NULL, NULL);
425 return regex->pattern->pattern;
430 * @regex: a #GRegex structure
432 * Clears out the members of @regex that are holding information about the
433 * last set of matches for this pattern. g_regex_clear() needs to be
434 * called between uses of g_regex_match_next() or g_regex_match_next_full()
435 * against new target strings.
440 g_regex_clear (GRegex *regex)
442 g_return_if_fail (regex != NULL);
444 if (regex->match == NULL)
447 regex->match->matches = -1000; /* an error code not used by PCRE */
448 regex->match->string_len = -1;
449 regex->match->pos = 0;
451 /* if the pattern was used with g_regex_split_next(), it may have
452 * delimiter offsets stored. Free up those guys as well. */
453 if (regex->match->delims != NULL)
455 g_slist_foreach (regex->match->delims, (GFunc) g_free, NULL);
456 g_slist_free (regex->match->delims);
457 regex->match->delims = NULL;
463 * @regex: a #GRegex structure
464 * @error: return location for a #GError
466 * If the pattern will be used many times, then it may be worth the
467 * effort to optimize it to improve the speed of matches.
469 * Returns: %TRUE if @regex has been optimized or was already optimized,
475 g_regex_optimize (GRegex *regex,
480 pcre_extra G_GNUC_MAY_ALIAS **extra_p;
482 g_return_val_if_fail (regex != NULL, FALSE);
483 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
485 if (REGEX_GET_EXTRA (regex) != NULL)
486 /* already optimized. */
489 extra = pcre_study (regex->pattern->pcre_re, 0, &errmsg);
493 GError *tmp_error = g_error_new (G_REGEX_ERROR,
494 G_REGEX_ERROR_OPTIMIZE,
495 _("Error while optimizing "
496 "regular expression %s: %s"),
497 regex->pattern->pattern,
499 g_propagate_error (error, tmp_error);
506 extra_p = ®ex->pattern->extra;
507 if (!g_atomic_pointer_compare_and_exchange ((gpointer *)extra_p, NULL, extra))
508 /* someone else has optimized the regex while this function was running */
515 * g_regex_match_simple:
516 * @pattern: the regular expression
517 * @string: the string to scan for matches
518 * @compile_options: compile options for the regular expression
519 * @match_options: match options
521 * Scans for a match in @string for @pattern.
523 * This function is equivalent to g_regex_match() but it does not
524 * require to compile the pattern with g_regex_new(), avoiding some
525 * lines of code when you need just to do a match without extracting
526 * substrings, capture counts, and so on.
528 * If this function is to be called on the same @pattern more than
529 * once, it's more efficient to compile the pattern once with
530 * g_regex_new() and then use g_regex_match().
532 * Returns: %TRUE is the string matched, %FALSE otherwise
537 g_regex_match_simple (const gchar *pattern,
539 GRegexCompileFlags compile_options,
540 GRegexMatchFlags match_options)
545 regex = g_regex_new (pattern, compile_options, 0, NULL);
548 result = g_regex_match_full (regex, string, -1, 0, match_options, NULL);
549 g_regex_free (regex);
555 * @regex: a #GRegex structure from g_regex_new()
556 * @string: the string to scan for matches
557 * @match_options: match options
559 * Scans for a match in string for the pattern in @regex. The @match_options
560 * are combined with the match options specified when the @regex structure
561 * was created, letting you have more flexibility in reusing #GRegex
564 * Returns: %TRUE is the string matched, %FALSE otherwise
569 g_regex_match (GRegex *regex,
571 GRegexMatchFlags match_options)
573 return g_regex_match_full (regex, string, -1, 0,
574 match_options, NULL);
578 * g_regex_match_full:
579 * @regex: a #GRegex structure from g_regex_new()
580 * @string: the string to scan for matches
581 * @string_len: the length of @string, or -1 if @string is nul-terminated
582 * @start_position: starting index of the string to match
583 * @match_options: match options
584 * @error: location to store the error occuring, or %NULL to ignore errors
586 * Scans for a match in string for the pattern in @regex. The @match_options
587 * are combined with the match options specified when the @regex structure
588 * was created, letting you have more flexibility in reusing #GRegex
591 * Setting @start_position differs from just passing over a shortened string
592 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
593 * with any kind of lookbehind assertion, such as "\b".
595 * Returns: %TRUE is the string matched, %FALSE otherwise
600 g_regex_match_full (GRegex *regex,
604 GRegexMatchFlags match_options,
607 g_return_val_if_fail (regex != NULL, FALSE);
608 g_return_val_if_fail (string != NULL, FALSE);
609 g_return_val_if_fail (start_position >= 0, FALSE);
610 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
611 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
613 regex_lazy_init_match (regex, 0);
616 string_len = strlen (string);
618 regex->match->string_len = string_len;
620 /* create regex->match->offsets if it does not exist */
621 regex_lazy_init_match (regex, 0);
623 /* perform the match */
624 regex->match->matches = pcre_exec (regex->pattern->pcre_re,
625 REGEX_GET_EXTRA (regex),
626 string, regex->match->string_len,
628 regex->pattern->match_opts | match_options,
629 regex->match->offsets, regex->match->n_offsets);
630 if (IS_PCRE_ERROR (regex->match->matches))
632 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
633 _("Error while matching regular expression %s: %s"),
634 regex->pattern->pattern, match_error (regex->match->matches));
638 /* set regex->match->pos to -1 so that a call to g_regex_match_next()
639 * fails without a previous call to g_regex_clear(). */
640 regex->match->pos = -1;
642 return regex->match->matches >= 0;
646 * g_regex_match_next:
647 * @regex: a #GRegex structure
648 * @string: the string to scan for matches
649 * @match_options: the match options
651 * Scans for the next match in @string of the pattern in @regex.
652 * array. The match options are combined with the match options set when
653 * the @regex was created.
655 * You have to call g_regex_clear() to reuse the same pattern on a new
658 * Returns: %TRUE is the string matched, %FALSE otherwise
663 g_regex_match_next (GRegex *regex,
665 GRegexMatchFlags match_options)
667 return g_regex_match_next_full (regex, string, -1, 0,
668 match_options, NULL);
672 * g_regex_match_next_full:
673 * @regex: a #GRegex structure
674 * @string: the string to scan for matches
675 * @string_len: the length of @string, or -1 if @string is nul-terminated
676 * @start_position: starting index of the string to match
677 * @match_options: the match options
678 * @error: location to store the error occuring, or %NULL to ignore errors
680 * Scans for the next match in @string of the pattern in @regex. Calling
681 * g_regex_match_next_full() until it returns %FALSE, you can retrieve
682 * all the non-overlapping matches of the pattern in @string. Empty matches
683 * are included, so matching the string "ab" with the pattern "b*" will
684 * find three matches: "" at position 0, "b" from position 1 to 2 and
687 * The match options are combined with the match options set when the
688 * @regex was created.
690 * You have to call g_regex_clear() to reuse the same pattern on a new
693 * Setting @start_position differs from just passing over a shortened string
694 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
695 * with any kind of lookbehind assertion, such as "\b".
697 * Returns: %TRUE is the string matched, %FALSE otherwise
702 g_regex_match_next_full (GRegex *regex,
706 GRegexMatchFlags match_options,
709 g_return_val_if_fail (regex != NULL, FALSE);
710 g_return_val_if_fail (string != NULL, FALSE);
711 g_return_val_if_fail (start_position >= 0, FALSE);
712 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
713 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
715 regex_lazy_init_match (regex, 0);
717 if (G_UNLIKELY (regex->match->pos < 0))
719 const gchar *msg = _("g_regex_match_next_full: called without a "
720 "previous call to g_regex_clear()");
721 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, msg);
722 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH, msg);
726 /* if this regex hasn't been used on this string before, then we
727 * need to calculate the length of the string, and set pos to the
729 * Knowing if this regex has been used on this string is a bit of
730 * a challenge. For now, we require the user to call g_regex_clear()
731 * in between usages on a new string. Not perfect, but not such a
732 * bad solution either.
734 if (regex->match->string_len == -1)
737 string_len = strlen (string);
738 regex->match->string_len = string_len;
740 regex->match->pos = start_position;
743 /* create regex->match->offsets if it does not exist */
744 regex_lazy_init_match (regex, 0);
746 /* perform the match */
747 regex->match->matches = pcre_exec (regex->pattern->pcre_re,
748 REGEX_GET_EXTRA (regex),
749 string, regex->match->string_len,
751 regex->pattern->match_opts | match_options,
752 regex->match->offsets, regex->match->n_offsets);
753 if (IS_PCRE_ERROR (regex->match->matches))
755 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
756 _("Error while matching regular expression %s: %s"),
757 regex->pattern->pattern, match_error (regex->match->matches));
761 /* avoid infinite loops if regex is an empty string or something
763 if (regex->match->pos == regex->match->offsets[1])
765 if (regex->match->pos > regex->match->string_len)
767 /* we have reached the end of the string */
768 regex->match->pos = -1;
771 regex->match->pos = NEXT_CHAR (regex, &string[regex->match->pos]) - string;
775 regex->match->pos = regex->match->offsets[1];
778 return regex->match->matches >= 0;
783 * @regex: a #GRegex structure from g_regex_new()
784 * @string: the string to scan for matches
785 * @match_options: match options
787 * Using the standard algorithm for regular expression matching only the
788 * longest match in the string is retrieved. This function uses a
789 * different algorithm so it can retrieve all the possible matches.
790 * For more documentation see g_regex_match_all_full().
792 * Returns: %TRUE is the string matched, %FALSE otherwise
797 g_regex_match_all (GRegex *regex,
799 GRegexMatchFlags match_options)
801 return g_regex_match_all_full (regex, string, -1, 0,
802 match_options, NULL);
806 * g_regex_match_all_full:
807 * @regex: a #GRegex structure from g_regex_new()
808 * @string: the string to scan for matches
809 * @string_len: the length of @string, or -1 if @string is nul-terminated
810 * @start_position: starting index of the string to match
811 * @match_options: match options
812 * @error: location to store the error occuring, or %NULL to ignore errors
814 * Using the standard algorithm for regular expression matching only the
815 * longest match in the string is retrieved, it is not possibile to obtain
816 * all the available matches. For instance matching
817 * "<a> <b> <c>" against the pattern "<.*>" you get
818 * "<a> <b> <c>".
820 * This function uses a different algorithm (called DFA, i.e. deterministic
821 * finite automaton), so it can retrieve all the possible matches, all
822 * starting at the same point in the string. For instance matching
823 * "<a> <b> <c>" against the pattern "<.*>" you
824 * would obtain three matches: "<a> <b> <c>",
825 * "<a> <b>" and "<a>".
827 * The number of matched strings is retrieved using
828 * g_regex_get_match_count().
829 * To obtain the matched strings and their position you can use,
830 * respectively, g_regex_fetch() and g_regex_fetch_pos(). Note that the
831 * strings are returned in reverse order of length; that is, the longest
832 * matching string is given first.
834 * Note that the DFA algorithm is slower than the standard one and it is not
835 * able to capture substrings, so backreferences do not work.
837 * Setting @start_position differs from just passing over a shortened string
838 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
839 * with any kind of lookbehind assertion, such as "\b".
841 * Returns: %TRUE is the string matched, %FALSE otherwise
846 g_regex_match_all_full (GRegex *regex,
850 GRegexMatchFlags match_options,
853 g_return_val_if_fail (regex != NULL, FALSE);
854 g_return_val_if_fail (string != NULL, FALSE);
855 g_return_val_if_fail (start_position >= 0, FALSE);
856 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
857 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
859 regex_lazy_init_match (regex, 0);
862 string_len = strlen (string);
864 regex->match->string_len = string_len;
866 if (regex->match->workspace == NULL)
868 regex->match->n_workspace = WORKSPACE_INITIAL;
869 regex->match->workspace = g_new (gint, regex->match->n_workspace);
872 if (regex->match->n_offsets < OFFSETS_DFA_MIN_SIZE)
874 regex->match->n_offsets = OFFSETS_DFA_MIN_SIZE;
875 regex->match->offsets = g_realloc (regex->match->offsets,
876 regex->match->n_offsets * sizeof(gint));
879 /* perform the match */
880 regex->match->matches = pcre_dfa_exec (regex->pattern->pcre_re,
881 REGEX_GET_EXTRA (regex),
882 string, regex->match->string_len,
884 regex->pattern->match_opts | match_options,
885 regex->match->offsets, regex->match->n_offsets,
886 regex->match->workspace,
887 regex->match->n_workspace);
888 if (regex->match->matches == PCRE_ERROR_DFA_WSSIZE)
890 /* regex->match->workspace is too small. */
891 regex->match->n_workspace *= 2;
892 regex->match->workspace = g_realloc (regex->match->workspace,
893 regex->match->n_workspace * sizeof (gint));
894 return g_regex_match_all_full (regex, string, string_len,
895 start_position, match_options, error);
897 else if (regex->match->matches == 0)
899 /* regex->match->offsets is too small. */
900 regex->match->n_offsets *= 2;
901 regex->match->offsets = g_realloc (regex->match->offsets,
902 regex->match->n_offsets * sizeof (gint));
903 return g_regex_match_all_full (regex, string, string_len,
904 start_position, match_options, error);
906 else if (IS_PCRE_ERROR (regex->match->matches))
908 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
909 _("Error while matching regular expression %s: %s"),
910 regex->pattern->pattern, match_error (regex->match->matches));
914 /* set regex->match->pos to -1 so that a call to g_regex_match_next()
915 * fails without a previous call to g_regex_clear(). */
916 regex->match->pos = -1;
918 return regex->match->matches >= 0;
922 * g_regex_get_match_count:
923 * @regex: a #GRegex structure
925 * Retrieves the number of matched substrings (including substring 0, that
926 * is the whole matched text) in the last call to g_regex_match*(), so 1
927 * is returned if the pattern has no substrings in it and 0 is returned if
930 * If the last match was obtained using the DFA algorithm, that is using
931 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
932 * count is not that of the number of capturing parentheses but that of
933 * the number of matched substrings.
935 * Returns: Number of matched substrings, or -1 if an error occurred
940 g_regex_get_match_count (const GRegex *regex)
942 g_return_val_if_fail (regex != NULL, -1);
944 if (regex->match == NULL)
947 if (regex->match->matches == PCRE_ERROR_NOMATCH)
950 else if (regex->match->matches < PCRE_ERROR_NOMATCH)
955 return regex->match->matches;
959 * g_regex_is_partial_match:
960 * @regex: a #GRegex structure
962 * Usually if the string passed to g_regex_match*() matches as far as
963 * it goes, but is too short to match the entire pattern, %FALSE is
964 * returned. There are circumstances where it might be helpful to
965 * distinguish this case from other cases in which there is no match.
967 * Consider, for example, an application where a human is required to
968 * type in data for a field with specific formatting requirements. An
969 * example might be a date in the form ddmmmyy, defined by the pattern
970 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
971 * If the application sees the user’s keystrokes one by one, and can
972 * check that what has been typed so far is potentially valid, it is
973 * able to raise an error as soon as a mistake is made.
975 * GRegex supports the concept of partial matching by means of the
976 * #G_REGEX_MATCH_PARTIAL flag. When this is set the return code for
977 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
978 * for a complete match, %FALSE otherwise. But, when this functions
979 * returns %FALSE, you can check if the match was partial calling
980 * g_regex_is_partial_match().
982 * When using partial matching you cannot use g_regex_fetch*().
984 * Because of the way certain internal optimizations are implemented the
985 * partial matching algorithm cannot be used with all patterns. So repeated
986 * single characters such as "a{2,4}" and repeated single metasequences such
987 * as "\d+" are not permitted if the maximum number of occurrences is
988 * greater than one. Optional items such as "\d?" (where the maximum is one)
989 * are permitted. Quantifiers with any values are permitted after
990 * parentheses, so the invalid examples above can be coded thus "(a){2,4}"
991 * and "(\d)+". If #G_REGEX_MATCH_PARTIAL is set for a pattern that does
992 * not conform to the restrictions, matching functions return an error.
994 * Returns: %TRUE if the match was partial, %FALSE otherwise
999 g_regex_is_partial_match (const GRegex *regex)
1001 g_return_val_if_fail (regex != NULL, FALSE);
1003 if (regex->match == NULL)
1006 return regex->match->matches == PCRE_ERROR_PARTIAL;
1011 * @regex: #GRegex structure used in last match
1012 * @match_num: number of the sub expression
1013 * @string: the string on which the last match was made
1015 * Retrieves the text matching the @match_num<!-- -->'th capturing parentheses.
1016 * 0 is the full text of the match, 1 is the first paren set, 2 the second,
1019 * If @match_num is a valid sub pattern but it didn't match anything (e.g.
1020 * sub pattern 1, matching "b" against "(a)?b") then an empty string is
1023 * If the last match was obtained using the DFA algorithm, that is using
1024 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1025 * string is not that of a set of parentheses but that of a matched
1026 * substring. Substrings are matched in reverse order of length, so 0 is
1027 * the longest match.
1029 * Returns: The matched substring, or %NULL if an error occurred.
1030 * You have to free the string yourself.
1035 g_regex_fetch (const GRegex *regex,
1037 const gchar *string)
1039 /* we cannot use pcre_get_substring() because it allocates the
1040 * string using pcre_malloc(). */
1041 gchar *match = NULL;
1044 g_return_val_if_fail (regex != NULL, NULL);
1045 g_return_val_if_fail (match_num >= 0, NULL);
1047 if (regex->match == NULL)
1050 if (regex->match->string_len < 0)
1053 /* match_num does not exist or it didn't matched, i.e. matching "b"
1054 * against "(a)?b" then group 0 is empty. */
1055 if (!g_regex_fetch_pos (regex, match_num, &start, &end))
1057 else if (start == -1)
1058 match = g_strdup ("");
1060 match = g_strndup (&string[start], end - start);
1066 * g_regex_fetch_pos:
1067 * @regex: #GRegex structure used in last match
1068 * @match_num: number of the sub expression
1069 * @start_pos: pointer to location where to store the start position
1070 * @end_pos: pointer to location where to store the end position
1072 * Retrieves the position of the @match_num<!-- -->'th capturing parentheses.
1073 * 0 is the full text of the match, 1 is the first paren set, 2 the second,
1076 * If @match_num is a valid sub pattern but it didn't match anything (e.g.
1077 * sub pattern 1, matching "b" against "(a)?b") then @start_pos and @end_pos
1078 * are set to -1 and %TRUE is returned.
1080 * If the last match was obtained using the DFA algorithm, that is using
1081 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1082 * position is not that of a set of parentheses but that of a matched
1083 * substring. Substrings are matched in reverse order of length, so 0 is
1084 * the longest match.
1086 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If the
1087 * position cannot be fetched, @start_pos and @end_pos are left
1093 g_regex_fetch_pos (const GRegex *regex,
1098 g_return_val_if_fail (regex != NULL, FALSE);
1099 g_return_val_if_fail (match_num >= 0, FALSE);
1101 if (regex->match == NULL)
1104 /* make sure the sub expression number they're requesting is less than
1105 * the total number of sub expressions that were matched. */
1106 if (match_num >= regex->match->matches)
1109 if (start_pos != NULL)
1110 *start_pos = regex->match->offsets[2 * match_num];
1112 if (end_pos != NULL)
1113 *end_pos = regex->match->offsets[2 * match_num + 1];
1119 * g_regex_fetch_named:
1120 * @regex: #GRegex structure used in last match
1121 * @name: name of the subexpression
1122 * @string: the string on which the last match was made
1124 * Retrieves the text matching the capturing parentheses named @name.
1126 * If @name is a valid sub pattern name but it didn't match anything (e.g.
1127 * sub pattern "X", matching "b" against "(?P<X>a)?b") then an empty
1128 * string is returned.
1130 * Returns: The matched substring, or %NULL if an error occurred.
1131 * You have to free the string yourself.
1136 g_regex_fetch_named (const GRegex *regex,
1138 const gchar *string)
1140 /* we cannot use pcre_get_named_substring() because it allocates the
1141 * string using pcre_malloc(). */
1144 g_return_val_if_fail (regex != NULL, NULL);
1145 g_return_val_if_fail (string != NULL, NULL);
1146 g_return_val_if_fail (name != NULL, NULL);
1148 num = g_regex_get_string_number (regex, name);
1152 return g_regex_fetch (regex, num, string);
1156 * g_regex_fetch_named_pos:
1157 * @regex: #GRegex structure used in last match
1158 * @name: name of the subexpression
1159 * @start_pos: pointer to location where to store the start position
1160 * @end_pos: pointer to location where to store the end position
1162 * Retrieves the position of the capturing parentheses named @name.
1164 * If @name is a valid sub pattern name but it didn't match anything (e.g.
1165 * sub pattern "X", matching "b" against "(?P<X>a)?b") then @start_pos and
1166 * @end_pos are set to -1 and %TRUE is returned.
1168 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If the
1169 * position cannot be fetched, @start_pos and @end_pos are left
1175 g_regex_fetch_named_pos (const GRegex *regex,
1182 num = g_regex_get_string_number (regex, name);
1186 return g_regex_fetch_pos (regex, num, start_pos, end_pos);
1190 * g_regex_fetch_all:
1191 * @regex: a #GRegex structure
1192 * @string: the string on which the last match was made
1194 * Bundles up pointers to each of the matching substrings from a match
1195 * and stores them in an array of gchar pointers. The first element in
1196 * the returned array is the match number 0, i.e. the entire matched
1199 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
1200 * "b" against "(a)?b") then an empty string is inserted.
1202 * If the last match was obtained using the DFA algorithm, that is using
1203 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1204 * strings are not that matched by sets of parentheses but that of the
1205 * matched substring. Substrings are matched in reverse order of length,
1206 * so the first one is the longest match.
1208 * Returns: a %NULL-terminated array of gchar * pointers. It must be freed
1209 * using g_strfreev(). If the memory can't be allocated, returns
1215 g_regex_fetch_all (const GRegex *regex,
1216 const gchar *string)
1218 /* we cannot use pcre_get_substring_list() because the returned value
1219 * isn't suitable for g_strfreev(). */
1223 g_return_val_if_fail (regex != NULL, FALSE);
1224 g_return_val_if_fail (string != NULL, FALSE);
1226 if (regex->match == NULL)
1229 if (regex->match->matches < 0)
1232 result = g_new (gchar *, regex->match->matches + 1);
1233 for (i = 0; i < regex->match->matches; i++)
1234 result[i] = g_regex_fetch (regex, i, string);
1241 * g_regex_get_string_number:
1242 * @regex: #GRegex structure
1243 * @name: name of the subexpression
1245 * Retrieves the number of the subexpression named @name.
1247 * Returns: The number of the subexpression or -1 if @name does not exists.
1252 g_regex_get_string_number (const GRegex *regex,
1257 g_return_val_if_fail (regex != NULL, -1);
1258 g_return_val_if_fail (name != NULL, -1);
1260 num = pcre_get_stringnumber (regex->pattern->pcre_re, name);
1261 if (num == PCRE_ERROR_NOSUBSTRING)
1268 * g_regex_split_simple:
1269 * @pattern: the regular expression
1270 * @string: the string to scan for matches
1271 * @compile_options: compile options for the regular expression
1272 * @match_options: match options
1274 * Breaks the string on the pattern, and returns an array of the tokens.
1275 * If the pattern contains capturing parentheses, then the text for each
1276 * of the substrings will also be returned. If the pattern does not match
1277 * anywhere in the string, then the whole string is returned as the first
1280 * This function is equivalent to g_regex_split() but it does not
1281 * require to compile the pattern with g_regex_new(), avoiding some
1282 * lines of code when you need just to do a split without extracting
1283 * substrings, capture counts, and so on.
1285 * If this function is to be called on the same @pattern more than
1286 * once, it's more efficient to compile the pattern once with
1287 * g_regex_new() and then use g_regex_split().
1289 * As a special case, the result of splitting the empty string "" is an
1290 * empty vector, not a vector containing a single string. The reason for
1291 * this special case is that being able to represent a empty vector is
1292 * typically more useful than consistent handling of empty elements. If
1293 * you do need to represent empty elements, you'll need to check for the
1294 * empty string before calling this function.
1296 * A pattern that can match empty strings splits @string into separate
1297 * characters wherever it matches the empty string between characters.
1298 * For example splitting "ab c" using as a separator "\s*", you will get
1301 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev().
1306 g_regex_split_simple (const gchar *pattern,
1307 const gchar *string,
1308 GRegexCompileFlags compile_options,
1309 GRegexMatchFlags match_options)
1314 regex = g_regex_new (pattern, compile_options, 0, NULL);
1317 result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
1318 g_regex_free (regex);
1324 * @regex: a #GRegex structure
1325 * @string: the string to split with the pattern
1326 * @match_options: match time option flags
1328 * Breaks the string on the pattern, and returns an array of the tokens.
1329 * If the pattern contains capturing parentheses, then the text for each
1330 * of the substrings will also be returned. If the pattern does not match
1331 * anywhere in the string, then the whole string is returned as the first
1334 * As a special case, the result of splitting the empty string "" is an
1335 * empty vector, not a vector containing a single string. The reason for
1336 * this special case is that being able to represent a empty vector is
1337 * typically more useful than consistent handling of empty elements. If
1338 * you do need to represent empty elements, you'll need to check for the
1339 * empty string before calling this function.
1341 * A pattern that can match empty strings splits @string into separate
1342 * characters wherever it matches the empty string between characters.
1343 * For example splitting "ab c" using as a separator "\s*", you will get
1346 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev().
1351 g_regex_split (GRegex *regex,
1352 const gchar *string,
1353 GRegexMatchFlags match_options)
1355 return g_regex_split_full (regex, string, -1, 0,
1356 match_options, 0, NULL);
1360 * g_regex_split_full:
1361 * @regex: a #GRegex structure
1362 * @string: the string to split with the pattern
1363 * @string_len: the length of @string, or -1 if @string is nul-terminated
1364 * @start_position: starting index of the string to match
1365 * @match_options: match time option flags
1366 * @max_tokens: the maximum number of tokens to split @string into. If this
1367 * is less than 1, the string is split completely.
1368 * @error: return location for a #GError
1370 * Breaks the string on the pattern, and returns an array of the tokens.
1371 * If the pattern contains capturing parentheses, then the text for each
1372 * of the substrings will also be returned. If the pattern does not match
1373 * anywhere in the string, then the whole string is returned as the first
1376 * As a special case, the result of splitting the empty string "" is an
1377 * empty vector, not a vector containing a single string. The reason for
1378 * this special case is that being able to represent a empty vector is
1379 * typically more useful than consistent handling of empty elements. If
1380 * you do need to represent empty elements, you'll need to check for the
1381 * empty string before calling this function.
1383 * A pattern that can match empty strings splits @string into separate
1384 * characters wherever it matches the empty string between characters.
1385 * For example splitting "ab c" using as a separator "\s*", you will get
1388 * Setting @start_position differs from just passing over a shortened string
1389 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1390 * with any kind of lookbehind assertion, such as "\b".
1392 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev().
1397 g_regex_split_full (GRegex *regex,
1398 const gchar *string,
1400 gint start_position,
1401 GRegexMatchFlags match_options,
1405 gchar **string_list; /* The array of char **s worked on */
1409 GError *tmp_error = NULL;
1411 g_return_val_if_fail (regex != NULL, NULL);
1412 g_return_val_if_fail (string != NULL, NULL);
1413 g_return_val_if_fail (start_position >= 0, NULL);
1414 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1415 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1417 regex_lazy_init_match (regex, 0);
1419 if (max_tokens <= 0)
1420 max_tokens = G_MAXINT;
1423 string_len = strlen (string);
1425 if (string_len - start_position == 0)
1426 return g_new0 (gchar *, 1);
1428 /* clear out the regex for reuse, just in case */
1429 g_regex_clear (regex);
1437 /* -1 to leave room for the last part. */
1438 if (tokens >= max_tokens - 1)
1440 /* we have reached the maximum number of tokens, so we copy
1441 * the remaining part of the string. */
1442 if (regex->match->last_match_is_empty)
1444 /* the last match was empty, so we have moved one char
1445 * after the real position to avoid empty matches at the
1447 regex->match->pos = PREV_CHAR (regex, &string[regex->match->pos]) - string;
1449 /* the if is needed in the case we have terminated the available
1450 * tokens, but we are at the end of the string, so there are no
1451 * characters left to copy. */
1452 if (string_len > regex->match->pos)
1454 token = g_strndup (string + regex->match->pos,
1455 string_len - regex->match->pos);
1456 list = g_list_prepend (list, token);
1462 token = g_regex_split_next_full (regex, string, string_len, start_position,
1463 match_options, &tmp_error);
1464 if (tmp_error != NULL)
1466 g_propagate_error (error, tmp_error);
1467 g_list_foreach (list, (GFunc)g_free, NULL);
1469 regex->match->pos = -1;
1474 /* no more tokens. */
1478 list = g_list_prepend (list, token);
1481 string_list = g_new (gchar *, g_list_length (list) + 1);
1483 for (last = g_list_last (list); last; last = g_list_previous (last))
1484 string_list[pos++] = last->data;
1485 string_list[pos] = 0;
1487 regex->match->pos = -1;
1494 * g_regex_split_next:
1495 * @regex: a #GRegex structure from g_regex_new()
1496 * @string: the string to split on pattern
1497 * @match_options: match time options for the regex
1499 * g_regex_split_next() breaks the string on pattern, and returns the
1500 * tokens, one per call. If the pattern contains capturing parentheses,
1501 * then the text for each of the substrings will also be returned.
1502 * If the pattern does not match anywhere in the string, then the whole
1503 * string is returned as the first token.
1505 * A pattern that can match empty strings splits @string into separate
1506 * characters wherever it matches the empty string between characters.
1507 * For example splitting "ab c" using as a separator "\s*", you will get
1510 * You have to call g_regex_clear() to reuse the same pattern on a new
1513 * Returns: a gchar * to the next token of the string
1518 g_regex_split_next (GRegex *regex,
1519 const gchar *string,
1520 GRegexMatchFlags match_options)
1522 return g_regex_split_next_full (regex, string, -1, 0, match_options,
1527 * g_regex_split_next_full:
1528 * @regex: a #GRegex structure from g_regex_new()
1529 * @string: the string to split on pattern
1530 * @string_len: the length of @string, or -1 if @string is nul-terminated
1531 * @start_position: starting index of the string to match
1532 * @match_options: match time options for the regex
1533 * @error: return location for a #GError
1535 * g_regex_split_next_full() breaks the string on pattern, and returns
1536 * the tokens, one per call. If the pattern contains capturing parentheses,
1537 * then the text for each of the substrings will also be returned.
1538 * If the pattern does not match anywhere in the string, then the whole
1539 * string is returned as the first token.
1541 * A pattern that can match empty strings splits @string into separate
1542 * characters wherever it matches the empty string between characters.
1543 * For example splitting "ab c" using as a separator "\s*", you will get
1546 * You have to call g_regex_clear() to reuse the same pattern on a new
1549 * Setting @start_position differs from just passing over a shortened string
1550 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1551 * with any kind of lookbehind assertion, such as "\b".
1553 * Returns: a gchar * to the next token of the string
1558 g_regex_split_next_full (GRegex *regex,
1559 const gchar *string,
1561 gint start_position,
1562 GRegexMatchFlags match_options,
1566 gchar *token = NULL;
1569 GError *tmp_error = NULL;
1571 g_return_val_if_fail (regex != NULL, NULL);
1572 g_return_val_if_fail (string != NULL, NULL);
1573 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1574 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1576 regex_lazy_init_match (regex, 0);
1578 new_pos = MAX (regex->match->pos, start_position);
1579 if (regex->match->last_match_is_empty)
1580 /* if the last match was empty, g_regex_match_next_full() has moved
1581 * forward to avoid infinite loops, but we still need to copy that
1583 new_pos = PREV_CHAR (regex, &string[new_pos]) - string;
1585 /* if there are delimiter substrings stored, return those one at a
1588 if (regex->match->delims != NULL)
1590 token = regex->match->delims->data;
1591 regex->match->delims = g_slist_remove (regex->match->delims, token);
1595 if (regex->match->pos == -1)
1596 /* the last call to g_regex_match_next_full() returned NULL. */
1599 if (regex->match->string_len < 0)
1601 regex->match->last_match_is_empty = FALSE;
1602 /* initialize last_separator_end to start_position to skip the
1603 * empty token at the beginning of the string. */
1604 regex->match->last_separator_end = start_position;
1607 /* use g_regex_match_next() to find the next occurance of the pattern
1608 * in the string. We use new_pos to keep track of where the stuff
1609 * up to the current match starts. Copy that token of the string off
1610 * and append it to the buffer using g_strndup. */
1611 match_ok = g_regex_match_next_full (regex, string, string_len,
1612 start_position, match_options,
1614 if (tmp_error != NULL)
1616 g_propagate_error (error, tmp_error);
1622 regex->match->last_match_is_empty =
1623 (regex->match->offsets[0] == regex->match->offsets[1]);
1625 /* we need to skip empty separators at the same position of the end
1626 * of another separator. e.g. the string is "a b" and the separator
1627 * is "*", so from 1 to 2 we have a match and at position 2 we have
1628 * an empty match. */
1629 if (regex->match->last_separator_end != regex->match->offsets[1])
1631 token = g_strndup (string + new_pos, regex->match->offsets[0] - new_pos);
1633 /* if there were substrings, these need to get added to the
1635 match_count = g_regex_get_match_count (regex);
1636 if (match_count > 1)
1639 for (i = 1; i < match_count; i++)
1640 regex->match->delims = g_slist_append (regex->match->delims,
1641 g_regex_fetch (regex, i, string));
1644 regex->match->last_separator_end = regex->match->offsets[1];
1648 /* we have skipped an empty separator so we need to find the
1650 return g_regex_split_next_full (regex, string, string_len,
1651 start_position, match_options,
1657 /* if there was no match, copy to end of string. */
1658 if (!regex->match->last_match_is_empty)
1659 token = g_strndup (string + new_pos, regex->match->string_len - new_pos);
1670 REPL_TYPE_CHARACTER,
1671 REPL_TYPE_SYMBOLIC_REFERENCE,
1672 REPL_TYPE_NUMERIC_REFERENCE,
1673 REPL_TYPE_CHANGE_CASE
1678 CHANGE_CASE_NONE = 1 << 0,
1679 CHANGE_CASE_UPPER = 1 << 1,
1680 CHANGE_CASE_LOWER = 1 << 2,
1681 CHANGE_CASE_UPPER_SINGLE = 1 << 3,
1682 CHANGE_CASE_LOWER_SINGLE = 1 << 4,
1683 CHANGE_CASE_SINGLE_MASK = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
1684 CHANGE_CASE_LOWER_MASK = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
1685 CHANGE_CASE_UPPER_MASK = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
1694 ChangeCase change_case;
1695 } InterpolationData;
1698 free_interpolation_data (InterpolationData *data)
1700 g_free (data->text);
1704 static const gchar *
1705 expand_escape (const gchar *replacement,
1707 InterpolationData *data,
1712 const gchar *error_detail;
1714 GError *tmp_error = NULL;
1722 data->type = REPL_TYPE_CHARACTER;
1727 data->type = REPL_TYPE_CHARACTER;
1732 data->type = REPL_TYPE_CHARACTER;
1737 data->type = REPL_TYPE_CHARACTER;
1742 data->type = REPL_TYPE_CHARACTER;
1747 data->type = REPL_TYPE_CHARACTER;
1752 data->type = REPL_TYPE_CHARACTER;
1757 data->type = REPL_TYPE_CHARACTER;
1767 h = g_ascii_xdigit_value (*p);
1770 error_detail = _("hexadecimal digit or '}' expected");
1781 for (i = 0; i < 2; i++)
1783 h = g_ascii_xdigit_value (*p);
1786 error_detail = _("hexadecimal digit expected");
1793 data->type = REPL_TYPE_STRING;
1794 data->text = g_new0 (gchar, 8);
1795 g_unichar_to_utf8 (x, data->text);
1799 data->type = REPL_TYPE_CHANGE_CASE;
1800 data->change_case = CHANGE_CASE_LOWER_SINGLE;
1804 data->type = REPL_TYPE_CHANGE_CASE;
1805 data->change_case = CHANGE_CASE_UPPER_SINGLE;
1809 data->type = REPL_TYPE_CHANGE_CASE;
1810 data->change_case = CHANGE_CASE_LOWER;
1814 data->type = REPL_TYPE_CHANGE_CASE;
1815 data->change_case = CHANGE_CASE_UPPER;
1819 data->type = REPL_TYPE_CHANGE_CASE;
1820 data->change_case = CHANGE_CASE_NONE;
1826 error_detail = _("missing '<' in symbolic reference");
1835 error_detail = _("unfinished symbolic reference");
1842 error_detail = _("zero-length symbolic reference");
1845 if (g_ascii_isdigit (*q))
1850 h = g_ascii_digit_value (*q);
1853 error_detail = _("digit expected");
1862 data->type = REPL_TYPE_NUMERIC_REFERENCE;
1869 if (!g_ascii_isalnum (*r))
1871 error_detail = _("illegal symbolic reference");
1878 data->text = g_strndup (q, p - q);
1879 data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
1884 /* if \0 is followed by a number is an octal number representing a
1885 * character, else it is a numeric reference. */
1886 if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
1889 p = g_utf8_next_char (p);
1902 for (i = 0; i < 3; i++)
1904 h = g_ascii_digit_value (*p);
1914 if (i == 2 && base == 10)
1920 if (base == 8 || i == 3)
1922 data->type = REPL_TYPE_STRING;
1923 data->text = g_new0 (gchar, 8);
1924 g_unichar_to_utf8 (x, data->text);
1928 data->type = REPL_TYPE_NUMERIC_REFERENCE;
1933 error_detail = _("stray final '\\'");
1937 error_detail = _("unknown escape sequence");
1944 /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
1945 tmp_error = g_error_new (G_REGEX_ERROR,
1946 G_REGEX_ERROR_REPLACE,
1947 _("Error while parsing replacement "
1948 "text \"%s\" at char %lu: %s"),
1950 (gulong)(p - replacement),
1952 g_propagate_error (error, tmp_error);
1958 split_replacement (const gchar *replacement,
1962 InterpolationData *data;
1963 const gchar *p, *start;
1965 start = p = replacement;
1970 data = g_new0 (InterpolationData, 1);
1971 start = p = expand_escape (replacement, p, data, error);
1974 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
1976 free_interpolation_data (data);
1980 list = g_list_prepend (list, data);
1985 if (*p == '\\' || *p == '\0')
1989 data = g_new0 (InterpolationData, 1);
1990 data->text = g_strndup (start, p - start);
1991 data->type = REPL_TYPE_STRING;
1992 list = g_list_prepend (list, data);
1998 return g_list_reverse (list);
2001 /* Change the case of c based on change_case. */
2002 #define CHANGE_CASE(c, change_case) \
2003 (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
2004 g_unichar_tolower (c) : \
2005 g_unichar_toupper (c))
2008 string_append (GString *string,
2010 ChangeCase *change_case)
2014 if (text[0] == '\0')
2017 if (*change_case == CHANGE_CASE_NONE)
2019 g_string_append (string, text);
2021 else if (*change_case & CHANGE_CASE_SINGLE_MASK)
2023 c = g_utf8_get_char (text);
2024 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2025 g_string_append (string, g_utf8_next_char (text));
2026 *change_case = CHANGE_CASE_NONE;
2030 while (*text != '\0')
2032 c = g_utf8_get_char (text);
2033 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2034 text = g_utf8_next_char (text);
2040 interpolate_replacement (const GRegex *regex,
2041 const gchar *string,
2046 InterpolationData *idata;
2048 ChangeCase change_case = CHANGE_CASE_NONE;
2050 for (list = data; list; list = list->next)
2053 switch (idata->type)
2055 case REPL_TYPE_STRING:
2056 string_append (result, idata->text, &change_case);
2058 case REPL_TYPE_CHARACTER:
2059 g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
2060 if (change_case & CHANGE_CASE_SINGLE_MASK)
2061 change_case = CHANGE_CASE_NONE;
2063 case REPL_TYPE_NUMERIC_REFERENCE:
2064 match = g_regex_fetch (regex, idata->num, string);
2067 string_append (result, match, &change_case);
2071 case REPL_TYPE_SYMBOLIC_REFERENCE:
2072 match = g_regex_fetch_named (regex, idata->text, string);
2075 string_append (result, match, &change_case);
2079 case REPL_TYPE_CHANGE_CASE:
2080 change_case = idata->change_case;
2089 * g_regex_expand_references:
2090 * @regex: #GRegex structure used in last match
2091 * @string: the string on which the last match was made
2092 * @string_to_expand: the string to expand
2093 * @error: location to store the error occuring, or %NULL to ignore errors
2095 * Returns a new string containing the text in @string_to_expand with
2096 * references expanded. References refer to the last match done with
2097 * @string against @regex and have the same syntax used by g_regex_replace().
2099 * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
2100 * passed to g_regex_new().
2102 * Returns: the expanded string, or %NULL if an error occurred
2107 g_regex_expand_references (GRegex *regex,
2108 const gchar *string,
2109 const gchar *string_to_expand,
2114 GError *tmp_error = NULL;
2116 g_return_val_if_fail (regex != NULL, NULL);
2117 g_return_val_if_fail (string != NULL, NULL);
2118 g_return_val_if_fail (string_to_expand != NULL, NULL);
2119 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2121 list = split_replacement (string_to_expand, &tmp_error);
2122 if (tmp_error != NULL)
2124 g_propagate_error (error, tmp_error);
2128 result = g_string_sized_new (strlen (string_to_expand));
2129 interpolate_replacement (regex, string, result, list);
2131 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
2134 return g_string_free (result, FALSE);
2139 * @regex: a #GRegex structure
2140 * @string: the string to perform matches against
2141 * @string_len: the length of @string, or -1 if @string is nul-terminated
2142 * @start_position: starting index of the string to match
2143 * @replacement: text to replace each match with
2144 * @match_options: options for the match
2145 * @error: location to store the error occuring, or %NULL to ignore errors
2147 * Replaces all occurances of the pattern in @regex with the
2148 * replacement text. Backreferences of the form '\number' or '\g<number>'
2149 * in the replacement text are interpolated by the number-th captured
2150 * subexpression of the match, '\g<name>' refers to the captured subexpression
2151 * with the given name. '\0' refers to the complete match, but '\0' followed
2152 * by a number is the octal representation of a character. To include a
2153 * literal '\' in the replacement, write '\\'.
2154 * There are also escapes that changes the case of the following text:
2157 * <varlistentry><term>\l</term>
2159 * <para>Convert to lower case the next character</para>
2162 * <varlistentry><term>\u</term>
2164 * <para>Convert to upper case the next character</para>
2167 * <varlistentry><term>\L</term>
2169 * <para>Convert to lower case till \E</para>
2172 * <varlistentry><term>\U</term>
2174 * <para>Convert to upper case till \E</para>
2177 * <varlistentry><term>\E</term>
2179 * <para>End case modification</para>
2184 * If you do not need to use backreferences use g_regex_replace_literal().
2186 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was
2187 * passed to g_regex_new(). If you want to use not UTF-8 encoded stings
2188 * you can use g_regex_replace_literal().
2190 * Setting @start_position differs from just passing over a shortened string
2191 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2192 * with any kind of lookbehind assertion, such as "\b".
2194 * Returns: a newly allocated string containing the replacements
2199 g_regex_replace (GRegex *regex,
2200 const gchar *string,
2202 gint start_position,
2203 const gchar *replacement,
2204 GRegexMatchFlags match_options,
2209 GError *tmp_error = NULL;
2211 g_return_val_if_fail (regex != NULL, NULL);
2212 g_return_val_if_fail (string != NULL, NULL);
2213 g_return_val_if_fail (start_position >= 0, NULL);
2214 g_return_val_if_fail (replacement != NULL, NULL);
2215 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2216 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2218 list = split_replacement (replacement, &tmp_error);
2219 if (tmp_error != NULL)
2221 g_propagate_error (error, tmp_error);
2225 result = g_regex_replace_eval (regex,
2226 string, string_len, start_position,
2228 interpolate_replacement,
2231 if (tmp_error != NULL)
2232 g_propagate_error (error, tmp_error);
2234 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
2241 literal_replacement (const GRegex *regex,
2242 const gchar *string,
2246 g_string_append (result, data);
2251 * g_regex_replace_literal:
2252 * @regex: a #GRegex structure
2253 * @string: the string to perform matches against
2254 * @string_len: the length of @string, or -1 if @string is nul-terminated
2255 * @start_position: starting index of the string to match
2256 * @replacement: text to replace each match with
2257 * @match_options: options for the match
2258 * @error: location to store the error occuring, or %NULL to ignore errors
2260 * Replaces all occurances of the pattern in @regex with the
2261 * replacement text. @replacement is replaced literally, to
2262 * include backreferences use g_regex_replace().
2264 * Setting @start_position differs from just passing over a shortened string
2265 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2266 * with any kind of lookbehind assertion, such as "\b".
2268 * Returns: a newly allocated string containing the replacements
2273 g_regex_replace_literal (GRegex *regex,
2274 const gchar *string,
2276 gint start_position,
2277 const gchar *replacement,
2278 GRegexMatchFlags match_options,
2281 g_return_val_if_fail (replacement != NULL, NULL);
2282 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2284 return g_regex_replace_eval (regex,
2285 string, string_len, start_position,
2287 literal_replacement,
2288 (gpointer)replacement,
2293 * g_regex_replace_eval:
2294 * @regex: a #GRegex structure from g_regex_new()
2295 * @string: string to perform matches against
2296 * @string_len: the length of @string, or -1 if @string is nul-terminated
2297 * @start_position: starting index of the string to match
2298 * @match_options: options for the match
2299 * @eval: a function to call for each match
2300 * @user_data: user data to pass to the function
2301 * @error: location to store the error occuring, or %NULL to ignore errors
2303 * Replaces occurances of the pattern in regex with the output of @eval
2304 * for that occurance.
2306 * Setting @start_position differs from just passing over a shortened string
2307 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2308 * with any kind of lookbehind assertion, such as "\b".
2310 * Returns: a newly allocated string containing the replacements
2315 g_regex_replace_eval (GRegex *regex,
2316 const gchar *string,
2318 gint start_position,
2319 GRegexMatchFlags match_options,
2320 GRegexEvalCallback eval,
2326 gboolean done = FALSE;
2327 GError *tmp_error = NULL;
2329 g_return_val_if_fail (regex != NULL, NULL);
2330 g_return_val_if_fail (string != NULL, NULL);
2331 g_return_val_if_fail (start_position >= 0, NULL);
2332 g_return_val_if_fail (eval != NULL, NULL);
2333 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2335 regex_lazy_init_match (regex, 0);
2338 string_len = strlen (string);
2340 /* clear out the regex for reuse, just in case */
2341 g_regex_clear (regex);
2343 result = g_string_sized_new (string_len);
2345 /* run down the string making matches. */
2347 g_regex_match_next_full (regex, string, string_len,
2348 start_position, match_options, &tmp_error))
2350 g_string_append_len (result,
2352 regex->match->offsets[0] - str_pos);
2353 done = (*eval) (regex, string, result, user_data);
2354 str_pos = regex->match->offsets[1];
2357 if (tmp_error != NULL)
2359 g_propagate_error (error, tmp_error);
2360 g_string_free (result, TRUE);
2364 g_string_append_len (result, string + str_pos, string_len - str_pos);
2366 return g_string_free (result, FALSE);
2370 * g_regex_escape_string:
2371 * @string: the string to escape
2372 * @length: the length of @string, or -1 if @string is nul-terminated
2374 * Escapes the special characters used for regular expressions in @string,
2375 * for instance "a.b*c" becomes "a\.b\*c". This function is useful to
2376 * dynamically generate regular expressions.
2378 * @string can contain nul characters that are replaced with "\0", in this
2379 * case remember to specify the correct length of @string in @length.
2381 * Returns: a newly-allocated escaped string
2386 g_regex_escape_string (const gchar *string,
2390 const char *p, *piece_start, *end;
2392 g_return_val_if_fail (string != NULL, NULL);
2395 length = strlen (string);
2397 end = string + length;
2398 p = piece_start = string;
2399 escaped = g_string_sized_new (length + 1);
2420 if (p != piece_start)
2421 /* copy the previous piece. */
2422 g_string_append_len (escaped, piece_start, p - piece_start);
2423 g_string_append_c (escaped, '\\');
2425 g_string_append_c (escaped, '0');
2427 g_string_append_c (escaped, *p);
2431 p = g_utf8_next_char (p);
2436 if (piece_start < end)
2437 g_string_append_len (escaped, piece_start, end - piece_start);
2439 return g_string_free (escaped, FALSE);
2442 #define __G_REGEX_C__
2443 #include "galiasdef.c"