regex: Add FIRSTLINE compile flag
[platform/upstream/glib.git] / glib / gregex.c
1 /* GRegex -- regular expression API wrapper around PCRE.
2  *
3  * Copyright (C) 1999, 2000 Scott Wimer
4  * Copyright (C) 2004, Matthias Clasen <mclasen@redhat.com>
5  * Copyright (C) 2005 - 2007, Marco Barisione <marco@barisione.org>
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25
26 #ifdef USE_SYSTEM_PCRE
27 #include <pcre.h>
28 #else
29 #include "pcre/pcre.h"
30 #endif
31
32 #include "gtypes.h"
33 #include "gregex.h"
34 #include "glibintl.h"
35 #include "glist.h"
36 #include "gmessages.h"
37 #include "gstrfuncs.h"
38 #include "gatomic.h"
39 #include "gthread.h"
40
41 /**
42  * SECTION:gregex
43  * @title: Perl-compatible regular expressions
44  * @short_description: matches strings against regular expressions
45  * @see_also: <xref linkend="glib-regex-syntax"/>
46  *
47  * The <function>g_regex_*()</function> functions implement regular
48  * expression pattern matching using syntax and semantics similar to
49  * Perl regular expression.
50  *
51  * Some functions accept a @start_position argument, setting it differs
52  * from just passing over a shortened string and setting #G_REGEX_MATCH_NOTBOL
53  * in the case of a pattern that begins with any kind of lookbehind assertion.
54  * For example, consider the pattern "\Biss\B" which finds occurrences of "iss"
55  * in the middle of words. ("\B" matches only if the current position in the
56  * subject is not a word boundary.) When applied to the string "Mississipi"
57  * from the fourth byte, namely "issipi", it does not match, because "\B" is
58  * always false at the start of the subject, which is deemed to be a word
59  * boundary. However, if the entire string is passed , but with
60  * @start_position set to 4, it finds the second occurrence of "iss" because
61  * it is able to look behind the starting point to discover that it is
62  * preceded by a letter.
63  *
64  * Note that, unless you set the #G_REGEX_RAW flag, all the strings passed
65  * to these functions must be encoded in UTF-8. The lengths and the positions
66  * inside the strings are in bytes and not in characters, so, for instance,
67  * "\xc3\xa0" (i.e. "&agrave;") is two bytes long but it is treated as a
68  * single character. If you set #G_REGEX_RAW the strings can be non-valid
69  * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two
70  * bytes and two characters long.
71  *
72  * When matching a pattern, "\n" matches only against a "\n" character in
73  * the string, and "\r" matches only a "\r" character. To match any newline
74  * sequence use "\R". This particular group matches either the two-character
75  * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed,
76  * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"),
77  * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line
78  * separator, U+2028), or PS (paragraph separator, U+2029).
79  *
80  * The behaviour of the dot, circumflex, and dollar metacharacters are
81  * affected by newline characters, the default is to recognize any newline
82  * character (the same characters recognized by "\R"). This can be changed
83  * with #G_REGEX_NEWLINE_CR, #G_REGEX_NEWLINE_LF and #G_REGEX_NEWLINE_CRLF
84  * compile options, and with #G_REGEX_MATCH_NEWLINE_ANY,
85  * #G_REGEX_MATCH_NEWLINE_CR, #G_REGEX_MATCH_NEWLINE_LF and
86  * #G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also
87  * relevant when compiling a pattern if #G_REGEX_EXTENDED is set, and an
88  * unescaped "#" outside a character class is encountered. This indicates
89  * a comment that lasts until after the next newline.
90  *
91  * When setting the %G_REGEX_JAVASCRIPT_COMPAT flag, pattern syntax and pattern
92  * matching is changed to be compatible with the way that regular expressions
93  * work in JavaScript. More precisely, a lonely ']' character in the pattern
94  * is a syntax error; the '\x' escape only allows 0 to 2 hexadecimal digits, and
95  * you must use the '\u' escape sequence with 4 hex digits to specify a unicode
96  * codepoint instead of '\x' or 'x{....}'. If '\x' or '\u' are not followed by
97  * the specified number of hex digits, they match 'x' and 'u' literally; also
98  * '\U' always matches 'U' instead of being an error in the pattern. Finally,
99  * pattern matching is modified so that back references to an unset subpattern
100  * group produces a match with the empty string instead of an error. See
101  * <ulink>man:pcreapi(3)<ulink> for more information.
102  *
103  * Creating and manipulating the same #GRegex structure from different
104  * threads is not a problem as #GRegex does not modify its internal
105  * state between creation and destruction, on the other hand #GMatchInfo
106  * is not threadsafe.
107  *
108  * The regular expressions low-level functionalities are obtained through
109  * the excellent <ulink url="http://www.pcre.org/">PCRE</ulink> library
110  * written by Philip Hazel.
111  */
112
113 /* Mask of all the possible values for GRegexCompileFlags. */
114 #define G_REGEX_COMPILE_MASK (G_REGEX_CASELESS          | \
115                               G_REGEX_MULTILINE         | \
116                               G_REGEX_DOTALL            | \
117                               G_REGEX_EXTENDED          | \
118                               G_REGEX_ANCHORED          | \
119                               G_REGEX_DOLLAR_ENDONLY    | \
120                               G_REGEX_UNGREEDY          | \
121                               G_REGEX_RAW               | \
122                               G_REGEX_NO_AUTO_CAPTURE   | \
123                               G_REGEX_OPTIMIZE          | \
124                               G_REGEX_FIRSTLINE         | \
125                               G_REGEX_DUPNAMES          | \
126                               G_REGEX_NEWLINE_CR        | \
127                               G_REGEX_NEWLINE_LF        | \
128                               G_REGEX_NEWLINE_CRLF      | \
129                               G_REGEX_NEWLINE_ANYCRLF   | \
130                               G_REGEX_BSR_ANYCRLF       | \
131                               G_REGEX_JAVASCRIPT_COMPAT)
132
133 /* Mask of all GRegexCompileFlags values that are (not) passed trough to PCRE */
134 #define G_REGEX_COMPILE_PCRE_MASK (G_REGEX_COMPILE_MASK & ~G_REGEX_COMPILE_NONPCRE_MASK)
135 #define G_REGEX_COMPILE_NONPCRE_MASK (G_REGEX_RAW              | \
136                                       G_REGEX_OPTIMIZE)
137
138 /* Mask of all the possible values for GRegexMatchFlags. */
139 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_ANCHORED         | \
140                             G_REGEX_MATCH_NOTBOL           | \
141                             G_REGEX_MATCH_NOTEOL           | \
142                             G_REGEX_MATCH_NOTEMPTY         | \
143                             G_REGEX_MATCH_PARTIAL          | \
144                             G_REGEX_MATCH_NEWLINE_CR       | \
145                             G_REGEX_MATCH_NEWLINE_LF       | \
146                             G_REGEX_MATCH_NEWLINE_CRLF     | \
147                             G_REGEX_MATCH_NEWLINE_ANY      | \
148                             G_REGEX_MATCH_NEWLINE_ANYCRLF  | \
149                             G_REGEX_MATCH_BSR_ANYCRLF      | \
150                             G_REGEX_MATCH_BSR_ANY          | \
151                             G_REGEX_MATCH_PARTIAL_SOFT     | \
152                             G_REGEX_MATCH_PARTIAL_HARD     | \
153                             G_REGEX_MATCH_NOTEMPTY_ATSTART)
154
155 /* we rely on these flags having the same values */
156 G_STATIC_ASSERT (G_REGEX_CASELESS          == PCRE_CASELESS);
157 G_STATIC_ASSERT (G_REGEX_MULTILINE         == PCRE_MULTILINE);
158 G_STATIC_ASSERT (G_REGEX_DOTALL            == PCRE_DOTALL);
159 G_STATIC_ASSERT (G_REGEX_EXTENDED          == PCRE_EXTENDED);
160 G_STATIC_ASSERT (G_REGEX_ANCHORED          == PCRE_ANCHORED);
161 G_STATIC_ASSERT (G_REGEX_DOLLAR_ENDONLY    == PCRE_DOLLAR_ENDONLY);
162 G_STATIC_ASSERT (G_REGEX_UNGREEDY          == PCRE_UNGREEDY);
163 G_STATIC_ASSERT (G_REGEX_NO_AUTO_CAPTURE   == PCRE_NO_AUTO_CAPTURE);
164 G_STATIC_ASSERT (G_REGEX_FIRSTLINE         == PCRE_FIRSTLINE);
165 G_STATIC_ASSERT (G_REGEX_DUPNAMES          == PCRE_DUPNAMES);
166 G_STATIC_ASSERT (G_REGEX_NEWLINE_CR        == PCRE_NEWLINE_CR);
167 G_STATIC_ASSERT (G_REGEX_NEWLINE_LF        == PCRE_NEWLINE_LF);
168 G_STATIC_ASSERT (G_REGEX_NEWLINE_CRLF      == PCRE_NEWLINE_CRLF);
169 G_STATIC_ASSERT (G_REGEX_NEWLINE_ANYCRLF   == PCRE_NEWLINE_ANYCRLF);
170 G_STATIC_ASSERT (G_REGEX_BSR_ANYCRLF       == PCRE_BSR_ANYCRLF);
171 G_STATIC_ASSERT (G_REGEX_JAVASCRIPT_COMPAT == PCRE_JAVASCRIPT_COMPAT);
172
173 G_STATIC_ASSERT (G_REGEX_MATCH_ANCHORED         == PCRE_ANCHORED);
174 G_STATIC_ASSERT (G_REGEX_MATCH_NOTBOL           == PCRE_NOTBOL);
175 G_STATIC_ASSERT (G_REGEX_MATCH_NOTEOL           == PCRE_NOTEOL);
176 G_STATIC_ASSERT (G_REGEX_MATCH_NOTEMPTY         == PCRE_NOTEMPTY);
177 G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL          == PCRE_PARTIAL);
178 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_CR       == PCRE_NEWLINE_CR);
179 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_LF       == PCRE_NEWLINE_LF);
180 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_CRLF     == PCRE_NEWLINE_CRLF);
181 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_ANY      == PCRE_NEWLINE_ANY);
182 G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_ANYCRLF  == PCRE_NEWLINE_ANYCRLF);
183 G_STATIC_ASSERT (G_REGEX_MATCH_BSR_ANYCRLF      == PCRE_BSR_ANYCRLF);
184 G_STATIC_ASSERT (G_REGEX_MATCH_BSR_ANY          == PCRE_BSR_UNICODE);
185 G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL_SOFT     == PCRE_PARTIAL_SOFT);
186 G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL_HARD     == PCRE_PARTIAL_HARD);
187 G_STATIC_ASSERT (G_REGEX_MATCH_NOTEMPTY_ATSTART == PCRE_NOTEMPTY_ATSTART);
188
189 /* These PCRE flags are unused or not exposed publically in GRegexFlags, so
190  * it should be ok to reuse them for different things.
191  */
192 G_STATIC_ASSERT (G_REGEX_OPTIMIZE          == PCRE_NO_UTF8_CHECK);
193 G_STATIC_ASSERT (G_REGEX_RAW               == PCRE_UTF8);
194
195 /* if the string is in UTF-8 use g_utf8_ functions, else use
196  * use just +/- 1. */
197 #define NEXT_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
198                                 ((s) + 1) : \
199                                 g_utf8_next_char (s))
200 #define PREV_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
201                                 ((s) - 1) : \
202                                 g_utf8_prev_char (s))
203
204 struct _GMatchInfo
205 {
206   volatile gint ref_count;      /* the ref count */
207   GRegex *regex;                /* the regex */
208   GRegexMatchFlags match_opts;  /* options used at match time on the regex */
209   gint matches;                 /* number of matching sub patterns */
210   gint pos;                     /* position in the string where last match left off */
211   gint  n_offsets;              /* number of offsets */
212   gint *offsets;                /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
213   gint *workspace;              /* workspace for pcre_dfa_exec() */
214   gint n_workspace;             /* number of workspace elements */
215   const gchar *string;          /* string passed to the match function */
216   gssize string_len;            /* length of string */
217 };
218
219 struct _GRegex
220 {
221   volatile gint ref_count;      /* the ref count for the immutable part */
222   gchar *pattern;               /* the pattern */
223   pcre *pcre_re;                /* compiled form of the pattern */
224   GRegexCompileFlags compile_opts;      /* options used at compile time on the pattern */
225   GRegexMatchFlags match_opts;  /* options used at match time on the regex */
226   pcre_extra *extra;            /* data stored when G_REGEX_OPTIMIZE is used */
227 };
228
229 /* TRUE if ret is an error code, FALSE otherwise. */
230 #define IS_PCRE_ERROR(ret) ((ret) < PCRE_ERROR_NOMATCH && (ret) != PCRE_ERROR_PARTIAL)
231
232 typedef struct _InterpolationData InterpolationData;
233 static gboolean  interpolation_list_needs_match (GList *list);
234 static gboolean  interpolate_replacement        (const GMatchInfo *match_info,
235                                                  GString *result,
236                                                  gpointer data);
237 static GList    *split_replacement              (const gchar *replacement,
238                                                  GError **error);
239 static void      free_interpolation_data        (InterpolationData *data);
240
241
242 static const gchar *
243 match_error (gint errcode)
244 {
245   switch (errcode)
246     {
247     case PCRE_ERROR_NOMATCH:
248       /* not an error */
249       break;
250     case PCRE_ERROR_NULL:
251       /* NULL argument, this should not happen in GRegex */
252       g_warning ("A NULL argument was passed to PCRE");
253       break;
254     case PCRE_ERROR_BADOPTION:
255       return "bad options";
256     case PCRE_ERROR_BADMAGIC:
257       return _("corrupted object");
258     case PCRE_ERROR_UNKNOWN_OPCODE:
259       return N_("internal error or corrupted object");
260     case PCRE_ERROR_NOMEMORY:
261       return _("out of memory");
262     case PCRE_ERROR_NOSUBSTRING:
263       /* not used by pcre_exec() */
264       break;
265     case PCRE_ERROR_MATCHLIMIT:
266       return _("backtracking limit reached");
267     case PCRE_ERROR_CALLOUT:
268       /* callouts are not implemented */
269       break;
270     case PCRE_ERROR_BADUTF8:
271     case PCRE_ERROR_BADUTF8_OFFSET:
272       /* we do not check if strings are valid */
273       break;
274     case PCRE_ERROR_PARTIAL:
275       /* not an error */
276       break;
277     case PCRE_ERROR_BADPARTIAL:
278       return _("the pattern contains items not supported for partial matching");
279     case PCRE_ERROR_INTERNAL:
280       return _("internal error");
281     case PCRE_ERROR_BADCOUNT:
282       /* negative ovecsize, this should not happen in GRegex */
283       g_warning ("A negative ovecsize was passed to PCRE");
284       break;
285     case PCRE_ERROR_DFA_UITEM:
286       return _("the pattern contains items not supported for partial matching");
287     case PCRE_ERROR_DFA_UCOND:
288       return _("back references as conditions are not supported for partial matching");
289     case PCRE_ERROR_DFA_UMLIMIT:
290       /* the match_field field is not used in GRegex */
291       break;
292     case PCRE_ERROR_DFA_WSSIZE:
293       /* handled expanding the workspace */
294       break;
295     case PCRE_ERROR_DFA_RECURSE:
296     case PCRE_ERROR_RECURSIONLIMIT:
297       return _("recursion limit reached");
298     case PCRE_ERROR_BADNEWLINE:
299       return _("invalid combination of newline flags");
300     case PCRE_ERROR_BADOFFSET:
301       return _("bad offset");
302     case PCRE_ERROR_SHORTUTF8:
303       return _("short utf8");
304     case PCRE_ERROR_RECURSELOOP:
305       return _("recursion loop");
306     default:
307       break;
308     }
309   return _("unknown error");
310 }
311
312 static void
313 translate_compile_error (gint *errcode, const gchar **errmsg)
314 {
315   /* Compile errors are created adding 100 to the error code returned
316    * by PCRE.
317    * If errcode is known we put the translatable error message in
318    * erromsg. If errcode is unknown we put the generic
319    * G_REGEX_ERROR_COMPILE error code in errcode and keep the
320    * untranslated error message returned by PCRE.
321    * Note that there can be more PCRE errors with the same GRegexError
322    * and that some PCRE errors are useless for us.
323    */
324   *errcode += 100;
325
326   switch (*errcode)
327     {
328     case G_REGEX_ERROR_STRAY_BACKSLASH:
329       *errmsg = _("\\ at end of pattern");
330       break;
331     case G_REGEX_ERROR_MISSING_CONTROL_CHAR:
332       *errmsg = _("\\c at end of pattern");
333       break;
334     case G_REGEX_ERROR_UNRECOGNIZED_ESCAPE:
335       *errmsg = _("unrecognized character follows \\");
336       break;
337     case G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER:
338       *errmsg = _("numbers out of order in {} quantifier");
339       break;
340     case G_REGEX_ERROR_QUANTIFIER_TOO_BIG:
341       *errmsg = _("number too big in {} quantifier");
342       break;
343     case G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS:
344       *errmsg = _("missing terminating ] for character class");
345       break;
346     case G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS:
347       *errmsg = _("invalid escape sequence in character class");
348       break;
349     case G_REGEX_ERROR_RANGE_OUT_OF_ORDER:
350       *errmsg = _("range out of order in character class");
351       break;
352     case G_REGEX_ERROR_NOTHING_TO_REPEAT:
353       *errmsg = _("nothing to repeat");
354       break;
355     case 111: /* internal error: unexpected repeat */
356       *errcode = G_REGEX_ERROR_INTERNAL;
357       *errmsg = _("unexpected repeat");
358       break;
359     case G_REGEX_ERROR_UNRECOGNIZED_CHARACTER:
360       *errmsg = _("unrecognized character after (? or (?-");
361       break;
362     case G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS:
363       *errmsg = _("POSIX named classes are supported only within a class");
364       break;
365     case G_REGEX_ERROR_UNMATCHED_PARENTHESIS:
366       *errmsg = _("missing terminating )");
367       break;
368     case G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE:
369       *errmsg = _("reference to non-existent subpattern");
370       break;
371     case G_REGEX_ERROR_UNTERMINATED_COMMENT:
372       *errmsg = _("missing ) after comment");
373       break;
374     case G_REGEX_ERROR_EXPRESSION_TOO_LARGE:
375       *errmsg = _("regular expression is too large");
376       break;
377     case G_REGEX_ERROR_MEMORY_ERROR:
378       *errmsg = _("failed to get memory");
379       break;
380     case 122: /* unmatched parentheses */
381       *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
382       *errmsg = _(") without opening (");
383       break;
384     case 123: /* internal error: code overflow */
385       *errcode = G_REGEX_ERROR_INTERNAL;
386       *errmsg = _("code overflow");
387       break;
388     case 124: /* "unrecognized character after (?<\0 */
389       *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
390       *errmsg = _("unrecognized character after (?<");
391       break;
392     case G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND:
393       *errmsg = _("lookbehind assertion is not fixed length");
394       break;
395     case G_REGEX_ERROR_MALFORMED_CONDITION:
396       *errmsg = _("malformed number or name after (?(");
397       break;
398     case G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES:
399       *errmsg = _("conditional group contains more than two branches");
400       break;
401     case G_REGEX_ERROR_ASSERTION_EXPECTED:
402       *errmsg = _("assertion expected after (?(");
403       break;
404     case 129:
405       *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
406       /* translators: '(?R' and '(?[+-]digits' are both meant as (groups of)
407        * sequences here, '(?-54' would be an example for the second group.
408        */
409       *errmsg = _("(?R or (?[+-]digits must be followed by )");
410       break;
411     case G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME:
412       *errmsg = _("unknown POSIX class name");
413       break;
414     case G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED:
415       *errmsg = _("POSIX collating elements are not supported");
416       break;
417     case G_REGEX_ERROR_HEX_CODE_TOO_LARGE:
418       *errmsg = _("character value in \\x{...} sequence is too large");
419       break;
420     case G_REGEX_ERROR_INVALID_CONDITION:
421       *errmsg = _("invalid condition (?(0)");
422       break;
423     case G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND:
424       *errmsg = _("\\C not allowed in lookbehind assertion");
425       break;
426     case 137: /* PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0 */
427       /* A number of Perl escapes are not handled by PCRE.
428        * Therefore it explicitly raises ERR37.
429        */
430       *errcode = G_REGEX_ERROR_UNRECOGNIZED_ESCAPE;
431       *errmsg = _("escapes \\L, \\l, \\N{name}, \\U, and \\u are not supported");
432       break;
433     case G_REGEX_ERROR_INFINITE_LOOP:
434       *errmsg = _("recursive call could loop indefinitely");
435       break;
436     case 141: /* unrecognized character after (?P\0 */
437       *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
438       *errmsg = _("unrecognized character after (?P");
439       break;
440     case G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR:
441       *errmsg = _("missing terminator in subpattern name");
442       break;
443     case G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME:
444       *errmsg = _("two named subpatterns have the same name");
445       break;
446     case G_REGEX_ERROR_MALFORMED_PROPERTY:
447       *errmsg = _("malformed \\P or \\p sequence");
448       break;
449     case G_REGEX_ERROR_UNKNOWN_PROPERTY:
450       *errmsg = _("unknown property name after \\P or \\p");
451       break;
452     case G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG:
453       *errmsg = _("subpattern name is too long (maximum 32 characters)");
454       break;
455     case G_REGEX_ERROR_TOO_MANY_SUBPATTERNS:
456       *errmsg = _("too many named subpatterns (maximum 10,000)");
457       break;
458     case G_REGEX_ERROR_INVALID_OCTAL_VALUE:
459       *errmsg = _("octal value is greater than \\377");
460       break;
461     case 152: /* internal error: overran compiling workspace */
462       *errcode = G_REGEX_ERROR_INTERNAL;
463       *errmsg = _("overran compiling workspace");
464       break;
465     case 153: /* internal error: previously-checked referenced subpattern not found */
466       *errcode = G_REGEX_ERROR_INTERNAL;
467       *errmsg = _("previously-checked referenced subpattern not found");
468       break;
469     case G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE:
470       *errmsg = _("DEFINE group contains more than one branch");
471       break;
472     case G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS:
473       *errmsg = _("inconsistent NEWLINE options");
474       break;
475     case G_REGEX_ERROR_MISSING_BACK_REFERENCE:
476       *errmsg = _("\\g is not followed by a braced, angle-bracketed, or quoted name or "
477                   "number, or by a plain number");
478       break;
479     case G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE:
480       *errmsg = _("a numbered reference must not be zero");
481       break;
482     case G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN:
483       *errmsg = _("an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)");
484       break;
485     case G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB:
486       *errmsg = _("(*VERB) not recognized");
487       break;
488     case G_REGEX_ERROR_NUMBER_TOO_BIG:
489       *errmsg = _("number is too bug");
490       break;
491     case G_REGEX_ERROR_MISSING_SUBPATTERN_NAME:
492       *errmsg = _("missing subpattern name after (?&");
493       break;
494     case G_REGEX_ERROR_MISSING_DIGIT:
495       *errmsg = _("digit expected after (?+");
496       break;
497     case G_REGEX_ERROR_INVALID_DATA_CHARACTER:
498       *errmsg = _("] is an invalid data character in JavaScript compatibility mode");
499       break;
500     case G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME:
501       *errmsg = _("different names for subpatterns of the same number are not allowed");
502       break;
503     case G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED:
504       *errmsg = _("(*MARK) must have an argument");
505       break;
506     case G_REGEX_ERROR_INVALID_CONTROL_CHAR:
507       *errmsg = _( "\\c must be followed by an ASCII character");
508       break;
509     case G_REGEX_ERROR_MISSING_NAME:
510       *errmsg = _("\\k is not followed by a braced, angle-bracketed, or quoted name");
511       break;
512     case G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS:
513       *errmsg = _("\\N is not supported in a class");
514       break;
515     case G_REGEX_ERROR_TOO_MANY_FORWARD_REFERENCES:
516       *errmsg = _("too many forward references");
517       break;
518     case G_REGEX_ERROR_NAME_TOO_LONG:
519       *errmsg = _("name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)");
520       break;
521
522     case 116: /* erroffset passed as NULL */
523       /* This should not happen as we never pass a NULL erroffset */
524       g_warning ("erroffset passed as NULL");
525       *errcode = G_REGEX_ERROR_COMPILE;
526       break;
527     case 117: /* unknown option bit(s) set */
528       /* This should not happen as we check options before passing them
529        * to pcre_compile2() */
530       g_warning ("unknown option bit(s) set");
531       *errcode = G_REGEX_ERROR_COMPILE;
532       break;
533     case 132: /* this version of PCRE is compiled without UTF support */
534     case 144: /* invalid UTF-8 string */
535     case 145: /* support for \\P, \\p, and \\X has not been compiled */
536     case 167: /* this version of PCRE is not compiled with Unicode property support */
537     case 173: /* disallowed Unicode code point (>= 0xd800 && <= 0xdfff) */
538     case 174: /* invalid UTF-16 string */
539       /* These errors should not happen as we are using an UTF-8 and UCP-enabled PCRE
540        * and we do not check if strings are valid */
541     case 170: /* internal error: unknown opcode in find_fixedlength() */
542       *errcode = G_REGEX_ERROR_INTERNAL;
543       break;
544
545     default:
546       *errcode = G_REGEX_ERROR_COMPILE;
547     }
548 }
549
550 /* GMatchInfo */
551
552 static GMatchInfo *
553 match_info_new (const GRegex *regex,
554                 const gchar  *string,
555                 gint          string_len,
556                 gint          start_position,
557                 gint          match_options,
558                 gboolean      is_dfa)
559 {
560   GMatchInfo *match_info;
561
562   if (string_len < 0)
563     string_len = strlen (string);
564
565   match_info = g_new0 (GMatchInfo, 1);
566   match_info->ref_count = 1;
567   match_info->regex = g_regex_ref ((GRegex *)regex);
568   match_info->string = string;
569   match_info->string_len = string_len;
570   match_info->matches = PCRE_ERROR_NOMATCH;
571   match_info->pos = start_position;
572   match_info->match_opts = match_options;
573
574   if (is_dfa)
575     {
576       /* These values should be enough for most cases, if they are not
577        * enough g_regex_match_all_full() will expand them. */
578       match_info->n_offsets = 24;
579       match_info->n_workspace = 100;
580       match_info->workspace = g_new (gint, match_info->n_workspace);
581     }
582   else
583     {
584       gint capture_count;
585       pcre_fullinfo (regex->pcre_re, regex->extra,
586                      PCRE_INFO_CAPTURECOUNT, &capture_count);
587       match_info->n_offsets = (capture_count + 1) * 3;
588     }
589
590   match_info->offsets = g_new0 (gint, match_info->n_offsets);
591   /* Set an invalid position for the previous match. */
592   match_info->offsets[0] = -1;
593   match_info->offsets[1] = -1;
594
595   return match_info;
596 }
597
598 /**
599  * g_match_info_get_regex:
600  * @match_info: a #GMatchInfo
601  *
602  * Returns #GRegex object used in @match_info. It belongs to Glib
603  * and must not be freed. Use g_regex_ref() if you need to keep it
604  * after you free @match_info object.
605  *
606  * Returns: #GRegex object used in @match_info
607  *
608  * Since: 2.14
609  */
610 GRegex *
611 g_match_info_get_regex (const GMatchInfo *match_info)
612 {
613   g_return_val_if_fail (match_info != NULL, NULL);
614   return match_info->regex;
615 }
616
617 /**
618  * g_match_info_get_string:
619  * @match_info: a #GMatchInfo
620  *
621  * Returns the string searched with @match_info. This is the
622  * string passed to g_regex_match() or g_regex_replace() so
623  * you may not free it before calling this function.
624  *
625  * Returns: the string searched with @match_info
626  *
627  * Since: 2.14
628  */
629 const gchar *
630 g_match_info_get_string (const GMatchInfo *match_info)
631 {
632   g_return_val_if_fail (match_info != NULL, NULL);
633   return match_info->string;
634 }
635
636 /**
637  * g_match_info_ref:
638  * @match_info: a #GMatchInfo
639  *
640  * Increases reference count of @match_info by 1.
641  *
642  * Returns: @match_info
643  *
644  * Since: 2.30
645  */
646 GMatchInfo       *
647 g_match_info_ref (GMatchInfo *match_info)
648 {
649   g_return_val_if_fail (match_info != NULL, NULL);
650   g_atomic_int_inc (&match_info->ref_count);
651   return match_info;
652 }
653
654 /**
655  * g_match_info_unref:
656  * @match_info: a #GMatchInfo
657  *
658  * Decreases reference count of @match_info by 1. When reference count drops
659  * to zero, it frees all the memory associated with the match_info structure.
660  *
661  * Since: 2.30
662  */
663 void
664 g_match_info_unref (GMatchInfo *match_info)
665 {
666   if (g_atomic_int_dec_and_test (&match_info->ref_count))
667     {
668       g_regex_unref (match_info->regex);
669       g_free (match_info->offsets);
670       g_free (match_info->workspace);
671       g_free (match_info);
672     }
673 }
674
675 /**
676  * g_match_info_free:
677  * @match_info: (allow-none): a #GMatchInfo, or %NULL
678  *
679  * If @match_info is not %NULL, calls g_match_info_unref(); otherwise does
680  * nothing.
681  *
682  * Since: 2.14
683  */
684 void
685 g_match_info_free (GMatchInfo *match_info)
686 {
687   if (match_info == NULL)
688     return;
689
690   g_match_info_unref (match_info);
691 }
692
693 /**
694  * g_match_info_next:
695  * @match_info: a #GMatchInfo structure
696  * @error: location to store the error occurring, or %NULL to ignore errors
697  *
698  * Scans for the next match using the same parameters of the previous
699  * call to g_regex_match_full() or g_regex_match() that returned
700  * @match_info.
701  *
702  * The match is done on the string passed to the match function, so you
703  * cannot free it before calling this function.
704  *
705  * Returns: %TRUE is the string matched, %FALSE otherwise
706  *
707  * Since: 2.14
708  */
709 gboolean
710 g_match_info_next (GMatchInfo  *match_info,
711                    GError     **error)
712 {
713   gint prev_match_start;
714   gint prev_match_end;
715
716   g_return_val_if_fail (match_info != NULL, FALSE);
717   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
718   g_return_val_if_fail (match_info->pos >= 0, FALSE);
719
720   prev_match_start = match_info->offsets[0];
721   prev_match_end = match_info->offsets[1];
722
723   if (match_info->pos > match_info->string_len)
724     {
725       /* we have reached the end of the string */
726       match_info->pos = -1;
727       match_info->matches = PCRE_ERROR_NOMATCH;
728       return FALSE;
729     }
730
731   match_info->matches = pcre_exec (match_info->regex->pcre_re,
732                                    match_info->regex->extra,
733                                    match_info->string,
734                                    match_info->string_len,
735                                    match_info->pos,
736                                    match_info->regex->match_opts | match_info->match_opts,
737                                    match_info->offsets,
738                                    match_info->n_offsets);
739   if (IS_PCRE_ERROR (match_info->matches))
740     {
741       g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
742                    _("Error while matching regular expression %s: %s"),
743                    match_info->regex->pattern, match_error (match_info->matches));
744       return FALSE;
745     }
746
747   /* avoid infinite loops if the pattern is an empty string or something
748    * equivalent */
749   if (match_info->pos == match_info->offsets[1])
750     {
751       if (match_info->pos > match_info->string_len)
752         {
753           /* we have reached the end of the string */
754           match_info->pos = -1;
755           match_info->matches = PCRE_ERROR_NOMATCH;
756           return FALSE;
757         }
758
759       match_info->pos = NEXT_CHAR (match_info->regex,
760                                    &match_info->string[match_info->pos]) -
761                                    match_info->string;
762     }
763   else
764     {
765       match_info->pos = match_info->offsets[1];
766     }
767
768   /* it's possible to get two identical matches when we are matching
769    * empty strings, for instance if the pattern is "(?=[A-Z0-9])" and
770    * the string is "RegExTest" we have:
771    *  - search at position 0: match from 0 to 0
772    *  - search at position 1: match from 3 to 3
773    *  - search at position 3: match from 3 to 3 (duplicate)
774    *  - search at position 4: match from 5 to 5
775    *  - search at position 5: match from 5 to 5 (duplicate)
776    *  - search at position 6: no match -> stop
777    * so we have to ignore the duplicates.
778    * see bug #515944: http://bugzilla.gnome.org/show_bug.cgi?id=515944 */
779   if (match_info->matches >= 0 &&
780       prev_match_start == match_info->offsets[0] &&
781       prev_match_end == match_info->offsets[1])
782     {
783       /* ignore this match and search the next one */
784       return g_match_info_next (match_info, error);
785     }
786
787   return match_info->matches >= 0;
788 }
789
790 /**
791  * g_match_info_matches:
792  * @match_info: a #GMatchInfo structure
793  *
794  * Returns whether the previous match operation succeeded.
795  *
796  * Returns: %TRUE if the previous match operation succeeded,
797  *   %FALSE otherwise
798  *
799  * Since: 2.14
800  */
801 gboolean
802 g_match_info_matches (const GMatchInfo *match_info)
803 {
804   g_return_val_if_fail (match_info != NULL, FALSE);
805
806   return match_info->matches >= 0;
807 }
808
809 /**
810  * g_match_info_get_match_count:
811  * @match_info: a #GMatchInfo structure
812  *
813  * Retrieves the number of matched substrings (including substring 0,
814  * that is the whole matched text), so 1 is returned if the pattern
815  * has no substrings in it and 0 is returned if the match failed.
816  *
817  * If the last match was obtained using the DFA algorithm, that is
818  * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
819  * count is not that of the number of capturing parentheses but that of
820  * the number of matched substrings.
821  *
822  * Returns: Number of matched substrings, or -1 if an error occurred
823  *
824  * Since: 2.14
825  */
826 gint
827 g_match_info_get_match_count (const GMatchInfo *match_info)
828 {
829   g_return_val_if_fail (match_info, -1);
830
831   if (match_info->matches == PCRE_ERROR_NOMATCH)
832     /* no match */
833     return 0;
834   else if (match_info->matches < PCRE_ERROR_NOMATCH)
835     /* error */
836     return -1;
837   else
838     /* match */
839     return match_info->matches;
840 }
841
842 /**
843  * g_match_info_is_partial_match:
844  * @match_info: a #GMatchInfo structure
845  *
846  * Usually if the string passed to g_regex_match*() matches as far as
847  * it goes, but is too short to match the entire pattern, %FALSE is
848  * returned. There are circumstances where it might be helpful to
849  * distinguish this case from other cases in which there is no match.
850  *
851  * Consider, for example, an application where a human is required to
852  * type in data for a field with specific formatting requirements. An
853  * example might be a date in the form ddmmmyy, defined by the pattern
854  * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
855  * If the application sees the user’s keystrokes one by one, and can
856  * check that what has been typed so far is potentially valid, it is
857  * able to raise an error as soon as a mistake is made.
858  *
859  * GRegex supports the concept of partial matching by means of the
860  * #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD flags.
861  * When they are used, the return code for
862  * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
863  * for a complete match, %FALSE otherwise. But, when these functions
864  * return %FALSE, you can check if the match was partial calling
865  * g_match_info_is_partial_match().
866  *
867  * The difference between #G_REGEX_MATCH_PARTIAL_SOFT and 
868  * #G_REGEX_MATCH_PARTIAL_HARD is that when a partial match is encountered
869  * with #G_REGEX_MATCH_PARTIAL_SOFT, matching continues to search for a
870  * possible complete match, while with #G_REGEX_MATCH_PARTIAL_HARD matching
871  * stops at the partial match.
872  * When both #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD
873  * are set, the latter takes precedence.
874  * See <ulink>man:pcrepartial</ulink> for more information on partial matching.
875  *
876  * Because of the way certain internal optimizations are implemented
877  * the partial matching algorithm cannot be used with all patterns.
878  * So repeated single characters such as "a{2,4}" and repeated single
879  * meta-sequences such as "\d+" are not permitted if the maximum number
880  * of occurrences is greater than one. Optional items such as "\d?"
881  * (where the maximum is one) are permitted. Quantifiers with any values
882  * are permitted after parentheses, so the invalid examples above can be
883  * coded thus "(a){2,4}" and "(\d)+". If #G_REGEX_MATCH_PARTIAL or 
884  * #G_REGEX_MATCH_PARTIAL_HARD is set
885  * for a pattern that does not conform to the restrictions, matching
886  * functions return an error.
887  *
888  * Returns: %TRUE if the match was partial, %FALSE otherwise
889  *
890  * Since: 2.14
891  */
892 gboolean
893 g_match_info_is_partial_match (const GMatchInfo *match_info)
894 {
895   g_return_val_if_fail (match_info != NULL, FALSE);
896
897   return match_info->matches == PCRE_ERROR_PARTIAL;
898 }
899
900 /**
901  * g_match_info_expand_references:
902  * @match_info: (allow-none): a #GMatchInfo or %NULL
903  * @string_to_expand: the string to expand
904  * @error: location to store the error occurring, or %NULL to ignore errors
905  *
906  * Returns a new string containing the text in @string_to_expand with
907  * references and escape sequences expanded. References refer to the last
908  * match done with @string against @regex and have the same syntax used by
909  * g_regex_replace().
910  *
911  * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
912  * passed to g_regex_new().
913  *
914  * The backreferences are extracted from the string passed to the match
915  * function, so you cannot call this function after freeing the string.
916  *
917  * @match_info may be %NULL in which case @string_to_expand must not
918  * contain references. For instance "foo\n" does not refer to an actual
919  * pattern and '\n' merely will be replaced with \n character,
920  * while to expand "\0" (whole match) one needs the result of a match.
921  * Use g_regex_check_replacement() to find out whether @string_to_expand
922  * contains references.
923  *
924  * Returns: (allow-none): the expanded string, or %NULL if an error occurred
925  *
926  * Since: 2.14
927  */
928 gchar *
929 g_match_info_expand_references (const GMatchInfo  *match_info,
930                                 const gchar       *string_to_expand,
931                                 GError           **error)
932 {
933   GString *result;
934   GList *list;
935   GError *tmp_error = NULL;
936
937   g_return_val_if_fail (string_to_expand != NULL, NULL);
938   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
939
940   list = split_replacement (string_to_expand, &tmp_error);
941   if (tmp_error != NULL)
942     {
943       g_propagate_error (error, tmp_error);
944       return NULL;
945     }
946
947   if (!match_info && interpolation_list_needs_match (list))
948     {
949       g_critical ("String '%s' contains references to the match, can't "
950                   "expand references without GMatchInfo object",
951                   string_to_expand);
952       return NULL;
953     }
954
955   result = g_string_sized_new (strlen (string_to_expand));
956   interpolate_replacement (match_info, result, list);
957
958   g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
959
960   return g_string_free (result, FALSE);
961 }
962
963 /**
964  * g_match_info_fetch:
965  * @match_info: #GMatchInfo structure
966  * @match_num: number of the sub expression
967  *
968  * Retrieves the text matching the @match_num<!-- -->'th capturing
969  * parentheses. 0 is the full text of the match, 1 is the first paren
970  * set, 2 the second, and so on.
971  *
972  * If @match_num is a valid sub pattern but it didn't match anything
973  * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
974  * string is returned.
975  *
976  * If the match was obtained using the DFA algorithm, that is using
977  * g_regex_match_all() or g_regex_match_all_full(), the retrieved
978  * string is not that of a set of parentheses but that of a matched
979  * substring. Substrings are matched in reverse order of length, so
980  * 0 is the longest match.
981  *
982  * The string is fetched from the string passed to the match function,
983  * so you cannot call this function after freeing the string.
984  *
985  * Returns: (allow-none): The matched substring, or %NULL if an error
986  *     occurred. You have to free the string yourself
987  *
988  * Since: 2.14
989  */
990 gchar *
991 g_match_info_fetch (const GMatchInfo *match_info,
992                     gint              match_num)
993 {
994   /* we cannot use pcre_get_substring() because it allocates the
995    * string using pcre_malloc(). */
996   gchar *match = NULL;
997   gint start, end;
998
999   g_return_val_if_fail (match_info != NULL, NULL);
1000   g_return_val_if_fail (match_num >= 0, NULL);
1001
1002   /* match_num does not exist or it didn't matched, i.e. matching "b"
1003    * against "(a)?b" then group 0 is empty. */
1004   if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
1005     match = NULL;
1006   else if (start == -1)
1007     match = g_strdup ("");
1008   else
1009     match = g_strndup (&match_info->string[start], end - start);
1010
1011   return match;
1012 }
1013
1014 /**
1015  * g_match_info_fetch_pos:
1016  * @match_info: #GMatchInfo structure
1017  * @match_num: number of the sub expression
1018  * @start_pos: (out) (allow-none): pointer to location where to store
1019  *     the start position, or %NULL
1020  * @end_pos: (out) (allow-none): pointer to location where to store
1021  *     the end position, or %NULL
1022  *
1023  * Retrieves the position in bytes of the @match_num<!-- -->'th capturing
1024  * parentheses. 0 is the full text of the match, 1 is the first
1025  * paren set, 2 the second, and so on.
1026  *
1027  * If @match_num is a valid sub pattern but it didn't match anything
1028  * (e.g. sub pattern 1, matching "b" against "(a)?b") then @start_pos
1029  * and @end_pos are set to -1 and %TRUE is returned.
1030  *
1031  * If the match was obtained using the DFA algorithm, that is using
1032  * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1033  * position is not that of a set of parentheses but that of a matched
1034  * substring. Substrings are matched in reverse order of length, so
1035  * 0 is the longest match.
1036  *
1037  * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
1038  *   the position cannot be fetched, @start_pos and @end_pos are left
1039  *   unchanged
1040  *
1041  * Since: 2.14
1042  */
1043 gboolean
1044 g_match_info_fetch_pos (const GMatchInfo *match_info,
1045                         gint              match_num,
1046                         gint             *start_pos,
1047                         gint             *end_pos)
1048 {
1049   g_return_val_if_fail (match_info != NULL, FALSE);
1050   g_return_val_if_fail (match_num >= 0, FALSE);
1051
1052   /* make sure the sub expression number they're requesting is less than
1053    * the total number of sub expressions that were matched. */
1054   if (match_num >= match_info->matches)
1055     return FALSE;
1056
1057   if (start_pos != NULL)
1058     *start_pos = match_info->offsets[2 * match_num];
1059
1060   if (end_pos != NULL)
1061     *end_pos = match_info->offsets[2 * match_num + 1];
1062
1063   return TRUE;
1064 }
1065
1066 /*
1067  * Returns number of first matched subpattern with name @name.
1068  * There may be more than one in case when DUPNAMES is used,
1069  * and not all subpatterns with that name match;
1070  * pcre_get_stringnumber() does not work in that case.
1071  */
1072 static gint
1073 get_matched_substring_number (const GMatchInfo *match_info,
1074                               const gchar      *name)
1075 {
1076   gint entrysize;
1077   gchar *first, *last;
1078   guchar *entry;
1079
1080   if (!(match_info->regex->compile_opts & G_REGEX_DUPNAMES))
1081     return pcre_get_stringnumber (match_info->regex->pcre_re, name);
1082
1083   /* This code is copied from pcre_get.c: get_first_set() */
1084   entrysize = pcre_get_stringtable_entries (match_info->regex->pcre_re,
1085                                             name,
1086                                             &first,
1087                                             &last);
1088
1089   if (entrysize <= 0)
1090     return entrysize;
1091
1092   for (entry = (guchar*) first; entry <= (guchar*) last; entry += entrysize)
1093     {
1094       gint n = (entry[0] << 8) + entry[1];
1095       if (match_info->offsets[n*2] >= 0)
1096         return n;
1097     }
1098
1099   return (first[0] << 8) + first[1];
1100 }
1101
1102 /**
1103  * g_match_info_fetch_named:
1104  * @match_info: #GMatchInfo structure
1105  * @name: name of the subexpression
1106  *
1107  * Retrieves the text matching the capturing parentheses named @name.
1108  *
1109  * If @name is a valid sub pattern name but it didn't match anything
1110  * (e.g. sub pattern "X", matching "b" against "(?P&lt;X&gt;a)?b")
1111  * then an empty string is returned.
1112  *
1113  * The string is fetched from the string passed to the match function,
1114  * so you cannot call this function after freeing the string.
1115  *
1116  * Returns: (allow-none): The matched substring, or %NULL if an error
1117  *     occurred. You have to free the string yourself
1118  *
1119  * Since: 2.14
1120  */
1121 gchar *
1122 g_match_info_fetch_named (const GMatchInfo *match_info,
1123                           const gchar      *name)
1124 {
1125   /* we cannot use pcre_get_named_substring() because it allocates the
1126    * string using pcre_malloc(). */
1127   gint num;
1128
1129   g_return_val_if_fail (match_info != NULL, NULL);
1130   g_return_val_if_fail (name != NULL, NULL);
1131
1132   num = get_matched_substring_number (match_info, name);
1133   if (num < 0)
1134     return NULL;
1135   else
1136     return g_match_info_fetch (match_info, num);
1137 }
1138
1139 /**
1140  * g_match_info_fetch_named_pos:
1141  * @match_info: #GMatchInfo structure
1142  * @name: name of the subexpression
1143  * @start_pos: (out) (allow-none): pointer to location where to store
1144  *     the start position, or %NULL
1145  * @end_pos: (out) (allow-none): pointer to location where to store
1146  *     the end position, or %NULL
1147  *
1148  * Retrieves the position in bytes of the capturing parentheses named @name.
1149  *
1150  * If @name is a valid sub pattern name but it didn't match anything
1151  * (e.g. sub pattern "X", matching "b" against "(?P&lt;X&gt;a)?b")
1152  * then @start_pos and @end_pos are set to -1 and %TRUE is returned.
1153  *
1154  * Returns: %TRUE if the position was fetched, %FALSE otherwise.
1155  *     If the position cannot be fetched, @start_pos and @end_pos
1156  *     are left unchanged.
1157  *
1158  * Since: 2.14
1159  */
1160 gboolean
1161 g_match_info_fetch_named_pos (const GMatchInfo *match_info,
1162                               const gchar      *name,
1163                               gint             *start_pos,
1164                               gint             *end_pos)
1165 {
1166   gint num;
1167
1168   g_return_val_if_fail (match_info != NULL, FALSE);
1169   g_return_val_if_fail (name != NULL, FALSE);
1170
1171   num = get_matched_substring_number (match_info, name);
1172   if (num < 0)
1173     return FALSE;
1174
1175   return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
1176 }
1177
1178 /**
1179  * g_match_info_fetch_all:
1180  * @match_info: a #GMatchInfo structure
1181  *
1182  * Bundles up pointers to each of the matching substrings from a match
1183  * and stores them in an array of gchar pointers. The first element in
1184  * the returned array is the match number 0, i.e. the entire matched
1185  * text.
1186  *
1187  * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
1188  * "b" against "(a)?b") then an empty string is inserted.
1189  *
1190  * If the last match was obtained using the DFA algorithm, that is using
1191  * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1192  * strings are not that matched by sets of parentheses but that of the
1193  * matched substring. Substrings are matched in reverse order of length,
1194  * so the first one is the longest match.
1195  *
1196  * The strings are fetched from the string passed to the match function,
1197  * so you cannot call this function after freeing the string.
1198  *
1199  * Returns: (allow-none): a %NULL-terminated array of gchar * pointers.
1200  *     It must be freed using g_strfreev(). If the previous match failed
1201  *     %NULL is returned
1202  *
1203  * Since: 2.14
1204  */
1205 gchar **
1206 g_match_info_fetch_all (const GMatchInfo *match_info)
1207 {
1208   /* we cannot use pcre_get_substring_list() because the returned value
1209    * isn't suitable for g_strfreev(). */
1210   gchar **result;
1211   gint i;
1212
1213   g_return_val_if_fail (match_info != NULL, NULL);
1214
1215   if (match_info->matches < 0)
1216     return NULL;
1217
1218   result = g_new (gchar *, match_info->matches + 1);
1219   for (i = 0; i < match_info->matches; i++)
1220     result[i] = g_match_info_fetch (match_info, i);
1221   result[i] = NULL;
1222
1223   return result;
1224 }
1225
1226
1227 /* GRegex */
1228
1229 GQuark
1230 g_regex_error_quark (void)
1231 {
1232   static GQuark error_quark = 0;
1233
1234   if (error_quark == 0)
1235     error_quark = g_quark_from_static_string ("g-regex-error-quark");
1236
1237   return error_quark;
1238 }
1239
1240 /**
1241  * g_regex_ref:
1242  * @regex: a #GRegex
1243  *
1244  * Increases reference count of @regex by 1.
1245  *
1246  * Returns: @regex
1247  *
1248  * Since: 2.14
1249  */
1250 GRegex *
1251 g_regex_ref (GRegex *regex)
1252 {
1253   g_return_val_if_fail (regex != NULL, NULL);
1254   g_atomic_int_inc (&regex->ref_count);
1255   return regex;
1256 }
1257
1258 /**
1259  * g_regex_unref:
1260  * @regex: a #GRegex
1261  *
1262  * Decreases reference count of @regex by 1. When reference count drops
1263  * to zero, it frees all the memory associated with the regex structure.
1264  *
1265  * Since: 2.14
1266  */
1267 void
1268 g_regex_unref (GRegex *regex)
1269 {
1270   g_return_if_fail (regex != NULL);
1271
1272   if (g_atomic_int_dec_and_test (&regex->ref_count))
1273     {
1274       g_free (regex->pattern);
1275       if (regex->pcre_re != NULL)
1276         pcre_free (regex->pcre_re);
1277       if (regex->extra != NULL)
1278         pcre_free (regex->extra);
1279       g_free (regex);
1280     }
1281 }
1282
1283 /**
1284  * g_regex_new:
1285  * @pattern: the regular expression
1286  * @compile_options: compile options for the regular expression, or 0
1287  * @match_options: match options for the regular expression, or 0
1288  * @error: return location for a #GError
1289  *
1290  * Compiles the regular expression to an internal form, and does
1291  * the initial setup of the #GRegex structure.
1292  *
1293  * Returns: a #GRegex structure. Call g_regex_unref() when you
1294  *   are done with it
1295  *
1296  * Since: 2.14
1297  */
1298 GRegex *
1299 g_regex_new (const gchar         *pattern,
1300              GRegexCompileFlags   compile_options,
1301              GRegexMatchFlags     match_options,
1302              GError             **error)
1303 {
1304   GRegex *regex;
1305   pcre *re;
1306   const gchar *errmsg;
1307   gint erroffset;
1308   gint errcode;
1309   gboolean optimize = FALSE;
1310   static volatile gsize initialised = 0;
1311   unsigned long int pcre_compile_options;
1312   GRegexCompileFlags nonpcre_compile_options;
1313
1314   g_return_val_if_fail (pattern != NULL, NULL);
1315   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1316   g_return_val_if_fail ((compile_options & ~G_REGEX_COMPILE_MASK) == 0, NULL);
1317   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1318
1319   if (g_once_init_enter (&initialised))
1320     {
1321       int supports_utf8, supports_ucp;
1322
1323       pcre_config (PCRE_CONFIG_UTF8, &supports_utf8);
1324       if (!supports_utf8)
1325         g_critical (_("PCRE library is compiled without UTF8 support"));
1326
1327       pcre_config (PCRE_CONFIG_UNICODE_PROPERTIES, &supports_ucp);
1328       if (!supports_ucp)
1329         g_critical (_("PCRE library is compiled without UTF8 properties support"));
1330
1331       g_once_init_leave (&initialised, supports_utf8 && supports_ucp ? 1 : 2);
1332     }
1333
1334   if (G_UNLIKELY (initialised != 1)) 
1335     {
1336       g_set_error_literal (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, 
1337                            _("PCRE library is compiled with incompatible options"));
1338       return NULL;
1339     }
1340
1341   nonpcre_compile_options = compile_options & G_REGEX_COMPILE_NONPCRE_MASK;
1342
1343   /* G_REGEX_OPTIMIZE has the same numeric value of PCRE_NO_UTF8_CHECK,
1344    * as we do not need to wrap PCRE_NO_UTF8_CHECK. */
1345   if (compile_options & G_REGEX_OPTIMIZE)
1346     optimize = TRUE;
1347
1348   /* In GRegex the string are, by default, UTF-8 encoded. PCRE
1349    * instead uses UTF-8 only if required with PCRE_UTF8. */
1350   if (compile_options & G_REGEX_RAW)
1351     {
1352       /* disable utf-8 */
1353       compile_options &= ~G_REGEX_RAW;
1354     }
1355   else
1356     {
1357       /* enable utf-8 */
1358       compile_options |= PCRE_UTF8 | PCRE_NO_UTF8_CHECK;
1359       match_options |= PCRE_NO_UTF8_CHECK;
1360     }
1361
1362   /* PCRE_NEWLINE_ANY is the default for the internal PCRE but
1363    * not for the system one. */
1364   if (!(compile_options & G_REGEX_NEWLINE_CR) &&
1365       !(compile_options & G_REGEX_NEWLINE_LF))
1366     {
1367       compile_options |= PCRE_NEWLINE_ANY;
1368     }
1369
1370   compile_options |= PCRE_UCP;
1371
1372   /* PCRE_BSR_UNICODE is the default for the internal PCRE but
1373    * possibly not for the system one.
1374    */
1375   if (~compile_options & G_REGEX_BSR_ANYCRLF)
1376     compile_options |= PCRE_BSR_UNICODE;
1377
1378   /* compile the pattern */
1379   re = pcre_compile2 (pattern, compile_options, &errcode,
1380                       &errmsg, &erroffset, NULL);
1381
1382   /* if the compilation failed, set the error member and return
1383    * immediately */
1384   if (re == NULL)
1385     {
1386       GError *tmp_error;
1387
1388       /* Translate the PCRE error code to GRegexError and use a translated
1389        * error message if possible */
1390       translate_compile_error (&errcode, &errmsg);
1391
1392       /* PCRE uses byte offsets but we want to show character offsets */
1393       erroffset = g_utf8_pointer_to_offset (pattern, &pattern[erroffset]);
1394
1395       tmp_error = g_error_new (G_REGEX_ERROR, errcode,
1396                                _("Error while compiling regular "
1397                                  "expression %s at char %d: %s"),
1398                                pattern, erroffset, errmsg);
1399       g_propagate_error (error, tmp_error);
1400
1401       return NULL;
1402     }
1403
1404   /* For options set at the beginning of the pattern, pcre puts them into
1405    * compile options, e.g. "(?i)foo" will make the pcre structure store
1406    * PCRE_CASELESS even though it wasn't explicitly given for compilation. */
1407   pcre_fullinfo (re, NULL, PCRE_INFO_OPTIONS, &pcre_compile_options);
1408   compile_options = pcre_compile_options & G_REGEX_COMPILE_PCRE_MASK;
1409
1410   /* Don't leak PCRE_NEWLINE_ANY, which is part of PCRE_NEWLINE_ANYCRLF */
1411   if ((pcre_compile_options & PCRE_NEWLINE_ANYCRLF) != PCRE_NEWLINE_ANYCRLF)
1412     compile_options &= ~PCRE_NEWLINE_ANY;
1413
1414   compile_options |= nonpcre_compile_options;
1415
1416   if (!(compile_options & G_REGEX_DUPNAMES))
1417     {
1418       gboolean jchanged = FALSE;
1419       pcre_fullinfo (re, NULL, PCRE_INFO_JCHANGED, &jchanged);
1420       if (jchanged)
1421         compile_options |= G_REGEX_DUPNAMES;
1422     }
1423
1424   regex = g_new0 (GRegex, 1);
1425   regex->ref_count = 1;
1426   regex->pattern = g_strdup (pattern);
1427   regex->pcre_re = re;
1428   regex->compile_opts = compile_options;
1429   regex->match_opts = match_options;
1430
1431   if (optimize)
1432     {
1433       regex->extra = pcre_study (regex->pcre_re, 0, &errmsg);
1434       if (errmsg != NULL)
1435         {
1436           GError *tmp_error = g_error_new (G_REGEX_ERROR,
1437                                            G_REGEX_ERROR_OPTIMIZE,
1438                                            _("Error while optimizing "
1439                                              "regular expression %s: %s"),
1440                                            regex->pattern,
1441                                            errmsg);
1442           g_propagate_error (error, tmp_error);
1443
1444           g_regex_unref (regex);
1445           return NULL;
1446         }
1447     }
1448
1449   return regex;
1450 }
1451
1452 /**
1453  * g_regex_get_pattern:
1454  * @regex: a #GRegex structure
1455  *
1456  * Gets the pattern string associated with @regex, i.e. a copy of
1457  * the string passed to g_regex_new().
1458  *
1459  * Returns: the pattern of @regex
1460  *
1461  * Since: 2.14
1462  */
1463 const gchar *
1464 g_regex_get_pattern (const GRegex *regex)
1465 {
1466   g_return_val_if_fail (regex != NULL, NULL);
1467
1468   return regex->pattern;
1469 }
1470
1471 /**
1472  * g_regex_get_max_backref:
1473  * @regex: a #GRegex
1474  *
1475  * Returns the number of the highest back reference
1476  * in the pattern, or 0 if the pattern does not contain
1477  * back references.
1478  *
1479  * Returns: the number of the highest back reference
1480  *
1481  * Since: 2.14
1482  */
1483 gint
1484 g_regex_get_max_backref (const GRegex *regex)
1485 {
1486   gint value;
1487
1488   pcre_fullinfo (regex->pcre_re, regex->extra,
1489                  PCRE_INFO_BACKREFMAX, &value);
1490
1491   return value;
1492 }
1493
1494 /**
1495  * g_regex_get_capture_count:
1496  * @regex: a #GRegex
1497  *
1498  * Returns the number of capturing subpatterns in the pattern.
1499  *
1500  * Returns: the number of capturing subpatterns
1501  *
1502  * Since: 2.14
1503  */
1504 gint
1505 g_regex_get_capture_count (const GRegex *regex)
1506 {
1507   gint value;
1508
1509   pcre_fullinfo (regex->pcre_re, regex->extra,
1510                  PCRE_INFO_CAPTURECOUNT, &value);
1511
1512   return value;
1513 }
1514
1515 /**
1516  * g_regex_get_has_cr_or_lf:
1517  * @regex: a #GRegex structure
1518  *
1519  * Checks whether the pattern contains explicit CR or LF references.
1520  *
1521  * Returns: %TRUE if the pattern contains explicit CR or LF references
1522  *
1523  * Since: 2.34
1524  */
1525 gboolean
1526 g_regex_get_has_cr_or_lf (const GRegex *regex)
1527 {
1528   gint value;
1529
1530   pcre_fullinfo (regex->pcre_re, regex->extra,
1531                  PCRE_INFO_HASCRORLF, &value);
1532
1533   return !!value;
1534 }
1535
1536 /**
1537  * g_regex_get_compile_flags:
1538  * @regex: a #GRegex
1539  *
1540  * Returns the compile options that @regex was created with.
1541  *
1542  * Returns: flags from #GRegexCompileFlags
1543  *
1544  * Since: 2.26
1545  */
1546 GRegexCompileFlags
1547 g_regex_get_compile_flags (const GRegex *regex)
1548 {
1549   g_return_val_if_fail (regex != NULL, 0);
1550
1551   return regex->compile_opts;
1552 }
1553
1554 /**
1555  * g_regex_get_match_flags:
1556  * @regex: a #GRegex
1557  *
1558  * Returns the match options that @regex was created with.
1559  *
1560  * Returns: flags from #GRegexMatchFlags
1561  *
1562  * Since: 2.26
1563  */
1564 GRegexMatchFlags
1565 g_regex_get_match_flags (const GRegex *regex)
1566 {
1567   g_return_val_if_fail (regex != NULL, 0);
1568
1569   return regex->match_opts & G_REGEX_MATCH_MASK;
1570 }
1571
1572 /**
1573  * g_regex_match_simple:
1574  * @pattern: the regular expression
1575  * @string: the string to scan for matches
1576  * @compile_options: compile options for the regular expression, or 0
1577  * @match_options: match options, or 0
1578  *
1579  * Scans for a match in @string for @pattern.
1580  *
1581  * This function is equivalent to g_regex_match() but it does not
1582  * require to compile the pattern with g_regex_new(), avoiding some
1583  * lines of code when you need just to do a match without extracting
1584  * substrings, capture counts, and so on.
1585  *
1586  * If this function is to be called on the same @pattern more than
1587  * once, it's more efficient to compile the pattern once with
1588  * g_regex_new() and then use g_regex_match().
1589  *
1590  * Returns: %TRUE if the string matched, %FALSE otherwise
1591  *
1592  * Since: 2.14
1593  */
1594 gboolean
1595 g_regex_match_simple (const gchar        *pattern,
1596                       const gchar        *string,
1597                       GRegexCompileFlags  compile_options,
1598                       GRegexMatchFlags    match_options)
1599 {
1600   GRegex *regex;
1601   gboolean result;
1602
1603   regex = g_regex_new (pattern, compile_options, 0, NULL);
1604   if (!regex)
1605     return FALSE;
1606   result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
1607   g_regex_unref (regex);
1608   return result;
1609 }
1610
1611 /**
1612  * g_regex_match:
1613  * @regex: a #GRegex structure from g_regex_new()
1614  * @string: the string to scan for matches
1615  * @match_options: match options
1616  * @match_info: (out) (allow-none): pointer to location where to store
1617  *     the #GMatchInfo, or %NULL if you do not need it
1618  *
1619  * Scans for a match in string for the pattern in @regex.
1620  * The @match_options are combined with the match options specified
1621  * when the @regex structure was created, letting you have more
1622  * flexibility in reusing #GRegex structures.
1623  *
1624  * A #GMatchInfo structure, used to get information on the match,
1625  * is stored in @match_info if not %NULL. Note that if @match_info
1626  * is not %NULL then it is created even if the function returns %FALSE,
1627  * i.e. you must free it regardless if regular expression actually matched.
1628  *
1629  * To retrieve all the non-overlapping matches of the pattern in
1630  * string you can use g_match_info_next().
1631  *
1632  * |[
1633  * static void
1634  * print_uppercase_words (const gchar *string)
1635  * {
1636  *   /&ast; Print all uppercase-only words. &ast;/
1637  *   GRegex *regex;
1638  *   GMatchInfo *match_info;
1639  *   &nbsp;
1640  *   regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1641  *   g_regex_match (regex, string, 0, &amp;match_info);
1642  *   while (g_match_info_matches (match_info))
1643  *     {
1644  *       gchar *word = g_match_info_fetch (match_info, 0);
1645  *       g_print ("Found: %s\n", word);
1646  *       g_free (word);
1647  *       g_match_info_next (match_info, NULL);
1648  *     }
1649  *   g_match_info_free (match_info);
1650  *   g_regex_unref (regex);
1651  * }
1652  * ]|
1653  *
1654  * @string is not copied and is used in #GMatchInfo internally. If
1655  * you use any #GMatchInfo method (except g_match_info_free()) after
1656  * freeing or modifying @string then the behaviour is undefined.
1657  *
1658  * Returns: %TRUE is the string matched, %FALSE otherwise
1659  *
1660  * Since: 2.14
1661  */
1662 gboolean
1663 g_regex_match (const GRegex      *regex,
1664                const gchar       *string,
1665                GRegexMatchFlags   match_options,
1666                GMatchInfo       **match_info)
1667 {
1668   return g_regex_match_full (regex, string, -1, 0, match_options,
1669                              match_info, NULL);
1670 }
1671
1672 /**
1673  * g_regex_match_full:
1674  * @regex: a #GRegex structure from g_regex_new()
1675  * @string: (array length=string_len): the string to scan for matches
1676  * @string_len: the length of @string, or -1 if @string is nul-terminated
1677  * @start_position: starting index of the string to match
1678  * @match_options: match options
1679  * @match_info: (out) (allow-none): pointer to location where to store
1680  *     the #GMatchInfo, or %NULL if you do not need it
1681  * @error: location to store the error occurring, or %NULL to ignore errors
1682  *
1683  * Scans for a match in string for the pattern in @regex.
1684  * The @match_options are combined with the match options specified
1685  * when the @regex structure was created, letting you have more
1686  * flexibility in reusing #GRegex structures.
1687  *
1688  * Setting @start_position differs from just passing over a shortened
1689  * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
1690  * that begins with any kind of lookbehind assertion, such as "\b".
1691  *
1692  * A #GMatchInfo structure, used to get information on the match, is
1693  * stored in @match_info if not %NULL. Note that if @match_info is
1694  * not %NULL then it is created even if the function returns %FALSE,
1695  * i.e. you must free it regardless if regular expression actually
1696  * matched.
1697  *
1698  * @string is not copied and is used in #GMatchInfo internally. If
1699  * you use any #GMatchInfo method (except g_match_info_free()) after
1700  * freeing or modifying @string then the behaviour is undefined.
1701  *
1702  * To retrieve all the non-overlapping matches of the pattern in
1703  * string you can use g_match_info_next().
1704  *
1705  * |[
1706  * static void
1707  * print_uppercase_words (const gchar *string)
1708  * {
1709  *   /&ast; Print all uppercase-only words. &ast;/
1710  *   GRegex *regex;
1711  *   GMatchInfo *match_info;
1712  *   GError *error = NULL;
1713  *   &nbsp;
1714  *   regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1715  *   g_regex_match_full (regex, string, -1, 0, 0, &amp;match_info, &amp;error);
1716  *   while (g_match_info_matches (match_info))
1717  *     {
1718  *       gchar *word = g_match_info_fetch (match_info, 0);
1719  *       g_print ("Found: %s\n", word);
1720  *       g_free (word);
1721  *       g_match_info_next (match_info, &amp;error);
1722  *     }
1723  *   g_match_info_free (match_info);
1724  *   g_regex_unref (regex);
1725  *   if (error != NULL)
1726  *     {
1727  *       g_printerr ("Error while matching: %s\n", error->message);
1728  *       g_error_free (error);
1729  *     }
1730  * }
1731  * ]|
1732  *
1733  * Returns: %TRUE is the string matched, %FALSE otherwise
1734  *
1735  * Since: 2.14
1736  */
1737 gboolean
1738 g_regex_match_full (const GRegex      *regex,
1739                     const gchar       *string,
1740                     gssize             string_len,
1741                     gint               start_position,
1742                     GRegexMatchFlags   match_options,
1743                     GMatchInfo       **match_info,
1744                     GError           **error)
1745 {
1746   GMatchInfo *info;
1747   gboolean match_ok;
1748
1749   g_return_val_if_fail (regex != NULL, FALSE);
1750   g_return_val_if_fail (string != NULL, FALSE);
1751   g_return_val_if_fail (start_position >= 0, FALSE);
1752   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1753   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1754
1755   info = match_info_new (regex, string, string_len, start_position,
1756                          match_options, FALSE);
1757   match_ok = g_match_info_next (info, error);
1758   if (match_info != NULL)
1759     *match_info = info;
1760   else
1761     g_match_info_free (info);
1762
1763   return match_ok;
1764 }
1765
1766 /**
1767  * g_regex_match_all:
1768  * @regex: a #GRegex structure from g_regex_new()
1769  * @string: the string to scan for matches
1770  * @match_options: match options
1771  * @match_info: (out) (allow-none): pointer to location where to store
1772  *     the #GMatchInfo, or %NULL if you do not need it
1773  *
1774  * Using the standard algorithm for regular expression matching only
1775  * the longest match in the string is retrieved. This function uses
1776  * a different algorithm so it can retrieve all the possible matches.
1777  * For more documentation see g_regex_match_all_full().
1778  *
1779  * A #GMatchInfo structure, used to get information on the match, is
1780  * stored in @match_info if not %NULL. Note that if @match_info is
1781  * not %NULL then it is created even if the function returns %FALSE,
1782  * i.e. you must free it regardless if regular expression actually
1783  * matched.
1784  *
1785  * @string is not copied and is used in #GMatchInfo internally. If
1786  * you use any #GMatchInfo method (except g_match_info_free()) after
1787  * freeing or modifying @string then the behaviour is undefined.
1788  *
1789  * Returns: %TRUE is the string matched, %FALSE otherwise
1790  *
1791  * Since: 2.14
1792  */
1793 gboolean
1794 g_regex_match_all (const GRegex      *regex,
1795                    const gchar       *string,
1796                    GRegexMatchFlags   match_options,
1797                    GMatchInfo       **match_info)
1798 {
1799   return g_regex_match_all_full (regex, string, -1, 0, match_options,
1800                                  match_info, NULL);
1801 }
1802
1803 /**
1804  * g_regex_match_all_full:
1805  * @regex: a #GRegex structure from g_regex_new()
1806  * @string: (array length=string_len): the string to scan for matches
1807  * @string_len: the length of @string, or -1 if @string is nul-terminated
1808  * @start_position: starting index of the string to match
1809  * @match_options: match options
1810  * @match_info: (out) (allow-none): pointer to location where to store
1811  *     the #GMatchInfo, or %NULL if you do not need it
1812  * @error: location to store the error occurring, or %NULL to ignore errors
1813  *
1814  * Using the standard algorithm for regular expression matching only
1815  * the longest match in the string is retrieved, it is not possible
1816  * to obtain all the available matches. For instance matching
1817  * "&lt;a&gt; &lt;b&gt; &lt;c&gt;" against the pattern "&lt;.*&gt;"
1818  * you get "&lt;a&gt; &lt;b&gt; &lt;c&gt;".
1819  *
1820  * This function uses a different algorithm (called DFA, i.e. deterministic
1821  * finite automaton), so it can retrieve all the possible matches, all
1822  * starting at the same point in the string. For instance matching
1823  * "&lt;a&gt; &lt;b&gt; &lt;c&gt;" against the pattern "&lt;.*&gt;"
1824  * you would obtain three matches: "&lt;a&gt; &lt;b&gt; &lt;c&gt;",
1825  * "&lt;a&gt; &lt;b&gt;" and "&lt;a&gt;".
1826  *
1827  * The number of matched strings is retrieved using
1828  * g_match_info_get_match_count(). To obtain the matched strings and
1829  * their position you can use, respectively, g_match_info_fetch() and
1830  * g_match_info_fetch_pos(). Note that the strings are returned in
1831  * reverse order of length; that is, the longest matching string is
1832  * given first.
1833  *
1834  * Note that the DFA algorithm is slower than the standard one and it
1835  * is not able to capture substrings, so backreferences do not work.
1836  *
1837  * Setting @start_position differs from just passing over a shortened
1838  * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
1839  * that begins with any kind of lookbehind assertion, such as "\b".
1840  *
1841  * A #GMatchInfo structure, used to get information on the match, is
1842  * stored in @match_info if not %NULL. Note that if @match_info is
1843  * not %NULL then it is created even if the function returns %FALSE,
1844  * i.e. you must free it regardless if regular expression actually
1845  * matched.
1846  *
1847  * @string is not copied and is used in #GMatchInfo internally. If
1848  * you use any #GMatchInfo method (except g_match_info_free()) after
1849  * freeing or modifying @string then the behaviour is undefined.
1850  *
1851  * Returns: %TRUE is the string matched, %FALSE otherwise
1852  *
1853  * Since: 2.14
1854  */
1855 gboolean
1856 g_regex_match_all_full (const GRegex      *regex,
1857                         const gchar       *string,
1858                         gssize             string_len,
1859                         gint               start_position,
1860                         GRegexMatchFlags   match_options,
1861                         GMatchInfo       **match_info,
1862                         GError           **error)
1863 {
1864   GMatchInfo *info;
1865   gboolean done;
1866
1867   g_return_val_if_fail (regex != NULL, FALSE);
1868   g_return_val_if_fail (string != NULL, FALSE);
1869   g_return_val_if_fail (start_position >= 0, FALSE);
1870   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1871   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1872
1873   info = match_info_new (regex, string, string_len, start_position,
1874                          match_options, TRUE);
1875
1876   done = FALSE;
1877   while (!done)
1878     {
1879       done = TRUE;
1880       info->matches = pcre_dfa_exec (regex->pcre_re, regex->extra,
1881                                      info->string, info->string_len,
1882                                      info->pos,
1883                                      regex->match_opts | match_options,
1884                                      info->offsets, info->n_offsets,
1885                                      info->workspace, info->n_workspace);
1886       if (info->matches == PCRE_ERROR_DFA_WSSIZE)
1887         {
1888           /* info->workspace is too small. */
1889           info->n_workspace *= 2;
1890           info->workspace = g_realloc (info->workspace,
1891                                        info->n_workspace * sizeof (gint));
1892           done = FALSE;
1893         }
1894       else if (info->matches == 0)
1895         {
1896           /* info->offsets is too small. */
1897           info->n_offsets *= 2;
1898           info->offsets = g_realloc (info->offsets,
1899                                      info->n_offsets * sizeof (gint));
1900           done = FALSE;
1901         }
1902       else if (IS_PCRE_ERROR (info->matches))
1903         {
1904           g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
1905                        _("Error while matching regular expression %s: %s"),
1906                        regex->pattern, match_error (info->matches));
1907         }
1908     }
1909
1910   /* set info->pos to -1 so that a call to g_match_info_next() fails. */
1911   info->pos = -1;
1912
1913   if (match_info != NULL)
1914     *match_info = info;
1915   else
1916     g_match_info_free (info);
1917
1918   return info->matches >= 0;
1919 }
1920
1921 /**
1922  * g_regex_get_string_number:
1923  * @regex: #GRegex structure
1924  * @name: name of the subexpression
1925  *
1926  * Retrieves the number of the subexpression named @name.
1927  *
1928  * Returns: The number of the subexpression or -1 if @name
1929  *   does not exists
1930  *
1931  * Since: 2.14
1932  */
1933 gint
1934 g_regex_get_string_number (const GRegex *regex,
1935                            const gchar  *name)
1936 {
1937   gint num;
1938
1939   g_return_val_if_fail (regex != NULL, -1);
1940   g_return_val_if_fail (name != NULL, -1);
1941
1942   num = pcre_get_stringnumber (regex->pcre_re, name);
1943   if (num == PCRE_ERROR_NOSUBSTRING)
1944     num = -1;
1945
1946   return num;
1947 }
1948
1949 /**
1950  * g_regex_split_simple:
1951  * @pattern: the regular expression
1952  * @string: the string to scan for matches
1953  * @compile_options: compile options for the regular expression, or 0
1954  * @match_options: match options, or 0
1955  *
1956  * Breaks the string on the pattern, and returns an array of
1957  * the tokens. If the pattern contains capturing parentheses,
1958  * then the text for each of the substrings will also be returned.
1959  * If the pattern does not match anywhere in the string, then the
1960  * whole string is returned as the first token.
1961  *
1962  * This function is equivalent to g_regex_split() but it does
1963  * not require to compile the pattern with g_regex_new(), avoiding
1964  * some lines of code when you need just to do a split without
1965  * extracting substrings, capture counts, and so on.
1966  *
1967  * If this function is to be called on the same @pattern more than
1968  * once, it's more efficient to compile the pattern once with
1969  * g_regex_new() and then use g_regex_split().
1970  *
1971  * As a special case, the result of splitting the empty string ""
1972  * is an empty vector, not a vector containing a single string.
1973  * The reason for this special case is that being able to represent
1974  * a empty vector is typically more useful than consistent handling
1975  * of empty elements. If you do need to represent empty elements,
1976  * you'll need to check for the empty string before calling this
1977  * function.
1978  *
1979  * A pattern that can match empty strings splits @string into
1980  * separate characters wherever it matches the empty string between
1981  * characters. For example splitting "ab c" using as a separator
1982  * "\s*", you will get "a", "b" and "c".
1983  *
1984  * Returns: a %NULL-terminated array of strings. Free it using g_strfreev()
1985  *
1986  * Since: 2.14
1987  **/
1988 gchar **
1989 g_regex_split_simple (const gchar        *pattern,
1990                       const gchar        *string,
1991                       GRegexCompileFlags  compile_options,
1992                       GRegexMatchFlags    match_options)
1993 {
1994   GRegex *regex;
1995   gchar **result;
1996
1997   regex = g_regex_new (pattern, compile_options, 0, NULL);
1998   if (!regex)
1999     return NULL;
2000
2001   result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
2002   g_regex_unref (regex);
2003   return result;
2004 }
2005
2006 /**
2007  * g_regex_split:
2008  * @regex: a #GRegex structure
2009  * @string: the string to split with the pattern
2010  * @match_options: match time option flags
2011  *
2012  * Breaks the string on the pattern, and returns an array of the tokens.
2013  * If the pattern contains capturing parentheses, then the text for each
2014  * of the substrings will also be returned. If the pattern does not match
2015  * anywhere in the string, then the whole string is returned as the first
2016  * token.
2017  *
2018  * As a special case, the result of splitting the empty string "" is an
2019  * empty vector, not a vector containing a single string. The reason for
2020  * this special case is that being able to represent a empty vector is
2021  * typically more useful than consistent handling of empty elements. If
2022  * you do need to represent empty elements, you'll need to check for the
2023  * empty string before calling this function.
2024  *
2025  * A pattern that can match empty strings splits @string into separate
2026  * characters wherever it matches the empty string between characters.
2027  * For example splitting "ab c" using as a separator "\s*", you will get
2028  * "a", "b" and "c".
2029  *
2030  * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
2031  *
2032  * Since: 2.14
2033  **/
2034 gchar **
2035 g_regex_split (const GRegex     *regex,
2036                const gchar      *string,
2037                GRegexMatchFlags  match_options)
2038 {
2039   return g_regex_split_full (regex, string, -1, 0,
2040                              match_options, 0, NULL);
2041 }
2042
2043 /**
2044  * g_regex_split_full:
2045  * @regex: a #GRegex structure
2046  * @string: (array length=string_len): the string to split with the pattern
2047  * @string_len: the length of @string, or -1 if @string is nul-terminated
2048  * @start_position: starting index of the string to match
2049  * @match_options: match time option flags
2050  * @max_tokens: the maximum number of tokens to split @string into.
2051  *   If this is less than 1, the string is split completely
2052  * @error: return location for a #GError
2053  *
2054  * Breaks the string on the pattern, and returns an array of the tokens.
2055  * If the pattern contains capturing parentheses, then the text for each
2056  * of the substrings will also be returned. If the pattern does not match
2057  * anywhere in the string, then the whole string is returned as the first
2058  * token.
2059  *
2060  * As a special case, the result of splitting the empty string "" is an
2061  * empty vector, not a vector containing a single string. The reason for
2062  * this special case is that being able to represent a empty vector is
2063  * typically more useful than consistent handling of empty elements. If
2064  * you do need to represent empty elements, you'll need to check for the
2065  * empty string before calling this function.
2066  *
2067  * A pattern that can match empty strings splits @string into separate
2068  * characters wherever it matches the empty string between characters.
2069  * For example splitting "ab c" using as a separator "\s*", you will get
2070  * "a", "b" and "c".
2071  *
2072  * Setting @start_position differs from just passing over a shortened
2073  * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
2074  * that begins with any kind of lookbehind assertion, such as "\b".
2075  *
2076  * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
2077  *
2078  * Since: 2.14
2079  **/
2080 gchar **
2081 g_regex_split_full (const GRegex      *regex,
2082                     const gchar       *string,
2083                     gssize             string_len,
2084                     gint               start_position,
2085                     GRegexMatchFlags   match_options,
2086                     gint               max_tokens,
2087                     GError           **error)
2088 {
2089   GError *tmp_error = NULL;
2090   GMatchInfo *match_info;
2091   GList *list, *last;
2092   gint i;
2093   gint token_count;
2094   gboolean match_ok;
2095   /* position of the last separator. */
2096   gint last_separator_end;
2097   /* was the last match 0 bytes long? */
2098   gboolean last_match_is_empty;
2099   /* the returned array of char **s */
2100   gchar **string_list;
2101
2102   g_return_val_if_fail (regex != NULL, NULL);
2103   g_return_val_if_fail (string != NULL, NULL);
2104   g_return_val_if_fail (start_position >= 0, NULL);
2105   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2106   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2107
2108   if (max_tokens <= 0)
2109     max_tokens = G_MAXINT;
2110
2111   if (string_len < 0)
2112     string_len = strlen (string);
2113
2114   /* zero-length string */
2115   if (string_len - start_position == 0)
2116     return g_new0 (gchar *, 1);
2117
2118   if (max_tokens == 1)
2119     {
2120       string_list = g_new0 (gchar *, 2);
2121       string_list[0] = g_strndup (&string[start_position],
2122                                   string_len - start_position);
2123       return string_list;
2124     }
2125
2126   list = NULL;
2127   token_count = 0;
2128   last_separator_end = start_position;
2129   last_match_is_empty = FALSE;
2130
2131   match_ok = g_regex_match_full (regex, string, string_len, start_position,
2132                                  match_options, &match_info, &tmp_error);
2133
2134   while (tmp_error == NULL)
2135     {
2136       if (match_ok)
2137         {
2138           last_match_is_empty =
2139                     (match_info->offsets[0] == match_info->offsets[1]);
2140
2141           /* we need to skip empty separators at the same position of the end
2142            * of another separator. e.g. the string is "a b" and the separator
2143            * is " *", so from 1 to 2 we have a match and at position 2 we have
2144            * an empty match. */
2145           if (last_separator_end != match_info->offsets[1])
2146             {
2147               gchar *token;
2148               gint match_count;
2149
2150               token = g_strndup (string + last_separator_end,
2151                                  match_info->offsets[0] - last_separator_end);
2152               list = g_list_prepend (list, token);
2153               token_count++;
2154
2155               /* if there were substrings, these need to be added to
2156                * the list. */
2157               match_count = g_match_info_get_match_count (match_info);
2158               if (match_count > 1)
2159                 {
2160                   for (i = 1; i < match_count; i++)
2161                     list = g_list_prepend (list, g_match_info_fetch (match_info, i));
2162                 }
2163             }
2164         }
2165       else
2166         {
2167           /* if there was no match, copy to end of string. */
2168           if (!last_match_is_empty)
2169             {
2170               gchar *token = g_strndup (string + last_separator_end,
2171                                         match_info->string_len - last_separator_end);
2172               list = g_list_prepend (list, token);
2173             }
2174           /* no more tokens, end the loop. */
2175           break;
2176         }
2177
2178       /* -1 to leave room for the last part. */
2179       if (token_count >= max_tokens - 1)
2180         {
2181           /* we have reached the maximum number of tokens, so we copy
2182            * the remaining part of the string. */
2183           if (last_match_is_empty)
2184             {
2185               /* the last match was empty, so we have moved one char
2186                * after the real position to avoid empty matches at the
2187                * same position. */
2188               match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
2189             }
2190           /* the if is needed in the case we have terminated the available
2191            * tokens, but we are at the end of the string, so there are no
2192            * characters left to copy. */
2193           if (string_len > match_info->pos)
2194             {
2195               gchar *token = g_strndup (string + match_info->pos,
2196                                         string_len - match_info->pos);
2197               list = g_list_prepend (list, token);
2198             }
2199           /* end the loop. */
2200           break;
2201         }
2202
2203       last_separator_end = match_info->pos;
2204       if (last_match_is_empty)
2205         /* if the last match was empty, g_match_info_next() has moved
2206          * forward to avoid infinite loops, but we still need to copy that
2207          * character. */
2208         last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
2209
2210       match_ok = g_match_info_next (match_info, &tmp_error);
2211     }
2212   g_match_info_free (match_info);
2213   if (tmp_error != NULL)
2214     {
2215       g_propagate_error (error, tmp_error);
2216       g_list_free_full (list, g_free);
2217       match_info->pos = -1;
2218       return NULL;
2219     }
2220
2221   string_list = g_new (gchar *, g_list_length (list) + 1);
2222   i = 0;
2223   for (last = g_list_last (list); last; last = g_list_previous (last))
2224     string_list[i++] = last->data;
2225   string_list[i] = NULL;
2226   g_list_free (list);
2227
2228   return string_list;
2229 }
2230
2231 enum
2232 {
2233   REPL_TYPE_STRING,
2234   REPL_TYPE_CHARACTER,
2235   REPL_TYPE_SYMBOLIC_REFERENCE,
2236   REPL_TYPE_NUMERIC_REFERENCE,
2237   REPL_TYPE_CHANGE_CASE
2238 };
2239
2240 typedef enum
2241 {
2242   CHANGE_CASE_NONE         = 1 << 0,
2243   CHANGE_CASE_UPPER        = 1 << 1,
2244   CHANGE_CASE_LOWER        = 1 << 2,
2245   CHANGE_CASE_UPPER_SINGLE = 1 << 3,
2246   CHANGE_CASE_LOWER_SINGLE = 1 << 4,
2247   CHANGE_CASE_SINGLE_MASK  = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
2248   CHANGE_CASE_LOWER_MASK   = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
2249   CHANGE_CASE_UPPER_MASK   = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
2250 } ChangeCase;
2251
2252 struct _InterpolationData
2253 {
2254   gchar     *text;
2255   gint       type;
2256   gint       num;
2257   gchar      c;
2258   ChangeCase change_case;
2259 };
2260
2261 static void
2262 free_interpolation_data (InterpolationData *data)
2263 {
2264   g_free (data->text);
2265   g_free (data);
2266 }
2267
2268 static const gchar *
2269 expand_escape (const gchar        *replacement,
2270                const gchar        *p,
2271                InterpolationData  *data,
2272                GError            **error)
2273 {
2274   const gchar *q, *r;
2275   gint x, d, h, i;
2276   const gchar *error_detail;
2277   gint base = 0;
2278   GError *tmp_error = NULL;
2279
2280   p++;
2281   switch (*p)
2282     {
2283     case 't':
2284       p++;
2285       data->c = '\t';
2286       data->type = REPL_TYPE_CHARACTER;
2287       break;
2288     case 'n':
2289       p++;
2290       data->c = '\n';
2291       data->type = REPL_TYPE_CHARACTER;
2292       break;
2293     case 'v':
2294       p++;
2295       data->c = '\v';
2296       data->type = REPL_TYPE_CHARACTER;
2297       break;
2298     case 'r':
2299       p++;
2300       data->c = '\r';
2301       data->type = REPL_TYPE_CHARACTER;
2302       break;
2303     case 'f':
2304       p++;
2305       data->c = '\f';
2306       data->type = REPL_TYPE_CHARACTER;
2307       break;
2308     case 'a':
2309       p++;
2310       data->c = '\a';
2311       data->type = REPL_TYPE_CHARACTER;
2312       break;
2313     case 'b':
2314       p++;
2315       data->c = '\b';
2316       data->type = REPL_TYPE_CHARACTER;
2317       break;
2318     case '\\':
2319       p++;
2320       data->c = '\\';
2321       data->type = REPL_TYPE_CHARACTER;
2322       break;
2323     case 'x':
2324       p++;
2325       x = 0;
2326       if (*p == '{')
2327         {
2328           p++;
2329           do
2330             {
2331               h = g_ascii_xdigit_value (*p);
2332               if (h < 0)
2333                 {
2334                   error_detail = _("hexadecimal digit or '}' expected");
2335                   goto error;
2336                 }
2337               x = x * 16 + h;
2338               p++;
2339             }
2340           while (*p != '}');
2341           p++;
2342         }
2343       else
2344         {
2345           for (i = 0; i < 2; i++)
2346             {
2347               h = g_ascii_xdigit_value (*p);
2348               if (h < 0)
2349                 {
2350                   error_detail = _("hexadecimal digit expected");
2351                   goto error;
2352                 }
2353               x = x * 16 + h;
2354               p++;
2355             }
2356         }
2357       data->type = REPL_TYPE_STRING;
2358       data->text = g_new0 (gchar, 8);
2359       g_unichar_to_utf8 (x, data->text);
2360       break;
2361     case 'l':
2362       p++;
2363       data->type = REPL_TYPE_CHANGE_CASE;
2364       data->change_case = CHANGE_CASE_LOWER_SINGLE;
2365       break;
2366     case 'u':
2367       p++;
2368       data->type = REPL_TYPE_CHANGE_CASE;
2369       data->change_case = CHANGE_CASE_UPPER_SINGLE;
2370       break;
2371     case 'L':
2372       p++;
2373       data->type = REPL_TYPE_CHANGE_CASE;
2374       data->change_case = CHANGE_CASE_LOWER;
2375       break;
2376     case 'U':
2377       p++;
2378       data->type = REPL_TYPE_CHANGE_CASE;
2379       data->change_case = CHANGE_CASE_UPPER;
2380       break;
2381     case 'E':
2382       p++;
2383       data->type = REPL_TYPE_CHANGE_CASE;
2384       data->change_case = CHANGE_CASE_NONE;
2385       break;
2386     case 'g':
2387       p++;
2388       if (*p != '<')
2389         {
2390           error_detail = _("missing '<' in symbolic reference");
2391           goto error;
2392         }
2393       q = p + 1;
2394       do
2395         {
2396           p++;
2397           if (!*p)
2398             {
2399               error_detail = _("unfinished symbolic reference");
2400               goto error;
2401             }
2402         }
2403       while (*p != '>');
2404       if (p - q == 0)
2405         {
2406           error_detail = _("zero-length symbolic reference");
2407           goto error;
2408         }
2409       if (g_ascii_isdigit (*q))
2410         {
2411           x = 0;
2412           do
2413             {
2414               h = g_ascii_digit_value (*q);
2415               if (h < 0)
2416                 {
2417                   error_detail = _("digit expected");
2418                   p = q;
2419                   goto error;
2420                 }
2421               x = x * 10 + h;
2422               q++;
2423             }
2424           while (q != p);
2425           data->num = x;
2426           data->type = REPL_TYPE_NUMERIC_REFERENCE;
2427         }
2428       else
2429         {
2430           r = q;
2431           do
2432             {
2433               if (!g_ascii_isalnum (*r))
2434                 {
2435                   error_detail = _("illegal symbolic reference");
2436                   p = r;
2437                   goto error;
2438                 }
2439               r++;
2440             }
2441           while (r != p);
2442           data->text = g_strndup (q, p - q);
2443           data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
2444         }
2445       p++;
2446       break;
2447     case '0':
2448       /* if \0 is followed by a number is an octal number representing a
2449        * character, else it is a numeric reference. */
2450       if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
2451         {
2452           base = 8;
2453           p = g_utf8_next_char (p);
2454         }
2455     case '1':
2456     case '2':
2457     case '3':
2458     case '4':
2459     case '5':
2460     case '6':
2461     case '7':
2462     case '8':
2463     case '9':
2464       x = 0;
2465       d = 0;
2466       for (i = 0; i < 3; i++)
2467         {
2468           h = g_ascii_digit_value (*p);
2469           if (h < 0)
2470             break;
2471           if (h > 7)
2472             {
2473               if (base == 8)
2474                 break;
2475               else
2476                 base = 10;
2477             }
2478           if (i == 2 && base == 10)
2479             break;
2480           x = x * 8 + h;
2481           d = d * 10 + h;
2482           p++;
2483         }
2484       if (base == 8 || i == 3)
2485         {
2486           data->type = REPL_TYPE_STRING;
2487           data->text = g_new0 (gchar, 8);
2488           g_unichar_to_utf8 (x, data->text);
2489         }
2490       else
2491         {
2492           data->type = REPL_TYPE_NUMERIC_REFERENCE;
2493           data->num = d;
2494         }
2495       break;
2496     case 0:
2497       error_detail = _("stray final '\\'");
2498       goto error;
2499       break;
2500     default:
2501       error_detail = _("unknown escape sequence");
2502       goto error;
2503     }
2504
2505   return p;
2506
2507  error:
2508   /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
2509   tmp_error = g_error_new (G_REGEX_ERROR,
2510                            G_REGEX_ERROR_REPLACE,
2511                            _("Error while parsing replacement "
2512                              "text \"%s\" at char %lu: %s"),
2513                            replacement,
2514                            (gulong)(p - replacement),
2515                            error_detail);
2516   g_propagate_error (error, tmp_error);
2517
2518   return NULL;
2519 }
2520
2521 static GList *
2522 split_replacement (const gchar  *replacement,
2523                    GError      **error)
2524 {
2525   GList *list = NULL;
2526   InterpolationData *data;
2527   const gchar *p, *start;
2528
2529   start = p = replacement;
2530   while (*p)
2531     {
2532       if (*p == '\\')
2533         {
2534           data = g_new0 (InterpolationData, 1);
2535           start = p = expand_escape (replacement, p, data, error);
2536           if (p == NULL)
2537             {
2538               g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
2539               free_interpolation_data (data);
2540
2541               return NULL;
2542             }
2543           list = g_list_prepend (list, data);
2544         }
2545       else
2546         {
2547           p++;
2548           if (*p == '\\' || *p == '\0')
2549             {
2550               if (p - start > 0)
2551                 {
2552                   data = g_new0 (InterpolationData, 1);
2553                   data->text = g_strndup (start, p - start);
2554                   data->type = REPL_TYPE_STRING;
2555                   list = g_list_prepend (list, data);
2556                 }
2557             }
2558         }
2559     }
2560
2561   return g_list_reverse (list);
2562 }
2563
2564 /* Change the case of c based on change_case. */
2565 #define CHANGE_CASE(c, change_case) \
2566         (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
2567                 g_unichar_tolower (c) : \
2568                 g_unichar_toupper (c))
2569
2570 static void
2571 string_append (GString     *string,
2572                const gchar *text,
2573                ChangeCase  *change_case)
2574 {
2575   gunichar c;
2576
2577   if (text[0] == '\0')
2578     return;
2579
2580   if (*change_case == CHANGE_CASE_NONE)
2581     {
2582       g_string_append (string, text);
2583     }
2584   else if (*change_case & CHANGE_CASE_SINGLE_MASK)
2585     {
2586       c = g_utf8_get_char (text);
2587       g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2588       g_string_append (string, g_utf8_next_char (text));
2589       *change_case = CHANGE_CASE_NONE;
2590     }
2591   else
2592     {
2593       while (*text != '\0')
2594         {
2595           c = g_utf8_get_char (text);
2596           g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
2597           text = g_utf8_next_char (text);
2598         }
2599     }
2600 }
2601
2602 static gboolean
2603 interpolate_replacement (const GMatchInfo *match_info,
2604                          GString          *result,
2605                          gpointer          data)
2606 {
2607   GList *list;
2608   InterpolationData *idata;
2609   gchar *match;
2610   ChangeCase change_case = CHANGE_CASE_NONE;
2611
2612   for (list = data; list; list = list->next)
2613     {
2614       idata = list->data;
2615       switch (idata->type)
2616         {
2617         case REPL_TYPE_STRING:
2618           string_append (result, idata->text, &change_case);
2619           break;
2620         case REPL_TYPE_CHARACTER:
2621           g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
2622           if (change_case & CHANGE_CASE_SINGLE_MASK)
2623             change_case = CHANGE_CASE_NONE;
2624           break;
2625         case REPL_TYPE_NUMERIC_REFERENCE:
2626           match = g_match_info_fetch (match_info, idata->num);
2627           if (match)
2628             {
2629               string_append (result, match, &change_case);
2630               g_free (match);
2631             }
2632           break;
2633         case REPL_TYPE_SYMBOLIC_REFERENCE:
2634           match = g_match_info_fetch_named (match_info, idata->text);
2635           if (match)
2636             {
2637               string_append (result, match, &change_case);
2638               g_free (match);
2639             }
2640           break;
2641         case REPL_TYPE_CHANGE_CASE:
2642           change_case = idata->change_case;
2643           break;
2644         }
2645     }
2646
2647   return FALSE;
2648 }
2649
2650 /* whether actual match_info is needed for replacement, i.e.
2651  * whether there are references
2652  */
2653 static gboolean
2654 interpolation_list_needs_match (GList *list)
2655 {
2656   while (list != NULL)
2657     {
2658       InterpolationData *data = list->data;
2659
2660       if (data->type == REPL_TYPE_SYMBOLIC_REFERENCE ||
2661           data->type == REPL_TYPE_NUMERIC_REFERENCE)
2662         {
2663           return TRUE;
2664         }
2665
2666       list = list->next;
2667     }
2668
2669   return FALSE;
2670 }
2671
2672 /**
2673  * g_regex_replace:
2674  * @regex: a #GRegex structure
2675  * @string: (array length=string_len): the string to perform matches against
2676  * @string_len: the length of @string, or -1 if @string is nul-terminated
2677  * @start_position: starting index of the string to match
2678  * @replacement: text to replace each match with
2679  * @match_options: options for the match
2680  * @error: location to store the error occurring, or %NULL to ignore errors
2681  *
2682  * Replaces all occurrences of the pattern in @regex with the
2683  * replacement text. Backreferences of the form '\number' or
2684  * '\g&lt;number&gt;' in the replacement text are interpolated by the
2685  * number-th captured subexpression of the match, '\g&lt;name&gt;' refers
2686  * to the captured subexpression with the given name. '\0' refers to the
2687  * complete match, but '\0' followed by a number is the octal representation
2688  * of a character. To include a literal '\' in the replacement, write '\\'.
2689  * There are also escapes that changes the case of the following text:
2690  *
2691  * <variablelist>
2692  * <varlistentry><term>\l</term>
2693  * <listitem>
2694  * <para>Convert to lower case the next character</para>
2695  * </listitem>
2696  * </varlistentry>
2697  * <varlistentry><term>\u</term>
2698  * <listitem>
2699  * <para>Convert to upper case the next character</para>
2700  * </listitem>
2701  * </varlistentry>
2702  * <varlistentry><term>\L</term>
2703  * <listitem>
2704  * <para>Convert to lower case till \E</para>
2705  * </listitem>
2706  * </varlistentry>
2707  * <varlistentry><term>\U</term>
2708  * <listitem>
2709  * <para>Convert to upper case till \E</para>
2710  * </listitem>
2711  * </varlistentry>
2712  * <varlistentry><term>\E</term>
2713  * <listitem>
2714  * <para>End case modification</para>
2715  * </listitem>
2716  * </varlistentry>
2717  * </variablelist>
2718  *
2719  * If you do not need to use backreferences use g_regex_replace_literal().
2720  *
2721  * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was
2722  * passed to g_regex_new(). If you want to use not UTF-8 encoded stings
2723  * you can use g_regex_replace_literal().
2724  *
2725  * Setting @start_position differs from just passing over a shortened
2726  * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that
2727  * begins with any kind of lookbehind assertion, such as "\b".
2728  *
2729  * Returns: a newly allocated string containing the replacements
2730  *
2731  * Since: 2.14
2732  */
2733 gchar *
2734 g_regex_replace (const GRegex      *regex,
2735                  const gchar       *string,
2736                  gssize             string_len,
2737                  gint               start_position,
2738                  const gchar       *replacement,
2739                  GRegexMatchFlags   match_options,
2740                  GError           **error)
2741 {
2742   gchar *result;
2743   GList *list;
2744   GError *tmp_error = NULL;
2745
2746   g_return_val_if_fail (regex != NULL, NULL);
2747   g_return_val_if_fail (string != NULL, NULL);
2748   g_return_val_if_fail (start_position >= 0, NULL);
2749   g_return_val_if_fail (replacement != NULL, NULL);
2750   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2751   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2752
2753   list = split_replacement (replacement, &tmp_error);
2754   if (tmp_error != NULL)
2755     {
2756       g_propagate_error (error, tmp_error);
2757       return NULL;
2758     }
2759
2760   result = g_regex_replace_eval (regex,
2761                                  string, string_len, start_position,
2762                                  match_options,
2763                                  interpolate_replacement,
2764                                  (gpointer)list,
2765                                  &tmp_error);
2766   if (tmp_error != NULL)
2767     g_propagate_error (error, tmp_error);
2768
2769   g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
2770
2771   return result;
2772 }
2773
2774 static gboolean
2775 literal_replacement (const GMatchInfo *match_info,
2776                      GString          *result,
2777                      gpointer          data)
2778 {
2779   g_string_append (result, data);
2780   return FALSE;
2781 }
2782
2783 /**
2784  * g_regex_replace_literal:
2785  * @regex: a #GRegex structure
2786  * @string: (array length=string_len): the string to perform matches against
2787  * @string_len: the length of @string, or -1 if @string is nul-terminated
2788  * @start_position: starting index of the string to match
2789  * @replacement: text to replace each match with
2790  * @match_options: options for the match
2791  * @error: location to store the error occurring, or %NULL to ignore errors
2792  *
2793  * Replaces all occurrences of the pattern in @regex with the
2794  * replacement text. @replacement is replaced literally, to
2795  * include backreferences use g_regex_replace().
2796  *
2797  * Setting @start_position differs from just passing over a
2798  * shortened string and setting #G_REGEX_MATCH_NOTBOL in the
2799  * case of a pattern that begins with any kind of lookbehind
2800  * assertion, such as "\b".
2801  *
2802  * Returns: a newly allocated string containing the replacements
2803  *
2804  * Since: 2.14
2805  */
2806 gchar *
2807 g_regex_replace_literal (const GRegex      *regex,
2808                          const gchar       *string,
2809                          gssize             string_len,
2810                          gint               start_position,
2811                          const gchar       *replacement,
2812                          GRegexMatchFlags   match_options,
2813                          GError           **error)
2814 {
2815   g_return_val_if_fail (replacement != NULL, NULL);
2816   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2817
2818   return g_regex_replace_eval (regex,
2819                                string, string_len, start_position,
2820                                match_options,
2821                                literal_replacement,
2822                                (gpointer)replacement,
2823                                error);
2824 }
2825
2826 /**
2827  * g_regex_replace_eval:
2828  * @regex: a #GRegex structure from g_regex_new()
2829  * @string: (array length=string_len): string to perform matches against
2830  * @string_len: the length of @string, or -1 if @string is nul-terminated
2831  * @start_position: starting index of the string to match
2832  * @match_options: options for the match
2833  * @eval: a function to call for each match
2834  * @user_data: user data to pass to the function
2835  * @error: location to store the error occurring, or %NULL to ignore errors
2836  *
2837  * Replaces occurrences of the pattern in regex with the output of
2838  * @eval for that occurrence.
2839  *
2840  * Setting @start_position differs from just passing over a shortened
2841  * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
2842  * that begins with any kind of lookbehind assertion, such as "\b".
2843  *
2844  * The following example uses g_regex_replace_eval() to replace multiple
2845  * strings at once:
2846  * |[
2847  * static gboolean
2848  * eval_cb (const GMatchInfo *info,
2849  *          GString          *res,
2850  *          gpointer          data)
2851  * {
2852  *   gchar *match;
2853  *   gchar *r;
2854  *
2855  *    match = g_match_info_fetch (info, 0);
2856  *    r = g_hash_table_lookup ((GHashTable *)data, match);
2857  *    g_string_append (res, r);
2858  *    g_free (match);
2859  *
2860  *    return FALSE;
2861  * }
2862  *
2863  * /&ast; ... &ast;/
2864  *
2865  * GRegex *reg;
2866  * GHashTable *h;
2867  * gchar *res;
2868  *
2869  * h = g_hash_table_new (g_str_hash, g_str_equal);
2870  *
2871  * g_hash_table_insert (h, "1", "ONE");
2872  * g_hash_table_insert (h, "2", "TWO");
2873  * g_hash_table_insert (h, "3", "THREE");
2874  * g_hash_table_insert (h, "4", "FOUR");
2875  *
2876  * reg = g_regex_new ("1|2|3|4", 0, 0, NULL);
2877  * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL);
2878  * g_hash_table_destroy (h);
2879  *
2880  * /&ast; ... &ast;/
2881  * ]|
2882  *
2883  * Returns: a newly allocated string containing the replacements
2884  *
2885  * Since: 2.14
2886  */
2887 gchar *
2888 g_regex_replace_eval (const GRegex        *regex,
2889                       const gchar         *string,
2890                       gssize               string_len,
2891                       gint                 start_position,
2892                       GRegexMatchFlags     match_options,
2893                       GRegexEvalCallback   eval,
2894                       gpointer             user_data,
2895                       GError             **error)
2896 {
2897   GMatchInfo *match_info;
2898   GString *result;
2899   gint str_pos = 0;
2900   gboolean done = FALSE;
2901   GError *tmp_error = NULL;
2902
2903   g_return_val_if_fail (regex != NULL, NULL);
2904   g_return_val_if_fail (string != NULL, NULL);
2905   g_return_val_if_fail (start_position >= 0, NULL);
2906   g_return_val_if_fail (eval != NULL, NULL);
2907   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2908
2909   if (string_len < 0)
2910     string_len = strlen (string);
2911
2912   result = g_string_sized_new (string_len);
2913
2914   /* run down the string making matches. */
2915   g_regex_match_full (regex, string, string_len, start_position,
2916                       match_options, &match_info, &tmp_error);
2917   while (!done && g_match_info_matches (match_info))
2918     {
2919       g_string_append_len (result,
2920                            string + str_pos,
2921                            match_info->offsets[0] - str_pos);
2922       done = (*eval) (match_info, result, user_data);
2923       str_pos = match_info->offsets[1];
2924       g_match_info_next (match_info, &tmp_error);
2925     }
2926   g_match_info_free (match_info);
2927   if (tmp_error != NULL)
2928     {
2929       g_propagate_error (error, tmp_error);
2930       g_string_free (result, TRUE);
2931       return NULL;
2932     }
2933
2934   g_string_append_len (result, string + str_pos, string_len - str_pos);
2935   return g_string_free (result, FALSE);
2936 }
2937
2938 /**
2939  * g_regex_check_replacement:
2940  * @replacement: the replacement string
2941  * @has_references: (out) (allow-none): location to store information about
2942  *   references in @replacement or %NULL
2943  * @error: location to store error
2944  *
2945  * Checks whether @replacement is a valid replacement string
2946  * (see g_regex_replace()), i.e. that all escape sequences in
2947  * it are valid.
2948  *
2949  * If @has_references is not %NULL then @replacement is checked
2950  * for pattern references. For instance, replacement text 'foo\n'
2951  * does not contain references and may be evaluated without information
2952  * about actual match, but '\0\1' (whole match followed by first
2953  * subpattern) requires valid #GMatchInfo object.
2954  *
2955  * Returns: whether @replacement is a valid replacement string
2956  *
2957  * Since: 2.14
2958  */
2959 gboolean
2960 g_regex_check_replacement (const gchar  *replacement,
2961                            gboolean     *has_references,
2962                            GError      **error)
2963 {
2964   GList *list;
2965   GError *tmp = NULL;
2966
2967   list = split_replacement (replacement, &tmp);
2968
2969   if (tmp)
2970   {
2971     g_propagate_error (error, tmp);
2972     return FALSE;
2973   }
2974
2975   if (has_references)
2976     *has_references = interpolation_list_needs_match (list);
2977
2978   g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
2979
2980   return TRUE;
2981 }
2982
2983 /**
2984  * g_regex_escape_nul:
2985  * @string: the string to escape
2986  * @length: the length of @string
2987  *
2988  * Escapes the nul characters in @string to "\x00".  It can be used
2989  * to compile a regex with embedded nul characters.
2990  *
2991  * For completeness, @length can be -1 for a nul-terminated string.
2992  * In this case the output string will be of course equal to @string.
2993  *
2994  * Returns: a newly-allocated escaped string
2995  *
2996  * Since: 2.30
2997  */
2998 gchar *
2999 g_regex_escape_nul (const gchar *string,
3000                     gint         length)
3001 {
3002   GString *escaped;
3003   const gchar *p, *piece_start, *end;
3004   gint backslashes;
3005
3006   g_return_val_if_fail (string != NULL, NULL);
3007
3008   if (length < 0)
3009     return g_strdup (string);
3010
3011   end = string + length;
3012   p = piece_start = string;
3013   escaped = g_string_sized_new (length + 1);
3014
3015   backslashes = 0;
3016   while (p < end)
3017     {
3018       switch (*p)
3019         {
3020         case '\0':
3021           if (p != piece_start)
3022             {
3023               /* copy the previous piece. */
3024               g_string_append_len (escaped, piece_start, p - piece_start);
3025             }
3026           if ((backslashes & 1) == 0)
3027             g_string_append_c (escaped, '\\');
3028           g_string_append_c (escaped, 'x');
3029           g_string_append_c (escaped, '0');
3030           g_string_append_c (escaped, '0');
3031           piece_start = ++p;
3032           backslashes = 0;
3033           break;
3034         case '\\':
3035           backslashes++;
3036           ++p;
3037           break;
3038         default:
3039           backslashes = 0;
3040           p = g_utf8_next_char (p);
3041           break;
3042         }
3043     }
3044
3045   if (piece_start < end)
3046     g_string_append_len (escaped, piece_start, end - piece_start);
3047
3048   return g_string_free (escaped, FALSE);
3049 }
3050
3051 /**
3052  * g_regex_escape_string:
3053  * @string: (array length=length): the string to escape
3054  * @length: the length of @string, or -1 if @string is nul-terminated
3055  *
3056  * Escapes the special characters used for regular expressions
3057  * in @string, for instance "a.b*c" becomes "a\.b\*c". This
3058  * function is useful to dynamically generate regular expressions.
3059  *
3060  * @string can contain nul characters that are replaced with "\0",
3061  * in this case remember to specify the correct length of @string
3062  * in @length.
3063  *
3064  * Returns: a newly-allocated escaped string
3065  *
3066  * Since: 2.14
3067  */
3068 gchar *
3069 g_regex_escape_string (const gchar *string,
3070                        gint         length)
3071 {
3072   GString *escaped;
3073   const char *p, *piece_start, *end;
3074
3075   g_return_val_if_fail (string != NULL, NULL);
3076
3077   if (length < 0)
3078     length = strlen (string);
3079
3080   end = string + length;
3081   p = piece_start = string;
3082   escaped = g_string_sized_new (length + 1);
3083
3084   while (p < end)
3085     {
3086       switch (*p)
3087         {
3088         case '\0':
3089         case '\\':
3090         case '|':
3091         case '(':
3092         case ')':
3093         case '[':
3094         case ']':
3095         case '{':
3096         case '}':
3097         case '^':
3098         case '$':
3099         case '*':
3100         case '+':
3101         case '?':
3102         case '.':
3103           if (p != piece_start)
3104             /* copy the previous piece. */
3105             g_string_append_len (escaped, piece_start, p - piece_start);
3106           g_string_append_c (escaped, '\\');
3107           if (*p == '\0')
3108             g_string_append_c (escaped, '0');
3109           else
3110             g_string_append_c (escaped, *p);
3111           piece_start = ++p;
3112           break;
3113         default:
3114           p = g_utf8_next_char (p);
3115           break;
3116         }
3117   }
3118
3119   if (piece_start < end)
3120     g_string_append_len (escaped, piece_start, end - piece_start);
3121
3122   return g_string_free (escaped, FALSE);
3123 }