Add ensure_required_types constructor
[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  * Copyright (C) 2022, Marco Trevisan <marco.trevisan@canonical.com>
7  *
8  * SPDX-License-Identifier: LGPL-2.1-or-later
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "config.h"
25
26 #include <stdint.h>
27 #include <string.h>
28
29 #define PCRE2_CODE_UNIT_WIDTH 8
30 #include <pcre2.h>
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 "gtestutils.h"
40 #include "gthread.h"
41
42 /**
43  * SECTION:gregex
44  * @title: Perl-compatible regular expressions
45  * @short_description: matches strings against regular expressions
46  * @see_also: [Regular expression syntax][glib-regex-syntax]
47  *
48  * The g_regex_*() functions implement regular
49  * expression pattern matching using syntax and semantics similar to
50  * Perl regular expression.
51  *
52  * Some functions accept a @start_position argument, setting it differs
53  * from just passing over a shortened string and setting %G_REGEX_MATCH_NOTBOL
54  * in the case of a pattern that begins with any kind of lookbehind assertion.
55  * For example, consider the pattern "\Biss\B" which finds occurrences of "iss"
56  * in the middle of words. ("\B" matches only if the current position in the
57  * subject is not a word boundary.) When applied to the string "Mississipi"
58  * from the fourth byte, namely "issipi", it does not match, because "\B" is
59  * always false at the start of the subject, which is deemed to be a word
60  * boundary. However, if the entire string is passed , but with
61  * @start_position set to 4, it finds the second occurrence of "iss" because
62  * it is able to look behind the starting point to discover that it is
63  * preceded by a letter.
64  *
65  * Note that, unless you set the %G_REGEX_RAW flag, all the strings passed
66  * to these functions must be encoded in UTF-8. The lengths and the positions
67  * inside the strings are in bytes and not in characters, so, for instance,
68  * "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a
69  * single character. If you set %G_REGEX_RAW the strings can be non-valid
70  * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two
71  * bytes and two characters long.
72  *
73  * When matching a pattern, "\n" matches only against a "\n" character in
74  * the string, and "\r" matches only a "\r" character. To match any newline
75  * sequence use "\R". This particular group matches either the two-character
76  * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed,
77  * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"),
78  * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line
79  * separator, U+2028), or PS (paragraph separator, U+2029).
80  *
81  * The behaviour of the dot, circumflex, and dollar metacharacters are
82  * affected by newline characters, the default is to recognize any newline
83  * character (the same characters recognized by "\R"). This can be changed
84  * with %G_REGEX_NEWLINE_CR, %G_REGEX_NEWLINE_LF and %G_REGEX_NEWLINE_CRLF
85  * compile options, and with %G_REGEX_MATCH_NEWLINE_ANY,
86  * %G_REGEX_MATCH_NEWLINE_CR, %G_REGEX_MATCH_NEWLINE_LF and
87  * %G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also
88  * relevant when compiling a pattern if %G_REGEX_EXTENDED is set, and an
89  * unescaped "#" outside a character class is encountered. This indicates
90  * a comment that lasts until after the next newline.
91  *
92  * Creating and manipulating the same #GRegex structure from different
93  * threads is not a problem as #GRegex does not modify its internal
94  * state between creation and destruction, on the other hand #GMatchInfo
95  * is not threadsafe.
96  *
97  * The regular expressions low-level functionalities are obtained through
98  * the excellent
99  * [PCRE](http://www.pcre.org/)
100  * library written by Philip Hazel.
101  */
102
103 #define G_REGEX_PCRE_GENERIC_MASK (PCRE2_ANCHORED       | \
104                                    PCRE2_NO_UTF_CHECK   | \
105                                    PCRE2_ENDANCHORED)
106
107 /* Mask of all the possible values for GRegexCompileFlags. */
108 #define G_REGEX_COMPILE_MASK (G_REGEX_DEFAULT          | \
109                               G_REGEX_CASELESS         | \
110                               G_REGEX_MULTILINE        | \
111                               G_REGEX_DOTALL           | \
112                               G_REGEX_EXTENDED         | \
113                               G_REGEX_ANCHORED         | \
114                               G_REGEX_DOLLAR_ENDONLY   | \
115                               G_REGEX_UNGREEDY         | \
116                               G_REGEX_RAW              | \
117                               G_REGEX_NO_AUTO_CAPTURE  | \
118                               G_REGEX_OPTIMIZE         | \
119                               G_REGEX_FIRSTLINE        | \
120                               G_REGEX_DUPNAMES         | \
121                               G_REGEX_NEWLINE_CR       | \
122                               G_REGEX_NEWLINE_LF       | \
123                               G_REGEX_NEWLINE_CRLF     | \
124                               G_REGEX_NEWLINE_ANYCRLF  | \
125                               G_REGEX_BSR_ANYCRLF)
126
127 #define G_REGEX_PCRE2_COMPILE_MASK (PCRE2_ALLOW_EMPTY_CLASS    | \
128                                     PCRE2_ALT_BSUX             | \
129                                     PCRE2_AUTO_CALLOUT         | \
130                                     PCRE2_CASELESS             | \
131                                     PCRE2_DOLLAR_ENDONLY       | \
132                                     PCRE2_DOTALL               | \
133                                     PCRE2_DUPNAMES             | \
134                                     PCRE2_EXTENDED             | \
135                                     PCRE2_FIRSTLINE            | \
136                                     PCRE2_MATCH_UNSET_BACKREF  | \
137                                     PCRE2_MULTILINE            | \
138                                     PCRE2_NEVER_UCP            | \
139                                     PCRE2_NEVER_UTF            | \
140                                     PCRE2_NO_AUTO_CAPTURE      | \
141                                     PCRE2_NO_AUTO_POSSESS      | \
142                                     PCRE2_NO_DOTSTAR_ANCHOR    | \
143                                     PCRE2_NO_START_OPTIMIZE    | \
144                                     PCRE2_UCP                  | \
145                                     PCRE2_UNGREEDY             | \
146                                     PCRE2_UTF                  | \
147                                     PCRE2_NEVER_BACKSLASH_C    | \
148                                     PCRE2_ALT_CIRCUMFLEX       | \
149                                     PCRE2_ALT_VERBNAMES        | \
150                                     PCRE2_USE_OFFSET_LIMIT     | \
151                                     PCRE2_EXTENDED_MORE        | \
152                                     PCRE2_LITERAL              | \
153                                     PCRE2_MATCH_INVALID_UTF    | \
154                                     G_REGEX_PCRE_GENERIC_MASK)
155
156 #define G_REGEX_COMPILE_NONPCRE_MASK (PCRE2_UTF)
157
158 /* Mask of all the possible values for GRegexMatchFlags. */
159 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_DEFAULT          | \
160                             G_REGEX_MATCH_ANCHORED         | \
161                             G_REGEX_MATCH_NOTBOL           | \
162                             G_REGEX_MATCH_NOTEOL           | \
163                             G_REGEX_MATCH_NOTEMPTY         | \
164                             G_REGEX_MATCH_PARTIAL          | \
165                             G_REGEX_MATCH_NEWLINE_CR       | \
166                             G_REGEX_MATCH_NEWLINE_LF       | \
167                             G_REGEX_MATCH_NEWLINE_CRLF     | \
168                             G_REGEX_MATCH_NEWLINE_ANY      | \
169                             G_REGEX_MATCH_NEWLINE_ANYCRLF  | \
170                             G_REGEX_MATCH_BSR_ANYCRLF      | \
171                             G_REGEX_MATCH_BSR_ANY          | \
172                             G_REGEX_MATCH_PARTIAL_SOFT     | \
173                             G_REGEX_MATCH_PARTIAL_HARD     | \
174                             G_REGEX_MATCH_NOTEMPTY_ATSTART)
175
176 #define G_REGEX_PCRE2_MATCH_MASK (PCRE2_NOTBOL                      |\
177                                   PCRE2_NOTEOL                      |\
178                                   PCRE2_NOTEMPTY                    |\
179                                   PCRE2_NOTEMPTY_ATSTART            |\
180                                   PCRE2_PARTIAL_SOFT                |\
181                                   PCRE2_PARTIAL_HARD                |\
182                                   PCRE2_NO_JIT                      |\
183                                   PCRE2_COPY_MATCHED_SUBJECT        |\
184                                   G_REGEX_PCRE_GENERIC_MASK)
185
186 /* TODO: Support PCRE2_NEWLINE_NUL */
187 #define G_REGEX_NEWLINE_MASK (PCRE2_NEWLINE_CR |     \
188                               PCRE2_NEWLINE_LF |     \
189                               PCRE2_NEWLINE_CRLF |   \
190                               PCRE2_NEWLINE_ANYCRLF)
191
192 /* Some match options are not supported when using JIT as stated in the
193  * pcre2jit man page under the «UNSUPPORTED OPTIONS AND PATTERN ITEMS» section:
194  *   https://www.pcre.org/current/doc/html/pcre2jit.html#SEC5
195  */
196 #define G_REGEX_PCRE2_JIT_UNSUPPORTED_OPTIONS (PCRE2_ANCHORED | \
197                                                PCRE2_ENDANCHORED)
198
199 #define G_REGEX_COMPILE_NEWLINE_MASK (G_REGEX_NEWLINE_CR      | \
200                                       G_REGEX_NEWLINE_LF      | \
201                                       G_REGEX_NEWLINE_CRLF    | \
202                                       G_REGEX_NEWLINE_ANYCRLF)
203
204 #define G_REGEX_MATCH_NEWLINE_MASK (G_REGEX_MATCH_NEWLINE_CR      | \
205                                     G_REGEX_MATCH_NEWLINE_LF      | \
206                                     G_REGEX_MATCH_NEWLINE_CRLF    | \
207                                     G_REGEX_MATCH_NEWLINE_ANY    | \
208                                     G_REGEX_MATCH_NEWLINE_ANYCRLF)
209
210 /* if the string is in UTF-8 use g_utf8_ functions, else use
211  * use just +/- 1. */
212 #define NEXT_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
213                                 ((s) + 1) : \
214                                 g_utf8_next_char (s))
215 #define PREV_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
216                                 ((s) - 1) : \
217                                 g_utf8_prev_char (s))
218
219 struct _GMatchInfo
220 {
221   gint ref_count;               /* the ref count (atomic) */
222   GRegex *regex;                /* the regex */
223   uint32_t match_opts;          /* pcre match options used at match time on the regex */
224   gint matches;                 /* number of matching sub patterns, guaranteed to be <= (n_subpatterns + 1) if doing a single match (rather than matching all) */
225   uint32_t n_subpatterns;       /* total number of sub patterns in the regex */
226   gint pos;                     /* position in the string where last match left off */
227   uint32_t n_offsets;           /* number of offsets */
228   gint *offsets;                /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
229   gint *workspace;              /* workspace for pcre2_dfa_match() */
230   PCRE2_SIZE n_workspace;       /* number of workspace elements */
231   const gchar *string;          /* string passed to the match function */
232   gssize string_len;            /* length of string, in bytes */
233   pcre2_match_context *match_context;
234   pcre2_match_data *match_data;
235   pcre2_jit_stack *jit_stack;
236 };
237
238 typedef enum
239 {
240   JIT_STATUS_DEFAULT,
241   JIT_STATUS_ENABLED,
242   JIT_STATUS_DISABLED
243 } JITStatus;
244
245 struct _GRegex
246 {
247   gint ref_count;               /* the ref count for the immutable part (atomic) */
248   gchar *pattern;               /* the pattern */
249   pcre2_code *pcre_re;          /* compiled form of the pattern */
250   uint32_t compile_opts;        /* options used at compile time on the pattern, pcre2 values */
251   GRegexCompileFlags orig_compile_opts; /* options used at compile time on the pattern, gregex values */
252   uint32_t match_opts;          /* pcre2 options used at match time on the regex */
253   GRegexMatchFlags orig_match_opts; /* options used as default match options, gregex values */
254   uint32_t jit_options;         /* options which were enabled for jit compiler */
255   JITStatus jit_status;         /* indicates the status of jit compiler for this compiled regex */
256   /* The jit_status here does _not_ correspond to whether we used the JIT in the last invocation,
257    * which may be affected by match_options or a JIT_STACK_LIMIT error, but whether it was ever
258    * enabled for the current regex AND current set of jit_options.
259    * JIT_STATUS_DEFAULT means enablement was never tried,
260    * JIT_STATUS_ENABLED means it was tried and successful (even if we're not currently using it),
261    * and JIT_STATUS_DISABLED means it was tried and failed (so we shouldn't try again).
262    */
263 };
264
265 /* TRUE if ret is an error code, FALSE otherwise. */
266 #define IS_PCRE2_ERROR(ret) ((ret) < PCRE2_ERROR_NOMATCH && (ret) != PCRE2_ERROR_PARTIAL)
267
268 typedef struct _InterpolationData InterpolationData;
269 static gboolean  interpolation_list_needs_match (GList *list);
270 static gboolean  interpolate_replacement        (const GMatchInfo *match_info,
271                                                  GString *result,
272                                                  gpointer data);
273 static GList    *split_replacement              (const gchar *replacement,
274                                                  GError **error);
275 static void      free_interpolation_data        (InterpolationData *data);
276
277 static uint32_t
278 get_pcre2_compile_options (GRegexCompileFlags compile_flags)
279 {
280   /* Maps compile flags to pcre2 values */
281   uint32_t pcre2_flags = 0;
282
283   if (compile_flags & G_REGEX_CASELESS)
284     pcre2_flags |= PCRE2_CASELESS;
285   if (compile_flags & G_REGEX_MULTILINE)
286     pcre2_flags |= PCRE2_MULTILINE;
287   if (compile_flags & G_REGEX_DOTALL)
288     pcre2_flags |= PCRE2_DOTALL;
289   if (compile_flags & G_REGEX_EXTENDED)
290     pcre2_flags |= PCRE2_EXTENDED;
291   if (compile_flags & G_REGEX_ANCHORED)
292     pcre2_flags |= PCRE2_ANCHORED;
293   if (compile_flags & G_REGEX_DOLLAR_ENDONLY)
294     pcre2_flags |= PCRE2_DOLLAR_ENDONLY;
295   if (compile_flags & G_REGEX_UNGREEDY)
296     pcre2_flags |= PCRE2_UNGREEDY;
297   if (!(compile_flags & G_REGEX_RAW))
298     pcre2_flags |= PCRE2_UTF;
299   if (compile_flags & G_REGEX_NO_AUTO_CAPTURE)
300     pcre2_flags |= PCRE2_NO_AUTO_CAPTURE;
301   if (compile_flags & G_REGEX_FIRSTLINE)
302     pcre2_flags |= PCRE2_FIRSTLINE;
303   if (compile_flags & G_REGEX_DUPNAMES)
304     pcre2_flags |= PCRE2_DUPNAMES;
305
306   return pcre2_flags & G_REGEX_PCRE2_COMPILE_MASK;
307 }
308
309 static uint32_t
310 get_pcre2_match_options (GRegexMatchFlags   match_flags,
311                          GRegexCompileFlags compile_flags)
312 {
313   /* Maps match flags to pcre2 values */
314   uint32_t pcre2_flags = 0;
315
316   if (match_flags & G_REGEX_MATCH_ANCHORED)
317     pcre2_flags |= PCRE2_ANCHORED;
318   if (match_flags & G_REGEX_MATCH_NOTBOL)
319     pcre2_flags |= PCRE2_NOTBOL;
320   if (match_flags & G_REGEX_MATCH_NOTEOL)
321     pcre2_flags |= PCRE2_NOTEOL;
322   if (match_flags & G_REGEX_MATCH_NOTEMPTY)
323     pcre2_flags |= PCRE2_NOTEMPTY;
324   if (match_flags & G_REGEX_MATCH_PARTIAL_SOFT)
325     pcre2_flags |= PCRE2_PARTIAL_SOFT;
326   if (match_flags & G_REGEX_MATCH_PARTIAL_HARD)
327     pcre2_flags |= PCRE2_PARTIAL_HARD;
328   if (match_flags & G_REGEX_MATCH_NOTEMPTY_ATSTART)
329     pcre2_flags |= PCRE2_NOTEMPTY_ATSTART;
330
331   if (compile_flags & G_REGEX_RAW)
332     pcre2_flags |= PCRE2_NO_UTF_CHECK;
333
334   return pcre2_flags & G_REGEX_PCRE2_MATCH_MASK;
335 }
336
337 static GRegexCompileFlags
338 g_regex_compile_flags_from_pcre2 (uint32_t pcre2_flags)
339 {
340   GRegexCompileFlags compile_flags = G_REGEX_DEFAULT;
341
342   if (pcre2_flags & PCRE2_CASELESS)
343     compile_flags |= G_REGEX_CASELESS;
344   if (pcre2_flags & PCRE2_MULTILINE)
345     compile_flags |= G_REGEX_MULTILINE;
346   if (pcre2_flags & PCRE2_DOTALL)
347     compile_flags |= G_REGEX_DOTALL;
348   if (pcre2_flags & PCRE2_EXTENDED)
349     compile_flags |= G_REGEX_EXTENDED;
350   if (pcre2_flags & PCRE2_ANCHORED)
351     compile_flags |= G_REGEX_ANCHORED;
352   if (pcre2_flags & PCRE2_DOLLAR_ENDONLY)
353     compile_flags |= G_REGEX_DOLLAR_ENDONLY;
354   if (pcre2_flags & PCRE2_UNGREEDY)
355     compile_flags |= G_REGEX_UNGREEDY;
356   if (!(pcre2_flags & PCRE2_UTF))
357     compile_flags |= G_REGEX_RAW;
358   if (pcre2_flags & PCRE2_NO_AUTO_CAPTURE)
359     compile_flags |= G_REGEX_NO_AUTO_CAPTURE;
360   if (pcre2_flags & PCRE2_FIRSTLINE)
361     compile_flags |= G_REGEX_FIRSTLINE;
362   if (pcre2_flags & PCRE2_DUPNAMES)
363     compile_flags |= G_REGEX_DUPNAMES;
364
365   return compile_flags & G_REGEX_COMPILE_MASK;
366 }
367
368 static GRegexMatchFlags
369 g_regex_match_flags_from_pcre2 (uint32_t pcre2_flags)
370 {
371   GRegexMatchFlags match_flags = G_REGEX_MATCH_DEFAULT;
372
373   if (pcre2_flags & PCRE2_ANCHORED)
374     match_flags |= G_REGEX_MATCH_ANCHORED;
375   if (pcre2_flags & PCRE2_NOTBOL)
376     match_flags |= G_REGEX_MATCH_NOTBOL;
377   if (pcre2_flags & PCRE2_NOTEOL)
378     match_flags |= G_REGEX_MATCH_NOTEOL;
379   if (pcre2_flags & PCRE2_NOTEMPTY)
380     match_flags |= G_REGEX_MATCH_NOTEMPTY;
381   if (pcre2_flags & PCRE2_PARTIAL_SOFT)
382     match_flags |= G_REGEX_MATCH_PARTIAL_SOFT;
383   if (pcre2_flags & PCRE2_PARTIAL_HARD)
384     match_flags |= G_REGEX_MATCH_PARTIAL_HARD;
385   if (pcre2_flags & PCRE2_NOTEMPTY_ATSTART)
386     match_flags |= G_REGEX_MATCH_NOTEMPTY_ATSTART;
387
388   return (match_flags & G_REGEX_MATCH_MASK);
389 }
390
391 static uint32_t
392 get_pcre2_newline_compile_options (GRegexCompileFlags compile_flags)
393 {
394   compile_flags &= G_REGEX_COMPILE_NEWLINE_MASK;
395
396   switch (compile_flags)
397     {
398     case G_REGEX_NEWLINE_CR:
399       return PCRE2_NEWLINE_CR;
400     case G_REGEX_NEWLINE_LF:
401       return PCRE2_NEWLINE_LF;
402     case G_REGEX_NEWLINE_CRLF:
403       return PCRE2_NEWLINE_CRLF;
404     case G_REGEX_NEWLINE_ANYCRLF:
405       return PCRE2_NEWLINE_ANYCRLF;
406     default:
407       if (compile_flags != 0)
408         return 0;
409
410       return PCRE2_NEWLINE_ANY;
411     }
412 }
413
414 static uint32_t
415 get_pcre2_newline_match_options (GRegexMatchFlags match_flags)
416 {
417   switch (match_flags & G_REGEX_MATCH_NEWLINE_MASK)
418     {
419     case G_REGEX_MATCH_NEWLINE_CR:
420       return PCRE2_NEWLINE_CR;
421     case G_REGEX_MATCH_NEWLINE_LF:
422       return PCRE2_NEWLINE_LF;
423     case G_REGEX_MATCH_NEWLINE_CRLF:
424       return PCRE2_NEWLINE_CRLF;
425     case G_REGEX_MATCH_NEWLINE_ANY:
426       return PCRE2_NEWLINE_ANY;
427     case G_REGEX_MATCH_NEWLINE_ANYCRLF:
428       return PCRE2_NEWLINE_ANYCRLF;
429     default:
430       return 0;
431     }
432 }
433
434 static uint32_t
435 get_pcre2_bsr_compile_options (GRegexCompileFlags compile_flags)
436 {
437   if (compile_flags & G_REGEX_BSR_ANYCRLF)
438     return PCRE2_BSR_ANYCRLF;
439
440   return PCRE2_BSR_UNICODE;
441 }
442
443 static uint32_t
444 get_pcre2_bsr_match_options (GRegexMatchFlags match_flags)
445 {
446   if (match_flags & G_REGEX_MATCH_BSR_ANYCRLF)
447     return PCRE2_BSR_ANYCRLF;
448
449   if (match_flags & G_REGEX_MATCH_BSR_ANY)
450     return PCRE2_BSR_UNICODE;
451
452   return 0;
453 }
454
455 static char *
456 get_pcre2_error_string (int errcode)
457 {
458   PCRE2_UCHAR8 error_msg[2048];
459   int err_length;
460
461   err_length = pcre2_get_error_message (errcode, error_msg,
462                                         G_N_ELEMENTS (error_msg));
463
464   if (err_length <= 0)
465     return NULL;
466
467   /* The array is always filled with a trailing zero */
468   g_assert ((size_t) err_length < G_N_ELEMENTS (error_msg));
469   return g_memdup2 (error_msg, err_length + 1);
470 }
471
472 static const gchar *
473 translate_match_error (gint errcode)
474 {
475   switch (errcode)
476     {
477     case PCRE2_ERROR_NOMATCH:
478       /* not an error */
479       break;
480     case PCRE2_ERROR_NULL:
481       /* NULL argument, this should not happen in GRegex */
482       g_critical ("A NULL argument was passed to PCRE");
483       break;
484     case PCRE2_ERROR_BADOPTION:
485       return "bad options";
486     case PCRE2_ERROR_BADMAGIC:
487       return _("corrupted object");
488     case PCRE2_ERROR_NOMEMORY:
489       return _("out of memory");
490     case PCRE2_ERROR_NOSUBSTRING:
491       /* not used by pcre2_match() */
492       break;
493     case PCRE2_ERROR_MATCHLIMIT:
494     case PCRE2_ERROR_CALLOUT:
495       /* callouts are not implemented */
496       break;
497     case PCRE2_ERROR_BADUTFOFFSET:
498       /* we do not check if strings are valid */
499       break;
500     case PCRE2_ERROR_PARTIAL:
501       /* not an error */
502       break;
503     case PCRE2_ERROR_INTERNAL:
504       return _("internal error");
505     case PCRE2_ERROR_DFA_UITEM:
506       return _("the pattern contains items not supported for partial matching");
507     case PCRE2_ERROR_DFA_UCOND:
508       return _("back references as conditions are not supported for partial matching");
509     case PCRE2_ERROR_DFA_WSSIZE:
510       /* handled expanding the workspace */
511       break;
512     case PCRE2_ERROR_DFA_RECURSE:
513     case PCRE2_ERROR_RECURSIONLIMIT:
514       return _("recursion limit reached");
515     case PCRE2_ERROR_BADOFFSET:
516       return _("bad offset");
517     case PCRE2_ERROR_RECURSELOOP:
518       return _("recursion loop");
519     case PCRE2_ERROR_JIT_BADOPTION:
520       /* should not happen in GRegex since we check modes before each match */
521       return _("matching mode is requested that was not compiled for JIT");
522     default:
523       break;
524     }
525   return NULL;
526 }
527
528 static char *
529 get_match_error_message (int errcode)
530 {
531   const char *msg = translate_match_error (errcode);
532   char *error_string;
533
534   if (msg)
535     return g_strdup (msg);
536
537   error_string = get_pcre2_error_string (errcode);
538
539   if (error_string)
540     return error_string;
541
542   return g_strdup (_("unknown error"));
543 }
544
545 static void
546 translate_compile_error (gint *errcode, const gchar **errmsg)
547 {
548   /* If errcode is known we put the translatable error message in
549    * errmsg. If errcode is unknown we put the generic
550    * G_REGEX_ERROR_COMPILE error code in errcode.
551    * Note that there can be more PCRE errors with the same GRegexError
552    * and that some PCRE errors are useless for us.
553    */
554   gint original_errcode = *errcode;
555
556   *errcode = -1;
557   *errmsg = NULL;
558
559   switch (original_errcode)
560     {
561     case PCRE2_ERROR_END_BACKSLASH:
562       *errcode = G_REGEX_ERROR_STRAY_BACKSLASH;
563       *errmsg = _("\\ at end of pattern");
564       break;
565     case PCRE2_ERROR_END_BACKSLASH_C:
566       *errcode = G_REGEX_ERROR_MISSING_CONTROL_CHAR;
567       *errmsg = _("\\c at end of pattern");
568       break;
569     case PCRE2_ERROR_UNKNOWN_ESCAPE:
570     case PCRE2_ERROR_UNSUPPORTED_ESCAPE_SEQUENCE:
571       *errcode = G_REGEX_ERROR_UNRECOGNIZED_ESCAPE;
572       *errmsg = _("unrecognized character following \\");
573       break;
574     case PCRE2_ERROR_QUANTIFIER_OUT_OF_ORDER:
575       *errcode = G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER;
576       *errmsg = _("numbers out of order in {} quantifier");
577       break;
578     case PCRE2_ERROR_QUANTIFIER_TOO_BIG:
579       *errcode = G_REGEX_ERROR_QUANTIFIER_TOO_BIG;
580       *errmsg = _("number too big in {} quantifier");
581       break;
582     case PCRE2_ERROR_MISSING_SQUARE_BRACKET:
583       *errcode = G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS;
584       *errmsg = _("missing terminating ] for character class");
585       break;
586     case PCRE2_ERROR_ESCAPE_INVALID_IN_CLASS:
587       *errcode = G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS;
588       *errmsg = _("invalid escape sequence in character class");
589       break;
590     case PCRE2_ERROR_CLASS_RANGE_ORDER:
591       *errcode = G_REGEX_ERROR_RANGE_OUT_OF_ORDER;
592       *errmsg = _("range out of order in character class");
593       break;
594     case PCRE2_ERROR_QUANTIFIER_INVALID:
595     case PCRE2_ERROR_INTERNAL_UNEXPECTED_REPEAT:
596       *errcode = G_REGEX_ERROR_NOTHING_TO_REPEAT;
597       *errmsg = _("nothing to repeat");
598       break;
599     case PCRE2_ERROR_INVALID_AFTER_PARENS_QUERY:
600       *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
601       *errmsg = _("unrecognized character after (? or (?-");
602       break;
603     case PCRE2_ERROR_POSIX_CLASS_NOT_IN_CLASS:
604       *errcode = G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS;
605       *errmsg = _("POSIX named classes are supported only within a class");
606       break;
607     case PCRE2_ERROR_POSIX_NO_SUPPORT_COLLATING:
608       *errcode = G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED;
609       *errmsg = _("POSIX collating elements are not supported");
610       break;
611     case PCRE2_ERROR_MISSING_CLOSING_PARENTHESIS:
612     case PCRE2_ERROR_UNMATCHED_CLOSING_PARENTHESIS:
613     case PCRE2_ERROR_PARENS_QUERY_R_MISSING_CLOSING:
614       *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
615       *errmsg = _("missing terminating )");
616       break;
617     case PCRE2_ERROR_BAD_SUBPATTERN_REFERENCE:
618       *errcode = G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE;
619       *errmsg = _("reference to non-existent subpattern");
620       break;
621     case PCRE2_ERROR_MISSING_COMMENT_CLOSING:
622       *errcode = G_REGEX_ERROR_UNTERMINATED_COMMENT;
623       *errmsg = _("missing ) after comment");
624       break;
625     case PCRE2_ERROR_PATTERN_TOO_LARGE:
626       *errcode = G_REGEX_ERROR_EXPRESSION_TOO_LARGE;
627       *errmsg = _("regular expression is too large");
628       break;
629     case PCRE2_ERROR_MISSING_CONDITION_CLOSING:
630       *errcode = G_REGEX_ERROR_MALFORMED_CONDITION;
631       *errmsg = _("malformed number or name after (?(");
632       break;
633     case PCRE2_ERROR_LOOKBEHIND_NOT_FIXED_LENGTH:
634       *errcode = G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND;
635       *errmsg = _("lookbehind assertion is not fixed length");
636       break;
637     case PCRE2_ERROR_TOO_MANY_CONDITION_BRANCHES:
638       *errcode = G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES;
639       *errmsg = _("conditional group contains more than two branches");
640       break;
641     case PCRE2_ERROR_CONDITION_ASSERTION_EXPECTED:
642       *errcode = G_REGEX_ERROR_ASSERTION_EXPECTED;
643       *errmsg = _("assertion expected after (?(");
644       break;
645     case PCRE2_ERROR_BAD_RELATIVE_REFERENCE:
646       *errcode = G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE;
647       *errmsg = _("a numbered reference must not be zero");
648       break;
649     case PCRE2_ERROR_UNKNOWN_POSIX_CLASS:
650       *errcode = G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME;
651       *errmsg = _("unknown POSIX class name");
652       break;
653     case PCRE2_ERROR_CODE_POINT_TOO_BIG:
654     case PCRE2_ERROR_INVALID_HEXADECIMAL:
655       *errcode = G_REGEX_ERROR_HEX_CODE_TOO_LARGE;
656       *errmsg = _("character value in \\x{...} sequence is too large");
657       break;
658     case PCRE2_ERROR_LOOKBEHIND_INVALID_BACKSLASH_C:
659       *errcode = G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND;
660       *errmsg = _("\\C not allowed in lookbehind assertion");
661       break;
662     case PCRE2_ERROR_MISSING_NAME_TERMINATOR:
663       *errcode = G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR;
664       *errmsg = _("missing terminator in subpattern name");
665       break;
666     case PCRE2_ERROR_DUPLICATE_SUBPATTERN_NAME:
667       *errcode = G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME;
668       *errmsg = _("two named subpatterns have the same name");
669       break;
670     case PCRE2_ERROR_MALFORMED_UNICODE_PROPERTY:
671       *errcode = G_REGEX_ERROR_MALFORMED_PROPERTY;
672       *errmsg = _("malformed \\P or \\p sequence");
673       break;
674     case PCRE2_ERROR_UNKNOWN_UNICODE_PROPERTY:
675       *errcode = G_REGEX_ERROR_UNKNOWN_PROPERTY;
676       *errmsg = _("unknown property name after \\P or \\p");
677       break;
678     case PCRE2_ERROR_SUBPATTERN_NAME_TOO_LONG:
679       *errcode = G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG;
680       *errmsg = _("subpattern name is too long (maximum 32 characters)");
681       break;
682     case PCRE2_ERROR_TOO_MANY_NAMED_SUBPATTERNS:
683       *errcode = G_REGEX_ERROR_TOO_MANY_SUBPATTERNS;
684       *errmsg = _("too many named subpatterns (maximum 10,000)");
685       break;
686     case PCRE2_ERROR_OCTAL_BYTE_TOO_BIG:
687       *errcode = G_REGEX_ERROR_INVALID_OCTAL_VALUE;
688       *errmsg = _("octal value is greater than \\377");
689       break;
690     case PCRE2_ERROR_DEFINE_TOO_MANY_BRANCHES:
691       *errcode = G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE;
692       *errmsg = _("DEFINE group contains more than one branch");
693       break;
694     case PCRE2_ERROR_INTERNAL_UNKNOWN_NEWLINE:
695       *errcode = G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS;
696       *errmsg = _("inconsistent NEWLINE options");
697       break;
698     case PCRE2_ERROR_BACKSLASH_G_SYNTAX:
699       *errcode = G_REGEX_ERROR_MISSING_BACK_REFERENCE;
700       *errmsg = _("\\g is not followed by a braced, angle-bracketed, or quoted name or "
701                   "number, or by a plain number");
702       break;
703     case PCRE2_ERROR_VERB_ARGUMENT_NOT_ALLOWED:
704       *errcode = G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN;
705       *errmsg = _("an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)");
706       break;
707     case PCRE2_ERROR_VERB_UNKNOWN:
708       *errcode = G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB;
709       *errmsg = _("(*VERB) not recognized");
710       break;
711     case PCRE2_ERROR_SUBPATTERN_NUMBER_TOO_BIG:
712       *errcode = G_REGEX_ERROR_NUMBER_TOO_BIG;
713       *errmsg = _("number is too big");
714       break;
715     case PCRE2_ERROR_SUBPATTERN_NAME_EXPECTED:
716       *errcode = G_REGEX_ERROR_MISSING_SUBPATTERN_NAME;
717       *errmsg = _("missing subpattern name after (?&");
718       break;
719     case PCRE2_ERROR_SUBPATTERN_NAMES_MISMATCH:
720       *errcode = G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME;
721       *errmsg = _("different names for subpatterns of the same number are not allowed");
722       break;
723     case PCRE2_ERROR_MARK_MISSING_ARGUMENT:
724       *errcode = G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED;
725       *errmsg = _("(*MARK) must have an argument");
726       break;
727     case PCRE2_ERROR_BACKSLASH_C_SYNTAX:
728       *errcode = G_REGEX_ERROR_INVALID_CONTROL_CHAR;
729       *errmsg = _( "\\c must be followed by an ASCII character");
730       break;
731     case PCRE2_ERROR_BACKSLASH_K_SYNTAX:
732       *errcode = G_REGEX_ERROR_MISSING_NAME;
733       *errmsg = _("\\k is not followed by a braced, angle-bracketed, or quoted name");
734       break;
735     case PCRE2_ERROR_BACKSLASH_N_IN_CLASS:
736       *errcode = G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS;
737       *errmsg = _("\\N is not supported in a class");
738       break;
739     case PCRE2_ERROR_VERB_NAME_TOO_LONG:
740       *errcode = G_REGEX_ERROR_NAME_TOO_LONG;
741       *errmsg = _("name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)");
742       break;
743     case PCRE2_ERROR_INTERNAL_CODE_OVERFLOW:
744       *errcode = G_REGEX_ERROR_INTERNAL;
745       *errmsg = _("code overflow");
746       break;
747     case PCRE2_ERROR_UNRECOGNIZED_AFTER_QUERY_P:
748       *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
749       *errmsg = _("unrecognized character after (?P");
750       break;
751     case PCRE2_ERROR_INTERNAL_OVERRAN_WORKSPACE:
752       *errcode = G_REGEX_ERROR_INTERNAL;
753       *errmsg = _("overran compiling workspace");
754       break;
755     case PCRE2_ERROR_INTERNAL_MISSING_SUBPATTERN:
756       *errcode = G_REGEX_ERROR_INTERNAL;
757       *errmsg = _("previously-checked referenced subpattern not found");
758       break;
759     case PCRE2_ERROR_HEAP_FAILED:
760     case PCRE2_ERROR_INTERNAL_PARSED_OVERFLOW:
761     case PCRE2_ERROR_UNICODE_NOT_SUPPORTED:
762     case PCRE2_ERROR_UNICODE_DISALLOWED_CODE_POINT:
763     case PCRE2_ERROR_NO_SURROGATES_IN_UTF16:
764     case PCRE2_ERROR_INTERNAL_BAD_CODE_LOOKBEHINDS:
765     case PCRE2_ERROR_UNICODE_PROPERTIES_UNAVAILABLE:
766     case PCRE2_ERROR_INTERNAL_STUDY_ERROR:
767     case PCRE2_ERROR_UTF_IS_DISABLED:
768     case PCRE2_ERROR_UCP_IS_DISABLED:
769     case PCRE2_ERROR_INTERNAL_BAD_CODE_AUTO_POSSESS:
770     case PCRE2_ERROR_BACKSLASH_C_LIBRARY_DISABLED:
771     case PCRE2_ERROR_INTERNAL_BAD_CODE:
772     case PCRE2_ERROR_INTERNAL_BAD_CODE_IN_SKIP:
773       *errcode = G_REGEX_ERROR_INTERNAL;
774       break;
775     case PCRE2_ERROR_INVALID_SUBPATTERN_NAME:
776     case PCRE2_ERROR_CLASS_INVALID_RANGE:
777     case PCRE2_ERROR_ZERO_RELATIVE_REFERENCE:
778     case PCRE2_ERROR_PARENTHESES_STACK_CHECK:
779     case PCRE2_ERROR_LOOKBEHIND_TOO_COMPLICATED:
780     case PCRE2_ERROR_CALLOUT_NUMBER_TOO_BIG:
781     case PCRE2_ERROR_MISSING_CALLOUT_CLOSING:
782     case PCRE2_ERROR_ESCAPE_INVALID_IN_VERB:
783     case PCRE2_ERROR_NULL_PATTERN:
784     case PCRE2_ERROR_BAD_OPTIONS:
785     case PCRE2_ERROR_PARENTHESES_NEST_TOO_DEEP:
786     case PCRE2_ERROR_BACKSLASH_O_MISSING_BRACE:
787     case PCRE2_ERROR_INVALID_OCTAL:
788     case PCRE2_ERROR_CALLOUT_STRING_TOO_LONG:
789     case PCRE2_ERROR_BACKSLASH_U_CODE_POINT_TOO_BIG:
790     case PCRE2_ERROR_MISSING_OCTAL_OR_HEX_DIGITS:
791     case PCRE2_ERROR_VERSION_CONDITION_SYNTAX:
792     case PCRE2_ERROR_CALLOUT_NO_STRING_DELIMITER:
793     case PCRE2_ERROR_CALLOUT_BAD_STRING_DELIMITER:
794     case PCRE2_ERROR_BACKSLASH_C_CALLER_DISABLED:
795     case PCRE2_ERROR_QUERY_BARJX_NEST_TOO_DEEP:
796     case PCRE2_ERROR_PATTERN_TOO_COMPLICATED:
797     case PCRE2_ERROR_LOOKBEHIND_TOO_LONG:
798     case PCRE2_ERROR_PATTERN_STRING_TOO_LONG:
799     case PCRE2_ERROR_BAD_LITERAL_OPTIONS:
800     default:
801       *errcode = G_REGEX_ERROR_COMPILE;
802       break;
803     }
804
805   g_assert (*errcode != -1);
806 }
807
808 /* GMatchInfo */
809
810 static GMatchInfo *
811 match_info_new (const GRegex     *regex,
812                 const gchar      *string,
813                 gint              string_len,
814                 gint              start_position,
815                 GRegexMatchFlags  match_options,
816                 gboolean          is_dfa)
817 {
818   GMatchInfo *match_info;
819
820   if (string_len < 0)
821     string_len = strlen (string);
822
823   match_info = g_new0 (GMatchInfo, 1);
824   match_info->ref_count = 1;
825   match_info->regex = g_regex_ref ((GRegex *)regex);
826   match_info->string = string;
827   match_info->string_len = string_len;
828   match_info->matches = PCRE2_ERROR_NOMATCH;
829   match_info->pos = start_position;
830   match_info->match_opts =
831     get_pcre2_match_options (match_options, regex->orig_compile_opts);
832
833   pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_CAPTURECOUNT,
834                       &match_info->n_subpatterns);
835
836   match_info->match_context = pcre2_match_context_create (NULL);
837
838   if (is_dfa)
839     {
840       /* These values should be enough for most cases, if they are not
841        * enough g_regex_match_all_full() will expand them. */
842       match_info->n_workspace = 100;
843       match_info->workspace = g_new (gint, match_info->n_workspace);
844     }
845
846   match_info->n_offsets = 2;
847   match_info->offsets = g_new0 (gint, match_info->n_offsets);
848   /* Set an invalid position for the previous match. */
849   match_info->offsets[0] = -1;
850   match_info->offsets[1] = -1;
851
852   match_info->match_data = pcre2_match_data_create_from_pattern (
853       match_info->regex->pcre_re,
854       NULL);
855
856   return match_info;
857 }
858
859 static gboolean
860 recalc_match_offsets (GMatchInfo *match_info,
861                       GError     **error)
862 {
863   PCRE2_SIZE *ovector;
864   uint32_t ovector_size = 0;
865   uint32_t pre_n_offset;
866   uint32_t i;
867
868   g_assert (!IS_PCRE2_ERROR (match_info->matches));
869
870   if (match_info->matches == PCRE2_ERROR_PARTIAL)
871     ovector_size = 1;
872   else if (match_info->matches > 0)
873     ovector_size = match_info->matches;
874
875   g_assert (ovector_size != 0);
876
877   if (pcre2_get_ovector_count (match_info->match_data) < ovector_size)
878     {
879       g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
880                    _("Error while matching regular expression %s: %s"),
881                    match_info->regex->pattern, _("code overflow"));
882       return FALSE;
883     }
884
885   pre_n_offset = match_info->n_offsets;
886   match_info->n_offsets = ovector_size * 2;
887   ovector = pcre2_get_ovector_pointer (match_info->match_data);
888
889   if (match_info->n_offsets != pre_n_offset)
890     {
891       match_info->offsets = g_realloc_n (match_info->offsets,
892                                          match_info->n_offsets,
893                                          sizeof (gint));
894     }
895
896   for (i = 0; i < match_info->n_offsets; i++)
897     {
898       match_info->offsets[i] = (int) ovector[i];
899     }
900
901   return TRUE;
902 }
903
904 static JITStatus
905 enable_jit_with_match_options (GMatchInfo  *match_info,
906                                uint32_t  match_options)
907 {
908   gint retval;
909   uint32_t old_jit_options, new_jit_options;
910
911   if (!(match_info->regex->orig_compile_opts & G_REGEX_OPTIMIZE))
912     return JIT_STATUS_DISABLED;
913
914   if (match_info->regex->jit_status == JIT_STATUS_DISABLED)
915     return JIT_STATUS_DISABLED;
916
917   if (match_options & G_REGEX_PCRE2_JIT_UNSUPPORTED_OPTIONS)
918     return JIT_STATUS_DISABLED;
919
920   old_jit_options = match_info->regex->jit_options;
921   new_jit_options = old_jit_options | PCRE2_JIT_COMPLETE;
922   if (match_options & PCRE2_PARTIAL_HARD)
923     new_jit_options |= PCRE2_JIT_PARTIAL_HARD;
924   if (match_options & PCRE2_PARTIAL_SOFT)
925     new_jit_options |= PCRE2_JIT_PARTIAL_SOFT;
926
927   /* no new options enabled */
928   if (new_jit_options == old_jit_options)
929     {
930       g_assert (match_info->regex->jit_status != JIT_STATUS_DEFAULT);
931       return match_info->regex->jit_status;
932     }
933
934   retval = pcre2_jit_compile (match_info->regex->pcre_re, new_jit_options);
935   if (retval == 0)
936     {
937       match_info->regex->jit_status = JIT_STATUS_ENABLED;
938
939       match_info->regex->jit_options = new_jit_options;
940       /* Set min stack size for JIT to 32KiB and max to 512KiB */
941       match_info->jit_stack = pcre2_jit_stack_create (1 << 15, 1 << 19, NULL);
942       pcre2_jit_stack_assign (match_info->match_context, NULL, match_info->jit_stack);
943     }
944   else
945     {
946       match_info->regex->jit_status = JIT_STATUS_DISABLED;
947
948       switch (retval)
949         {
950         case PCRE2_ERROR_NOMEMORY:
951           g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
952                    "but JIT was unable to allocate executable memory for the "
953                    "compiler. Falling back to interpretive code.");
954           break;
955         case PCRE2_ERROR_JIT_BADOPTION:
956           g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
957                    "but JIT support is not available. Falling back to "
958                    "interpretive code.");
959           break;
960         default:
961           g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
962                    "but request for JIT support had unexpectedly failed (error %d). "
963                    "Falling back to interpretive code.",
964                    retval);
965           break;
966         }
967     }
968
969   return match_info->regex->jit_status;
970
971   g_assert_not_reached ();
972 }
973
974 /**
975  * g_match_info_get_regex:
976  * @match_info: a #GMatchInfo
977  *
978  * Returns #GRegex object used in @match_info. It belongs to Glib
979  * and must not be freed. Use g_regex_ref() if you need to keep it
980  * after you free @match_info object.
981  *
982  * Returns: (transfer none): #GRegex object used in @match_info
983  *
984  * Since: 2.14
985  */
986 GRegex *
987 g_match_info_get_regex (const GMatchInfo *match_info)
988 {
989   g_return_val_if_fail (match_info != NULL, NULL);
990   return match_info->regex;
991 }
992
993 /**
994  * g_match_info_get_string:
995  * @match_info: a #GMatchInfo
996  *
997  * Returns the string searched with @match_info. This is the
998  * string passed to g_regex_match() or g_regex_replace() so
999  * you may not free it before calling this function.
1000  *
1001  * Returns: the string searched with @match_info
1002  *
1003  * Since: 2.14
1004  */
1005 const gchar *
1006 g_match_info_get_string (const GMatchInfo *match_info)
1007 {
1008   g_return_val_if_fail (match_info != NULL, NULL);
1009   return match_info->string;
1010 }
1011
1012 /**
1013  * g_match_info_ref:
1014  * @match_info: a #GMatchInfo
1015  *
1016  * Increases reference count of @match_info by 1.
1017  *
1018  * Returns: @match_info
1019  *
1020  * Since: 2.30
1021  */
1022 GMatchInfo       *
1023 g_match_info_ref (GMatchInfo *match_info)
1024 {
1025   g_return_val_if_fail (match_info != NULL, NULL);
1026   g_atomic_int_inc (&match_info->ref_count);
1027   return match_info;
1028 }
1029
1030 /**
1031  * g_match_info_unref:
1032  * @match_info: a #GMatchInfo
1033  *
1034  * Decreases reference count of @match_info by 1. When reference count drops
1035  * to zero, it frees all the memory associated with the match_info structure.
1036  *
1037  * Since: 2.30
1038  */
1039 void
1040 g_match_info_unref (GMatchInfo *match_info)
1041 {
1042   if (g_atomic_int_dec_and_test (&match_info->ref_count))
1043     {
1044       g_regex_unref (match_info->regex);
1045       if (match_info->match_context)
1046         pcre2_match_context_free (match_info->match_context);
1047       if (match_info->jit_stack)
1048         pcre2_jit_stack_free (match_info->jit_stack);
1049       if (match_info->match_data)
1050         pcre2_match_data_free (match_info->match_data);
1051       g_free (match_info->offsets);
1052       g_free (match_info->workspace);
1053       g_free (match_info);
1054     }
1055 }
1056
1057 /**
1058  * g_match_info_free:
1059  * @match_info: (nullable): a #GMatchInfo, or %NULL
1060  *
1061  * If @match_info is not %NULL, calls g_match_info_unref(); otherwise does
1062  * nothing.
1063  *
1064  * Since: 2.14
1065  */
1066 void
1067 g_match_info_free (GMatchInfo *match_info)
1068 {
1069   if (match_info == NULL)
1070     return;
1071
1072   g_match_info_unref (match_info);
1073 }
1074
1075 /**
1076  * g_match_info_next:
1077  * @match_info: a #GMatchInfo structure
1078  * @error: location to store the error occurring, or %NULL to ignore errors
1079  *
1080  * Scans for the next match using the same parameters of the previous
1081  * call to g_regex_match_full() or g_regex_match() that returned
1082  * @match_info.
1083  *
1084  * The match is done on the string passed to the match function, so you
1085  * cannot free it before calling this function.
1086  *
1087  * Returns: %TRUE is the string matched, %FALSE otherwise
1088  *
1089  * Since: 2.14
1090  */
1091 gboolean
1092 g_match_info_next (GMatchInfo  *match_info,
1093                    GError     **error)
1094 {
1095   JITStatus jit_status;
1096   gint prev_match_start;
1097   gint prev_match_end;
1098   uint32_t opts;
1099
1100   g_return_val_if_fail (match_info != NULL, FALSE);
1101   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1102   g_return_val_if_fail (match_info->pos >= 0, FALSE);
1103
1104   prev_match_start = match_info->offsets[0];
1105   prev_match_end = match_info->offsets[1];
1106
1107   if (match_info->pos > match_info->string_len)
1108     {
1109       /* we have reached the end of the string */
1110       match_info->pos = -1;
1111       match_info->matches = PCRE2_ERROR_NOMATCH;
1112       return FALSE;
1113     }
1114
1115   opts = match_info->regex->match_opts | match_info->match_opts;
1116
1117   jit_status = enable_jit_with_match_options (match_info, opts);
1118   if (jit_status == JIT_STATUS_ENABLED)
1119     {
1120       match_info->matches = pcre2_jit_match (match_info->regex->pcre_re,
1121                                              (PCRE2_SPTR8) match_info->string,
1122                                              match_info->string_len,
1123                                              match_info->pos,
1124                                              opts,
1125                                              match_info->match_data,
1126                                              match_info->match_context);
1127       /* if the JIT stack limit was reached, fall back to non-JIT matching in
1128        * the next conditional statement */
1129       if (match_info->matches == PCRE2_ERROR_JIT_STACKLIMIT)
1130         {
1131           g_debug ("PCRE2 JIT stack limit reached, falling back to "
1132                    "non-optimized matching.");
1133           opts |= PCRE2_NO_JIT;
1134           jit_status = JIT_STATUS_DISABLED;
1135         }
1136     }
1137
1138   if (jit_status != JIT_STATUS_ENABLED)
1139     {
1140       match_info->matches = pcre2_match (match_info->regex->pcre_re,
1141                                          (PCRE2_SPTR8) match_info->string,
1142                                          match_info->string_len,
1143                                          match_info->pos,
1144                                          opts,
1145                                          match_info->match_data,
1146                                          match_info->match_context);
1147     }
1148
1149   if (IS_PCRE2_ERROR (match_info->matches))
1150     {
1151       gchar *error_msg = get_match_error_message (match_info->matches);
1152
1153       g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
1154                    _("Error while matching regular expression %s: %s"),
1155                    match_info->regex->pattern, error_msg);
1156       g_clear_pointer (&error_msg, g_free);
1157       return FALSE;
1158     }
1159   else if (match_info->matches == 0)
1160     {
1161       /* info->offsets is too small. */
1162       match_info->n_offsets *= 2;
1163       match_info->offsets = g_realloc_n (match_info->offsets,
1164                                          match_info->n_offsets,
1165                                          sizeof (gint));
1166
1167       pcre2_match_data_free (match_info->match_data);
1168       match_info->match_data = pcre2_match_data_create (match_info->n_offsets, NULL);
1169
1170       return g_match_info_next (match_info, error);
1171     }
1172   else if (match_info->matches == PCRE2_ERROR_NOMATCH)
1173     {
1174       /* We're done with this match info */
1175       match_info->pos = -1;
1176       return FALSE;
1177     }
1178   else
1179     if (!recalc_match_offsets (match_info, error))
1180       return FALSE;
1181
1182   /* avoid infinite loops if the pattern is an empty string or something
1183    * equivalent */
1184   if (match_info->pos == match_info->offsets[1])
1185     {
1186       if (match_info->pos > match_info->string_len)
1187         {
1188           /* we have reached the end of the string */
1189           match_info->pos = -1;
1190           match_info->matches = PCRE2_ERROR_NOMATCH;
1191           return FALSE;
1192         }
1193
1194       match_info->pos = NEXT_CHAR (match_info->regex,
1195                                    &match_info->string[match_info->pos]) -
1196                                    match_info->string;
1197     }
1198   else
1199     {
1200       match_info->pos = match_info->offsets[1];
1201     }
1202
1203   g_assert (match_info->matches < 0 ||
1204             (uint32_t) match_info->matches <= match_info->n_subpatterns + 1);
1205
1206   /* it's possible to get two identical matches when we are matching
1207    * empty strings, for instance if the pattern is "(?=[A-Z0-9])" and
1208    * the string is "RegExTest" we have:
1209    *  - search at position 0: match from 0 to 0
1210    *  - search at position 1: match from 3 to 3
1211    *  - search at position 3: match from 3 to 3 (duplicate)
1212    *  - search at position 4: match from 5 to 5
1213    *  - search at position 5: match from 5 to 5 (duplicate)
1214    *  - search at position 6: no match -> stop
1215    * so we have to ignore the duplicates.
1216    * see bug #515944: http://bugzilla.gnome.org/show_bug.cgi?id=515944 */
1217   if (match_info->matches >= 0 &&
1218       prev_match_start == match_info->offsets[0] &&
1219       prev_match_end == match_info->offsets[1])
1220     {
1221       /* ignore this match and search the next one */
1222       return g_match_info_next (match_info, error);
1223     }
1224
1225   return match_info->matches >= 0;
1226 }
1227
1228 /**
1229  * g_match_info_matches:
1230  * @match_info: a #GMatchInfo structure
1231  *
1232  * Returns whether the previous match operation succeeded.
1233  *
1234  * Returns: %TRUE if the previous match operation succeeded,
1235  *   %FALSE otherwise
1236  *
1237  * Since: 2.14
1238  */
1239 gboolean
1240 g_match_info_matches (const GMatchInfo *match_info)
1241 {
1242   g_return_val_if_fail (match_info != NULL, FALSE);
1243
1244   return match_info->matches >= 0;
1245 }
1246
1247 /**
1248  * g_match_info_get_match_count:
1249  * @match_info: a #GMatchInfo structure
1250  *
1251  * Retrieves the number of matched substrings (including substring 0,
1252  * that is the whole matched text), so 1 is returned if the pattern
1253  * has no substrings in it and 0 is returned if the match failed.
1254  *
1255  * If the last match was obtained using the DFA algorithm, that is
1256  * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
1257  * count is not that of the number of capturing parentheses but that of
1258  * the number of matched substrings.
1259  *
1260  * Returns: Number of matched substrings, or -1 if an error occurred
1261  *
1262  * Since: 2.14
1263  */
1264 gint
1265 g_match_info_get_match_count (const GMatchInfo *match_info)
1266 {
1267   g_return_val_if_fail (match_info, -1);
1268
1269   if (match_info->matches == PCRE2_ERROR_NOMATCH)
1270     /* no match */
1271     return 0;
1272   else if (match_info->matches < PCRE2_ERROR_NOMATCH)
1273     /* error */
1274     return -1;
1275   else
1276     /* match */
1277     return match_info->matches;
1278 }
1279
1280 /**
1281  * g_match_info_is_partial_match:
1282  * @match_info: a #GMatchInfo structure
1283  *
1284  * Usually if the string passed to g_regex_match*() matches as far as
1285  * it goes, but is too short to match the entire pattern, %FALSE is
1286  * returned. There are circumstances where it might be helpful to
1287  * distinguish this case from other cases in which there is no match.
1288  *
1289  * Consider, for example, an application where a human is required to
1290  * type in data for a field with specific formatting requirements. An
1291  * example might be a date in the form ddmmmyy, defined by the pattern
1292  * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
1293  * If the application sees the user’s keystrokes one by one, and can
1294  * check that what has been typed so far is potentially valid, it is
1295  * able to raise an error as soon as a mistake is made.
1296  *
1297  * GRegex supports the concept of partial matching by means of the
1298  * %G_REGEX_MATCH_PARTIAL_SOFT and %G_REGEX_MATCH_PARTIAL_HARD flags.
1299  * When they are used, the return code for
1300  * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
1301  * for a complete match, %FALSE otherwise. But, when these functions
1302  * return %FALSE, you can check if the match was partial calling
1303  * g_match_info_is_partial_match().
1304  *
1305  * The difference between %G_REGEX_MATCH_PARTIAL_SOFT and
1306  * %G_REGEX_MATCH_PARTIAL_HARD is that when a partial match is encountered
1307  * with %G_REGEX_MATCH_PARTIAL_SOFT, matching continues to search for a
1308  * possible complete match, while with %G_REGEX_MATCH_PARTIAL_HARD matching
1309  * stops at the partial match.
1310  * When both %G_REGEX_MATCH_PARTIAL_SOFT and %G_REGEX_MATCH_PARTIAL_HARD
1311  * are set, the latter takes precedence.
1312  *
1313  * There were formerly some restrictions on the pattern for partial matching.
1314  * The restrictions no longer apply.
1315  *
1316  * See pcrepartial(3) for more information on partial matching.
1317  *
1318  * Returns: %TRUE if the match was partial, %FALSE otherwise
1319  *
1320  * Since: 2.14
1321  */
1322 gboolean
1323 g_match_info_is_partial_match (const GMatchInfo *match_info)
1324 {
1325   g_return_val_if_fail (match_info != NULL, FALSE);
1326
1327   return match_info->matches == PCRE2_ERROR_PARTIAL;
1328 }
1329
1330 /**
1331  * g_match_info_expand_references:
1332  * @match_info: (nullable): a #GMatchInfo or %NULL
1333  * @string_to_expand: the string to expand
1334  * @error: location to store the error occurring, or %NULL to ignore errors
1335  *
1336  * Returns a new string containing the text in @string_to_expand with
1337  * references and escape sequences expanded. References refer to the last
1338  * match done with @string against @regex and have the same syntax used by
1339  * g_regex_replace().
1340  *
1341  * The @string_to_expand must be UTF-8 encoded even if %G_REGEX_RAW was
1342  * passed to g_regex_new().
1343  *
1344  * The backreferences are extracted from the string passed to the match
1345  * function, so you cannot call this function after freeing the string.
1346  *
1347  * @match_info may be %NULL in which case @string_to_expand must not
1348  * contain references. For instance "foo\n" does not refer to an actual
1349  * pattern and '\n' merely will be replaced with \n character,
1350  * while to expand "\0" (whole match) one needs the result of a match.
1351  * Use g_regex_check_replacement() to find out whether @string_to_expand
1352  * contains references.
1353  *
1354  * Returns: (nullable): the expanded string, or %NULL if an error occurred
1355  *
1356  * Since: 2.14
1357  */
1358 gchar *
1359 g_match_info_expand_references (const GMatchInfo  *match_info,
1360                                 const gchar       *string_to_expand,
1361                                 GError           **error)
1362 {
1363   GString *result;
1364   GList *list;
1365   GError *tmp_error = NULL;
1366
1367   g_return_val_if_fail (string_to_expand != NULL, NULL);
1368   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1369
1370   list = split_replacement (string_to_expand, &tmp_error);
1371   if (tmp_error != NULL)
1372     {
1373       g_propagate_error (error, tmp_error);
1374       return NULL;
1375     }
1376
1377   if (!match_info && interpolation_list_needs_match (list))
1378     {
1379       g_critical ("String '%s' contains references to the match, can't "
1380                   "expand references without GMatchInfo object",
1381                   string_to_expand);
1382       return NULL;
1383     }
1384
1385   result = g_string_sized_new (strlen (string_to_expand));
1386   interpolate_replacement (match_info, result, list);
1387
1388   g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
1389
1390   return g_string_free (result, FALSE);
1391 }
1392
1393 /**
1394  * g_match_info_fetch:
1395  * @match_info: #GMatchInfo structure
1396  * @match_num: number of the sub expression
1397  *
1398  * Retrieves the text matching the @match_num'th capturing
1399  * parentheses. 0 is the full text of the match, 1 is the first paren
1400  * set, 2 the second, and so on.
1401  *
1402  * If @match_num is a valid sub pattern but it didn't match anything
1403  * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
1404  * string is returned.
1405  *
1406  * If the match was obtained using the DFA algorithm, that is using
1407  * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1408  * string is not that of a set of parentheses but that of a matched
1409  * substring. Substrings are matched in reverse order of length, so
1410  * 0 is the longest match.
1411  *
1412  * The string is fetched from the string passed to the match function,
1413  * so you cannot call this function after freeing the string.
1414  *
1415  * Returns: (nullable): The matched substring, or %NULL if an error
1416  *     occurred. You have to free the string yourself
1417  *
1418  * Since: 2.14
1419  */
1420 gchar *
1421 g_match_info_fetch (const GMatchInfo *match_info,
1422                     gint              match_num)
1423 {
1424   gchar *match = NULL;
1425   gint start, end;
1426
1427   g_return_val_if_fail (match_info != NULL, NULL);
1428   g_return_val_if_fail (match_num >= 0, NULL);
1429
1430   /* match_num does not exist or it didn't matched, i.e. matching "b"
1431    * against "(a)?b" then group 0 is empty. */
1432   if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
1433     match = NULL;
1434   else if (start == -1)
1435     match = g_strdup ("");
1436   else
1437     match = g_strndup (&match_info->string[start], end - start);
1438
1439   return match;
1440 }
1441
1442 /**
1443  * g_match_info_fetch_pos:
1444  * @match_info: #GMatchInfo structure
1445  * @match_num: number of the sub expression
1446  * @start_pos: (out) (optional): pointer to location where to store
1447  *     the start position, or %NULL
1448  * @end_pos: (out) (optional): pointer to location where to store
1449  *     the end position, or %NULL
1450  *
1451  * Retrieves the position in bytes of the @match_num'th capturing
1452  * parentheses. 0 is the full text of the match, 1 is the first
1453  * paren set, 2 the second, and so on.
1454  *
1455  * If @match_num is a valid sub pattern but it didn't match anything
1456  * (e.g. sub pattern 1, matching "b" against "(a)?b") then @start_pos
1457  * and @end_pos are set to -1 and %TRUE is returned.
1458  *
1459  * If the match was obtained using the DFA algorithm, that is using
1460  * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1461  * position is not that of a set of parentheses but that of a matched
1462  * substring. Substrings are matched in reverse order of length, so
1463  * 0 is the longest match.
1464  *
1465  * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
1466  *   the position cannot be fetched, @start_pos and @end_pos are left
1467  *   unchanged
1468  *
1469  * Since: 2.14
1470  */
1471 gboolean
1472 g_match_info_fetch_pos (const GMatchInfo *match_info,
1473                         gint              match_num,
1474                         gint             *start_pos,
1475                         gint             *end_pos)
1476 {
1477   g_return_val_if_fail (match_info != NULL, FALSE);
1478   g_return_val_if_fail (match_num >= 0, FALSE);
1479
1480   /* check whether there was an error */
1481   if (match_info->matches < 0)
1482     return FALSE;
1483
1484   /* make sure the sub expression number they're requesting is less than
1485    * the total number of sub expressions in the regex. When matching all
1486    * (g_regex_match_all()), also compare against the number of matches */
1487   if ((uint32_t) match_num >= MAX (match_info->n_subpatterns + 1, (uint32_t) match_info->matches))
1488     return FALSE;
1489
1490   if (start_pos != NULL)
1491     *start_pos = (match_num < match_info->matches) ? match_info->offsets[2 * match_num] : -1;
1492
1493   if (end_pos != NULL)
1494     *end_pos = (match_num < match_info->matches) ? match_info->offsets[2 * match_num + 1] : -1;
1495
1496   return TRUE;
1497 }
1498
1499 /*
1500  * Returns number of first matched subpattern with name @name.
1501  * There may be more than one in case when DUPNAMES is used,
1502  * and not all subpatterns with that name match;
1503  * pcre2_substring_number_from_name() does not work in that case.
1504  */
1505 static gint
1506 get_matched_substring_number (const GMatchInfo *match_info,
1507                               const gchar      *name)
1508 {
1509   gint entrysize;
1510   PCRE2_SPTR first, last;
1511   guchar *entry;
1512
1513   if (!(match_info->regex->compile_opts & PCRE2_DUPNAMES))
1514     return pcre2_substring_number_from_name (match_info->regex->pcre_re, (PCRE2_SPTR8) name);
1515
1516   /* This code is analogous to code from pcre2_substring.c:
1517    * pcre2_substring_get_byname() */
1518   entrysize = pcre2_substring_nametable_scan (match_info->regex->pcre_re,
1519                                               (PCRE2_SPTR8) name,
1520                                               &first,
1521                                               &last);
1522
1523   if (entrysize <= 0)
1524     return entrysize;
1525
1526   for (entry = (guchar*) first; entry <= (guchar*) last; entry += entrysize)
1527     {
1528       guint n = (entry[0] << 8) + entry[1];
1529       if (n * 2 < match_info->n_offsets && match_info->offsets[n * 2] >= 0)
1530         return n;
1531     }
1532
1533   return (first[0] << 8) + first[1];
1534 }
1535
1536 /**
1537  * g_match_info_fetch_named:
1538  * @match_info: #GMatchInfo structure
1539  * @name: name of the subexpression
1540  *
1541  * Retrieves the text matching the capturing parentheses named @name.
1542  *
1543  * If @name is a valid sub pattern name but it didn't match anything
1544  * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
1545  * then an empty string is returned.
1546  *
1547  * The string is fetched from the string passed to the match function,
1548  * so you cannot call this function after freeing the string.
1549  *
1550  * Returns: (nullable): The matched substring, or %NULL if an error
1551  *     occurred. You have to free the string yourself
1552  *
1553  * Since: 2.14
1554  */
1555 gchar *
1556 g_match_info_fetch_named (const GMatchInfo *match_info,
1557                           const gchar      *name)
1558 {
1559   gint num;
1560
1561   g_return_val_if_fail (match_info != NULL, NULL);
1562   g_return_val_if_fail (name != NULL, NULL);
1563
1564   num = get_matched_substring_number (match_info, name);
1565   if (num < 0)
1566     return NULL;
1567   else
1568     return g_match_info_fetch (match_info, num);
1569 }
1570
1571 /**
1572  * g_match_info_fetch_named_pos:
1573  * @match_info: #GMatchInfo structure
1574  * @name: name of the subexpression
1575  * @start_pos: (out) (optional): pointer to location where to store
1576  *     the start position, or %NULL
1577  * @end_pos: (out) (optional): pointer to location where to store
1578  *     the end position, or %NULL
1579  *
1580  * Retrieves the position in bytes of the capturing parentheses named @name.
1581  *
1582  * If @name is a valid sub pattern name but it didn't match anything
1583  * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
1584  * then @start_pos and @end_pos are set to -1 and %TRUE is returned.
1585  *
1586  * Returns: %TRUE if the position was fetched, %FALSE otherwise.
1587  *     If the position cannot be fetched, @start_pos and @end_pos
1588  *     are left unchanged.
1589  *
1590  * Since: 2.14
1591  */
1592 gboolean
1593 g_match_info_fetch_named_pos (const GMatchInfo *match_info,
1594                               const gchar      *name,
1595                               gint             *start_pos,
1596                               gint             *end_pos)
1597 {
1598   gint num;
1599
1600   g_return_val_if_fail (match_info != NULL, FALSE);
1601   g_return_val_if_fail (name != NULL, FALSE);
1602
1603   num = get_matched_substring_number (match_info, name);
1604   if (num < 0)
1605     return FALSE;
1606
1607   return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
1608 }
1609
1610 /**
1611  * g_match_info_fetch_all:
1612  * @match_info: a #GMatchInfo structure
1613  *
1614  * Bundles up pointers to each of the matching substrings from a match
1615  * and stores them in an array of gchar pointers. The first element in
1616  * the returned array is the match number 0, i.e. the entire matched
1617  * text.
1618  *
1619  * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
1620  * "b" against "(a)?b") then an empty string is inserted.
1621  *
1622  * If the last match was obtained using the DFA algorithm, that is using
1623  * g_regex_match_all() or g_regex_match_all_full(), the retrieved
1624  * strings are not that matched by sets of parentheses but that of the
1625  * matched substring. Substrings are matched in reverse order of length,
1626  * so the first one is the longest match.
1627  *
1628  * The strings are fetched from the string passed to the match function,
1629  * so you cannot call this function after freeing the string.
1630  *
1631  * Returns: (transfer full): a %NULL-terminated array of gchar *
1632  *     pointers.  It must be freed using g_strfreev(). If the previous
1633  *     match failed %NULL is returned
1634  *
1635  * Since: 2.14
1636  */
1637 gchar **
1638 g_match_info_fetch_all (const GMatchInfo *match_info)
1639 {
1640   gchar **result;
1641   gint i;
1642
1643   g_return_val_if_fail (match_info != NULL, NULL);
1644
1645   if (match_info->matches < 0)
1646     return NULL;
1647
1648   result = g_new (gchar *, match_info->matches + 1);
1649   for (i = 0; i < match_info->matches; i++)
1650     result[i] = g_match_info_fetch (match_info, i);
1651   result[i] = NULL;
1652
1653   return result;
1654 }
1655
1656
1657 /* GRegex */
1658
1659 G_DEFINE_QUARK (g-regex-error-quark, g_regex_error)
1660
1661 /**
1662  * g_regex_ref:
1663  * @regex: a #GRegex
1664  *
1665  * Increases reference count of @regex by 1.
1666  *
1667  * Returns: @regex
1668  *
1669  * Since: 2.14
1670  */
1671 GRegex *
1672 g_regex_ref (GRegex *regex)
1673 {
1674   g_return_val_if_fail (regex != NULL, NULL);
1675   g_atomic_int_inc (&regex->ref_count);
1676   return regex;
1677 }
1678
1679 /**
1680  * g_regex_unref:
1681  * @regex: a #GRegex
1682  *
1683  * Decreases reference count of @regex by 1. When reference count drops
1684  * to zero, it frees all the memory associated with the regex structure.
1685  *
1686  * Since: 2.14
1687  */
1688 void
1689 g_regex_unref (GRegex *regex)
1690 {
1691   g_return_if_fail (regex != NULL);
1692
1693   if (g_atomic_int_dec_and_test (&regex->ref_count))
1694     {
1695       g_free (regex->pattern);
1696       if (regex->pcre_re != NULL)
1697         pcre2_code_free (regex->pcre_re);
1698       g_free (regex);
1699     }
1700 }
1701
1702 static pcre2_code * regex_compile (const gchar  *pattern,
1703                                    uint32_t      compile_options,
1704                                    uint32_t      newline_options,
1705                                    uint32_t      bsr_options,
1706                                    GError      **error);
1707
1708 static uint32_t get_pcre2_inline_compile_options (pcre2_code *re,
1709                                                   uint32_t    compile_options);
1710
1711 /**
1712  * g_regex_new:
1713  * @pattern: the regular expression
1714  * @compile_options: compile options for the regular expression, or 0
1715  * @match_options: match options for the regular expression, or 0
1716  * @error: return location for a #GError
1717  *
1718  * Compiles the regular expression to an internal form, and does
1719  * the initial setup of the #GRegex structure.
1720  *
1721  * Returns: (nullable): a #GRegex structure or %NULL if an error occurred. Call
1722  *   g_regex_unref() when you are done with it
1723  *
1724  * Since: 2.14
1725  */
1726 GRegex *
1727 g_regex_new (const gchar         *pattern,
1728              GRegexCompileFlags   compile_options,
1729              GRegexMatchFlags     match_options,
1730              GError             **error)
1731 {
1732   GRegex *regex;
1733   pcre2_code *re;
1734   static gsize initialised = 0;
1735   uint32_t pcre_compile_options;
1736   uint32_t pcre_match_options;
1737   uint32_t newline_options;
1738   uint32_t bsr_options;
1739
1740   g_return_val_if_fail (pattern != NULL, NULL);
1741   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1742 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1743   g_return_val_if_fail ((compile_options & ~(G_REGEX_COMPILE_MASK |
1744                                              G_REGEX_JAVASCRIPT_COMPAT)) == 0, NULL);
1745 G_GNUC_END_IGNORE_DEPRECATIONS
1746   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1747
1748   if (g_once_init_enter (&initialised))
1749     {
1750       int supports_utf8;
1751
1752       pcre2_config (PCRE2_CONFIG_UNICODE, &supports_utf8);
1753       if (!supports_utf8)
1754         g_critical (_("PCRE library is compiled without UTF8 support"));
1755
1756       g_once_init_leave (&initialised, supports_utf8 ? 1 : 2);
1757     }
1758
1759   if (G_UNLIKELY (initialised != 1))
1760     {
1761       g_set_error_literal (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, 
1762                            _("PCRE library is compiled with incompatible options"));
1763       return NULL;
1764     }
1765
1766   pcre_compile_options = get_pcre2_compile_options (compile_options);
1767   pcre_match_options = get_pcre2_match_options (match_options, compile_options);
1768
1769   newline_options = get_pcre2_newline_match_options (match_options);
1770   if (newline_options == 0)
1771     newline_options = get_pcre2_newline_compile_options (compile_options);
1772
1773   if (newline_options == 0)
1774     {
1775       g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS,
1776                    "Invalid newline flags");
1777       return NULL;
1778     }
1779
1780   bsr_options = get_pcre2_bsr_match_options (match_options);
1781   if (!bsr_options)
1782     bsr_options = get_pcre2_bsr_compile_options (compile_options);
1783
1784   re = regex_compile (pattern, pcre_compile_options,
1785                       newline_options, bsr_options, error);
1786   if (re == NULL)
1787     return NULL;
1788
1789   pcre_compile_options |=
1790     get_pcre2_inline_compile_options (re, pcre_compile_options);
1791
1792   regex = g_new0 (GRegex, 1);
1793   regex->ref_count = 1;
1794   regex->pattern = g_strdup (pattern);
1795   regex->pcre_re = re;
1796   regex->compile_opts = pcre_compile_options;
1797   regex->orig_compile_opts = compile_options;
1798   regex->match_opts = pcre_match_options;
1799   regex->orig_match_opts = match_options;
1800
1801   return regex;
1802 }
1803
1804 static pcre2_code *
1805 regex_compile (const gchar  *pattern,
1806                uint32_t      compile_options,
1807                uint32_t      newline_options,
1808                uint32_t      bsr_options,
1809                GError      **error)
1810 {
1811   pcre2_code *re;
1812   pcre2_compile_context *context;
1813   const gchar *errmsg;
1814   PCRE2_SIZE erroffset;
1815   gint errcode;
1816
1817   context = pcre2_compile_context_create (NULL);
1818
1819   /* set newline options */
1820   if (pcre2_set_newline (context, newline_options) != 0)
1821     {
1822       g_set_error (error, G_REGEX_ERROR,
1823                    G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS,
1824                    "Invalid newline flags");
1825       pcre2_compile_context_free (context);
1826       return NULL;
1827     }
1828
1829   /* set bsr options */
1830   if (pcre2_set_bsr (context, bsr_options) != 0)
1831     {
1832       g_set_error (error, G_REGEX_ERROR,
1833                    G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS,
1834                    "Invalid BSR flags");
1835       pcre2_compile_context_free (context);
1836       return NULL;
1837     }
1838
1839   /* In case UTF-8 mode is used, also set PCRE2_NO_UTF_CHECK */
1840   if (compile_options & PCRE2_UTF)
1841     compile_options |= PCRE2_NO_UTF_CHECK;
1842
1843   compile_options |= PCRE2_UCP;
1844
1845   /* compile the pattern */
1846   re = pcre2_compile ((PCRE2_SPTR8) pattern,
1847                       PCRE2_ZERO_TERMINATED,
1848                       compile_options,
1849                       &errcode,
1850                       &erroffset,
1851                       context);
1852   pcre2_compile_context_free (context);
1853
1854   /* if the compilation failed, set the error member and return
1855    * immediately */
1856   if (re == NULL)
1857     {
1858       GError *tmp_error;
1859       gchar *offset_str;
1860       gchar *pcre2_errmsg = NULL;
1861       int original_errcode;
1862
1863       /* Translate the PCRE error code to GRegexError and use a translated
1864        * error message if possible */
1865       original_errcode = errcode;
1866       translate_compile_error (&errcode, &errmsg);
1867
1868       if (!errmsg)
1869         {
1870           errmsg = _("unknown error");
1871           pcre2_errmsg = get_pcre2_error_string (original_errcode);
1872         }
1873
1874       /* PCRE uses byte offsets but we want to show character offsets */
1875       erroffset = g_utf8_pointer_to_offset (pattern, &pattern[erroffset]);
1876
1877       offset_str = g_strdup_printf ("%" G_GSIZE_FORMAT, erroffset);
1878       tmp_error = g_error_new (G_REGEX_ERROR, errcode,
1879                                _("Error while compiling regular expression ‘%s’ "
1880                                  "at char %s: %s"),
1881                                pattern, offset_str,
1882                                pcre2_errmsg ? pcre2_errmsg : errmsg);
1883       g_propagate_error (error, tmp_error);
1884       g_free (offset_str);
1885       g_clear_pointer (&pcre2_errmsg, g_free);
1886
1887       return NULL;
1888     }
1889
1890   return re;
1891 }
1892
1893 static uint32_t
1894 get_pcre2_inline_compile_options (pcre2_code *re,
1895                                   uint32_t    compile_options)
1896 {
1897   uint32_t pcre_compile_options;
1898   uint32_t nonpcre_compile_options;
1899
1900   /* For options set at the beginning of the pattern, pcre puts them into
1901    * compile options, e.g. "(?i)foo" will make the pcre structure store
1902    * PCRE2_CASELESS even though it wasn't explicitly given for compilation. */
1903   nonpcre_compile_options = compile_options & G_REGEX_COMPILE_NONPCRE_MASK;
1904   pcre2_pattern_info (re, PCRE2_INFO_ALLOPTIONS, &pcre_compile_options);
1905   compile_options = pcre_compile_options & G_REGEX_PCRE2_COMPILE_MASK;
1906   compile_options |= nonpcre_compile_options;
1907
1908   if (!(compile_options & PCRE2_DUPNAMES))
1909     {
1910       uint32_t jchanged = 0;
1911       pcre2_pattern_info (re, PCRE2_INFO_JCHANGED, &jchanged);
1912       if (jchanged)
1913         compile_options |= PCRE2_DUPNAMES;
1914     }
1915
1916   return compile_options;
1917 }
1918
1919 /**
1920  * g_regex_get_pattern:
1921  * @regex: a #GRegex structure
1922  *
1923  * Gets the pattern string associated with @regex, i.e. a copy of
1924  * the string passed to g_regex_new().
1925  *
1926  * Returns: the pattern of @regex
1927  *
1928  * Since: 2.14
1929  */
1930 const gchar *
1931 g_regex_get_pattern (const GRegex *regex)
1932 {
1933   g_return_val_if_fail (regex != NULL, NULL);
1934
1935   return regex->pattern;
1936 }
1937
1938 /**
1939  * g_regex_get_max_backref:
1940  * @regex: a #GRegex
1941  *
1942  * Returns the number of the highest back reference
1943  * in the pattern, or 0 if the pattern does not contain
1944  * back references.
1945  *
1946  * Returns: the number of the highest back reference
1947  *
1948  * Since: 2.14
1949  */
1950 gint
1951 g_regex_get_max_backref (const GRegex *regex)
1952 {
1953   uint32_t value;
1954
1955   pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_BACKREFMAX, &value);
1956
1957   return value;
1958 }
1959
1960 /**
1961  * g_regex_get_capture_count:
1962  * @regex: a #GRegex
1963  *
1964  * Returns the number of capturing subpatterns in the pattern.
1965  *
1966  * Returns: the number of capturing subpatterns
1967  *
1968  * Since: 2.14
1969  */
1970 gint
1971 g_regex_get_capture_count (const GRegex *regex)
1972 {
1973   uint32_t value;
1974
1975   pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_CAPTURECOUNT, &value);
1976
1977   return value;
1978 }
1979
1980 /**
1981  * g_regex_get_has_cr_or_lf:
1982  * @regex: a #GRegex structure
1983  *
1984  * Checks whether the pattern contains explicit CR or LF references.
1985  *
1986  * Returns: %TRUE if the pattern contains explicit CR or LF references
1987  *
1988  * Since: 2.34
1989  */
1990 gboolean
1991 g_regex_get_has_cr_or_lf (const GRegex *regex)
1992 {
1993   uint32_t value;
1994
1995   pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_HASCRORLF, &value);
1996
1997   return !!value;
1998 }
1999
2000 /**
2001  * g_regex_get_max_lookbehind:
2002  * @regex: a #GRegex structure
2003  *
2004  * Gets the number of characters in the longest lookbehind assertion in the
2005  * pattern. This information is useful when doing multi-segment matching using
2006  * the partial matching facilities.
2007  *
2008  * Returns: the number of characters in the longest lookbehind assertion.
2009  *
2010  * Since: 2.38
2011  */
2012 gint
2013 g_regex_get_max_lookbehind (const GRegex *regex)
2014 {
2015   uint32_t max_lookbehind;
2016
2017   pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_MAXLOOKBEHIND,
2018                       &max_lookbehind);
2019
2020   return max_lookbehind;
2021 }
2022
2023 /**
2024  * g_regex_get_compile_flags:
2025  * @regex: a #GRegex
2026  *
2027  * Returns the compile options that @regex was created with.
2028  *
2029  * Depending on the version of PCRE that is used, this may or may not
2030  * include flags set by option expressions such as `(?i)` found at the
2031  * top-level within the compiled pattern.
2032  *
2033  * Returns: flags from #GRegexCompileFlags
2034  *
2035  * Since: 2.26
2036  */
2037 GRegexCompileFlags
2038 g_regex_get_compile_flags (const GRegex *regex)
2039 {
2040   GRegexCompileFlags extra_flags;
2041   uint32_t info_value;
2042
2043   g_return_val_if_fail (regex != NULL, 0);
2044
2045   /* Preserve original G_REGEX_OPTIMIZE */
2046   extra_flags = (regex->orig_compile_opts & G_REGEX_OPTIMIZE);
2047
2048   /* Also include the newline options */
2049   pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_NEWLINE, &info_value);
2050   switch (info_value)
2051     {
2052     case PCRE2_NEWLINE_ANYCRLF:
2053       extra_flags |= G_REGEX_NEWLINE_ANYCRLF;
2054       break;
2055     case PCRE2_NEWLINE_CRLF:
2056       extra_flags |= G_REGEX_NEWLINE_CRLF;
2057       break;
2058     case PCRE2_NEWLINE_LF:
2059       extra_flags |= G_REGEX_NEWLINE_LF;
2060       break;
2061     case PCRE2_NEWLINE_CR:
2062       extra_flags |= G_REGEX_NEWLINE_CR;
2063       break;
2064     default:
2065       break;
2066     }
2067
2068   /* Also include the bsr options */
2069   pcre2_pattern_info (regex->pcre_re, PCRE2_INFO_BSR, &info_value);
2070   switch (info_value)
2071     {
2072     case PCRE2_BSR_ANYCRLF:
2073       extra_flags |= G_REGEX_BSR_ANYCRLF;
2074       break;
2075     default:
2076       break;
2077     }
2078
2079   return g_regex_compile_flags_from_pcre2 (regex->compile_opts) | extra_flags;
2080 }
2081
2082 /**
2083  * g_regex_get_match_flags:
2084  * @regex: a #GRegex
2085  *
2086  * Returns the match options that @regex was created with.
2087  *
2088  * Returns: flags from #GRegexMatchFlags
2089  *
2090  * Since: 2.26
2091  */
2092 GRegexMatchFlags
2093 g_regex_get_match_flags (const GRegex *regex)
2094 {
2095   uint32_t flags;
2096
2097   g_return_val_if_fail (regex != NULL, 0);
2098
2099   flags = g_regex_match_flags_from_pcre2 (regex->match_opts);
2100   flags |= (regex->orig_match_opts & G_REGEX_MATCH_NEWLINE_MASK);
2101   flags |= (regex->orig_match_opts & (G_REGEX_MATCH_BSR_ANY | G_REGEX_MATCH_BSR_ANYCRLF));
2102
2103   return flags;
2104 }
2105
2106 /**
2107  * g_regex_match_simple:
2108  * @pattern: the regular expression
2109  * @string: the string to scan for matches
2110  * @compile_options: compile options for the regular expression, or 0
2111  * @match_options: match options, or 0
2112  *
2113  * Scans for a match in @string for @pattern.
2114  *
2115  * This function is equivalent to g_regex_match() but it does not
2116  * require to compile the pattern with g_regex_new(), avoiding some
2117  * lines of code when you need just to do a match without extracting
2118  * substrings, capture counts, and so on.
2119  *
2120  * If this function is to be called on the same @pattern more than
2121  * once, it's more efficient to compile the pattern once with
2122  * g_regex_new() and then use g_regex_match().
2123  *
2124  * Returns: %TRUE if the string matched, %FALSE otherwise
2125  *
2126  * Since: 2.14
2127  */
2128 gboolean
2129 g_regex_match_simple (const gchar        *pattern,
2130                       const gchar        *string,
2131                       GRegexCompileFlags  compile_options,
2132                       GRegexMatchFlags    match_options)
2133 {
2134   GRegex *regex;
2135   gboolean result;
2136
2137   regex = g_regex_new (pattern, compile_options, G_REGEX_MATCH_DEFAULT, NULL);
2138   if (!regex)
2139     return FALSE;
2140   result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
2141   g_regex_unref (regex);
2142   return result;
2143 }
2144
2145 /**
2146  * g_regex_match:
2147  * @regex: a #GRegex structure from g_regex_new()
2148  * @string: the string to scan for matches
2149  * @match_options: match options
2150  * @match_info: (out) (optional): pointer to location where to store
2151  *     the #GMatchInfo, or %NULL if you do not need it
2152  *
2153  * Scans for a match in @string for the pattern in @regex.
2154  * The @match_options are combined with the match options specified
2155  * when the @regex structure was created, letting you have more
2156  * flexibility in reusing #GRegex structures.
2157  *
2158  * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8.
2159  *
2160  * A #GMatchInfo structure, used to get information on the match,
2161  * is stored in @match_info if not %NULL. Note that if @match_info
2162  * is not %NULL then it is created even if the function returns %FALSE,
2163  * i.e. you must free it regardless if regular expression actually matched.
2164  *
2165  * To retrieve all the non-overlapping matches of the pattern in
2166  * string you can use g_match_info_next().
2167  *
2168  * |[<!-- language="C" --> 
2169  * static void
2170  * print_uppercase_words (const gchar *string)
2171  * {
2172  *   // Print all uppercase-only words.
2173  *   GRegex *regex;
2174  *   GMatchInfo *match_info;
2175  *  
2176  *   regex = g_regex_new ("[A-Z]+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
2177  *   g_regex_match (regex, string, 0, &match_info);
2178  *   while (g_match_info_matches (match_info))
2179  *     {
2180  *       gchar *word = g_match_info_fetch (match_info, 0);
2181  *       g_print ("Found: %s\n", word);
2182  *       g_free (word);
2183  *       g_match_info_next (match_info, NULL);
2184  *     }
2185  *   g_match_info_free (match_info);
2186  *   g_regex_unref (regex);
2187  * }
2188  * ]|
2189  *
2190  * @string is not copied and is used in #GMatchInfo internally. If
2191  * you use any #GMatchInfo method (except g_match_info_free()) after
2192  * freeing or modifying @string then the behaviour is undefined.
2193  *
2194  * Returns: %TRUE is the string matched, %FALSE otherwise
2195  *
2196  * Since: 2.14
2197  */
2198 gboolean
2199 g_regex_match (const GRegex      *regex,
2200                const gchar       *string,
2201                GRegexMatchFlags   match_options,
2202                GMatchInfo       **match_info)
2203 {
2204   return g_regex_match_full (regex, string, -1, 0, match_options,
2205                              match_info, NULL);
2206 }
2207
2208 /**
2209  * g_regex_match_full:
2210  * @regex: a #GRegex structure from g_regex_new()
2211  * @string: (array length=string_len): the string to scan for matches
2212  * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
2213  * @start_position: starting index of the string to match, in bytes
2214  * @match_options: match options
2215  * @match_info: (out) (optional): pointer to location where to store
2216  *     the #GMatchInfo, or %NULL if you do not need it
2217  * @error: location to store the error occurring, or %NULL to ignore errors
2218  *
2219  * Scans for a match in @string for the pattern in @regex.
2220  * The @match_options are combined with the match options specified
2221  * when the @regex structure was created, letting you have more
2222  * flexibility in reusing #GRegex structures.
2223  *
2224  * Setting @start_position differs from just passing over a shortened
2225  * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
2226  * that begins with any kind of lookbehind assertion, such as "\b".
2227  *
2228  * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8.
2229  *
2230  * A #GMatchInfo structure, used to get information on the match, is
2231  * stored in @match_info if not %NULL. Note that if @match_info is
2232  * not %NULL then it is created even if the function returns %FALSE,
2233  * i.e. you must free it regardless if regular expression actually
2234  * matched.
2235  *
2236  * @string is not copied and is used in #GMatchInfo internally. If
2237  * you use any #GMatchInfo method (except g_match_info_free()) after
2238  * freeing or modifying @string then the behaviour is undefined.
2239  *
2240  * To retrieve all the non-overlapping matches of the pattern in
2241  * string you can use g_match_info_next().
2242  *
2243  * |[<!-- language="C" --> 
2244  * static void
2245  * print_uppercase_words (const gchar *string)
2246  * {
2247  *   // Print all uppercase-only words.
2248  *   GRegex *regex;
2249  *   GMatchInfo *match_info;
2250  *   GError *error = NULL;
2251  *   
2252  *   regex = g_regex_new ("[A-Z]+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
2253  *   g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
2254  *   while (g_match_info_matches (match_info))
2255  *     {
2256  *       gchar *word = g_match_info_fetch (match_info, 0);
2257  *       g_print ("Found: %s\n", word);
2258  *       g_free (word);
2259  *       g_match_info_next (match_info, &error);
2260  *     }
2261  *   g_match_info_free (match_info);
2262  *   g_regex_unref (regex);
2263  *   if (error != NULL)
2264  *     {
2265  *       g_printerr ("Error while matching: %s\n", error->message);
2266  *       g_error_free (error);
2267  *     }
2268  * }
2269  * ]|
2270  *
2271  * Returns: %TRUE is the string matched, %FALSE otherwise
2272  *
2273  * Since: 2.14
2274  */
2275 gboolean
2276 g_regex_match_full (const GRegex      *regex,
2277                     const gchar       *string,
2278                     gssize             string_len,
2279                     gint               start_position,
2280                     GRegexMatchFlags   match_options,
2281                     GMatchInfo       **match_info,
2282                     GError           **error)
2283 {
2284   GMatchInfo *info;
2285   gboolean match_ok;
2286
2287   g_return_val_if_fail (regex != NULL, FALSE);
2288   g_return_val_if_fail (string != NULL, FALSE);
2289   g_return_val_if_fail (start_position >= 0, FALSE);
2290   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2291   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
2292
2293   info = match_info_new (regex, string, string_len, start_position,
2294                          match_options, FALSE);
2295   match_ok = g_match_info_next (info, error);
2296   if (match_info != NULL)
2297     *match_info = info;
2298   else
2299     g_match_info_free (info);
2300
2301   return match_ok;
2302 }
2303
2304 /**
2305  * g_regex_match_all:
2306  * @regex: a #GRegex structure from g_regex_new()
2307  * @string: the string to scan for matches
2308  * @match_options: match options
2309  * @match_info: (out) (optional): pointer to location where to store
2310  *     the #GMatchInfo, or %NULL if you do not need it
2311  *
2312  * Using the standard algorithm for regular expression matching only
2313  * the longest match in the string is retrieved. This function uses
2314  * a different algorithm so it can retrieve all the possible matches.
2315  * For more documentation see g_regex_match_all_full().
2316  *
2317  * A #GMatchInfo structure, used to get information on the match, is
2318  * stored in @match_info if not %NULL. Note that if @match_info is
2319  * not %NULL then it is created even if the function returns %FALSE,
2320  * i.e. you must free it regardless if regular expression actually
2321  * matched.
2322  *
2323  * @string is not copied and is used in #GMatchInfo internally. If
2324  * you use any #GMatchInfo method (except g_match_info_free()) after
2325  * freeing or modifying @string then the behaviour is undefined.
2326  *
2327  * Returns: %TRUE is the string matched, %FALSE otherwise
2328  *
2329  * Since: 2.14
2330  */
2331 gboolean
2332 g_regex_match_all (const GRegex      *regex,
2333                    const gchar       *string,
2334                    GRegexMatchFlags   match_options,
2335                    GMatchInfo       **match_info)
2336 {
2337   return g_regex_match_all_full (regex, string, -1, 0, match_options,
2338                                  match_info, NULL);
2339 }
2340
2341 /**
2342  * g_regex_match_all_full:
2343  * @regex: a #GRegex structure from g_regex_new()
2344  * @string: (array length=string_len): the string to scan for matches
2345  * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
2346  * @start_position: starting index of the string to match, in bytes
2347  * @match_options: match options
2348  * @match_info: (out) (optional): pointer to location where to store
2349  *     the #GMatchInfo, or %NULL if you do not need it
2350  * @error: location to store the error occurring, or %NULL to ignore errors
2351  *
2352  * Using the standard algorithm for regular expression matching only
2353  * the longest match in the @string is retrieved, it is not possible
2354  * to obtain all the available matches. For instance matching
2355  * "<a> <b> <c>" against the pattern "<.*>"
2356  * you get "<a> <b> <c>".
2357  *
2358  * This function uses a different algorithm (called DFA, i.e. deterministic
2359  * finite automaton), so it can retrieve all the possible matches, all
2360  * starting at the same point in the string. For instance matching
2361  * "<a> <b> <c>" against the pattern "<.*>;"
2362  * you would obtain three matches: "<a> <b> <c>",
2363  * "<a> <b>" and "<a>".
2364  *
2365  * The number of matched strings is retrieved using
2366  * g_match_info_get_match_count(). To obtain the matched strings and
2367  * their position you can use, respectively, g_match_info_fetch() and
2368  * g_match_info_fetch_pos(). Note that the strings are returned in
2369  * reverse order of length; that is, the longest matching string is
2370  * given first.
2371  *
2372  * Note that the DFA algorithm is slower than the standard one and it
2373  * is not able to capture substrings, so backreferences do not work.
2374  *
2375  * Setting @start_position differs from just passing over a shortened
2376  * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
2377  * that begins with any kind of lookbehind assertion, such as "\b".
2378  *
2379  * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8.
2380  *
2381  * A #GMatchInfo structure, used to get information on the match, is
2382  * stored in @match_info if not %NULL. Note that if @match_info is
2383  * not %NULL then it is created even if the function returns %FALSE,
2384  * i.e. you must free it regardless if regular expression actually
2385  * matched.
2386  *
2387  * @string is not copied and is used in #GMatchInfo internally. If
2388  * you use any #GMatchInfo method (except g_match_info_free()) after
2389  * freeing or modifying @string then the behaviour is undefined.
2390  *
2391  * Returns: %TRUE is the string matched, %FALSE otherwise
2392  *
2393  * Since: 2.14
2394  */
2395 gboolean
2396 g_regex_match_all_full (const GRegex      *regex,
2397                         const gchar       *string,
2398                         gssize             string_len,
2399                         gint               start_position,
2400                         GRegexMatchFlags   match_options,
2401                         GMatchInfo       **match_info,
2402                         GError           **error)
2403 {
2404   GMatchInfo *info;
2405   gboolean done;
2406   pcre2_code *pcre_re;
2407   gboolean retval;
2408   uint32_t newline_options;
2409   uint32_t bsr_options;
2410
2411   g_return_val_if_fail (regex != NULL, FALSE);
2412   g_return_val_if_fail (string != NULL, FALSE);
2413   g_return_val_if_fail (start_position >= 0, FALSE);
2414   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2415   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
2416
2417   newline_options = get_pcre2_newline_match_options (match_options);
2418   if (!newline_options)
2419     newline_options = get_pcre2_newline_compile_options (regex->orig_compile_opts);
2420
2421   bsr_options = get_pcre2_bsr_match_options (match_options);
2422   if (!bsr_options)
2423     bsr_options = get_pcre2_bsr_compile_options (regex->orig_compile_opts);
2424
2425   /* For PCRE2 we need to turn off PCRE2_NO_AUTO_POSSESS, which is an
2426    * optimization for normal regex matching, but results in omitting some
2427    * shorter matches here, and an observable behaviour change.
2428    *
2429    * DFA matching is rather niche, and very rarely used according to
2430    * codesearch.debian.net, so don't bother caching the recompiled RE. */
2431   pcre_re = regex_compile (regex->pattern,
2432                            regex->compile_opts | PCRE2_NO_AUTO_POSSESS,
2433                            newline_options, bsr_options, error);
2434   if (pcre_re == NULL)
2435     return FALSE;
2436
2437   info = match_info_new (regex, string, string_len, start_position,
2438                          match_options, TRUE);
2439
2440   done = FALSE;
2441   while (!done)
2442     {
2443       done = TRUE;
2444       info->matches = pcre2_dfa_match (pcre_re,
2445                                        (PCRE2_SPTR8) info->string, info->string_len,
2446                                        info->pos,
2447                                        (regex->match_opts | info->match_opts),
2448                                        info->match_data,
2449                                        info->match_context,
2450                                        info->workspace, info->n_workspace);
2451       if (info->matches == PCRE2_ERROR_DFA_WSSIZE)
2452         {
2453           /* info->workspace is too small. */
2454           info->n_workspace *= 2;
2455           info->workspace = g_realloc_n (info->workspace,
2456                                          info->n_workspace,
2457                                          sizeof (gint));
2458           done = FALSE;
2459         }
2460       else if (info->matches == 0)
2461         {
2462           /* info->offsets is too small. */
2463           info->n_offsets *= 2;
2464           info->offsets = g_realloc_n (info->offsets,
2465                                        info->n_offsets,
2466                                        sizeof (gint));
2467           pcre2_match_data_free (info->match_data);
2468           info->match_data = pcre2_match_data_create (info->n_offsets, NULL);
2469           done = FALSE;
2470         }
2471       else if (IS_PCRE2_ERROR (info->matches))
2472         {
2473           gchar *error_msg = get_match_error_message (info->matches);
2474
2475           g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
2476                        _("Error while matching regular expression %s: %s"),
2477                        regex->pattern, error_msg);
2478           g_clear_pointer (&error_msg, g_free);
2479         }
2480       else if (info->matches != PCRE2_ERROR_NOMATCH)
2481         {
2482           if (!recalc_match_offsets (info, error))
2483             info->matches = PCRE2_ERROR_NOMATCH;
2484         }
2485     }
2486
2487   pcre2_code_free (pcre_re);
2488
2489   /* don’t assert that (info->matches <= info->n_subpatterns + 1) as that only
2490    * holds true for a single match, rather than matching all */
2491
2492   /* set info->pos to -1 so that a call to g_match_info_next() fails. */
2493   info->pos = -1;
2494   retval = info->matches >= 0;
2495
2496   if (match_info != NULL)
2497     *match_info = info;
2498   else
2499     g_match_info_free (info);
2500
2501   return retval;
2502 }
2503
2504 /**
2505  * g_regex_get_string_number:
2506  * @regex: #GRegex structure
2507  * @name: name of the subexpression
2508  *
2509  * Retrieves the number of the subexpression named @name.
2510  *
2511  * Returns: The number of the subexpression or -1 if @name
2512  *   does not exists
2513  *
2514  * Since: 2.14
2515  */
2516 gint
2517 g_regex_get_string_number (const GRegex *regex,
2518                            const gchar  *name)
2519 {
2520   gint num;
2521
2522   g_return_val_if_fail (regex != NULL, -1);
2523   g_return_val_if_fail (name != NULL, -1);
2524
2525   num = pcre2_substring_number_from_name (regex->pcre_re, (PCRE2_SPTR8) name);
2526   if (num == PCRE2_ERROR_NOSUBSTRING)
2527     num = -1;
2528
2529   return num;
2530 }
2531
2532 /**
2533  * g_regex_split_simple:
2534  * @pattern: the regular expression
2535  * @string: the string to scan for matches
2536  * @compile_options: compile options for the regular expression, or 0
2537  * @match_options: match options, or 0
2538  *
2539  * Breaks the string on the pattern, and returns an array of
2540  * the tokens. If the pattern contains capturing parentheses,
2541  * then the text for each of the substrings will also be returned.
2542  * If the pattern does not match anywhere in the string, then the
2543  * whole string is returned as the first token.
2544  *
2545  * This function is equivalent to g_regex_split() but it does
2546  * not require to compile the pattern with g_regex_new(), avoiding
2547  * some lines of code when you need just to do a split without
2548  * extracting substrings, capture counts, and so on.
2549  *
2550  * If this function is to be called on the same @pattern more than
2551  * once, it's more efficient to compile the pattern once with
2552  * g_regex_new() and then use g_regex_split().
2553  *
2554  * As a special case, the result of splitting the empty string ""
2555  * is an empty vector, not a vector containing a single string.
2556  * The reason for this special case is that being able to represent
2557  * an empty vector is typically more useful than consistent handling
2558  * of empty elements. If you do need to represent empty elements,
2559  * you'll need to check for the empty string before calling this
2560  * function.
2561  *
2562  * A pattern that can match empty strings splits @string into
2563  * separate characters wherever it matches the empty string between
2564  * characters. For example splitting "ab c" using as a separator
2565  * "\s*", you will get "a", "b" and "c".
2566  *
2567  * Returns: (transfer full): a %NULL-terminated array of strings. Free
2568  * it using g_strfreev()
2569  *
2570  * Since: 2.14
2571  **/
2572 gchar **
2573 g_regex_split_simple (const gchar        *pattern,
2574                       const gchar        *string,
2575                       GRegexCompileFlags  compile_options,
2576                       GRegexMatchFlags    match_options)
2577 {
2578   GRegex *regex;
2579   gchar **result;
2580
2581   regex = g_regex_new (pattern, compile_options, 0, NULL);
2582   if (!regex)
2583     return NULL;
2584
2585   result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
2586   g_regex_unref (regex);
2587   return result;
2588 }
2589
2590 /**
2591  * g_regex_split:
2592  * @regex: a #GRegex structure
2593  * @string: the string to split with the pattern
2594  * @match_options: match time option flags
2595  *
2596  * Breaks the string on the pattern, and returns an array of the tokens.
2597  * If the pattern contains capturing parentheses, then the text for each
2598  * of the substrings will also be returned. If the pattern does not match
2599  * anywhere in the string, then the whole string is returned as the first
2600  * token.
2601  *
2602  * As a special case, the result of splitting the empty string "" is an
2603  * empty vector, not a vector containing a single string. The reason for
2604  * this special case is that being able to represent an empty vector is
2605  * typically more useful than consistent handling of empty elements. If
2606  * you do need to represent empty elements, you'll need to check for the
2607  * empty string before calling this function.
2608  *
2609  * A pattern that can match empty strings splits @string into separate
2610  * characters wherever it matches the empty string between characters.
2611  * For example splitting "ab c" using as a separator "\s*", you will get
2612  * "a", "b" and "c".
2613  *
2614  * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
2615  * it using g_strfreev()
2616  *
2617  * Since: 2.14
2618  **/
2619 gchar **
2620 g_regex_split (const GRegex     *regex,
2621                const gchar      *string,
2622                GRegexMatchFlags  match_options)
2623 {
2624   return g_regex_split_full (regex, string, -1, 0,
2625                              match_options, 0, NULL);
2626 }
2627
2628 /**
2629  * g_regex_split_full:
2630  * @regex: a #GRegex structure
2631  * @string: (array length=string_len): the string to split with the pattern
2632  * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
2633  * @start_position: starting index of the string to match, in bytes
2634  * @match_options: match time option flags
2635  * @max_tokens: the maximum number of tokens to split @string into.
2636  *   If this is less than 1, the string is split completely
2637  * @error: return location for a #GError
2638  *
2639  * Breaks the string on the pattern, and returns an array of the tokens.
2640  * If the pattern contains capturing parentheses, then the text for each
2641  * of the substrings will also be returned. If the pattern does not match
2642  * anywhere in the string, then the whole string is returned as the first
2643  * token.
2644  *
2645  * As a special case, the result of splitting the empty string "" is an
2646  * empty vector, not a vector containing a single string. The reason for
2647  * this special case is that being able to represent an empty vector is
2648  * typically more useful than consistent handling of empty elements. If
2649  * you do need to represent empty elements, you'll need to check for the
2650  * empty string before calling this function.
2651  *
2652  * A pattern that can match empty strings splits @string into separate
2653  * characters wherever it matches the empty string between characters.
2654  * For example splitting "ab c" using as a separator "\s*", you will get
2655  * "a", "b" and "c".
2656  *
2657  * Setting @start_position differs from just passing over a shortened
2658  * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
2659  * that begins with any kind of lookbehind assertion, such as "\b".
2660  *
2661  * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
2662  * it using g_strfreev()
2663  *
2664  * Since: 2.14
2665  **/
2666 gchar **
2667 g_regex_split_full (const GRegex      *regex,
2668                     const gchar       *string,
2669                     gssize             string_len,
2670                     gint               start_position,
2671                     GRegexMatchFlags   match_options,
2672                     gint               max_tokens,
2673                     GError           **error)
2674 {
2675   GError *tmp_error = NULL;
2676   GMatchInfo *match_info;
2677   GList *list, *last;
2678   gint i;
2679   gint token_count;
2680   gboolean match_ok;
2681   /* position of the last separator. */
2682   gint last_separator_end;
2683   /* was the last match 0 bytes long? */
2684   gboolean last_match_is_empty;
2685   /* the returned array of char **s */
2686   gchar **string_list;
2687
2688   g_return_val_if_fail (regex != NULL, NULL);
2689   g_return_val_if_fail (string != NULL, NULL);
2690   g_return_val_if_fail (start_position >= 0, NULL);
2691   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2692   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2693
2694   if (max_tokens <= 0)
2695     max_tokens = G_MAXINT;
2696
2697   if (string_len < 0)
2698     string_len = strlen (string);
2699
2700   /* zero-length string */
2701   if (string_len - start_position == 0)
2702     return g_new0 (gchar *, 1);
2703
2704   if (max_tokens == 1)
2705     {
2706       string_list = g_new0 (gchar *, 2);
2707       string_list[0] = g_strndup (&string[start_position],
2708                                   string_len - start_position);
2709       return string_list;
2710     }
2711
2712   list = NULL;
2713   token_count = 0;
2714   last_separator_end = start_position;
2715   last_match_is_empty = FALSE;
2716
2717   match_ok = g_regex_match_full (regex, string, string_len, start_position,
2718                                  match_options, &match_info, &tmp_error);
2719
2720   while (tmp_error == NULL)
2721     {
2722       if (match_ok)
2723         {
2724           last_match_is_empty =
2725                     (match_info->offsets[0] == match_info->offsets[1]);
2726
2727           /* we need to skip empty separators at the same position of the end
2728            * of another separator. e.g. the string is "a b" and the separator
2729            * is " *", so from 1 to 2 we have a match and at position 2 we have
2730            * an empty match. */
2731           if (last_separator_end != match_info->offsets[1])
2732             {
2733               gchar *token;
2734               gint match_count;
2735
2736               token = g_strndup (string + last_separator_end,
2737                                  match_info->offsets[0] - last_separator_end);
2738               list = g_list_prepend (list, token);
2739               token_count++;
2740
2741               /* if there were substrings, these need to be added to
2742                * the list. */
2743               match_count = g_match_info_get_match_count (match_info);
2744               if (match_count > 1)
2745                 {
2746                   for (i = 1; i < match_count; i++)
2747                     list = g_list_prepend (list, g_match_info_fetch (match_info, i));
2748                 }
2749             }
2750         }
2751       else
2752         {
2753           /* if there was no match, copy to end of string. */
2754           if (!last_match_is_empty)
2755             {
2756               gchar *token = g_strndup (string + last_separator_end,
2757                                         match_info->string_len - last_separator_end);
2758               list = g_list_prepend (list, token);
2759             }
2760           /* no more tokens, end the loop. */
2761           break;
2762         }
2763
2764       /* -1 to leave room for the last part. */
2765       if (token_count >= max_tokens - 1)
2766         {
2767           /* we have reached the maximum number of tokens, so we copy
2768            * the remaining part of the string. */
2769           if (last_match_is_empty)
2770             {
2771               /* the last match was empty, so we have moved one char
2772                * after the real position to avoid empty matches at the
2773                * same position. */
2774               match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
2775             }
2776           /* the if is needed in the case we have terminated the available
2777            * tokens, but we are at the end of the string, so there are no
2778            * characters left to copy. */
2779           if (string_len > match_info->pos)
2780             {
2781               gchar *token = g_strndup (string + match_info->pos,
2782                                         string_len - match_info->pos);
2783               list = g_list_prepend (list, token);
2784             }
2785           /* end the loop. */
2786           break;
2787         }
2788
2789       last_separator_end = match_info->pos;
2790       if (last_match_is_empty)
2791         /* if the last match was empty, g_match_info_next() has moved
2792          * forward to avoid infinite loops, but we still need to copy that
2793          * character. */
2794         last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
2795
2796       match_ok = g_match_info_next (match_info, &tmp_error);
2797     }
2798   g_match_info_free (match_info);
2799   if (tmp_error != NULL)
2800     {
2801       g_propagate_error (error, tmp_error);
2802       g_list_free_full (list, g_free);
2803       return NULL;
2804     }
2805
2806   string_list = g_new (gchar *, g_list_length (list) + 1);
2807   i = 0;
2808   for (last = g_list_last (list); last; last = g_list_previous (last))
2809     string_list[i++] = last->data;
2810   string_list[i] = NULL;
2811   g_list_free (list);
2812
2813   return string_list;
2814 }
2815
2816 enum
2817 {
2818   REPL_TYPE_STRING,
2819   REPL_TYPE_CHARACTER,
2820   REPL_TYPE_SYMBOLIC_REFERENCE,
2821   REPL_TYPE_NUMERIC_REFERENCE,
2822   REPL_TYPE_CHANGE_CASE
2823 };
2824
2825 typedef enum
2826 {
2827   CHANGE_CASE_NONE         = 1 << 0,
2828   CHANGE_CASE_UPPER        = 1 << 1,
2829   CHANGE_CASE_LOWER        = 1 << 2,
2830   CHANGE_CASE_UPPER_SINGLE = 1 << 3,
2831   CHANGE_CASE_LOWER_SINGLE = 1 << 4,
2832   CHANGE_CASE_SINGLE_MASK  = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
2833   CHANGE_CASE_LOWER_MASK   = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
2834   CHANGE_CASE_UPPER_MASK   = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
2835 } ChangeCase;
2836
2837 struct _InterpolationData
2838 {
2839   gchar     *text;
2840   gint       type;
2841   gint       num;
2842   gchar      c;
2843   ChangeCase change_case;
2844 };
2845
2846 static void
2847 free_interpolation_data (InterpolationData *data)
2848 {
2849   g_free (data->text);
2850   g_free (data);
2851 }
2852
2853 static const gchar *
2854 expand_escape (const gchar        *replacement,
2855                const gchar        *p,
2856                InterpolationData  *data,
2857                GError            **error)
2858 {
2859   const gchar *q, *r;
2860   gint x, d, h, i;
2861   const gchar *error_detail;
2862   gint base = 0;
2863   GError *tmp_error = NULL;
2864
2865   p++;
2866   switch (*p)
2867     {
2868     case 't':
2869       p++;
2870       data->c = '\t';
2871       data->type = REPL_TYPE_CHARACTER;
2872       break;
2873     case 'n':
2874       p++;
2875       data->c = '\n';
2876       data->type = REPL_TYPE_CHARACTER;
2877       break;
2878     case 'v':
2879       p++;
2880       data->c = '\v';
2881       data->type = REPL_TYPE_CHARACTER;
2882       break;
2883     case 'r':
2884       p++;
2885       data->c = '\r';
2886       data->type = REPL_TYPE_CHARACTER;
2887       break;
2888     case 'f':
2889       p++;
2890       data->c = '\f';
2891       data->type = REPL_TYPE_CHARACTER;
2892       break;
2893     case 'a':
2894       p++;
2895       data->c = '\a';
2896       data->type = REPL_TYPE_CHARACTER;
2897       break;
2898     case 'b':
2899       p++;
2900       data->c = '\b';
2901       data->type = REPL_TYPE_CHARACTER;
2902       break;
2903     case '\\':
2904       p++;
2905       data->c = '\\';
2906       data->type = REPL_TYPE_CHARACTER;
2907       break;
2908     case 'x':
2909       p++;
2910       x = 0;
2911       if (*p == '{')
2912         {
2913           p++;
2914           do
2915             {
2916               h = g_ascii_xdigit_value (*p);
2917               if (h < 0)
2918                 {
2919                   error_detail = _("hexadecimal digit or “}” expected");
2920                   goto error;
2921                 }
2922               x = x * 16 + h;
2923               p++;
2924             }
2925           while (*p != '}');
2926           p++;
2927         }
2928       else
2929         {
2930           for (i = 0; i < 2; i++)
2931             {
2932               h = g_ascii_xdigit_value (*p);
2933               if (h < 0)
2934                 {
2935                   error_detail = _("hexadecimal digit expected");
2936                   goto error;
2937                 }
2938               x = x * 16 + h;
2939               p++;
2940             }
2941         }
2942       data->type = REPL_TYPE_STRING;
2943       data->text = g_new0 (gchar, 8);
2944       g_unichar_to_utf8 (x, data->text);
2945       break;
2946     case 'l':
2947       p++;
2948       data->type = REPL_TYPE_CHANGE_CASE;
2949       data->change_case = CHANGE_CASE_LOWER_SINGLE;
2950       break;
2951     case 'u':
2952       p++;
2953       data->type = REPL_TYPE_CHANGE_CASE;
2954       data->change_case = CHANGE_CASE_UPPER_SINGLE;
2955       break;
2956     case 'L':
2957       p++;
2958       data->type = REPL_TYPE_CHANGE_CASE;
2959       data->change_case = CHANGE_CASE_LOWER;
2960       break;
2961     case 'U':
2962       p++;
2963       data->type = REPL_TYPE_CHANGE_CASE;
2964       data->change_case = CHANGE_CASE_UPPER;
2965       break;
2966     case 'E':
2967       p++;
2968       data->type = REPL_TYPE_CHANGE_CASE;
2969       data->change_case = CHANGE_CASE_NONE;
2970       break;
2971     case 'g':
2972       p++;
2973       if (*p != '<')
2974         {
2975           error_detail = _("missing “<” in symbolic reference");
2976           goto error;
2977         }
2978       q = p + 1;
2979       do
2980         {
2981           p++;
2982           if (!*p)
2983             {
2984               error_detail = _("unfinished symbolic reference");
2985               goto error;
2986             }
2987         }
2988       while (*p != '>');
2989       if (p - q == 0)
2990         {
2991           error_detail = _("zero-length symbolic reference");
2992           goto error;
2993         }
2994       if (g_ascii_isdigit (*q))
2995         {
2996           x = 0;
2997           do
2998             {
2999               h = g_ascii_digit_value (*q);
3000               if (h < 0)
3001                 {
3002                   error_detail = _("digit expected");
3003                   p = q;
3004                   goto error;
3005                 }
3006               x = x * 10 + h;
3007               q++;
3008             }
3009           while (q != p);
3010           data->num = x;
3011           data->type = REPL_TYPE_NUMERIC_REFERENCE;
3012         }
3013       else
3014         {
3015           r = q;
3016           do
3017             {
3018               if (!g_ascii_isalnum (*r))
3019                 {
3020                   error_detail = _("illegal symbolic reference");
3021                   p = r;
3022                   goto error;
3023                 }
3024               r++;
3025             }
3026           while (r != p);
3027           data->text = g_strndup (q, p - q);
3028           data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
3029         }
3030       p++;
3031       break;
3032     case '0':
3033       /* if \0 is followed by a number is an octal number representing a
3034        * character, else it is a numeric reference. */
3035       if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
3036         {
3037           base = 8;
3038           p = g_utf8_next_char (p);
3039         }
3040       G_GNUC_FALLTHROUGH;
3041     case '1':
3042     case '2':
3043     case '3':
3044     case '4':
3045     case '5':
3046     case '6':
3047     case '7':
3048     case '8':
3049     case '9':
3050       x = 0;
3051       d = 0;
3052       for (i = 0; i < 3; i++)
3053         {
3054           h = g_ascii_digit_value (*p);
3055           if (h < 0)
3056             break;
3057           if (h > 7)
3058             {
3059               if (base == 8)
3060                 break;
3061               else
3062                 base = 10;
3063             }
3064           if (i == 2 && base == 10)
3065             break;
3066           x = x * 8 + h;
3067           d = d * 10 + h;
3068           p++;
3069         }
3070       if (base == 8 || i == 3)
3071         {
3072           data->type = REPL_TYPE_STRING;
3073           data->text = g_new0 (gchar, 8);
3074           g_unichar_to_utf8 (x, data->text);
3075         }
3076       else
3077         {
3078           data->type = REPL_TYPE_NUMERIC_REFERENCE;
3079           data->num = d;
3080         }
3081       break;
3082     case 0:
3083       error_detail = _("stray final “\\”");
3084       goto error;
3085       break;
3086     default:
3087       error_detail = _("unknown escape sequence");
3088       goto error;
3089     }
3090
3091   return p;
3092
3093  error:
3094   /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
3095   tmp_error = g_error_new (G_REGEX_ERROR,
3096                            G_REGEX_ERROR_REPLACE,
3097                            _("Error while parsing replacement "
3098                              "text “%s” at char %lu: %s"),
3099                            replacement,
3100                            (gulong)(p - replacement),
3101                            error_detail);
3102   g_propagate_error (error, tmp_error);
3103
3104   return NULL;
3105 }
3106
3107 static GList *
3108 split_replacement (const gchar  *replacement,
3109                    GError      **error)
3110 {
3111   GList *list = NULL;
3112   InterpolationData *data;
3113   const gchar *p, *start;
3114
3115   start = p = replacement;
3116   while (*p)
3117     {
3118       if (*p == '\\')
3119         {
3120           data = g_new0 (InterpolationData, 1);
3121           start = p = expand_escape (replacement, p, data, error);
3122           if (p == NULL)
3123             {
3124               g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
3125               free_interpolation_data (data);
3126
3127               return NULL;
3128             }
3129           list = g_list_prepend (list, data);
3130         }
3131       else
3132         {
3133           p++;
3134           if (*p == '\\' || *p == '\0')
3135             {
3136               if (p - start > 0)
3137                 {
3138                   data = g_new0 (InterpolationData, 1);
3139                   data->text = g_strndup (start, p - start);
3140                   data->type = REPL_TYPE_STRING;
3141                   list = g_list_prepend (list, data);
3142                 }
3143             }
3144         }
3145     }
3146
3147   return g_list_reverse (list);
3148 }
3149
3150 /* Change the case of c based on change_case. */
3151 #define CHANGE_CASE(c, change_case) \
3152         (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
3153                 g_unichar_tolower (c) : \
3154                 g_unichar_toupper (c))
3155
3156 static void
3157 string_append (GString     *string,
3158                const gchar *text,
3159                ChangeCase  *change_case)
3160 {
3161   gunichar c;
3162
3163   if (text[0] == '\0')
3164     return;
3165
3166   if (*change_case == CHANGE_CASE_NONE)
3167     {
3168       g_string_append (string, text);
3169     }
3170   else if (*change_case & CHANGE_CASE_SINGLE_MASK)
3171     {
3172       c = g_utf8_get_char (text);
3173       g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
3174       g_string_append (string, g_utf8_next_char (text));
3175       *change_case = CHANGE_CASE_NONE;
3176     }
3177   else
3178     {
3179       while (*text != '\0')
3180         {
3181           c = g_utf8_get_char (text);
3182           g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
3183           text = g_utf8_next_char (text);
3184         }
3185     }
3186 }
3187
3188 static gboolean
3189 interpolate_replacement (const GMatchInfo *match_info,
3190                          GString          *result,
3191                          gpointer          data)
3192 {
3193   GList *list;
3194   InterpolationData *idata;
3195   gchar *match;
3196   ChangeCase change_case = CHANGE_CASE_NONE;
3197
3198   for (list = data; list; list = list->next)
3199     {
3200       idata = list->data;
3201       switch (idata->type)
3202         {
3203         case REPL_TYPE_STRING:
3204           string_append (result, idata->text, &change_case);
3205           break;
3206         case REPL_TYPE_CHARACTER:
3207           g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
3208           if (change_case & CHANGE_CASE_SINGLE_MASK)
3209             change_case = CHANGE_CASE_NONE;
3210           break;
3211         case REPL_TYPE_NUMERIC_REFERENCE:
3212           match = g_match_info_fetch (match_info, idata->num);
3213           if (match)
3214             {
3215               string_append (result, match, &change_case);
3216               g_free (match);
3217             }
3218           break;
3219         case REPL_TYPE_SYMBOLIC_REFERENCE:
3220           match = g_match_info_fetch_named (match_info, idata->text);
3221           if (match)
3222             {
3223               string_append (result, match, &change_case);
3224               g_free (match);
3225             }
3226           break;
3227         case REPL_TYPE_CHANGE_CASE:
3228           change_case = idata->change_case;
3229           break;
3230         }
3231     }
3232
3233   return FALSE;
3234 }
3235
3236 /* whether actual match_info is needed for replacement, i.e.
3237  * whether there are references
3238  */
3239 static gboolean
3240 interpolation_list_needs_match (GList *list)
3241 {
3242   while (list != NULL)
3243     {
3244       InterpolationData *data = list->data;
3245
3246       if (data->type == REPL_TYPE_SYMBOLIC_REFERENCE ||
3247           data->type == REPL_TYPE_NUMERIC_REFERENCE)
3248         {
3249           return TRUE;
3250         }
3251
3252       list = list->next;
3253     }
3254
3255   return FALSE;
3256 }
3257
3258 /**
3259  * g_regex_replace:
3260  * @regex: a #GRegex structure
3261  * @string: (array length=string_len): the string to perform matches against
3262  * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
3263  * @start_position: starting index of the string to match, in bytes
3264  * @replacement: text to replace each match with
3265  * @match_options: options for the match
3266  * @error: location to store the error occurring, or %NULL to ignore errors
3267  *
3268  * Replaces all occurrences of the pattern in @regex with the
3269  * replacement text. Backreferences of the form '\number' or
3270  * '\g<number>' in the replacement text are interpolated by the
3271  * number-th captured subexpression of the match, '\g<name>' refers
3272  * to the captured subexpression with the given name. '\0' refers
3273  * to the complete match, but '\0' followed by a number is the octal
3274  * representation of a character. To include a literal '\' in the
3275  * replacement, write '\\\\'.
3276  *
3277  * There are also escapes that changes the case of the following text:
3278  *
3279  * - \l: Convert to lower case the next character
3280  * - \u: Convert to upper case the next character
3281  * - \L: Convert to lower case till \E
3282  * - \U: Convert to upper case till \E
3283  * - \E: End case modification
3284  *
3285  * If you do not need to use backreferences use g_regex_replace_literal().
3286  *
3287  * The @replacement string must be UTF-8 encoded even if %G_REGEX_RAW was
3288  * passed to g_regex_new(). If you want to use not UTF-8 encoded strings
3289  * you can use g_regex_replace_literal().
3290  *
3291  * Setting @start_position differs from just passing over a shortened
3292  * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern that
3293  * begins with any kind of lookbehind assertion, such as "\b".
3294  *
3295  * Returns: a newly allocated string containing the replacements
3296  *
3297  * Since: 2.14
3298  */
3299 gchar *
3300 g_regex_replace (const GRegex      *regex,
3301                  const gchar       *string,
3302                  gssize             string_len,
3303                  gint               start_position,
3304                  const gchar       *replacement,
3305                  GRegexMatchFlags   match_options,
3306                  GError           **error)
3307 {
3308   gchar *result;
3309   GList *list;
3310   GError *tmp_error = NULL;
3311
3312   g_return_val_if_fail (regex != NULL, NULL);
3313   g_return_val_if_fail (string != NULL, NULL);
3314   g_return_val_if_fail (start_position >= 0, NULL);
3315   g_return_val_if_fail (replacement != NULL, NULL);
3316   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3317   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
3318
3319   list = split_replacement (replacement, &tmp_error);
3320   if (tmp_error != NULL)
3321     {
3322       g_propagate_error (error, tmp_error);
3323       return NULL;
3324     }
3325
3326   result = g_regex_replace_eval (regex,
3327                                  string, string_len, start_position,
3328                                  match_options,
3329                                  interpolate_replacement,
3330                                  (gpointer)list,
3331                                  &tmp_error);
3332   if (tmp_error != NULL)
3333     g_propagate_error (error, tmp_error);
3334
3335   g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
3336
3337   return result;
3338 }
3339
3340 static gboolean
3341 literal_replacement (const GMatchInfo *match_info,
3342                      GString          *result,
3343                      gpointer          data)
3344 {
3345   g_string_append (result, data);
3346   return FALSE;
3347 }
3348
3349 /**
3350  * g_regex_replace_literal:
3351  * @regex: a #GRegex structure
3352  * @string: (array length=string_len): the string to perform matches against
3353  * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
3354  * @start_position: starting index of the string to match, in bytes
3355  * @replacement: text to replace each match with
3356  * @match_options: options for the match
3357  * @error: location to store the error occurring, or %NULL to ignore errors
3358  *
3359  * Replaces all occurrences of the pattern in @regex with the
3360  * replacement text. @replacement is replaced literally, to
3361  * include backreferences use g_regex_replace().
3362  *
3363  * Setting @start_position differs from just passing over a
3364  * shortened string and setting %G_REGEX_MATCH_NOTBOL in the
3365  * case of a pattern that begins with any kind of lookbehind
3366  * assertion, such as "\b".
3367  *
3368  * Returns: a newly allocated string containing the replacements
3369  *
3370  * Since: 2.14
3371  */
3372 gchar *
3373 g_regex_replace_literal (const GRegex      *regex,
3374                          const gchar       *string,
3375                          gssize             string_len,
3376                          gint               start_position,
3377                          const gchar       *replacement,
3378                          GRegexMatchFlags   match_options,
3379                          GError           **error)
3380 {
3381   g_return_val_if_fail (replacement != NULL, NULL);
3382   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
3383
3384   return g_regex_replace_eval (regex,
3385                                string, string_len, start_position,
3386                                match_options,
3387                                literal_replacement,
3388                                (gpointer)replacement,
3389                                error);
3390 }
3391
3392 /**
3393  * g_regex_replace_eval:
3394  * @regex: a #GRegex structure from g_regex_new()
3395  * @string: (array length=string_len): string to perform matches against
3396  * @string_len: the length of @string, in bytes, or -1 if @string is nul-terminated
3397  * @start_position: starting index of the string to match, in bytes
3398  * @match_options: options for the match
3399  * @eval: a function to call for each match
3400  * @user_data: user data to pass to the function
3401  * @error: location to store the error occurring, or %NULL to ignore errors
3402  *
3403  * Replaces occurrences of the pattern in regex with the output of
3404  * @eval for that occurrence.
3405  *
3406  * Setting @start_position differs from just passing over a shortened
3407  * string and setting %G_REGEX_MATCH_NOTBOL in the case of a pattern
3408  * that begins with any kind of lookbehind assertion, such as "\b".
3409  *
3410  * The following example uses g_regex_replace_eval() to replace multiple
3411  * strings at once:
3412  * |[<!-- language="C" --> 
3413  * static gboolean
3414  * eval_cb (const GMatchInfo *info,
3415  *          GString          *res,
3416  *          gpointer          data)
3417  * {
3418  *   gchar *match;
3419  *   gchar *r;
3420  *
3421  *    match = g_match_info_fetch (info, 0);
3422  *    r = g_hash_table_lookup ((GHashTable *)data, match);
3423  *    g_string_append (res, r);
3424  *    g_free (match);
3425  *
3426  *    return FALSE;
3427  * }
3428  *
3429  * ...
3430  *
3431  * GRegex *reg;
3432  * GHashTable *h;
3433  * gchar *res;
3434  *
3435  * h = g_hash_table_new (g_str_hash, g_str_equal);
3436  *
3437  * g_hash_table_insert (h, "1", "ONE");
3438  * g_hash_table_insert (h, "2", "TWO");
3439  * g_hash_table_insert (h, "3", "THREE");
3440  * g_hash_table_insert (h, "4", "FOUR");
3441  *
3442  * reg = g_regex_new ("1|2|3|4", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
3443  * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL);
3444  * g_hash_table_destroy (h);
3445  *
3446  * ...
3447  * ]|
3448  *
3449  * Returns: a newly allocated string containing the replacements
3450  *
3451  * Since: 2.14
3452  */
3453 gchar *
3454 g_regex_replace_eval (const GRegex        *regex,
3455                       const gchar         *string,
3456                       gssize               string_len,
3457                       gint                 start_position,
3458                       GRegexMatchFlags     match_options,
3459                       GRegexEvalCallback   eval,
3460                       gpointer             user_data,
3461                       GError             **error)
3462 {
3463   GMatchInfo *match_info;
3464   GString *result;
3465   gint str_pos = 0;
3466   gboolean done = FALSE;
3467   GError *tmp_error = NULL;
3468
3469   g_return_val_if_fail (regex != NULL, NULL);
3470   g_return_val_if_fail (string != NULL, NULL);
3471   g_return_val_if_fail (start_position >= 0, NULL);
3472   g_return_val_if_fail (eval != NULL, NULL);
3473   g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
3474
3475   if (string_len < 0)
3476     string_len = strlen (string);
3477
3478   result = g_string_sized_new (string_len);
3479
3480   /* run down the string making matches. */
3481   g_regex_match_full (regex, string, string_len, start_position,
3482                       match_options, &match_info, &tmp_error);
3483   while (!done && g_match_info_matches (match_info))
3484     {
3485       g_string_append_len (result,
3486                            string + str_pos,
3487                            match_info->offsets[0] - str_pos);
3488       done = (*eval) (match_info, result, user_data);
3489       str_pos = match_info->offsets[1];
3490       g_match_info_next (match_info, &tmp_error);
3491     }
3492   g_match_info_free (match_info);
3493   if (tmp_error != NULL)
3494     {
3495       g_propagate_error (error, tmp_error);
3496       g_string_free (result, TRUE);
3497       return NULL;
3498     }
3499
3500   g_string_append_len (result, string + str_pos, string_len - str_pos);
3501   return g_string_free (result, FALSE);
3502 }
3503
3504 /**
3505  * g_regex_check_replacement:
3506  * @replacement: the replacement string
3507  * @has_references: (out) (optional): location to store information about
3508  *   references in @replacement or %NULL
3509  * @error: location to store error
3510  *
3511  * Checks whether @replacement is a valid replacement string
3512  * (see g_regex_replace()), i.e. that all escape sequences in
3513  * it are valid.
3514  *
3515  * If @has_references is not %NULL then @replacement is checked
3516  * for pattern references. For instance, replacement text 'foo\n'
3517  * does not contain references and may be evaluated without information
3518  * about actual match, but '\0\1' (whole match followed by first
3519  * subpattern) requires valid #GMatchInfo object.
3520  *
3521  * Returns: whether @replacement is a valid replacement string
3522  *
3523  * Since: 2.14
3524  */
3525 gboolean
3526 g_regex_check_replacement (const gchar  *replacement,
3527                            gboolean     *has_references,
3528                            GError      **error)
3529 {
3530   GList *list;
3531   GError *tmp = NULL;
3532
3533   list = split_replacement (replacement, &tmp);
3534
3535   if (tmp)
3536   {
3537     g_propagate_error (error, tmp);
3538     return FALSE;
3539   }
3540
3541   if (has_references)
3542     *has_references = interpolation_list_needs_match (list);
3543
3544   g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
3545
3546   return TRUE;
3547 }
3548
3549 /**
3550  * g_regex_escape_nul:
3551  * @string: the string to escape
3552  * @length: the length of @string
3553  *
3554  * Escapes the nul characters in @string to "\x00".  It can be used
3555  * to compile a regex with embedded nul characters.
3556  *
3557  * For completeness, @length can be -1 for a nul-terminated string.
3558  * In this case the output string will be of course equal to @string.
3559  *
3560  * Returns: a newly-allocated escaped string
3561  *
3562  * Since: 2.30
3563  */
3564 gchar *
3565 g_regex_escape_nul (const gchar *string,
3566                     gint         length)
3567 {
3568   GString *escaped;
3569   const gchar *p, *piece_start, *end;
3570   gint backslashes;
3571
3572   g_return_val_if_fail (string != NULL, NULL);
3573
3574   if (length < 0)
3575     return g_strdup (string);
3576
3577   end = string + length;
3578   p = piece_start = string;
3579   escaped = g_string_sized_new (length + 1);
3580
3581   backslashes = 0;
3582   while (p < end)
3583     {
3584       switch (*p)
3585         {
3586         case '\0':
3587           if (p != piece_start)
3588             {
3589               /* copy the previous piece. */
3590               g_string_append_len (escaped, piece_start, p - piece_start);
3591             }
3592           if ((backslashes & 1) == 0)
3593             g_string_append_c (escaped, '\\');
3594           g_string_append_c (escaped, 'x');
3595           g_string_append_c (escaped, '0');
3596           g_string_append_c (escaped, '0');
3597           piece_start = ++p;
3598           backslashes = 0;
3599           break;
3600         case '\\':
3601           backslashes++;
3602           ++p;
3603           break;
3604         default:
3605           backslashes = 0;
3606           p = g_utf8_next_char (p);
3607           break;
3608         }
3609     }
3610
3611   if (piece_start < end)
3612     g_string_append_len (escaped, piece_start, end - piece_start);
3613
3614   return g_string_free (escaped, FALSE);
3615 }
3616
3617 /**
3618  * g_regex_escape_string:
3619  * @string: the string to escape
3620  * @length: the length of @string, in bytes, or -1 if @string is nul-terminated
3621  *
3622  * Escapes the special characters used for regular expressions
3623  * in @string, for instance "a.b*c" becomes "a\.b\*c". This
3624  * function is useful to dynamically generate regular expressions.
3625  *
3626  * @string can contain nul characters that are replaced with "\0",
3627  * in this case remember to specify the correct length of @string
3628  * in @length.
3629  *
3630  * Returns: a newly-allocated escaped string
3631  *
3632  * Since: 2.14
3633  */
3634 gchar *
3635 g_regex_escape_string (const gchar *string,
3636                        gint         length)
3637 {
3638   GString *escaped;
3639   const char *p, *piece_start, *end;
3640
3641   g_return_val_if_fail (string != NULL, NULL);
3642
3643   if (length < 0)
3644     length = strlen (string);
3645
3646   end = string + length;
3647   p = piece_start = string;
3648   escaped = g_string_sized_new (length + 1);
3649
3650   while (p < end)
3651     {
3652       switch (*p)
3653         {
3654         case '\0':
3655         case '\\':
3656         case '|':
3657         case '(':
3658         case ')':
3659         case '[':
3660         case ']':
3661         case '{':
3662         case '}':
3663         case '^':
3664         case '$':
3665         case '*':
3666         case '+':
3667         case '?':
3668         case '.':
3669           if (p != piece_start)
3670             /* copy the previous piece. */
3671             g_string_append_len (escaped, piece_start, p - piece_start);
3672           g_string_append_c (escaped, '\\');
3673           if (*p == '\0')
3674             g_string_append_c (escaped, '0');
3675           else
3676             g_string_append_c (escaped, *p);
3677           piece_start = ++p;
3678           break;
3679         default:
3680           p = g_utf8_next_char (p);
3681           break;
3682         }
3683   }
3684
3685   if (piece_start < end)
3686     g_string_append_len (escaped, piece_start, end - piece_start);
3687
3688   return g_string_free (escaped, FALSE);
3689 }