Blame glib/gregex.c

Packit ae235b
/* GRegex -- regular expression API wrapper around PCRE.
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 1999, 2000 Scott Wimer
Packit ae235b
 * Copyright (C) 2004, Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 * Copyright (C) 2005 - 2007, Marco Barisione <marco@barisione.org>
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public License
Packit ae235b
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#ifdef USE_SYSTEM_PCRE
Packit ae235b
#include <pcre.h>
Packit ae235b
#else
Packit ae235b
#include "pcre/pcre.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gtypes.h"
Packit ae235b
#include "gregex.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
#include "glist.h"
Packit ae235b
#include "gmessages.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gatomic.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gregex
Packit ae235b
 * @title: Perl-compatible regular expressions
Packit ae235b
 * @short_description: matches strings against regular expressions
Packit ae235b
 * @see_also: [Regular expression syntax][glib-regex-syntax]
Packit ae235b
 *
Packit ae235b
 * The g_regex_*() functions implement regular
Packit ae235b
 * expression pattern matching using syntax and semantics similar to
Packit ae235b
 * Perl regular expression.
Packit ae235b
 *
Packit ae235b
 * Some functions accept a @start_position argument, setting it differs
Packit ae235b
 * from just passing over a shortened string and setting #G_REGEX_MATCH_NOTBOL
Packit ae235b
 * in the case of a pattern that begins with any kind of lookbehind assertion.
Packit ae235b
 * For example, consider the pattern "\Biss\B" which finds occurrences of "iss"
Packit ae235b
 * in the middle of words. ("\B" matches only if the current position in the
Packit ae235b
 * subject is not a word boundary.) When applied to the string "Mississipi"
Packit ae235b
 * from the fourth byte, namely "issipi", it does not match, because "\B" is
Packit ae235b
 * always false at the start of the subject, which is deemed to be a word
Packit ae235b
 * boundary. However, if the entire string is passed , but with
Packit ae235b
 * @start_position set to 4, it finds the second occurrence of "iss" because
Packit ae235b
 * it is able to look behind the starting point to discover that it is
Packit ae235b
 * preceded by a letter.
Packit ae235b
 *
Packit ae235b
 * Note that, unless you set the #G_REGEX_RAW flag, all the strings passed
Packit ae235b
 * to these functions must be encoded in UTF-8. The lengths and the positions
Packit ae235b
 * inside the strings are in bytes and not in characters, so, for instance,
Packit ae235b
 * "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a
Packit ae235b
 * single character. If you set #G_REGEX_RAW the strings can be non-valid
Packit ae235b
 * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two
Packit ae235b
 * bytes and two characters long.
Packit ae235b
 *
Packit ae235b
 * When matching a pattern, "\n" matches only against a "\n" character in
Packit ae235b
 * the string, and "\r" matches only a "\r" character. To match any newline
Packit ae235b
 * sequence use "\R". This particular group matches either the two-character
Packit ae235b
 * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed,
Packit ae235b
 * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"),
Packit ae235b
 * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line
Packit ae235b
 * separator, U+2028), or PS (paragraph separator, U+2029).
Packit ae235b
 *
Packit ae235b
 * The behaviour of the dot, circumflex, and dollar metacharacters are
Packit ae235b
 * affected by newline characters, the default is to recognize any newline
Packit ae235b
 * character (the same characters recognized by "\R"). This can be changed
Packit ae235b
 * with #G_REGEX_NEWLINE_CR, #G_REGEX_NEWLINE_LF and #G_REGEX_NEWLINE_CRLF
Packit ae235b
 * compile options, and with #G_REGEX_MATCH_NEWLINE_ANY,
Packit ae235b
 * #G_REGEX_MATCH_NEWLINE_CR, #G_REGEX_MATCH_NEWLINE_LF and
Packit ae235b
 * #G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also
Packit ae235b
 * relevant when compiling a pattern if #G_REGEX_EXTENDED is set, and an
Packit ae235b
 * unescaped "#" outside a character class is encountered. This indicates
Packit ae235b
 * a comment that lasts until after the next newline.
Packit ae235b
 *
Packit ae235b
 * When setting the %G_REGEX_JAVASCRIPT_COMPAT flag, pattern syntax and pattern
Packit ae235b
 * matching is changed to be compatible with the way that regular expressions
Packit ae235b
 * work in JavaScript. More precisely, a lonely ']' character in the pattern
Packit ae235b
 * is a syntax error; the '\x' escape only allows 0 to 2 hexadecimal digits, and
Packit ae235b
 * you must use the '\u' escape sequence with 4 hex digits to specify a unicode
Packit ae235b
 * codepoint instead of '\x' or 'x{....}'. If '\x' or '\u' are not followed by
Packit ae235b
 * the specified number of hex digits, they match 'x' and 'u' literally; also
Packit ae235b
 * '\U' always matches 'U' instead of being an error in the pattern. Finally,
Packit ae235b
 * pattern matching is modified so that back references to an unset subpattern
Packit ae235b
 * group produces a match with the empty string instead of an error. See
Packit ae235b
 * pcreapi(3) for more information.
Packit ae235b
 *
Packit ae235b
 * Creating and manipulating the same #GRegex structure from different
Packit ae235b
 * threads is not a problem as #GRegex does not modify its internal
Packit ae235b
 * state between creation and destruction, on the other hand #GMatchInfo
Packit ae235b
 * is not threadsafe.
Packit ae235b
 *
Packit ae235b
 * The regular expressions low-level functionalities are obtained through
Packit ae235b
 * the excellent
Packit ae235b
 * [PCRE](http://www.pcre.org/)
Packit ae235b
 * library written by Philip Hazel.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* Mask of all the possible values for GRegexCompileFlags. */
Packit ae235b
#define G_REGEX_COMPILE_MASK (G_REGEX_CASELESS          | \
Packit ae235b
                              G_REGEX_MULTILINE         | \
Packit ae235b
                              G_REGEX_DOTALL            | \
Packit ae235b
                              G_REGEX_EXTENDED          | \
Packit ae235b
                              G_REGEX_ANCHORED          | \
Packit ae235b
                              G_REGEX_DOLLAR_ENDONLY    | \
Packit ae235b
                              G_REGEX_UNGREEDY          | \
Packit ae235b
                              G_REGEX_RAW               | \
Packit ae235b
                              G_REGEX_NO_AUTO_CAPTURE   | \
Packit ae235b
                              G_REGEX_OPTIMIZE          | \
Packit ae235b
                              G_REGEX_FIRSTLINE         | \
Packit ae235b
                              G_REGEX_DUPNAMES          | \
Packit ae235b
                              G_REGEX_NEWLINE_CR        | \
Packit ae235b
                              G_REGEX_NEWLINE_LF        | \
Packit ae235b
                              G_REGEX_NEWLINE_CRLF      | \
Packit ae235b
                              G_REGEX_NEWLINE_ANYCRLF   | \
Packit ae235b
                              G_REGEX_BSR_ANYCRLF       | \
Packit ae235b
                              G_REGEX_JAVASCRIPT_COMPAT)
Packit ae235b
Packit ae235b
/* Mask of all GRegexCompileFlags values that are (not) passed trough to PCRE */
Packit ae235b
#define G_REGEX_COMPILE_PCRE_MASK (G_REGEX_COMPILE_MASK & ~G_REGEX_COMPILE_NONPCRE_MASK)
Packit ae235b
#define G_REGEX_COMPILE_NONPCRE_MASK (G_REGEX_RAW              | \
Packit ae235b
                                      G_REGEX_OPTIMIZE)
Packit ae235b
Packit ae235b
/* Mask of all the possible values for GRegexMatchFlags. */
Packit ae235b
#define G_REGEX_MATCH_MASK (G_REGEX_MATCH_ANCHORED         | \
Packit ae235b
                            G_REGEX_MATCH_NOTBOL           | \
Packit ae235b
                            G_REGEX_MATCH_NOTEOL           | \
Packit ae235b
                            G_REGEX_MATCH_NOTEMPTY         | \
Packit ae235b
                            G_REGEX_MATCH_PARTIAL          | \
Packit ae235b
                            G_REGEX_MATCH_NEWLINE_CR       | \
Packit ae235b
                            G_REGEX_MATCH_NEWLINE_LF       | \
Packit ae235b
                            G_REGEX_MATCH_NEWLINE_CRLF     | \
Packit ae235b
                            G_REGEX_MATCH_NEWLINE_ANY      | \
Packit ae235b
                            G_REGEX_MATCH_NEWLINE_ANYCRLF  | \
Packit ae235b
                            G_REGEX_MATCH_BSR_ANYCRLF      | \
Packit ae235b
                            G_REGEX_MATCH_BSR_ANY          | \
Packit ae235b
                            G_REGEX_MATCH_PARTIAL_SOFT     | \
Packit ae235b
                            G_REGEX_MATCH_PARTIAL_HARD     | \
Packit ae235b
                            G_REGEX_MATCH_NOTEMPTY_ATSTART)
Packit ae235b
Packit ae235b
/* we rely on these flags having the same values */
Packit ae235b
G_STATIC_ASSERT (G_REGEX_CASELESS          == PCRE_CASELESS);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MULTILINE         == PCRE_MULTILINE);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_DOTALL            == PCRE_DOTALL);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_EXTENDED          == PCRE_EXTENDED);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_ANCHORED          == PCRE_ANCHORED);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_DOLLAR_ENDONLY    == PCRE_DOLLAR_ENDONLY);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_UNGREEDY          == PCRE_UNGREEDY);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_NO_AUTO_CAPTURE   == PCRE_NO_AUTO_CAPTURE);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_FIRSTLINE         == PCRE_FIRSTLINE);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_DUPNAMES          == PCRE_DUPNAMES);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_NEWLINE_CR        == PCRE_NEWLINE_CR);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_NEWLINE_LF        == PCRE_NEWLINE_LF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_NEWLINE_CRLF      == PCRE_NEWLINE_CRLF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_NEWLINE_ANYCRLF   == PCRE_NEWLINE_ANYCRLF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_BSR_ANYCRLF       == PCRE_BSR_ANYCRLF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_JAVASCRIPT_COMPAT == PCRE_JAVASCRIPT_COMPAT);
Packit ae235b
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_ANCHORED         == PCRE_ANCHORED);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NOTBOL           == PCRE_NOTBOL);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NOTEOL           == PCRE_NOTEOL);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NOTEMPTY         == PCRE_NOTEMPTY);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL          == PCRE_PARTIAL);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_CR       == PCRE_NEWLINE_CR);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_LF       == PCRE_NEWLINE_LF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_CRLF     == PCRE_NEWLINE_CRLF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_ANY      == PCRE_NEWLINE_ANY);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NEWLINE_ANYCRLF  == PCRE_NEWLINE_ANYCRLF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_BSR_ANYCRLF      == PCRE_BSR_ANYCRLF);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_BSR_ANY          == PCRE_BSR_UNICODE);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL_SOFT     == PCRE_PARTIAL_SOFT);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_PARTIAL_HARD     == PCRE_PARTIAL_HARD);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_MATCH_NOTEMPTY_ATSTART == PCRE_NOTEMPTY_ATSTART);
Packit ae235b
Packit ae235b
/* These PCRE flags are unused or not exposed publically in GRegexFlags, so
Packit ae235b
 * it should be ok to reuse them for different things.
Packit ae235b
 */
Packit ae235b
G_STATIC_ASSERT (G_REGEX_OPTIMIZE          == PCRE_NO_UTF8_CHECK);
Packit ae235b
G_STATIC_ASSERT (G_REGEX_RAW               == PCRE_UTF8);
Packit ae235b
Packit ae235b
/* if the string is in UTF-8 use g_utf8_ functions, else use
Packit ae235b
 * use just +/- 1. */
Packit ae235b
#define NEXT_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
Packit ae235b
                                ((s) + 1) : \
Packit ae235b
                                g_utf8_next_char (s))
Packit ae235b
#define PREV_CHAR(re, s) (((re)->compile_opts & G_REGEX_RAW) ? \
Packit ae235b
                                ((s) - 1) : \
Packit ae235b
                                g_utf8_prev_char (s))
Packit ae235b
Packit ae235b
struct _GMatchInfo
Packit ae235b
{
Packit ae235b
  volatile gint ref_count;      /* the ref count */
Packit ae235b
  GRegex *regex;                /* the regex */
Packit ae235b
  GRegexMatchFlags match_opts;  /* options used at match time on the regex */
Packit ae235b
  gint matches;                 /* number of matching sub patterns */
Packit ae235b
  gint pos;                     /* position in the string where last match left off */
Packit ae235b
  gint  n_offsets;              /* number of offsets */
Packit ae235b
  gint *offsets;                /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
Packit ae235b
  gint *workspace;              /* workspace for pcre_dfa_exec() */
Packit ae235b
  gint n_workspace;             /* number of workspace elements */
Packit ae235b
  const gchar *string;          /* string passed to the match function */
Packit ae235b
  gssize string_len;            /* length of string */
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GRegex
Packit ae235b
{
Packit ae235b
  volatile gint ref_count;      /* the ref count for the immutable part */
Packit ae235b
  gchar *pattern;               /* the pattern */
Packit ae235b
  pcre *pcre_re;                /* compiled form of the pattern */
Packit ae235b
  GRegexCompileFlags compile_opts;      /* options used at compile time on the pattern */
Packit ae235b
  GRegexMatchFlags match_opts;  /* options used at match time on the regex */
Packit ae235b
  pcre_extra *extra;            /* data stored when G_REGEX_OPTIMIZE is used */
Packit ae235b
};
Packit ae235b
Packit ae235b
/* TRUE if ret is an error code, FALSE otherwise. */
Packit ae235b
#define IS_PCRE_ERROR(ret) ((ret) < PCRE_ERROR_NOMATCH && (ret) != PCRE_ERROR_PARTIAL)
Packit ae235b
Packit ae235b
typedef struct _InterpolationData InterpolationData;
Packit ae235b
static gboolean  interpolation_list_needs_match (GList *list);
Packit ae235b
static gboolean  interpolate_replacement        (const GMatchInfo *match_info,
Packit ae235b
                                                 GString *result,
Packit ae235b
                                                 gpointer data);
Packit ae235b
static GList    *split_replacement              (const gchar *replacement,
Packit ae235b
                                                 GError **error);
Packit ae235b
static void      free_interpolation_data        (InterpolationData *data);
Packit ae235b
Packit ae235b
Packit ae235b
static const gchar *
Packit ae235b
match_error (gint errcode)
Packit ae235b
{
Packit ae235b
  switch (errcode)
Packit ae235b
    {
Packit ae235b
    case PCRE_ERROR_NOMATCH:
Packit ae235b
      /* not an error */
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_NULL:
Packit ae235b
      /* NULL argument, this should not happen in GRegex */
Packit ae235b
      g_warning ("A NULL argument was passed to PCRE");
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_BADOPTION:
Packit ae235b
      return "bad options";
Packit ae235b
    case PCRE_ERROR_BADMAGIC:
Packit ae235b
      return _("corrupted object");
Packit ae235b
    case PCRE_ERROR_UNKNOWN_OPCODE:
Packit ae235b
      return N_("internal error or corrupted object");
Packit ae235b
    case PCRE_ERROR_NOMEMORY:
Packit ae235b
      return _("out of memory");
Packit ae235b
    case PCRE_ERROR_NOSUBSTRING:
Packit ae235b
      /* not used by pcre_exec() */
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_MATCHLIMIT:
Packit ae235b
      return _("backtracking limit reached");
Packit ae235b
    case PCRE_ERROR_CALLOUT:
Packit ae235b
      /* callouts are not implemented */
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_BADUTF8:
Packit ae235b
    case PCRE_ERROR_BADUTF8_OFFSET:
Packit ae235b
      /* we do not check if strings are valid */
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_PARTIAL:
Packit ae235b
      /* not an error */
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_BADPARTIAL:
Packit ae235b
      return _("the pattern contains items not supported for partial matching");
Packit ae235b
    case PCRE_ERROR_INTERNAL:
Packit ae235b
      return _("internal error");
Packit ae235b
    case PCRE_ERROR_BADCOUNT:
Packit ae235b
      /* negative ovecsize, this should not happen in GRegex */
Packit ae235b
      g_warning ("A negative ovecsize was passed to PCRE");
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_DFA_UITEM:
Packit ae235b
      return _("the pattern contains items not supported for partial matching");
Packit ae235b
    case PCRE_ERROR_DFA_UCOND:
Packit ae235b
      return _("back references as conditions are not supported for partial matching");
Packit ae235b
    case PCRE_ERROR_DFA_UMLIMIT:
Packit ae235b
      /* the match_field field is not used in GRegex */
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_DFA_WSSIZE:
Packit ae235b
      /* handled expanding the workspace */
Packit ae235b
      break;
Packit ae235b
    case PCRE_ERROR_DFA_RECURSE:
Packit ae235b
    case PCRE_ERROR_RECURSIONLIMIT:
Packit ae235b
      return _("recursion limit reached");
Packit ae235b
    case PCRE_ERROR_BADNEWLINE:
Packit ae235b
      return _("invalid combination of newline flags");
Packit ae235b
    case PCRE_ERROR_BADOFFSET:
Packit ae235b
      return _("bad offset");
Packit ae235b
    case PCRE_ERROR_SHORTUTF8:
Packit ae235b
      return _("short utf8");
Packit ae235b
    case PCRE_ERROR_RECURSELOOP:
Packit ae235b
      return _("recursion loop");
Packit ae235b
    default:
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
  return _("unknown error");
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
translate_compile_error (gint *errcode, const gchar **errmsg)
Packit ae235b
{
Packit ae235b
  /* Compile errors are created adding 100 to the error code returned
Packit ae235b
   * by PCRE.
Packit ae235b
   * If errcode is known we put the translatable error message in
Packit ae235b
   * erromsg. If errcode is unknown we put the generic
Packit ae235b
   * G_REGEX_ERROR_COMPILE error code in errcode and keep the
Packit ae235b
   * untranslated error message returned by PCRE.
Packit ae235b
   * Note that there can be more PCRE errors with the same GRegexError
Packit ae235b
   * and that some PCRE errors are useless for us.
Packit ae235b
   */
Packit ae235b
  *errcode += 100;
Packit ae235b
Packit ae235b
  switch (*errcode)
Packit ae235b
    {
Packit ae235b
    case G_REGEX_ERROR_STRAY_BACKSLASH:
Packit ae235b
      *errmsg = _("\\ at end of pattern");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MISSING_CONTROL_CHAR:
Packit ae235b
      *errmsg = _("\\c at end of pattern");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNRECOGNIZED_ESCAPE:
Packit ae235b
      *errmsg = _("unrecognized character following \\");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER:
Packit ae235b
      *errmsg = _("numbers out of order in {} quantifier");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_QUANTIFIER_TOO_BIG:
Packit ae235b
      *errmsg = _("number too big in {} quantifier");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS:
Packit ae235b
      *errmsg = _("missing terminating ] for character class");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS:
Packit ae235b
      *errmsg = _("invalid escape sequence in character class");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_RANGE_OUT_OF_ORDER:
Packit ae235b
      *errmsg = _("range out of order in character class");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_NOTHING_TO_REPEAT:
Packit ae235b
      *errmsg = _("nothing to repeat");
Packit ae235b
      break;
Packit ae235b
    case 111: /* internal error: unexpected repeat */
Packit ae235b
      *errcode = G_REGEX_ERROR_INTERNAL;
Packit ae235b
      *errmsg = _("unexpected repeat");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNRECOGNIZED_CHARACTER:
Packit ae235b
      *errmsg = _("unrecognized character after (? or (?-");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS:
Packit ae235b
      *errmsg = _("POSIX named classes are supported only within a class");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNMATCHED_PARENTHESIS:
Packit ae235b
      *errmsg = _("missing terminating )");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE:
Packit ae235b
      *errmsg = _("reference to non-existent subpattern");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNTERMINATED_COMMENT:
Packit ae235b
      *errmsg = _("missing ) after comment");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_EXPRESSION_TOO_LARGE:
Packit ae235b
      *errmsg = _("regular expression is too large");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MEMORY_ERROR:
Packit ae235b
      *errmsg = _("failed to get memory");
Packit ae235b
      break;
Packit ae235b
    case 122: /* unmatched parentheses */
Packit ae235b
      *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
Packit ae235b
      *errmsg = _(") without opening (");
Packit ae235b
      break;
Packit ae235b
    case 123: /* internal error: code overflow */
Packit ae235b
      *errcode = G_REGEX_ERROR_INTERNAL;
Packit ae235b
      *errmsg = _("code overflow");
Packit ae235b
      break;
Packit ae235b
    case 124: /* "unrecognized character after (?<\0 */
Packit ae235b
      *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
Packit ae235b
      *errmsg = _("unrecognized character after (?<");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND:
Packit ae235b
      *errmsg = _("lookbehind assertion is not fixed length");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MALFORMED_CONDITION:
Packit ae235b
      *errmsg = _("malformed number or name after (?(");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES:
Packit ae235b
      *errmsg = _("conditional group contains more than two branches");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_ASSERTION_EXPECTED:
Packit ae235b
      *errmsg = _("assertion expected after (?(");
Packit ae235b
      break;
Packit ae235b
    case 129:
Packit ae235b
      *errcode = G_REGEX_ERROR_UNMATCHED_PARENTHESIS;
Packit ae235b
      /* translators: '(?R' and '(?[+-]digits' are both meant as (groups of)
Packit ae235b
       * sequences here, '(?-54' would be an example for the second group.
Packit ae235b
       */
Packit ae235b
      *errmsg = _("(?R or (?[+-]digits must be followed by )");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME:
Packit ae235b
      *errmsg = _("unknown POSIX class name");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED:
Packit ae235b
      *errmsg = _("POSIX collating elements are not supported");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_HEX_CODE_TOO_LARGE:
Packit ae235b
      *errmsg = _("character value in \\x{...} sequence is too large");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INVALID_CONDITION:
Packit ae235b
      *errmsg = _("invalid condition (?(0)");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND:
Packit ae235b
      *errmsg = _("\\C not allowed in lookbehind assertion");
Packit ae235b
      break;
Packit ae235b
    case 137: /* PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0 */
Packit ae235b
      /* A number of Perl escapes are not handled by PCRE.
Packit ae235b
       * Therefore it explicitly raises ERR37.
Packit ae235b
       */
Packit ae235b
      *errcode = G_REGEX_ERROR_UNRECOGNIZED_ESCAPE;
Packit ae235b
      *errmsg = _("escapes \\L, \\l, \\N{name}, \\U, and \\u are not supported");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INFINITE_LOOP:
Packit ae235b
      *errmsg = _("recursive call could loop indefinitely");
Packit ae235b
      break;
Packit ae235b
    case 141: /* unrecognized character after (?P\0 */
Packit ae235b
      *errcode = G_REGEX_ERROR_UNRECOGNIZED_CHARACTER;
Packit ae235b
      *errmsg = _("unrecognized character after (?P");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR:
Packit ae235b
      *errmsg = _("missing terminator in subpattern name");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME:
Packit ae235b
      *errmsg = _("two named subpatterns have the same name");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MALFORMED_PROPERTY:
Packit ae235b
      *errmsg = _("malformed \\P or \\p sequence");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNKNOWN_PROPERTY:
Packit ae235b
      *errmsg = _("unknown property name after \\P or \\p");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG:
Packit ae235b
      *errmsg = _("subpattern name is too long (maximum 32 characters)");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_TOO_MANY_SUBPATTERNS:
Packit ae235b
      *errmsg = _("too many named subpatterns (maximum 10,000)");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INVALID_OCTAL_VALUE:
Packit ae235b
      *errmsg = _("octal value is greater than \\377");
Packit ae235b
      break;
Packit ae235b
    case 152: /* internal error: overran compiling workspace */
Packit ae235b
      *errcode = G_REGEX_ERROR_INTERNAL;
Packit ae235b
      *errmsg = _("overran compiling workspace");
Packit ae235b
      break;
Packit ae235b
    case 153: /* internal error: previously-checked referenced subpattern not found */
Packit ae235b
      *errcode = G_REGEX_ERROR_INTERNAL;
Packit ae235b
      *errmsg = _("previously-checked referenced subpattern not found");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE:
Packit ae235b
      *errmsg = _("DEFINE group contains more than one branch");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS:
Packit ae235b
      *errmsg = _("inconsistent NEWLINE options");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MISSING_BACK_REFERENCE:
Packit ae235b
      *errmsg = _("\\g is not followed by a braced, angle-bracketed, or quoted name or "
Packit ae235b
                  "number, or by a plain number");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE:
Packit ae235b
      *errmsg = _("a numbered reference must not be zero");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN:
Packit ae235b
      *errmsg = _("an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB:
Packit ae235b
      *errmsg = _("(*VERB) not recognized");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_NUMBER_TOO_BIG:
Packit ae235b
      *errmsg = _("number is too big");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MISSING_SUBPATTERN_NAME:
Packit ae235b
      *errmsg = _("missing subpattern name after (?&";;
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MISSING_DIGIT:
Packit ae235b
      *errmsg = _("digit expected after (?+");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INVALID_DATA_CHARACTER:
Packit ae235b
      *errmsg = _("] is an invalid data character in JavaScript compatibility mode");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME:
Packit ae235b
      *errmsg = _("different names for subpatterns of the same number are not allowed");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED:
Packit ae235b
      *errmsg = _("(*MARK) must have an argument");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_INVALID_CONTROL_CHAR:
Packit ae235b
      *errmsg = _( "\\c must be followed by an ASCII character");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_MISSING_NAME:
Packit ae235b
      *errmsg = _("\\k is not followed by a braced, angle-bracketed, or quoted name");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS:
Packit ae235b
      *errmsg = _("\\N is not supported in a class");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_TOO_MANY_FORWARD_REFERENCES:
Packit ae235b
      *errmsg = _("too many forward references");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_NAME_TOO_LONG:
Packit ae235b
      *errmsg = _("name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)");
Packit ae235b
      break;
Packit ae235b
    case G_REGEX_ERROR_CHARACTER_VALUE_TOO_LARGE:
Packit ae235b
      *errmsg = _("character value in \\u.... sequence is too large");
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case 116: /* erroffset passed as NULL */
Packit ae235b
      /* This should not happen as we never pass a NULL erroffset */
Packit ae235b
      g_warning ("erroffset passed as NULL");
Packit ae235b
      *errcode = G_REGEX_ERROR_COMPILE;
Packit ae235b
      break;
Packit ae235b
    case 117: /* unknown option bit(s) set */
Packit ae235b
      /* This should not happen as we check options before passing them
Packit ae235b
       * to pcre_compile2() */
Packit ae235b
      g_warning ("unknown option bit(s) set");
Packit ae235b
      *errcode = G_REGEX_ERROR_COMPILE;
Packit ae235b
      break;
Packit ae235b
    case 132: /* this version of PCRE is compiled without UTF support */
Packit ae235b
    case 144: /* invalid UTF-8 string */
Packit ae235b
    case 145: /* support for \\P, \\p, and \\X has not been compiled */
Packit ae235b
    case 167: /* this version of PCRE is not compiled with Unicode property support */
Packit ae235b
    case 173: /* disallowed Unicode code point (>= 0xd800 && <= 0xdfff) */
Packit ae235b
    case 174: /* invalid UTF-16 string */
Packit ae235b
      /* These errors should not happen as we are using an UTF-8 and UCP-enabled PCRE
Packit ae235b
       * and we do not check if strings are valid */
Packit ae235b
    case 170: /* internal error: unknown opcode in find_fixedlength() */
Packit ae235b
      *errcode = G_REGEX_ERROR_INTERNAL;
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      *errcode = G_REGEX_ERROR_COMPILE;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GMatchInfo */
Packit ae235b
Packit ae235b
static GMatchInfo *
Packit ae235b
match_info_new (const GRegex *regex,
Packit ae235b
                const gchar  *string,
Packit ae235b
                gint          string_len,
Packit ae235b
                gint          start_position,
Packit ae235b
                gint          match_options,
Packit ae235b
                gboolean      is_dfa)
Packit ae235b
{
Packit ae235b
  GMatchInfo *match_info;
Packit ae235b
Packit ae235b
  if (string_len < 0)
Packit ae235b
    string_len = strlen (string);
Packit ae235b
Packit ae235b
  match_info = g_new0 (GMatchInfo, 1);
Packit ae235b
  match_info->ref_count = 1;
Packit ae235b
  match_info->regex = g_regex_ref ((GRegex *)regex);
Packit ae235b
  match_info->string = string;
Packit ae235b
  match_info->string_len = string_len;
Packit ae235b
  match_info->matches = PCRE_ERROR_NOMATCH;
Packit ae235b
  match_info->pos = start_position;
Packit ae235b
  match_info->match_opts = match_options;
Packit ae235b
Packit ae235b
  if (is_dfa)
Packit ae235b
    {
Packit ae235b
      /* These values should be enough for most cases, if they are not
Packit ae235b
       * enough g_regex_match_all_full() will expand them. */
Packit ae235b
      match_info->n_offsets = 24;
Packit ae235b
      match_info->n_workspace = 100;
Packit ae235b
      match_info->workspace = g_new (gint, match_info->n_workspace);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gint capture_count;
Packit ae235b
      pcre_fullinfo (regex->pcre_re, regex->extra,
Packit ae235b
                     PCRE_INFO_CAPTURECOUNT, &capture_count);
Packit ae235b
      match_info->n_offsets = (capture_count + 1) * 3;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  match_info->offsets = g_new0 (gint, match_info->n_offsets);
Packit ae235b
  /* Set an invalid position for the previous match. */
Packit ae235b
  match_info->offsets[0] = -1;
Packit ae235b
  match_info->offsets[1] = -1;
Packit ae235b
Packit ae235b
  return match_info;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_get_regex:
Packit ae235b
 * @match_info: a #GMatchInfo
Packit ae235b
 *
Packit ae235b
 * Returns #GRegex object used in @match_info. It belongs to Glib
Packit ae235b
 * and must not be freed. Use g_regex_ref() if you need to keep it
Packit ae235b
 * after you free @match_info object.
Packit ae235b
 *
Packit ae235b
 * Returns: #GRegex object used in @match_info
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
GRegex *
Packit ae235b
g_match_info_get_regex (const GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (match_info != NULL, NULL);
Packit ae235b
  return match_info->regex;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_get_string:
Packit ae235b
 * @match_info: a #GMatchInfo
Packit ae235b
 *
Packit ae235b
 * Returns the string searched with @match_info. This is the
Packit ae235b
 * string passed to g_regex_match() or g_regex_replace() so
Packit ae235b
 * you may not free it before calling this function.
Packit ae235b
 *
Packit ae235b
 * Returns: the string searched with @match_info
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_match_info_get_string (const GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (match_info != NULL, NULL);
Packit ae235b
  return match_info->string;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_ref:
Packit ae235b
 * @match_info: a #GMatchInfo
Packit ae235b
 *
Packit ae235b
 * Increases reference count of @match_info by 1.
Packit ae235b
 *
Packit ae235b
 * Returns: @match_info
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GMatchInfo       *
Packit ae235b
g_match_info_ref (GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (match_info != NULL, NULL);
Packit ae235b
  g_atomic_int_inc (&match_info->ref_count);
Packit ae235b
  return match_info;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_unref:
Packit ae235b
 * @match_info: a #GMatchInfo
Packit ae235b
 *
Packit ae235b
 * Decreases reference count of @match_info by 1. When reference count drops
Packit ae235b
 * to zero, it frees all the memory associated with the match_info structure.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_match_info_unref (GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  if (g_atomic_int_dec_and_test (&match_info->ref_count))
Packit ae235b
    {
Packit ae235b
      g_regex_unref (match_info->regex);
Packit ae235b
      g_free (match_info->offsets);
Packit ae235b
      g_free (match_info->workspace);
Packit ae235b
      g_free (match_info);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_free:
Packit ae235b
 * @match_info: (nullable): a #GMatchInfo, or %NULL
Packit ae235b
 *
Packit ae235b
 * If @match_info is not %NULL, calls g_match_info_unref(); otherwise does
Packit ae235b
 * nothing.
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_match_info_free (GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  if (match_info == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_match_info_unref (match_info);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_next:
Packit ae235b
 * @match_info: a #GMatchInfo structure
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore errors
Packit ae235b
 *
Packit ae235b
 * Scans for the next match using the same parameters of the previous
Packit ae235b
 * call to g_regex_match_full() or g_regex_match() that returned
Packit ae235b
 * @match_info.
Packit ae235b
 *
Packit ae235b
 * The match is done on the string passed to the match function, so you
Packit ae235b
 * cannot free it before calling this function.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE is the string matched, %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_match_info_next (GMatchInfo  *match_info,
Packit ae235b
                   GError     **error)
Packit ae235b
{
Packit ae235b
  gint prev_match_start;
Packit ae235b
  gint prev_match_end;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (match_info != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (match_info->pos >= 0, FALSE);
Packit ae235b
Packit ae235b
  prev_match_start = match_info->offsets[0];
Packit ae235b
  prev_match_end = match_info->offsets[1];
Packit ae235b
Packit ae235b
  if (match_info->pos > match_info->string_len)
Packit ae235b
    {
Packit ae235b
      /* we have reached the end of the string */
Packit ae235b
      match_info->pos = -1;
Packit ae235b
      match_info->matches = PCRE_ERROR_NOMATCH;
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  match_info->matches = pcre_exec (match_info->regex->pcre_re,
Packit ae235b
                                   match_info->regex->extra,
Packit ae235b
                                   match_info->string,
Packit ae235b
                                   match_info->string_len,
Packit ae235b
                                   match_info->pos,
Packit ae235b
                                   match_info->regex->match_opts | match_info->match_opts,
Packit ae235b
                                   match_info->offsets,
Packit ae235b
                                   match_info->n_offsets);
Packit ae235b
  if (IS_PCRE_ERROR (match_info->matches))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
Packit ae235b
                   _("Error while matching regular expression %s: %s"),
Packit ae235b
                   match_info->regex->pattern, match_error (match_info->matches));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* avoid infinite loops if the pattern is an empty string or something
Packit ae235b
   * equivalent */
Packit ae235b
  if (match_info->pos == match_info->offsets[1])
Packit ae235b
    {
Packit ae235b
      if (match_info->pos > match_info->string_len)
Packit ae235b
        {
Packit ae235b
          /* we have reached the end of the string */
Packit ae235b
          match_info->pos = -1;
Packit ae235b
          match_info->matches = PCRE_ERROR_NOMATCH;
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      match_info->pos = NEXT_CHAR (match_info->regex,
Packit ae235b
                                   &match_info->string[match_info->pos]) -
Packit ae235b
                                   match_info->string;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      match_info->pos = match_info->offsets[1];
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* it's possible to get two identical matches when we are matching
Packit ae235b
   * empty strings, for instance if the pattern is "(?=[A-Z0-9])" and
Packit ae235b
   * the string is "RegExTest" we have:
Packit ae235b
   *  - search at position 0: match from 0 to 0
Packit ae235b
   *  - search at position 1: match from 3 to 3
Packit ae235b
   *  - search at position 3: match from 3 to 3 (duplicate)
Packit ae235b
   *  - search at position 4: match from 5 to 5
Packit ae235b
   *  - search at position 5: match from 5 to 5 (duplicate)
Packit ae235b
   *  - search at position 6: no match -> stop
Packit ae235b
   * so we have to ignore the duplicates.
Packit ae235b
   * see bug #515944: http://bugzilla.gnome.org/show_bug.cgi?id=515944 */
Packit ae235b
  if (match_info->matches >= 0 &&
Packit ae235b
      prev_match_start == match_info->offsets[0] &&
Packit ae235b
      prev_match_end == match_info->offsets[1])
Packit ae235b
    {
Packit ae235b
      /* ignore this match and search the next one */
Packit ae235b
      return g_match_info_next (match_info, error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return match_info->matches >= 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_matches:
Packit ae235b
 * @match_info: a #GMatchInfo structure
Packit ae235b
 *
Packit ae235b
 * Returns whether the previous match operation succeeded.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the previous match operation succeeded,
Packit ae235b
 *   %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_match_info_matches (const GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (match_info != NULL, FALSE);
Packit ae235b
Packit ae235b
  return match_info->matches >= 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_get_match_count:
Packit ae235b
 * @match_info: a #GMatchInfo structure
Packit ae235b
 *
Packit ae235b
 * Retrieves the number of matched substrings (including substring 0,
Packit ae235b
 * that is the whole matched text), so 1 is returned if the pattern
Packit ae235b
 * has no substrings in it and 0 is returned if the match failed.
Packit ae235b
 *
Packit ae235b
 * If the last match was obtained using the DFA algorithm, that is
Packit ae235b
 * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
Packit ae235b
 * count is not that of the number of capturing parentheses but that of
Packit ae235b
 * the number of matched substrings.
Packit ae235b
 *
Packit ae235b
 * Returns: Number of matched substrings, or -1 if an error occurred
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_match_info_get_match_count (const GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (match_info, -1);
Packit ae235b
Packit ae235b
  if (match_info->matches == PCRE_ERROR_NOMATCH)
Packit ae235b
    /* no match */
Packit ae235b
    return 0;
Packit ae235b
  else if (match_info->matches < PCRE_ERROR_NOMATCH)
Packit ae235b
    /* error */
Packit ae235b
    return -1;
Packit ae235b
  else
Packit ae235b
    /* match */
Packit ae235b
    return match_info->matches;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_is_partial_match:
Packit ae235b
 * @match_info: a #GMatchInfo structure
Packit ae235b
 *
Packit ae235b
 * Usually if the string passed to g_regex_match*() matches as far as
Packit ae235b
 * it goes, but is too short to match the entire pattern, %FALSE is
Packit ae235b
 * returned. There are circumstances where it might be helpful to
Packit ae235b
 * distinguish this case from other cases in which there is no match.
Packit ae235b
 *
Packit ae235b
 * Consider, for example, an application where a human is required to
Packit ae235b
 * type in data for a field with specific formatting requirements. An
Packit ae235b
 * example might be a date in the form ddmmmyy, defined by the pattern
Packit ae235b
 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
Packit ae235b
 * If the application sees the user’s keystrokes one by one, and can
Packit ae235b
 * check that what has been typed so far is potentially valid, it is
Packit ae235b
 * able to raise an error as soon as a mistake is made.
Packit ae235b
 *
Packit ae235b
 * GRegex supports the concept of partial matching by means of the
Packit ae235b
 * #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD flags.
Packit ae235b
 * When they are used, the return code for
Packit ae235b
 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
Packit ae235b
 * for a complete match, %FALSE otherwise. But, when these functions
Packit ae235b
 * return %FALSE, you can check if the match was partial calling
Packit ae235b
 * g_match_info_is_partial_match().
Packit ae235b
 *
Packit ae235b
 * The difference between #G_REGEX_MATCH_PARTIAL_SOFT and 
Packit ae235b
 * #G_REGEX_MATCH_PARTIAL_HARD is that when a partial match is encountered
Packit ae235b
 * with #G_REGEX_MATCH_PARTIAL_SOFT, matching continues to search for a
Packit ae235b
 * possible complete match, while with #G_REGEX_MATCH_PARTIAL_HARD matching
Packit ae235b
 * stops at the partial match.
Packit ae235b
 * When both #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD
Packit ae235b
 * are set, the latter takes precedence.
Packit ae235b
 *
Packit ae235b
 * There were formerly some restrictions on the pattern for partial matching.
Packit ae235b
 * The restrictions no longer apply.
Packit ae235b
 *
Packit ae235b
 * See pcrepartial(3) for more information on partial matching.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the match was partial, %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_match_info_is_partial_match (const GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (match_info != NULL, FALSE);
Packit ae235b
Packit ae235b
  return match_info->matches == PCRE_ERROR_PARTIAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_expand_references:
Packit ae235b
 * @match_info: (nullable): a #GMatchInfo or %NULL
Packit ae235b
 * @string_to_expand: the string to expand
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore errors
Packit ae235b
 *
Packit ae235b
 * Returns a new string containing the text in @string_to_expand with
Packit ae235b
 * references and escape sequences expanded. References refer to the last
Packit ae235b
 * match done with @string against @regex and have the same syntax used by
Packit ae235b
 * g_regex_replace().
Packit ae235b
 *
Packit ae235b
 * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
Packit ae235b
 * passed to g_regex_new().
Packit ae235b
 *
Packit ae235b
 * The backreferences are extracted from the string passed to the match
Packit ae235b
 * function, so you cannot call this function after freeing the string.
Packit ae235b
 *
Packit ae235b
 * @match_info may be %NULL in which case @string_to_expand must not
Packit ae235b
 * contain references. For instance "foo\n" does not refer to an actual
Packit ae235b
 * pattern and '\n' merely will be replaced with \n character,
Packit ae235b
 * while to expand "\0" (whole match) one needs the result of a match.
Packit ae235b
 * Use g_regex_check_replacement() to find out whether @string_to_expand
Packit ae235b
 * contains references.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): the expanded string, or %NULL if an error occurred
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_match_info_expand_references (const GMatchInfo  *match_info,
Packit ae235b
                                const gchar       *string_to_expand,
Packit ae235b
                                GError           **error)
Packit ae235b
{
Packit ae235b
  GString *result;
Packit ae235b
  GList *list;
Packit ae235b
  GError *tmp_error = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (string_to_expand != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  list = split_replacement (string_to_expand, &tmp_error);
Packit ae235b
  if (tmp_error != NULL)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, tmp_error);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!match_info && interpolation_list_needs_match (list))
Packit ae235b
    {
Packit ae235b
      g_critical ("String '%s' contains references to the match, can't "
Packit ae235b
                  "expand references without GMatchInfo object",
Packit ae235b
                  string_to_expand);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = g_string_sized_new (strlen (string_to_expand));
Packit ae235b
  interpolate_replacement (match_info, result, list);
Packit ae235b
Packit ae235b
  g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
Packit ae235b
Packit ae235b
  return g_string_free (result, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_fetch:
Packit ae235b
 * @match_info: #GMatchInfo structure
Packit ae235b
 * @match_num: number of the sub expression
Packit ae235b
 *
Packit ae235b
 * Retrieves the text matching the @match_num'th capturing
Packit ae235b
 * parentheses. 0 is the full text of the match, 1 is the first paren
Packit ae235b
 * set, 2 the second, and so on.
Packit ae235b
 *
Packit ae235b
 * If @match_num is a valid sub pattern but it didn't match anything
Packit ae235b
 * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
Packit ae235b
 * string is returned.
Packit ae235b
 *
Packit ae235b
 * If the match was obtained using the DFA algorithm, that is using
Packit ae235b
 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
Packit ae235b
 * string is not that of a set of parentheses but that of a matched
Packit ae235b
 * substring. Substrings are matched in reverse order of length, so
Packit ae235b
 * 0 is the longest match.
Packit ae235b
 *
Packit ae235b
 * The string is fetched from the string passed to the match function,
Packit ae235b
 * so you cannot call this function after freeing the string.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): The matched substring, or %NULL if an error
Packit ae235b
 *     occurred. You have to free the string yourself
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_match_info_fetch (const GMatchInfo *match_info,
Packit ae235b
                    gint              match_num)
Packit ae235b
{
Packit ae235b
  /* we cannot use pcre_get_substring() because it allocates the
Packit ae235b
   * string using pcre_malloc(). */
Packit ae235b
  gchar *match = NULL;
Packit ae235b
  gint start, end;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (match_info != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (match_num >= 0, NULL);
Packit ae235b
Packit ae235b
  /* match_num does not exist or it didn't matched, i.e. matching "b"
Packit ae235b
   * against "(a)?b" then group 0 is empty. */
Packit ae235b
  if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
Packit ae235b
    match = NULL;
Packit ae235b
  else if (start == -1)
Packit ae235b
    match = g_strdup ("");
Packit ae235b
  else
Packit ae235b
    match = g_strndup (&match_info->string[start], end - start);
Packit ae235b
Packit ae235b
  return match;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_fetch_pos:
Packit ae235b
 * @match_info: #GMatchInfo structure
Packit ae235b
 * @match_num: number of the sub expression
Packit ae235b
 * @start_pos: (out) (optional): pointer to location where to store
Packit ae235b
 *     the start position, or %NULL
Packit ae235b
 * @end_pos: (out) (optional): pointer to location where to store
Packit ae235b
 *     the end position, or %NULL
Packit ae235b
 *
Packit ae235b
 * Retrieves the position in bytes of the @match_num'th capturing
Packit ae235b
 * parentheses. 0 is the full text of the match, 1 is the first
Packit ae235b
 * paren set, 2 the second, and so on.
Packit ae235b
 *
Packit ae235b
 * If @match_num is a valid sub pattern but it didn't match anything
Packit ae235b
 * (e.g. sub pattern 1, matching "b" against "(a)?b") then @start_pos
Packit ae235b
 * and @end_pos are set to -1 and %TRUE is returned.
Packit ae235b
 *
Packit ae235b
 * If the match was obtained using the DFA algorithm, that is using
Packit ae235b
 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
Packit ae235b
 * position is not that of a set of parentheses but that of a matched
Packit ae235b
 * substring. Substrings are matched in reverse order of length, so
Packit ae235b
 * 0 is the longest match.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
Packit ae235b
 *   the position cannot be fetched, @start_pos and @end_pos are left
Packit ae235b
 *   unchanged
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_match_info_fetch_pos (const GMatchInfo *match_info,
Packit ae235b
                        gint              match_num,
Packit ae235b
                        gint             *start_pos,
Packit ae235b
                        gint             *end_pos)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (match_info != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (match_num >= 0, FALSE);
Packit ae235b
Packit ae235b
  /* make sure the sub expression number they're requesting is less than
Packit ae235b
   * the total number of sub expressions that were matched. */
Packit ae235b
  if (match_num >= match_info->matches)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (start_pos != NULL)
Packit ae235b
    *start_pos = match_info->offsets[2 * match_num];
Packit ae235b
Packit ae235b
  if (end_pos != NULL)
Packit ae235b
    *end_pos = match_info->offsets[2 * match_num + 1];
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Returns number of first matched subpattern with name @name.
Packit ae235b
 * There may be more than one in case when DUPNAMES is used,
Packit ae235b
 * and not all subpatterns with that name match;
Packit ae235b
 * pcre_get_stringnumber() does not work in that case.
Packit ae235b
 */
Packit ae235b
static gint
Packit ae235b
get_matched_substring_number (const GMatchInfo *match_info,
Packit ae235b
                              const gchar      *name)
Packit ae235b
{
Packit ae235b
  gint entrysize;
Packit ae235b
  gchar *first, *last;
Packit ae235b
  guchar *entry;
Packit ae235b
Packit ae235b
  if (!(match_info->regex->compile_opts & G_REGEX_DUPNAMES))
Packit ae235b
    return pcre_get_stringnumber (match_info->regex->pcre_re, name);
Packit ae235b
Packit ae235b
  /* This code is copied from pcre_get.c: get_first_set() */
Packit ae235b
  entrysize = pcre_get_stringtable_entries (match_info->regex->pcre_re,
Packit ae235b
                                            name,
Packit ae235b
                                            &first,
Packit ae235b
                                            &last);
Packit ae235b
Packit ae235b
  if (entrysize <= 0)
Packit ae235b
    return entrysize;
Packit ae235b
Packit ae235b
  for (entry = (guchar*) first; entry <= (guchar*) last; entry += entrysize)
Packit ae235b
    {
Packit ae235b
      gint n = (entry[0] << 8) + entry[1];
Packit ae235b
      if (match_info->offsets[n*2] >= 0)
Packit ae235b
        return n;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (first[0] << 8) + first[1];
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_fetch_named:
Packit ae235b
 * @match_info: #GMatchInfo structure
Packit ae235b
 * @name: name of the subexpression
Packit ae235b
 *
Packit ae235b
 * Retrieves the text matching the capturing parentheses named @name.
Packit ae235b
 *
Packit ae235b
 * If @name is a valid sub pattern name but it didn't match anything
Packit ae235b
 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
Packit ae235b
 * then an empty string is returned.
Packit ae235b
 *
Packit ae235b
 * The string is fetched from the string passed to the match function,
Packit ae235b
 * so you cannot call this function after freeing the string.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): The matched substring, or %NULL if an error
Packit ae235b
 *     occurred. You have to free the string yourself
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_match_info_fetch_named (const GMatchInfo *match_info,
Packit ae235b
                          const gchar      *name)
Packit ae235b
{
Packit ae235b
  /* we cannot use pcre_get_named_substring() because it allocates the
Packit ae235b
   * string using pcre_malloc(). */
Packit ae235b
  gint num;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (match_info != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (name != NULL, NULL);
Packit ae235b
Packit ae235b
  num = get_matched_substring_number (match_info, name);
Packit ae235b
  if (num < 0)
Packit ae235b
    return NULL;
Packit ae235b
  else
Packit ae235b
    return g_match_info_fetch (match_info, num);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_fetch_named_pos:
Packit ae235b
 * @match_info: #GMatchInfo structure
Packit ae235b
 * @name: name of the subexpression
Packit ae235b
 * @start_pos: (out) (optional): pointer to location where to store
Packit ae235b
 *     the start position, or %NULL
Packit ae235b
 * @end_pos: (out) (optional): pointer to location where to store
Packit ae235b
 *     the end position, or %NULL
Packit ae235b
 *
Packit ae235b
 * Retrieves the position in bytes of the capturing parentheses named @name.
Packit ae235b
 *
Packit ae235b
 * If @name is a valid sub pattern name but it didn't match anything
Packit ae235b
 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
Packit ae235b
 * then @start_pos and @end_pos are set to -1 and %TRUE is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the position was fetched, %FALSE otherwise.
Packit ae235b
 *     If the position cannot be fetched, @start_pos and @end_pos
Packit ae235b
 *     are left unchanged.
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_match_info_fetch_named_pos (const GMatchInfo *match_info,
Packit ae235b
                              const gchar      *name,
Packit ae235b
                              gint             *start_pos,
Packit ae235b
                              gint             *end_pos)
Packit ae235b
{
Packit ae235b
  gint num;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (match_info != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (name != NULL, FALSE);
Packit ae235b
Packit ae235b
  num = get_matched_substring_number (match_info, name);
Packit ae235b
  if (num < 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_match_info_fetch_all:
Packit ae235b
 * @match_info: a #GMatchInfo structure
Packit ae235b
 *
Packit ae235b
 * Bundles up pointers to each of the matching substrings from a match
Packit ae235b
 * and stores them in an array of gchar pointers. The first element in
Packit ae235b
 * the returned array is the match number 0, i.e. the entire matched
Packit ae235b
 * text.
Packit ae235b
 *
Packit ae235b
 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
Packit ae235b
 * "b" against "(a)?b") then an empty string is inserted.
Packit ae235b
 *
Packit ae235b
 * If the last match was obtained using the DFA algorithm, that is using
Packit ae235b
 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
Packit ae235b
 * strings are not that matched by sets of parentheses but that of the
Packit ae235b
 * matched substring. Substrings are matched in reverse order of length,
Packit ae235b
 * so the first one is the longest match.
Packit ae235b
 *
Packit ae235b
 * The strings are fetched from the string passed to the match function,
Packit ae235b
 * so you cannot call this function after freeing the string.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a %NULL-terminated array of gchar *
Packit ae235b
 *     pointers.  It must be freed using g_strfreev(). If the previous
Packit ae235b
 *     match failed %NULL is returned
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_match_info_fetch_all (const GMatchInfo *match_info)
Packit ae235b
{
Packit ae235b
  /* we cannot use pcre_get_substring_list() because the returned value
Packit ae235b
   * isn't suitable for g_strfreev(). */
Packit ae235b
  gchar **result;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (match_info != NULL, NULL);
Packit ae235b
Packit ae235b
  if (match_info->matches < 0)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  result = g_new (gchar *, match_info->matches + 1);
Packit ae235b
  for (i = 0; i < match_info->matches; i++)
Packit ae235b
    result[i] = g_match_info_fetch (match_info, i);
Packit ae235b
  result[i] = NULL;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/* GRegex */
Packit ae235b
Packit ae235b
G_DEFINE_QUARK (g-regex-error-quark, g_regex_error)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_ref:
Packit ae235b
 * @regex: a #GRegex
Packit ae235b
 *
Packit ae235b
 * Increases reference count of @regex by 1.
Packit ae235b
 *
Packit ae235b
 * Returns: @regex
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
GRegex *
Packit ae235b
g_regex_ref (GRegex *regex)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (regex != NULL, NULL);
Packit ae235b
  g_atomic_int_inc (&regex->ref_count);
Packit ae235b
  return regex;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_unref:
Packit ae235b
 * @regex: a #GRegex
Packit ae235b
 *
Packit ae235b
 * Decreases reference count of @regex by 1. When reference count drops
Packit ae235b
 * to zero, it frees all the memory associated with the regex structure.
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_regex_unref (GRegex *regex)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (regex != NULL);
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&regex->ref_count))
Packit ae235b
    {
Packit ae235b
      g_free (regex->pattern);
Packit ae235b
      if (regex->pcre_re != NULL)
Packit ae235b
        pcre_free (regex->pcre_re);
Packit ae235b
      if (regex->extra != NULL)
Packit ae235b
        pcre_free (regex->extra);
Packit ae235b
      g_free (regex);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * @match_options: (inout) (optional):
Packit ae235b
 */
Packit ae235b
static pcre *regex_compile (const gchar         *pattern,
Packit ae235b
                            GRegexCompileFlags   compile_options,
Packit ae235b
                            GRegexCompileFlags  *compile_options_out,
Packit ae235b
                            GRegexMatchFlags    *match_options,
Packit ae235b
                            GError             **error);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_new:
Packit ae235b
 * @pattern: the regular expression
Packit ae235b
 * @compile_options: compile options for the regular expression, or 0
Packit ae235b
 * @match_options: match options for the regular expression, or 0
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Compiles the regular expression to an internal form, and does
Packit ae235b
 * the initial setup of the #GRegex structure.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): a #GRegex structure or %NULL if an error occured. Call
Packit ae235b
 *   g_regex_unref() when you are done with it
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
GRegex *
Packit ae235b
g_regex_new (const gchar         *pattern,
Packit ae235b
             GRegexCompileFlags   compile_options,
Packit ae235b
             GRegexMatchFlags     match_options,
Packit ae235b
             GError             **error)
Packit ae235b
{
Packit ae235b
  GRegex *regex;
Packit ae235b
  pcre *re;
Packit ae235b
  const gchar *errmsg;
Packit ae235b
  gboolean optimize = FALSE;
Packit ae235b
  static volatile gsize initialised = 0;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (pattern != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
  g_return_val_if_fail ((compile_options & ~G_REGEX_COMPILE_MASK) == 0, NULL);
Packit ae235b
  g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&initialised))
Packit ae235b
    {
Packit ae235b
      int supports_utf8, supports_ucp;
Packit ae235b
Packit ae235b
      pcre_config (PCRE_CONFIG_UTF8, &supports_utf8);
Packit ae235b
      if (!supports_utf8)
Packit ae235b
        g_critical (_("PCRE library is compiled without UTF8 support"));
Packit ae235b
Packit ae235b
      pcre_config (PCRE_CONFIG_UNICODE_PROPERTIES, &supports_ucp);
Packit ae235b
      if (!supports_ucp)
Packit ae235b
        g_critical (_("PCRE library is compiled without UTF8 properties support"));
Packit ae235b
Packit ae235b
      g_once_init_leave (&initialised, supports_utf8 && supports_ucp ? 1 : 2);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (initialised != 1)) 
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, 
Packit ae235b
                           _("PCRE library is compiled with incompatible options"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* G_REGEX_OPTIMIZE has the same numeric value of PCRE_NO_UTF8_CHECK,
Packit ae235b
   * as we do not need to wrap PCRE_NO_UTF8_CHECK. */
Packit ae235b
  if (compile_options & G_REGEX_OPTIMIZE)
Packit ae235b
    optimize = TRUE;
Packit ae235b
Packit ae235b
  re = regex_compile (pattern, compile_options, &compile_options,
Packit ae235b
                      &match_options, error);
Packit ae235b
Packit ae235b
  if (re == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  regex = g_new0 (GRegex, 1);
Packit ae235b
  regex->ref_count = 1;
Packit ae235b
  regex->pattern = g_strdup (pattern);
Packit ae235b
  regex->pcre_re = re;
Packit ae235b
  regex->compile_opts = compile_options;
Packit ae235b
  regex->match_opts = match_options;
Packit ae235b
Packit ae235b
  if (optimize)
Packit ae235b
    {
Packit ae235b
      regex->extra = pcre_study (regex->pcre_re, 0, &errmsg);
Packit ae235b
      if (errmsg != NULL)
Packit ae235b
        {
Packit ae235b
          GError *tmp_error = g_error_new (G_REGEX_ERROR,
Packit ae235b
                                           G_REGEX_ERROR_OPTIMIZE,
Packit ae235b
                                           _("Error while optimizing "
Packit ae235b
                                             "regular expression %s: %s"),
Packit ae235b
                                           regex->pattern,
Packit ae235b
                                           errmsg);
Packit ae235b
          g_propagate_error (error, tmp_error);
Packit ae235b
Packit ae235b
          g_regex_unref (regex);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return regex;
Packit ae235b
}
Packit ae235b
Packit ae235b
static pcre *
Packit ae235b
regex_compile (const gchar         *pattern,
Packit ae235b
               GRegexCompileFlags   compile_options,
Packit ae235b
               GRegexCompileFlags  *compile_options_out,
Packit ae235b
               GRegexMatchFlags    *match_options,
Packit ae235b
               GError             **error)
Packit ae235b
{
Packit ae235b
  pcre *re;
Packit ae235b
  const gchar *errmsg;
Packit ae235b
  gint erroffset;
Packit ae235b
  gint errcode;
Packit ae235b
  GRegexCompileFlags nonpcre_compile_options;
Packit ae235b
  unsigned long int pcre_compile_options;
Packit ae235b
Packit ae235b
  nonpcre_compile_options = compile_options & G_REGEX_COMPILE_NONPCRE_MASK;
Packit ae235b
Packit ae235b
  /* In GRegex the string are, by default, UTF-8 encoded. PCRE
Packit ae235b
   * instead uses UTF-8 only if required with PCRE_UTF8. */
Packit ae235b
  if (compile_options & G_REGEX_RAW)
Packit ae235b
    {
Packit ae235b
      /* disable utf-8 */
Packit ae235b
      compile_options &= ~G_REGEX_RAW;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* enable utf-8 */
Packit ae235b
      compile_options |= PCRE_UTF8 | PCRE_NO_UTF8_CHECK;
Packit ae235b
Packit ae235b
      if (match_options != NULL)
Packit ae235b
        *match_options |= PCRE_NO_UTF8_CHECK;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* PCRE_NEWLINE_ANY is the default for the internal PCRE but
Packit ae235b
   * not for the system one. */
Packit ae235b
  if (!(compile_options & G_REGEX_NEWLINE_CR) &&
Packit ae235b
      !(compile_options & G_REGEX_NEWLINE_LF))
Packit ae235b
    {
Packit ae235b
      compile_options |= PCRE_NEWLINE_ANY;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  compile_options |= PCRE_UCP;
Packit ae235b
Packit ae235b
  /* PCRE_BSR_UNICODE is the default for the internal PCRE but
Packit ae235b
   * possibly not for the system one.
Packit ae235b
   */
Packit ae235b
  if (~compile_options & G_REGEX_BSR_ANYCRLF)
Packit ae235b
    compile_options |= PCRE_BSR_UNICODE;
Packit ae235b
Packit ae235b
  /* compile the pattern */
Packit ae235b
  re = pcre_compile2 (pattern, compile_options, &errcode,
Packit ae235b
                      &errmsg, &erroffset, NULL);
Packit ae235b
Packit ae235b
  /* if the compilation failed, set the error member and return
Packit ae235b
   * immediately */
Packit ae235b
  if (re == NULL)
Packit ae235b
    {
Packit ae235b
      GError *tmp_error;
Packit ae235b
Packit ae235b
      /* Translate the PCRE error code to GRegexError and use a translated
Packit ae235b
       * error message if possible */
Packit ae235b
      translate_compile_error (&errcode, &errmsg);
Packit ae235b
Packit ae235b
      /* PCRE uses byte offsets but we want to show character offsets */
Packit ae235b
      erroffset = g_utf8_pointer_to_offset (pattern, &pattern[erroffset]);
Packit ae235b
Packit ae235b
      tmp_error = g_error_new (G_REGEX_ERROR, errcode,
Packit ae235b
                               _("Error while compiling regular "
Packit ae235b
                                 "expression %s at char %d: %s"),
Packit ae235b
                               pattern, erroffset, errmsg);
Packit ae235b
      g_propagate_error (error, tmp_error);
Packit ae235b
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* For options set at the beginning of the pattern, pcre puts them into
Packit ae235b
   * compile options, e.g. "(?i)foo" will make the pcre structure store
Packit ae235b
   * PCRE_CASELESS even though it wasn't explicitly given for compilation. */
Packit ae235b
  pcre_fullinfo (re, NULL, PCRE_INFO_OPTIONS, &pcre_compile_options);
Packit ae235b
  compile_options = pcre_compile_options & G_REGEX_COMPILE_PCRE_MASK;
Packit ae235b
Packit ae235b
  /* Don't leak PCRE_NEWLINE_ANY, which is part of PCRE_NEWLINE_ANYCRLF */
Packit ae235b
  if ((pcre_compile_options & PCRE_NEWLINE_ANYCRLF) != PCRE_NEWLINE_ANYCRLF)
Packit ae235b
    compile_options &= ~PCRE_NEWLINE_ANY;
Packit ae235b
Packit ae235b
  compile_options |= nonpcre_compile_options;
Packit ae235b
Packit ae235b
  if (!(compile_options & G_REGEX_DUPNAMES))
Packit ae235b
    {
Packit ae235b
      gboolean jchanged = FALSE;
Packit ae235b
      pcre_fullinfo (re, NULL, PCRE_INFO_JCHANGED, &jchanged);
Packit ae235b
      if (jchanged)
Packit ae235b
        compile_options |= G_REGEX_DUPNAMES;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (compile_options_out != 0)
Packit ae235b
    *compile_options_out = compile_options;
Packit ae235b
Packit ae235b
  return re;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_pattern:
Packit ae235b
 * @regex: a #GRegex structure
Packit ae235b
 *
Packit ae235b
 * Gets the pattern string associated with @regex, i.e. a copy of
Packit ae235b
 * the string passed to g_regex_new().
Packit ae235b
 *
Packit ae235b
 * Returns: the pattern of @regex
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_regex_get_pattern (const GRegex *regex)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (regex != NULL, NULL);
Packit ae235b
Packit ae235b
  return regex->pattern;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_max_backref:
Packit ae235b
 * @regex: a #GRegex
Packit ae235b
 *
Packit ae235b
 * Returns the number of the highest back reference
Packit ae235b
 * in the pattern, or 0 if the pattern does not contain
Packit ae235b
 * back references.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of the highest back reference
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_regex_get_max_backref (const GRegex *regex)
Packit ae235b
{
Packit ae235b
  gint value;
Packit ae235b
Packit ae235b
  pcre_fullinfo (regex->pcre_re, regex->extra,
Packit ae235b
                 PCRE_INFO_BACKREFMAX, &value);
Packit ae235b
Packit ae235b
  return value;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_capture_count:
Packit ae235b
 * @regex: a #GRegex
Packit ae235b
 *
Packit ae235b
 * Returns the number of capturing subpatterns in the pattern.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of capturing subpatterns
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_regex_get_capture_count (const GRegex *regex)
Packit ae235b
{
Packit ae235b
  gint value;
Packit ae235b
Packit ae235b
  pcre_fullinfo (regex->pcre_re, regex->extra,
Packit ae235b
                 PCRE_INFO_CAPTURECOUNT, &value);
Packit ae235b
Packit ae235b
  return value;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_has_cr_or_lf:
Packit ae235b
 * @regex: a #GRegex structure
Packit ae235b
 *
Packit ae235b
 * Checks whether the pattern contains explicit CR or LF references.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the pattern contains explicit CR or LF references
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_regex_get_has_cr_or_lf (const GRegex *regex)
Packit ae235b
{
Packit ae235b
  gint value;
Packit ae235b
Packit ae235b
  pcre_fullinfo (regex->pcre_re, regex->extra,
Packit ae235b
                 PCRE_INFO_HASCRORLF, &value);
Packit ae235b
Packit ae235b
  return !!value;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_max_lookbehind:
Packit ae235b
 * @regex: a #GRegex structure
Packit ae235b
 *
Packit ae235b
 * Gets the number of characters in the longest lookbehind assertion in the
Packit ae235b
 * pattern. This information is useful when doing multi-segment matching using
Packit ae235b
 * the partial matching facilities.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of characters in the longest lookbehind assertion.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_regex_get_max_lookbehind (const GRegex *regex)
Packit ae235b
{
Packit ae235b
  gint max_lookbehind;
Packit ae235b
Packit ae235b
  pcre_fullinfo (regex->pcre_re, regex->extra,
Packit ae235b
                 PCRE_INFO_MAXLOOKBEHIND, &max_lookbehind);
Packit ae235b
Packit ae235b
  return max_lookbehind;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_compile_flags:
Packit ae235b
 * @regex: a #GRegex
Packit ae235b
 *
Packit ae235b
 * Returns the compile options that @regex was created with.
Packit ae235b
 *
Packit ae235b
 * Depending on the version of PCRE that is used, this may or may not
Packit ae235b
 * include flags set by option expressions such as `(?i)` found at the
Packit ae235b
 * top-level within the compiled pattern.
Packit ae235b
 *
Packit ae235b
 * Returns: flags from #GRegexCompileFlags
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GRegexCompileFlags
Packit ae235b
g_regex_get_compile_flags (const GRegex *regex)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (regex != NULL, 0);
Packit ae235b
Packit ae235b
  return regex->compile_opts;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_match_flags:
Packit ae235b
 * @regex: a #GRegex
Packit ae235b
 *
Packit ae235b
 * Returns the match options that @regex was created with.
Packit ae235b
 *
Packit ae235b
 * Returns: flags from #GRegexMatchFlags
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GRegexMatchFlags
Packit ae235b
g_regex_get_match_flags (const GRegex *regex)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (regex != NULL, 0);
Packit ae235b
Packit ae235b
  return regex->match_opts & G_REGEX_MATCH_MASK;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_match_simple:
Packit ae235b
 * @pattern: the regular expression
Packit ae235b
 * @string: the string to scan for matches
Packit ae235b
 * @compile_options: compile options for the regular expression, or 0
Packit ae235b
 * @match_options: match options, or 0
Packit ae235b
 *
Packit ae235b
 * Scans for a match in @string for @pattern.
Packit ae235b
 *
Packit ae235b
 * This function is equivalent to g_regex_match() but it does not
Packit ae235b
 * require to compile the pattern with g_regex_new(), avoiding some
Packit ae235b
 * lines of code when you need just to do a match without extracting
Packit ae235b
 * substrings, capture counts, and so on.
Packit ae235b
 *
Packit ae235b
 * If this function is to be called on the same @pattern more than
Packit ae235b
 * once, it's more efficient to compile the pattern once with
Packit ae235b
 * g_regex_new() and then use g_regex_match().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the string matched, %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_regex_match_simple (const gchar        *pattern,
Packit ae235b
                      const gchar        *string,
Packit ae235b
                      GRegexCompileFlags  compile_options,
Packit ae235b
                      GRegexMatchFlags    match_options)
Packit ae235b
{
Packit ae235b
  GRegex *regex;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  regex = g_regex_new (pattern, compile_options, 0, NULL);
Packit ae235b
  if (!regex)
Packit ae235b
    return FALSE;
Packit ae235b
  result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
Packit ae235b
  g_regex_unref (regex);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_match:
Packit ae235b
 * @regex: a #GRegex structure from g_regex_new()
Packit ae235b
 * @string: the string to scan for matches
Packit ae235b
 * @match_options: match options
Packit ae235b
 * @match_info: (out) (optional): pointer to location where to store
Packit ae235b
 *     the #GMatchInfo, or %NULL if you do not need it
Packit ae235b
 *
Packit ae235b
 * Scans for a match in string for the pattern in @regex.
Packit ae235b
 * The @match_options are combined with the match options specified
Packit ae235b
 * when the @regex structure was created, letting you have more
Packit ae235b
 * flexibility in reusing #GRegex structures.
Packit ae235b
 *
Packit ae235b
 * A #GMatchInfo structure, used to get information on the match,
Packit ae235b
 * is stored in @match_info if not %NULL. Note that if @match_info
Packit ae235b
 * is not %NULL then it is created even if the function returns %FALSE,
Packit ae235b
 * i.e. you must free it regardless if regular expression actually matched.
Packit ae235b
 *
Packit ae235b
 * To retrieve all the non-overlapping matches of the pattern in
Packit ae235b
 * string you can use g_match_info_next().
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * static void
Packit ae235b
 * print_uppercase_words (const gchar *string)
Packit ae235b
 * {
Packit ae235b
 *   // Print all uppercase-only words.
Packit ae235b
 *   GRegex *regex;
Packit ae235b
 *   GMatchInfo *match_info;
Packit ae235b
 *  
Packit ae235b
 *   regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
Packit ae235b
 *   g_regex_match (regex, string, 0, &match_info);
Packit ae235b
 *   while (g_match_info_matches (match_info))
Packit ae235b
 *     {
Packit ae235b
 *       gchar *word = g_match_info_fetch (match_info, 0);
Packit ae235b
 *       g_print ("Found: %s\n", word);
Packit ae235b
 *       g_free (word);
Packit ae235b
 *       g_match_info_next (match_info, NULL);
Packit ae235b
 *     }
Packit ae235b
 *   g_match_info_free (match_info);
Packit ae235b
 *   g_regex_unref (regex);
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * @string is not copied and is used in #GMatchInfo internally. If
Packit ae235b
 * you use any #GMatchInfo method (except g_match_info_free()) after
Packit ae235b
 * freeing or modifying @string then the behaviour is undefined.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE is the string matched, %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_regex_match (const GRegex      *regex,
Packit ae235b
               const gchar       *string,
Packit ae235b
               GRegexMatchFlags   match_options,
Packit ae235b
               GMatchInfo       **match_info)
Packit ae235b
{
Packit ae235b
  return g_regex_match_full (regex, string, -1, 0, match_options,
Packit ae235b
                             match_info, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_match_full:
Packit ae235b
 * @regex: a #GRegex structure from g_regex_new()
Packit ae235b
 * @string: (array length=string_len): the string to scan for matches
Packit ae235b
 * @string_len: the length of @string, or -1 if @string is nul-terminated
Packit ae235b
 * @start_position: starting index of the string to match, in bytes
Packit ae235b
 * @match_options: match options
Packit ae235b
 * @match_info: (out) (optional): pointer to location where to store
Packit ae235b
 *     the #GMatchInfo, or %NULL if you do not need it
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore errors
Packit ae235b
 *
Packit ae235b
 * Scans for a match in string for the pattern in @regex.
Packit ae235b
 * The @match_options are combined with the match options specified
Packit ae235b
 * when the @regex structure was created, letting you have more
Packit ae235b
 * flexibility in reusing #GRegex structures.
Packit ae235b
 *
Packit ae235b
 * Setting @start_position differs from just passing over a shortened
Packit ae235b
 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
Packit ae235b
 * that begins with any kind of lookbehind assertion, such as "\b".
Packit ae235b
 *
Packit ae235b
 * A #GMatchInfo structure, used to get information on the match, is
Packit ae235b
 * stored in @match_info if not %NULL. Note that if @match_info is
Packit ae235b
 * not %NULL then it is created even if the function returns %FALSE,
Packit ae235b
 * i.e. you must free it regardless if regular expression actually
Packit ae235b
 * matched.
Packit ae235b
 *
Packit ae235b
 * @string is not copied and is used in #GMatchInfo internally. If
Packit ae235b
 * you use any #GMatchInfo method (except g_match_info_free()) after
Packit ae235b
 * freeing or modifying @string then the behaviour is undefined.
Packit ae235b
 *
Packit ae235b
 * To retrieve all the non-overlapping matches of the pattern in
Packit ae235b
 * string you can use g_match_info_next().
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * static void
Packit ae235b
 * print_uppercase_words (const gchar *string)
Packit ae235b
 * {
Packit ae235b
 *   // Print all uppercase-only words.
Packit ae235b
 *   GRegex *regex;
Packit ae235b
 *   GMatchInfo *match_info;
Packit ae235b
 *   GError *error = NULL;
Packit ae235b
 *   
Packit ae235b
 *   regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
Packit ae235b
 *   g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
Packit ae235b
 *   while (g_match_info_matches (match_info))
Packit ae235b
 *     {
Packit ae235b
 *       gchar *word = g_match_info_fetch (match_info, 0);
Packit ae235b
 *       g_print ("Found: %s\n", word);
Packit ae235b
 *       g_free (word);
Packit ae235b
 *       g_match_info_next (match_info, &error);
Packit ae235b
 *     }
Packit ae235b
 *   g_match_info_free (match_info);
Packit ae235b
 *   g_regex_unref (regex);
Packit ae235b
 *   if (error != NULL)
Packit ae235b
 *     {
Packit ae235b
 *       g_printerr ("Error while matching: %s\n", error->message);
Packit ae235b
 *       g_error_free (error);
Packit ae235b
 *     }
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE is the string matched, %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_regex_match_full (const GRegex      *regex,
Packit ae235b
                    const gchar       *string,
Packit ae235b
                    gssize             string_len,
Packit ae235b
                    gint               start_position,
Packit ae235b
                    GRegexMatchFlags   match_options,
Packit ae235b
                    GMatchInfo       **match_info,
Packit ae235b
                    GError           **error)
Packit ae235b
{
Packit ae235b
  GMatchInfo *info;
Packit ae235b
  gboolean match_ok;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (regex != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (string != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (start_position >= 0, FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
  g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
Packit ae235b
Packit ae235b
  info = match_info_new (regex, string, string_len, start_position,
Packit ae235b
                         match_options, FALSE);
Packit ae235b
  match_ok = g_match_info_next (info, error);
Packit ae235b
  if (match_info != NULL)
Packit ae235b
    *match_info = info;
Packit ae235b
  else
Packit ae235b
    g_match_info_free (info);
Packit ae235b
Packit ae235b
  return match_ok;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_match_all:
Packit ae235b
 * @regex: a #GRegex structure from g_regex_new()
Packit ae235b
 * @string: the string to scan for matches
Packit ae235b
 * @match_options: match options
Packit ae235b
 * @match_info: (out) (optional): pointer to location where to store
Packit ae235b
 *     the #GMatchInfo, or %NULL if you do not need it
Packit ae235b
 *
Packit ae235b
 * Using the standard algorithm for regular expression matching only
Packit ae235b
 * the longest match in the string is retrieved. This function uses
Packit ae235b
 * a different algorithm so it can retrieve all the possible matches.
Packit ae235b
 * For more documentation see g_regex_match_all_full().
Packit ae235b
 *
Packit ae235b
 * A #GMatchInfo structure, used to get information on the match, is
Packit ae235b
 * stored in @match_info if not %NULL. Note that if @match_info is
Packit ae235b
 * not %NULL then it is created even if the function returns %FALSE,
Packit ae235b
 * i.e. you must free it regardless if regular expression actually
Packit ae235b
 * matched.
Packit ae235b
 *
Packit ae235b
 * @string is not copied and is used in #GMatchInfo internally. If
Packit ae235b
 * you use any #GMatchInfo method (except g_match_info_free()) after
Packit ae235b
 * freeing or modifying @string then the behaviour is undefined.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE is the string matched, %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_regex_match_all (const GRegex      *regex,
Packit ae235b
                   const gchar       *string,
Packit ae235b
                   GRegexMatchFlags   match_options,
Packit ae235b
                   GMatchInfo       **match_info)
Packit ae235b
{
Packit ae235b
  return g_regex_match_all_full (regex, string, -1, 0, match_options,
Packit ae235b
                                 match_info, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_match_all_full:
Packit ae235b
 * @regex: a #GRegex structure from g_regex_new()
Packit ae235b
 * @string: (array length=string_len): the string to scan for matches
Packit ae235b
 * @string_len: the length of @string, or -1 if @string is nul-terminated
Packit ae235b
 * @start_position: starting index of the string to match, in bytes
Packit ae235b
 * @match_options: match options
Packit ae235b
 * @match_info: (out) (optional): pointer to location where to store
Packit ae235b
 *     the #GMatchInfo, or %NULL if you do not need it
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore errors
Packit ae235b
 *
Packit ae235b
 * Using the standard algorithm for regular expression matching only
Packit ae235b
 * the longest match in the string is retrieved, it is not possible
Packit ae235b
 * to obtain all the available matches. For instance matching
Packit ae235b
 * "  <c>" against the pattern "<.*>"
Packit ae235b
 * you get "  <c>".
Packit ae235b
 *
Packit ae235b
 * This function uses a different algorithm (called DFA, i.e. deterministic
Packit ae235b
 * finite automaton), so it can retrieve all the possible matches, all
Packit ae235b
 * starting at the same point in the string. For instance matching
Packit ae235b
 * "  <c>" against the pattern "<.*>;"
Packit ae235b
 * you would obtain three matches: "  <c>",
Packit ae235b
 * " " and "".
Packit ae235b
 *
Packit ae235b
 * The number of matched strings is retrieved using
Packit ae235b
 * g_match_info_get_match_count(). To obtain the matched strings and
Packit ae235b
 * their position you can use, respectively, g_match_info_fetch() and
Packit ae235b
 * g_match_info_fetch_pos(). Note that the strings are returned in
Packit ae235b
 * reverse order of length; that is, the longest matching string is
Packit ae235b
 * given first.
Packit ae235b
 *
Packit ae235b
 * Note that the DFA algorithm is slower than the standard one and it
Packit ae235b
 * is not able to capture substrings, so backreferences do not work.
Packit ae235b
 *
Packit ae235b
 * Setting @start_position differs from just passing over a shortened
Packit ae235b
 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
Packit ae235b
 * that begins with any kind of lookbehind assertion, such as "\b".
Packit ae235b
 *
Packit ae235b
 * A #GMatchInfo structure, used to get information on the match, is
Packit ae235b
 * stored in @match_info if not %NULL. Note that if @match_info is
Packit ae235b
 * not %NULL then it is created even if the function returns %FALSE,
Packit ae235b
 * i.e. you must free it regardless if regular expression actually
Packit ae235b
 * matched.
Packit ae235b
 *
Packit ae235b
 * @string is not copied and is used in #GMatchInfo internally. If
Packit ae235b
 * you use any #GMatchInfo method (except g_match_info_free()) after
Packit ae235b
 * freeing or modifying @string then the behaviour is undefined.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE is the string matched, %FALSE otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_regex_match_all_full (const GRegex      *regex,
Packit ae235b
                        const gchar       *string,
Packit ae235b
                        gssize             string_len,
Packit ae235b
                        gint               start_position,
Packit ae235b
                        GRegexMatchFlags   match_options,
Packit ae235b
                        GMatchInfo       **match_info,
Packit ae235b
                        GError           **error)
Packit ae235b
{
Packit ae235b
  GMatchInfo *info;
Packit ae235b
  gboolean done;
Packit ae235b
  pcre *pcre_re;
Packit ae235b
  pcre_extra *extra;
Packit ae235b
  gboolean retval;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (regex != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (string != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (start_position >= 0, FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
  g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
Packit ae235b
Packit ae235b
#ifdef PCRE_NO_AUTO_POSSESS
Packit ae235b
  /* For PCRE >= 8.34 we need to turn off PCRE_NO_AUTO_POSSESS, which
Packit ae235b
   * is an optimization for normal regex matching, but results in omitting
Packit ae235b
   * some shorter matches here, and an observable behaviour change.
Packit ae235b
   *
Packit ae235b
   * DFA matching is rather niche, and very rarely used according to
Packit ae235b
   * codesearch.debian.net, so don't bother caching the recompiled RE. */
Packit ae235b
  pcre_re = regex_compile (regex->pattern,
Packit ae235b
                           regex->compile_opts | PCRE_NO_AUTO_POSSESS,
Packit ae235b
                           NULL, NULL, error);
Packit ae235b
Packit ae235b
  if (pcre_re == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  /* Not bothering to cache the optimization data either, with similar
Packit ae235b
   * reasoning */
Packit ae235b
  extra = NULL;
Packit ae235b
#else
Packit ae235b
  /* For PCRE < 8.33 the precompiled regex is fine. */
Packit ae235b
  pcre_re = regex->pcre_re;
Packit ae235b
  extra = regex->extra;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  info = match_info_new (regex, string, string_len, start_position,
Packit ae235b
                         match_options, TRUE);
Packit ae235b
Packit ae235b
  done = FALSE;
Packit ae235b
  while (!done)
Packit ae235b
    {
Packit ae235b
      done = TRUE;
Packit ae235b
      info->matches = pcre_dfa_exec (pcre_re, extra,
Packit ae235b
                                     info->string, info->string_len,
Packit ae235b
                                     info->pos,
Packit ae235b
                                     regex->match_opts | match_options,
Packit ae235b
                                     info->offsets, info->n_offsets,
Packit ae235b
                                     info->workspace, info->n_workspace);
Packit ae235b
      if (info->matches == PCRE_ERROR_DFA_WSSIZE)
Packit ae235b
        {
Packit ae235b
          /* info->workspace is too small. */
Packit ae235b
          info->n_workspace *= 2;
Packit ae235b
          info->workspace = g_realloc (info->workspace,
Packit ae235b
                                       info->n_workspace * sizeof (gint));
Packit ae235b
          done = FALSE;
Packit ae235b
        }
Packit ae235b
      else if (info->matches == 0)
Packit ae235b
        {
Packit ae235b
          /* info->offsets is too small. */
Packit ae235b
          info->n_offsets *= 2;
Packit ae235b
          info->offsets = g_realloc (info->offsets,
Packit ae235b
                                     info->n_offsets * sizeof (gint));
Packit ae235b
          done = FALSE;
Packit ae235b
        }
Packit ae235b
      else if (IS_PCRE_ERROR (info->matches))
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
Packit ae235b
                       _("Error while matching regular expression %s: %s"),
Packit ae235b
                       regex->pattern, match_error (info->matches));
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef PCRE_NO_AUTO_POSSESS
Packit ae235b
  pcre_free (pcre_re);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  /* set info->pos to -1 so that a call to g_match_info_next() fails. */
Packit ae235b
  info->pos = -1;
Packit ae235b
  retval = info->matches >= 0;
Packit ae235b
Packit ae235b
  if (match_info != NULL)
Packit ae235b
    *match_info = info;
Packit ae235b
  else
Packit ae235b
    g_match_info_free (info);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_get_string_number:
Packit ae235b
 * @regex: #GRegex structure
Packit ae235b
 * @name: name of the subexpression
Packit ae235b
 *
Packit ae235b
 * Retrieves the number of the subexpression named @name.
Packit ae235b
 *
Packit ae235b
 * Returns: The number of the subexpression or -1 if @name
Packit ae235b
 *   does not exists
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_regex_get_string_number (const GRegex *regex,
Packit ae235b
                           const gchar  *name)
Packit ae235b
{
Packit ae235b
  gint num;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (regex != NULL, -1);
Packit ae235b
  g_return_val_if_fail (name != NULL, -1);
Packit ae235b
Packit ae235b
  num = pcre_get_stringnumber (regex->pcre_re, name);
Packit ae235b
  if (num == PCRE_ERROR_NOSUBSTRING)
Packit ae235b
    num = -1;
Packit ae235b
Packit ae235b
  return num;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_split_simple:
Packit ae235b
 * @pattern: the regular expression
Packit ae235b
 * @string: the string to scan for matches
Packit ae235b
 * @compile_options: compile options for the regular expression, or 0
Packit ae235b
 * @match_options: match options, or 0
Packit ae235b
 *
Packit ae235b
 * Breaks the string on the pattern, and returns an array of
Packit ae235b
 * the tokens. If the pattern contains capturing parentheses,
Packit ae235b
 * then the text for each of the substrings will also be returned.
Packit ae235b
 * If the pattern does not match anywhere in the string, then the
Packit ae235b
 * whole string is returned as the first token.
Packit ae235b
 *
Packit ae235b
 * This function is equivalent to g_regex_split() but it does
Packit ae235b
 * not require to compile the pattern with g_regex_new(), avoiding
Packit ae235b
 * some lines of code when you need just to do a split without
Packit ae235b
 * extracting substrings, capture counts, and so on.
Packit ae235b
 *
Packit ae235b
 * If this function is to be called on the same @pattern more than
Packit ae235b
 * once, it's more efficient to compile the pattern once with
Packit ae235b
 * g_regex_new() and then use g_regex_split().
Packit ae235b
 *
Packit ae235b
 * As a special case, the result of splitting the empty string ""
Packit ae235b
 * is an empty vector, not a vector containing a single string.
Packit ae235b
 * The reason for this special case is that being able to represent
Packit ae235b
 * a empty vector is typically more useful than consistent handling
Packit ae235b
 * of empty elements. If you do need to represent empty elements,
Packit ae235b
 * you'll need to check for the empty string before calling this
Packit ae235b
 * function.
Packit ae235b
 *
Packit ae235b
 * A pattern that can match empty strings splits @string into
Packit ae235b
 * separate characters wherever it matches the empty string between
Packit ae235b
 * characters. For example splitting "ab c" using as a separator
Packit ae235b
 * "\s*", you will get "a", "b" and "c".
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a %NULL-terminated array of strings. Free
Packit ae235b
 * it using g_strfreev()
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
g_regex_split_simple (const gchar        *pattern,
Packit ae235b
                      const gchar        *string,
Packit ae235b
                      GRegexCompileFlags  compile_options,
Packit ae235b
                      GRegexMatchFlags    match_options)
Packit ae235b
{
Packit ae235b
  GRegex *regex;
Packit ae235b
  gchar **result;
Packit ae235b
Packit ae235b
  regex = g_regex_new (pattern, compile_options, 0, NULL);
Packit ae235b
  if (!regex)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
Packit ae235b
  g_regex_unref (regex);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_split:
Packit ae235b
 * @regex: a #GRegex structure
Packit ae235b
 * @string: the string to split with the pattern
Packit ae235b
 * @match_options: match time option flags
Packit ae235b
 *
Packit ae235b
 * Breaks the string on the pattern, and returns an array of the tokens.
Packit ae235b
 * If the pattern contains capturing parentheses, then the text for each
Packit ae235b
 * of the substrings will also be returned. If the pattern does not match
Packit ae235b
 * anywhere in the string, then the whole string is returned as the first
Packit ae235b
 * token.
Packit ae235b
 *
Packit ae235b
 * As a special case, the result of splitting the empty string "" is an
Packit ae235b
 * empty vector, not a vector containing a single string. The reason for
Packit ae235b
 * this special case is that being able to represent a empty vector is
Packit ae235b
 * typically more useful than consistent handling of empty elements. If
Packit ae235b
 * you do need to represent empty elements, you'll need to check for the
Packit ae235b
 * empty string before calling this function.
Packit ae235b
 *
Packit ae235b
 * A pattern that can match empty strings splits @string into separate
Packit ae235b
 * characters wherever it matches the empty string between characters.
Packit ae235b
 * For example splitting "ab c" using as a separator "\s*", you will get
Packit ae235b
 * "a", "b" and "c".
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
Packit ae235b
 * it using g_strfreev()
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
g_regex_split (const GRegex     *regex,
Packit ae235b
               const gchar      *string,
Packit ae235b
               GRegexMatchFlags  match_options)
Packit ae235b
{
Packit ae235b
  return g_regex_split_full (regex, string, -1, 0,
Packit ae235b
                             match_options, 0, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_split_full:
Packit ae235b
 * @regex: a #GRegex structure
Packit ae235b
 * @string: (array length=string_len): the string to split with the pattern
Packit ae235b
 * @string_len: the length of @string, or -1 if @string is nul-terminated
Packit ae235b
 * @start_position: starting index of the string to match, in bytes
Packit ae235b
 * @match_options: match time option flags
Packit ae235b
 * @max_tokens: the maximum number of tokens to split @string into.
Packit ae235b
 *   If this is less than 1, the string is split completely
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Breaks the string on the pattern, and returns an array of the tokens.
Packit ae235b
 * If the pattern contains capturing parentheses, then the text for each
Packit ae235b
 * of the substrings will also be returned. If the pattern does not match
Packit ae235b
 * anywhere in the string, then the whole string is returned as the first
Packit ae235b
 * token.
Packit ae235b
 *
Packit ae235b
 * As a special case, the result of splitting the empty string "" is an
Packit ae235b
 * empty vector, not a vector containing a single string. The reason for
Packit ae235b
 * this special case is that being able to represent a empty vector is
Packit ae235b
 * typically more useful than consistent handling of empty elements. If
Packit ae235b
 * you do need to represent empty elements, you'll need to check for the
Packit ae235b
 * empty string before calling this function.
Packit ae235b
 *
Packit ae235b
 * A pattern that can match empty strings splits @string into separate
Packit ae235b
 * characters wherever it matches the empty string between characters.
Packit ae235b
 * For example splitting "ab c" using as a separator "\s*", you will get
Packit ae235b
 * "a", "b" and "c".
Packit ae235b
 *
Packit ae235b
 * Setting @start_position differs from just passing over a shortened
Packit ae235b
 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
Packit ae235b
 * that begins with any kind of lookbehind assertion, such as "\b".
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a %NULL-terminated gchar ** array. Free
Packit ae235b
 * it using g_strfreev()
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
g_regex_split_full (const GRegex      *regex,
Packit ae235b
                    const gchar       *string,
Packit ae235b
                    gssize             string_len,
Packit ae235b
                    gint               start_position,
Packit ae235b
                    GRegexMatchFlags   match_options,
Packit ae235b
                    gint               max_tokens,
Packit ae235b
                    GError           **error)
Packit ae235b
{
Packit ae235b
  GError *tmp_error = NULL;
Packit ae235b
  GMatchInfo *match_info;
Packit ae235b
  GList *list, *last;
Packit ae235b
  gint i;
Packit ae235b
  gint token_count;
Packit ae235b
  gboolean match_ok;
Packit ae235b
  /* position of the last separator. */
Packit ae235b
  gint last_separator_end;
Packit ae235b
  /* was the last match 0 bytes long? */
Packit ae235b
  gboolean last_match_is_empty;
Packit ae235b
  /* the returned array of char **s */
Packit ae235b
  gchar **string_list;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (regex != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (string != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (start_position >= 0, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
  g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
Packit ae235b
Packit ae235b
  if (max_tokens <= 0)
Packit ae235b
    max_tokens = G_MAXINT;
Packit ae235b
Packit ae235b
  if (string_len < 0)
Packit ae235b
    string_len = strlen (string);
Packit ae235b
Packit ae235b
  /* zero-length string */
Packit ae235b
  if (string_len - start_position == 0)
Packit ae235b
    return g_new0 (gchar *, 1);
Packit ae235b
Packit ae235b
  if (max_tokens == 1)
Packit ae235b
    {
Packit ae235b
      string_list = g_new0 (gchar *, 2);
Packit ae235b
      string_list[0] = g_strndup (&string[start_position],
Packit ae235b
                                  string_len - start_position);
Packit ae235b
      return string_list;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  list = NULL;
Packit ae235b
  token_count = 0;
Packit ae235b
  last_separator_end = start_position;
Packit ae235b
  last_match_is_empty = FALSE;
Packit ae235b
Packit ae235b
  match_ok = g_regex_match_full (regex, string, string_len, start_position,
Packit ae235b
                                 match_options, &match_info, &tmp_error);
Packit ae235b
Packit ae235b
  while (tmp_error == NULL)
Packit ae235b
    {
Packit ae235b
      if (match_ok)
Packit ae235b
        {
Packit ae235b
          last_match_is_empty =
Packit ae235b
                    (match_info->offsets[0] == match_info->offsets[1]);
Packit ae235b
Packit ae235b
          /* we need to skip empty separators at the same position of the end
Packit ae235b
           * of another separator. e.g. the string is "a b" and the separator
Packit ae235b
           * is " *", so from 1 to 2 we have a match and at position 2 we have
Packit ae235b
           * an empty match. */
Packit ae235b
          if (last_separator_end != match_info->offsets[1])
Packit ae235b
            {
Packit ae235b
              gchar *token;
Packit ae235b
              gint match_count;
Packit ae235b
Packit ae235b
              token = g_strndup (string + last_separator_end,
Packit ae235b
                                 match_info->offsets[0] - last_separator_end);
Packit ae235b
              list = g_list_prepend (list, token);
Packit ae235b
              token_count++;
Packit ae235b
Packit ae235b
              /* if there were substrings, these need to be added to
Packit ae235b
               * the list. */
Packit ae235b
              match_count = g_match_info_get_match_count (match_info);
Packit ae235b
              if (match_count > 1)
Packit ae235b
                {
Packit ae235b
                  for (i = 1; i < match_count; i++)
Packit ae235b
                    list = g_list_prepend (list, g_match_info_fetch (match_info, i));
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* if there was no match, copy to end of string. */
Packit ae235b
          if (!last_match_is_empty)
Packit ae235b
            {
Packit ae235b
              gchar *token = g_strndup (string + last_separator_end,
Packit ae235b
                                        match_info->string_len - last_separator_end);
Packit ae235b
              list = g_list_prepend (list, token);
Packit ae235b
            }
Packit ae235b
          /* no more tokens, end the loop. */
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* -1 to leave room for the last part. */
Packit ae235b
      if (token_count >= max_tokens - 1)
Packit ae235b
        {
Packit ae235b
          /* we have reached the maximum number of tokens, so we copy
Packit ae235b
           * the remaining part of the string. */
Packit ae235b
          if (last_match_is_empty)
Packit ae235b
            {
Packit ae235b
              /* the last match was empty, so we have moved one char
Packit ae235b
               * after the real position to avoid empty matches at the
Packit ae235b
               * same position. */
Packit ae235b
              match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
Packit ae235b
            }
Packit ae235b
          /* the if is needed in the case we have terminated the available
Packit ae235b
           * tokens, but we are at the end of the string, so there are no
Packit ae235b
           * characters left to copy. */
Packit ae235b
          if (string_len > match_info->pos)
Packit ae235b
            {
Packit ae235b
              gchar *token = g_strndup (string + match_info->pos,
Packit ae235b
                                        string_len - match_info->pos);
Packit ae235b
              list = g_list_prepend (list, token);
Packit ae235b
            }
Packit ae235b
          /* end the loop. */
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      last_separator_end = match_info->pos;
Packit ae235b
      if (last_match_is_empty)
Packit ae235b
        /* if the last match was empty, g_match_info_next() has moved
Packit ae235b
         * forward to avoid infinite loops, but we still need to copy that
Packit ae235b
         * character. */
Packit ae235b
        last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
Packit ae235b
Packit ae235b
      match_ok = g_match_info_next (match_info, &tmp_error);
Packit ae235b
    }
Packit ae235b
  g_match_info_free (match_info);
Packit ae235b
  if (tmp_error != NULL)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, tmp_error);
Packit ae235b
      g_list_free_full (list, g_free);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  string_list = g_new (gchar *, g_list_length (list) + 1);
Packit ae235b
  i = 0;
Packit ae235b
  for (last = g_list_last (list); last; last = g_list_previous (last))
Packit ae235b
    string_list[i++] = last->data;
Packit ae235b
  string_list[i] = NULL;
Packit ae235b
  g_list_free (list);
Packit ae235b
Packit ae235b
  return string_list;
Packit ae235b
}
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  REPL_TYPE_STRING,
Packit ae235b
  REPL_TYPE_CHARACTER,
Packit ae235b
  REPL_TYPE_SYMBOLIC_REFERENCE,
Packit ae235b
  REPL_TYPE_NUMERIC_REFERENCE,
Packit ae235b
  REPL_TYPE_CHANGE_CASE
Packit ae235b
};
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  CHANGE_CASE_NONE         = 1 << 0,
Packit ae235b
  CHANGE_CASE_UPPER        = 1 << 1,
Packit ae235b
  CHANGE_CASE_LOWER        = 1 << 2,
Packit ae235b
  CHANGE_CASE_UPPER_SINGLE = 1 << 3,
Packit ae235b
  CHANGE_CASE_LOWER_SINGLE = 1 << 4,
Packit ae235b
  CHANGE_CASE_SINGLE_MASK  = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
Packit ae235b
  CHANGE_CASE_LOWER_MASK   = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
Packit ae235b
  CHANGE_CASE_UPPER_MASK   = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
Packit ae235b
} ChangeCase;
Packit ae235b
Packit ae235b
struct _InterpolationData
Packit ae235b
{
Packit ae235b
  gchar     *text;
Packit ae235b
  gint       type;
Packit ae235b
  gint       num;
Packit ae235b
  gchar      c;
Packit ae235b
  ChangeCase change_case;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_interpolation_data (InterpolationData *data)
Packit ae235b
{
Packit ae235b
  g_free (data->text);
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static const gchar *
Packit ae235b
expand_escape (const gchar        *replacement,
Packit ae235b
               const gchar        *p,
Packit ae235b
               InterpolationData  *data,
Packit ae235b
               GError            **error)
Packit ae235b
{
Packit ae235b
  const gchar *q, *r;
Packit ae235b
  gint x, d, h, i;
Packit ae235b
  const gchar *error_detail;
Packit ae235b
  gint base = 0;
Packit ae235b
  GError *tmp_error = NULL;
Packit ae235b
Packit ae235b
  p++;
Packit ae235b
  switch (*p)
Packit ae235b
    {
Packit ae235b
    case 't':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\t';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case 'n':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\n';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case 'v':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\v';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case 'r':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\r';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case 'f':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\f';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case 'a':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\a';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case 'b':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\b';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case '\\':
Packit ae235b
      p++;
Packit ae235b
      data->c = '\\';
Packit ae235b
      data->type = REPL_TYPE_CHARACTER;
Packit ae235b
      break;
Packit ae235b
    case 'x':
Packit ae235b
      p++;
Packit ae235b
      x = 0;
Packit ae235b
      if (*p == '{')
Packit ae235b
        {
Packit ae235b
          p++;
Packit ae235b
          do
Packit ae235b
            {
Packit ae235b
              h = g_ascii_xdigit_value (*p);
Packit ae235b
              if (h < 0)
Packit ae235b
                {
Packit ae235b
                  error_detail = _("hexadecimal digit or “}” expected");
Packit ae235b
                  goto error;
Packit ae235b
                }
Packit ae235b
              x = x * 16 + h;
Packit ae235b
              p++;
Packit ae235b
            }
Packit ae235b
          while (*p != '}');
Packit ae235b
          p++;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          for (i = 0; i < 2; i++)
Packit ae235b
            {
Packit ae235b
              h = g_ascii_xdigit_value (*p);
Packit ae235b
              if (h < 0)
Packit ae235b
                {
Packit ae235b
                  error_detail = _("hexadecimal digit expected");
Packit ae235b
                  goto error;
Packit ae235b
                }
Packit ae235b
              x = x * 16 + h;
Packit ae235b
              p++;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      data->type = REPL_TYPE_STRING;
Packit ae235b
      data->text = g_new0 (gchar, 8);
Packit ae235b
      g_unichar_to_utf8 (x, data->text);
Packit ae235b
      break;
Packit ae235b
    case 'l':
Packit ae235b
      p++;
Packit ae235b
      data->type = REPL_TYPE_CHANGE_CASE;
Packit ae235b
      data->change_case = CHANGE_CASE_LOWER_SINGLE;
Packit ae235b
      break;
Packit ae235b
    case 'u':
Packit ae235b
      p++;
Packit ae235b
      data->type = REPL_TYPE_CHANGE_CASE;
Packit ae235b
      data->change_case = CHANGE_CASE_UPPER_SINGLE;
Packit ae235b
      break;
Packit ae235b
    case 'L':
Packit ae235b
      p++;
Packit ae235b
      data->type = REPL_TYPE_CHANGE_CASE;
Packit ae235b
      data->change_case = CHANGE_CASE_LOWER;
Packit ae235b
      break;
Packit ae235b
    case 'U':
Packit ae235b
      p++;
Packit ae235b
      data->type = REPL_TYPE_CHANGE_CASE;
Packit ae235b
      data->change_case = CHANGE_CASE_UPPER;
Packit ae235b
      break;
Packit ae235b
    case 'E':
Packit ae235b
      p++;
Packit ae235b
      data->type = REPL_TYPE_CHANGE_CASE;
Packit ae235b
      data->change_case = CHANGE_CASE_NONE;
Packit ae235b
      break;
Packit ae235b
    case 'g':
Packit ae235b
      p++;
Packit ae235b
      if (*p != '<')
Packit ae235b
        {
Packit ae235b
          error_detail = _("missing “<” in symbolic reference");
Packit ae235b
          goto error;
Packit ae235b
        }
Packit ae235b
      q = p + 1;
Packit ae235b
      do
Packit ae235b
        {
Packit ae235b
          p++;
Packit ae235b
          if (!*p)
Packit ae235b
            {
Packit ae235b
              error_detail = _("unfinished symbolic reference");
Packit ae235b
              goto error;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      while (*p != '>');
Packit ae235b
      if (p - q == 0)
Packit ae235b
        {
Packit ae235b
          error_detail = _("zero-length symbolic reference");
Packit ae235b
          goto error;
Packit ae235b
        }
Packit ae235b
      if (g_ascii_isdigit (*q))
Packit ae235b
        {
Packit ae235b
          x = 0;
Packit ae235b
          do
Packit ae235b
            {
Packit ae235b
              h = g_ascii_digit_value (*q);
Packit ae235b
              if (h < 0)
Packit ae235b
                {
Packit ae235b
                  error_detail = _("digit expected");
Packit ae235b
                  p = q;
Packit ae235b
                  goto error;
Packit ae235b
                }
Packit ae235b
              x = x * 10 + h;
Packit ae235b
              q++;
Packit ae235b
            }
Packit ae235b
          while (q != p);
Packit ae235b
          data->num = x;
Packit ae235b
          data->type = REPL_TYPE_NUMERIC_REFERENCE;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          r = q;
Packit ae235b
          do
Packit ae235b
            {
Packit ae235b
              if (!g_ascii_isalnum (*r))
Packit ae235b
                {
Packit ae235b
                  error_detail = _("illegal symbolic reference");
Packit ae235b
                  p = r;
Packit ae235b
                  goto error;
Packit ae235b
                }
Packit ae235b
              r++;
Packit ae235b
            }
Packit ae235b
          while (r != p);
Packit ae235b
          data->text = g_strndup (q, p - q);
Packit ae235b
          data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
Packit ae235b
        }
Packit ae235b
      p++;
Packit ae235b
      break;
Packit ae235b
    case '0':
Packit ae235b
      /* if \0 is followed by a number is an octal number representing a
Packit ae235b
       * character, else it is a numeric reference. */
Packit ae235b
      if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
Packit ae235b
        {
Packit ae235b
          base = 8;
Packit ae235b
          p = g_utf8_next_char (p);
Packit ae235b
        }
Packit ae235b
    case '1':
Packit ae235b
    case '2':
Packit ae235b
    case '3':
Packit ae235b
    case '4':
Packit ae235b
    case '5':
Packit ae235b
    case '6':
Packit ae235b
    case '7':
Packit ae235b
    case '8':
Packit ae235b
    case '9':
Packit ae235b
      x = 0;
Packit ae235b
      d = 0;
Packit ae235b
      for (i = 0; i < 3; i++)
Packit ae235b
        {
Packit ae235b
          h = g_ascii_digit_value (*p);
Packit ae235b
          if (h < 0)
Packit ae235b
            break;
Packit ae235b
          if (h > 7)
Packit ae235b
            {
Packit ae235b
              if (base == 8)
Packit ae235b
                break;
Packit ae235b
              else
Packit ae235b
                base = 10;
Packit ae235b
            }
Packit ae235b
          if (i == 2 && base == 10)
Packit ae235b
            break;
Packit ae235b
          x = x * 8 + h;
Packit ae235b
          d = d * 10 + h;
Packit ae235b
          p++;
Packit ae235b
        }
Packit ae235b
      if (base == 8 || i == 3)
Packit ae235b
        {
Packit ae235b
          data->type = REPL_TYPE_STRING;
Packit ae235b
          data->text = g_new0 (gchar, 8);
Packit ae235b
          g_unichar_to_utf8 (x, data->text);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          data->type = REPL_TYPE_NUMERIC_REFERENCE;
Packit ae235b
          data->num = d;
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
    case 0:
Packit ae235b
      error_detail = _("stray final “\\”");
Packit ae235b
      goto error;
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      error_detail = _("unknown escape sequence");
Packit ae235b
      goto error;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return p;
Packit ae235b
Packit ae235b
 error:
Packit ae235b
  /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
Packit ae235b
  tmp_error = g_error_new (G_REGEX_ERROR,
Packit ae235b
                           G_REGEX_ERROR_REPLACE,
Packit ae235b
                           _("Error while parsing replacement "
Packit ae235b
                             "text “%s” at char %lu: %s"),
Packit ae235b
                           replacement,
Packit ae235b
                           (gulong)(p - replacement),
Packit ae235b
                           error_detail);
Packit ae235b
  g_propagate_error (error, tmp_error);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GList *
Packit ae235b
split_replacement (const gchar  *replacement,
Packit ae235b
                   GError      **error)
Packit ae235b
{
Packit ae235b
  GList *list = NULL;
Packit ae235b
  InterpolationData *data;
Packit ae235b
  const gchar *p, *start;
Packit ae235b
Packit ae235b
  start = p = replacement;
Packit ae235b
  while (*p)
Packit ae235b
    {
Packit ae235b
      if (*p == '\\')
Packit ae235b
        {
Packit ae235b
          data = g_new0 (InterpolationData, 1);
Packit ae235b
          start = p = expand_escape (replacement, p, data, error);
Packit ae235b
          if (p == NULL)
Packit ae235b
            {
Packit ae235b
              g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
Packit ae235b
              free_interpolation_data (data);
Packit ae235b
Packit ae235b
              return NULL;
Packit ae235b
            }
Packit ae235b
          list = g_list_prepend (list, data);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          p++;
Packit ae235b
          if (*p == '\\' || *p == '\0')
Packit ae235b
            {
Packit ae235b
              if (p - start > 0)
Packit ae235b
                {
Packit ae235b
                  data = g_new0 (InterpolationData, 1);
Packit ae235b
                  data->text = g_strndup (start, p - start);
Packit ae235b
                  data->type = REPL_TYPE_STRING;
Packit ae235b
                  list = g_list_prepend (list, data);
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_list_reverse (list);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Change the case of c based on change_case. */
Packit ae235b
#define CHANGE_CASE(c, change_case) \
Packit ae235b
        (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
Packit ae235b
                g_unichar_tolower (c) : \
Packit ae235b
                g_unichar_toupper (c))
Packit ae235b
Packit ae235b
static void
Packit ae235b
string_append (GString     *string,
Packit ae235b
               const gchar *text,
Packit ae235b
               ChangeCase  *change_case)
Packit ae235b
{
Packit ae235b
  gunichar c;
Packit ae235b
Packit ae235b
  if (text[0] == '\0')
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (*change_case == CHANGE_CASE_NONE)
Packit ae235b
    {
Packit ae235b
      g_string_append (string, text);
Packit ae235b
    }
Packit ae235b
  else if (*change_case & CHANGE_CASE_SINGLE_MASK)
Packit ae235b
    {
Packit ae235b
      c = g_utf8_get_char (text);
Packit ae235b
      g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
Packit ae235b
      g_string_append (string, g_utf8_next_char (text));
Packit ae235b
      *change_case = CHANGE_CASE_NONE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      while (*text != '\0')
Packit ae235b
        {
Packit ae235b
          c = g_utf8_get_char (text);
Packit ae235b
          g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
Packit ae235b
          text = g_utf8_next_char (text);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
interpolate_replacement (const GMatchInfo *match_info,
Packit ae235b
                         GString          *result,
Packit ae235b
                         gpointer          data)
Packit ae235b
{
Packit ae235b
  GList *list;
Packit ae235b
  InterpolationData *idata;
Packit ae235b
  gchar *match;
Packit ae235b
  ChangeCase change_case = CHANGE_CASE_NONE;
Packit ae235b
Packit ae235b
  for (list = data; list; list = list->next)
Packit ae235b
    {
Packit ae235b
      idata = list->data;
Packit ae235b
      switch (idata->type)
Packit ae235b
        {
Packit ae235b
        case REPL_TYPE_STRING:
Packit ae235b
          string_append (result, idata->text, &change_case);
Packit ae235b
          break;
Packit ae235b
        case REPL_TYPE_CHARACTER:
Packit ae235b
          g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
Packit ae235b
          if (change_case & CHANGE_CASE_SINGLE_MASK)
Packit ae235b
            change_case = CHANGE_CASE_NONE;
Packit ae235b
          break;
Packit ae235b
        case REPL_TYPE_NUMERIC_REFERENCE:
Packit ae235b
          match = g_match_info_fetch (match_info, idata->num);
Packit ae235b
          if (match)
Packit ae235b
            {
Packit ae235b
              string_append (result, match, &change_case);
Packit ae235b
              g_free (match);
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
        case REPL_TYPE_SYMBOLIC_REFERENCE:
Packit ae235b
          match = g_match_info_fetch_named (match_info, idata->text);
Packit ae235b
          if (match)
Packit ae235b
            {
Packit ae235b
              string_append (result, match, &change_case);
Packit ae235b
              g_free (match);
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
        case REPL_TYPE_CHANGE_CASE:
Packit ae235b
          change_case = idata->change_case;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* whether actual match_info is needed for replacement, i.e.
Packit ae235b
 * whether there are references
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
interpolation_list_needs_match (GList *list)
Packit ae235b
{
Packit ae235b
  while (list != NULL)
Packit ae235b
    {
Packit ae235b
      InterpolationData *data = list->data;
Packit ae235b
Packit ae235b
      if (data->type == REPL_TYPE_SYMBOLIC_REFERENCE ||
Packit ae235b
          data->type == REPL_TYPE_NUMERIC_REFERENCE)
Packit ae235b
        {
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      list = list->next;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_replace:
Packit ae235b
 * @regex: a #GRegex structure
Packit ae235b
 * @string: (array length=string_len): the string to perform matches against
Packit ae235b
 * @string_len: the length of @string, or -1 if @string is nul-terminated
Packit ae235b
 * @start_position: starting index of the string to match, in bytes
Packit ae235b
 * @replacement: text to replace each match with
Packit ae235b
 * @match_options: options for the match
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore errors
Packit ae235b
 *
Packit ae235b
 * Replaces all occurrences of the pattern in @regex with the
Packit ae235b
 * replacement text. Backreferences of the form '\number' or
Packit ae235b
 * '\g<number>' in the replacement text are interpolated by the
Packit ae235b
 * number-th captured subexpression of the match, '\g<name>' refers
Packit ae235b
 * to the captured subexpression with the given name. '\0' refers
Packit ae235b
 * to the complete match, but '\0' followed by a number is the octal
Packit ae235b
 * representation of a character. To include a literal '\' in the
Packit ae235b
 * replacement, write '\\\\'.
Packit ae235b
 *
Packit ae235b
 * There are also escapes that changes the case of the following text:
Packit ae235b
 *
Packit ae235b
 * - \l: Convert to lower case the next character
Packit ae235b
 * - \u: Convert to upper case the next character
Packit ae235b
 * - \L: Convert to lower case till \E
Packit ae235b
 * - \U: Convert to upper case till \E
Packit ae235b
 * - \E: End case modification
Packit ae235b
 *
Packit ae235b
 * If you do not need to use backreferences use g_regex_replace_literal().
Packit ae235b
 *
Packit ae235b
 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was
Packit ae235b
 * passed to g_regex_new(). If you want to use not UTF-8 encoded stings
Packit ae235b
 * you can use g_regex_replace_literal().
Packit ae235b
 *
Packit ae235b
 * Setting @start_position differs from just passing over a shortened
Packit ae235b
 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that
Packit ae235b
 * begins with any kind of lookbehind assertion, such as "\b".
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated string containing the replacements
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_regex_replace (const GRegex      *regex,
Packit ae235b
                 const gchar       *string,
Packit ae235b
                 gssize             string_len,
Packit ae235b
                 gint               start_position,
Packit ae235b
                 const gchar       *replacement,
Packit ae235b
                 GRegexMatchFlags   match_options,
Packit ae235b
                 GError           **error)
Packit ae235b
{
Packit ae235b
  gchar *result;
Packit ae235b
  GList *list;
Packit ae235b
  GError *tmp_error = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (regex != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (string != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (start_position >= 0, NULL);
Packit ae235b
  g_return_val_if_fail (replacement != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
  g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
Packit ae235b
Packit ae235b
  list = split_replacement (replacement, &tmp_error);
Packit ae235b
  if (tmp_error != NULL)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, tmp_error);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = g_regex_replace_eval (regex,
Packit ae235b
                                 string, string_len, start_position,
Packit ae235b
                                 match_options,
Packit ae235b
                                 interpolate_replacement,
Packit ae235b
                                 (gpointer)list,
Packit ae235b
                                 &tmp_error);
Packit ae235b
  if (tmp_error != NULL)
Packit ae235b
    g_propagate_error (error, tmp_error);
Packit ae235b
Packit ae235b
  g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
literal_replacement (const GMatchInfo *match_info,
Packit ae235b
                     GString          *result,
Packit ae235b
                     gpointer          data)
Packit ae235b
{
Packit ae235b
  g_string_append (result, data);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_replace_literal:
Packit ae235b
 * @regex: a #GRegex structure
Packit ae235b
 * @string: (array length=string_len): the string to perform matches against
Packit ae235b
 * @string_len: the length of @string, or -1 if @string is nul-terminated
Packit ae235b
 * @start_position: starting index of the string to match, in bytes
Packit ae235b
 * @replacement: text to replace each match with
Packit ae235b
 * @match_options: options for the match
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore errors
Packit ae235b
 *
Packit ae235b
 * Replaces all occurrences of the pattern in @regex with the
Packit ae235b
 * replacement text. @replacement is replaced literally, to
Packit ae235b
 * include backreferences use g_regex_replace().
Packit ae235b
 *
Packit ae235b
 * Setting @start_position differs from just passing over a
Packit ae235b
 * shortened string and setting #G_REGEX_MATCH_NOTBOL in the
Packit ae235b
 * case of a pattern that begins with any kind of lookbehind
Packit ae235b
 * assertion, such as "\b".
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated string containing the replacements
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_regex_replace_literal (const GRegex      *regex,
Packit ae235b
                         const gchar       *string,
Packit ae235b
                         gssize             string_len,
Packit ae235b
                         gint               start_position,
Packit ae235b
                         const gchar       *replacement,
Packit ae235b
                         GRegexMatchFlags   match_options,
Packit ae235b
                         GError           **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (replacement != NULL, NULL);
Packit ae235b
  g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
Packit ae235b
Packit ae235b
  return g_regex_replace_eval (regex,
Packit ae235b
                               string, string_len, start_position,
Packit ae235b
                               match_options,
Packit ae235b
                               literal_replacement,
Packit ae235b
                               (gpointer)replacement,
Packit ae235b
                               error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_replace_eval:
Packit ae235b
 * @regex: a #GRegex structure from g_regex_new()
Packit ae235b
 * @string: (array length=string_len): string to perform matches against
Packit ae235b
 * @string_len: the length of @string, or -1 if @string is nul-terminated
Packit ae235b
 * @start_position: starting index of the string to match, in bytes
Packit ae235b
 * @match_options: options for the match
Packit ae235b
 * @eval: a function to call for each match
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore errors
Packit ae235b
 *
Packit ae235b
 * Replaces occurrences of the pattern in regex with the output of
Packit ae235b
 * @eval for that occurrence.
Packit ae235b
 *
Packit ae235b
 * Setting @start_position differs from just passing over a shortened
Packit ae235b
 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern
Packit ae235b
 * that begins with any kind of lookbehind assertion, such as "\b".
Packit ae235b
 *
Packit ae235b
 * The following example uses g_regex_replace_eval() to replace multiple
Packit ae235b
 * strings at once:
Packit ae235b
 * |[ 
Packit ae235b
 * static gboolean
Packit ae235b
 * eval_cb (const GMatchInfo *info,
Packit ae235b
 *          GString          *res,
Packit ae235b
 *          gpointer          data)
Packit ae235b
 * {
Packit ae235b
 *   gchar *match;
Packit ae235b
 *   gchar *r;
Packit ae235b
 *
Packit ae235b
 *    match = g_match_info_fetch (info, 0);
Packit ae235b
 *    r = g_hash_table_lookup ((GHashTable *)data, match);
Packit ae235b
 *    g_string_append (res, r);
Packit ae235b
 *    g_free (match);
Packit ae235b
 *
Packit ae235b
 *    return FALSE;
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * ...
Packit ae235b
 *
Packit ae235b
 * GRegex *reg;
Packit ae235b
 * GHashTable *h;
Packit ae235b
 * gchar *res;
Packit ae235b
 *
Packit ae235b
 * h = g_hash_table_new (g_str_hash, g_str_equal);
Packit ae235b
 *
Packit ae235b
 * g_hash_table_insert (h, "1", "ONE");
Packit ae235b
 * g_hash_table_insert (h, "2", "TWO");
Packit ae235b
 * g_hash_table_insert (h, "3", "THREE");
Packit ae235b
 * g_hash_table_insert (h, "4", "FOUR");
Packit ae235b
 *
Packit ae235b
 * reg = g_regex_new ("1|2|3|4", 0, 0, NULL);
Packit ae235b
 * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL);
Packit ae235b
 * g_hash_table_destroy (h);
Packit ae235b
 *
Packit ae235b
 * ...
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated string containing the replacements
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_regex_replace_eval (const GRegex        *regex,
Packit ae235b
                      const gchar         *string,
Packit ae235b
                      gssize               string_len,
Packit ae235b
                      gint                 start_position,
Packit ae235b
                      GRegexMatchFlags     match_options,
Packit ae235b
                      GRegexEvalCallback   eval,
Packit ae235b
                      gpointer             user_data,
Packit ae235b
                      GError             **error)
Packit ae235b
{
Packit ae235b
  GMatchInfo *match_info;
Packit ae235b
  GString *result;
Packit ae235b
  gint str_pos = 0;
Packit ae235b
  gboolean done = FALSE;
Packit ae235b
  GError *tmp_error = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (regex != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (string != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (start_position >= 0, NULL);
Packit ae235b
  g_return_val_if_fail (eval != NULL, NULL);
Packit ae235b
  g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
Packit ae235b
Packit ae235b
  if (string_len < 0)
Packit ae235b
    string_len = strlen (string);
Packit ae235b
Packit ae235b
  result = g_string_sized_new (string_len);
Packit ae235b
Packit ae235b
  /* run down the string making matches. */
Packit ae235b
  g_regex_match_full (regex, string, string_len, start_position,
Packit ae235b
                      match_options, &match_info, &tmp_error);
Packit ae235b
  while (!done && g_match_info_matches (match_info))
Packit ae235b
    {
Packit ae235b
      g_string_append_len (result,
Packit ae235b
                           string + str_pos,
Packit ae235b
                           match_info->offsets[0] - str_pos);
Packit ae235b
      done = (*eval) (match_info, result, user_data);
Packit ae235b
      str_pos = match_info->offsets[1];
Packit ae235b
      g_match_info_next (match_info, &tmp_error);
Packit ae235b
    }
Packit ae235b
  g_match_info_free (match_info);
Packit ae235b
  if (tmp_error != NULL)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, tmp_error);
Packit ae235b
      g_string_free (result, TRUE);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_string_append_len (result, string + str_pos, string_len - str_pos);
Packit ae235b
  return g_string_free (result, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_check_replacement:
Packit ae235b
 * @replacement: the replacement string
Packit ae235b
 * @has_references: (out) (optional): location to store information about
Packit ae235b
 *   references in @replacement or %NULL
Packit ae235b
 * @error: location to store error
Packit ae235b
 *
Packit ae235b
 * Checks whether @replacement is a valid replacement string
Packit ae235b
 * (see g_regex_replace()), i.e. that all escape sequences in
Packit ae235b
 * it are valid.
Packit ae235b
 *
Packit ae235b
 * If @has_references is not %NULL then @replacement is checked
Packit ae235b
 * for pattern references. For instance, replacement text 'foo\n'
Packit ae235b
 * does not contain references and may be evaluated without information
Packit ae235b
 * about actual match, but '\0\1' (whole match followed by first
Packit ae235b
 * subpattern) requires valid #GMatchInfo object.
Packit ae235b
 *
Packit ae235b
 * Returns: whether @replacement is a valid replacement string
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_regex_check_replacement (const gchar  *replacement,
Packit ae235b
                           gboolean     *has_references,
Packit ae235b
                           GError      **error)
Packit ae235b
{
Packit ae235b
  GList *list;
Packit ae235b
  GError *tmp = NULL;
Packit ae235b
Packit ae235b
  list = split_replacement (replacement, &tmp);
Packit ae235b
Packit ae235b
  if (tmp)
Packit ae235b
  {
Packit ae235b
    g_propagate_error (error, tmp);
Packit ae235b
    return FALSE;
Packit ae235b
  }
Packit ae235b
Packit ae235b
  if (has_references)
Packit ae235b
    *has_references = interpolation_list_needs_match (list);
Packit ae235b
Packit ae235b
  g_list_free_full (list, (GDestroyNotify) free_interpolation_data);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_escape_nul:
Packit ae235b
 * @string: the string to escape
Packit ae235b
 * @length: the length of @string
Packit ae235b
 *
Packit ae235b
 * Escapes the nul characters in @string to "\x00".  It can be used
Packit ae235b
 * to compile a regex with embedded nul characters.
Packit ae235b
 *
Packit ae235b
 * For completeness, @length can be -1 for a nul-terminated string.
Packit ae235b
 * In this case the output string will be of course equal to @string.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly-allocated escaped string
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_regex_escape_nul (const gchar *string,
Packit ae235b
                    gint         length)
Packit ae235b
{
Packit ae235b
  GString *escaped;
Packit ae235b
  const gchar *p, *piece_start, *end;
Packit ae235b
  gint backslashes;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (string != NULL, NULL);
Packit ae235b
Packit ae235b
  if (length < 0)
Packit ae235b
    return g_strdup (string);
Packit ae235b
Packit ae235b
  end = string + length;
Packit ae235b
  p = piece_start = string;
Packit ae235b
  escaped = g_string_sized_new (length + 1);
Packit ae235b
Packit ae235b
  backslashes = 0;
Packit ae235b
  while (p < end)
Packit ae235b
    {
Packit ae235b
      switch (*p)
Packit ae235b
        {
Packit ae235b
        case '\0':
Packit ae235b
          if (p != piece_start)
Packit ae235b
            {
Packit ae235b
              /* copy the previous piece. */
Packit ae235b
              g_string_append_len (escaped, piece_start, p - piece_start);
Packit ae235b
            }
Packit ae235b
          if ((backslashes & 1) == 0)
Packit ae235b
            g_string_append_c (escaped, '\\');
Packit ae235b
          g_string_append_c (escaped, 'x');
Packit ae235b
          g_string_append_c (escaped, '0');
Packit ae235b
          g_string_append_c (escaped, '0');
Packit ae235b
          piece_start = ++p;
Packit ae235b
          backslashes = 0;
Packit ae235b
          break;
Packit ae235b
        case '\\':
Packit ae235b
          backslashes++;
Packit ae235b
          ++p;
Packit ae235b
          break;
Packit ae235b
        default:
Packit ae235b
          backslashes = 0;
Packit ae235b
          p = g_utf8_next_char (p);
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (piece_start < end)
Packit ae235b
    g_string_append_len (escaped, piece_start, end - piece_start);
Packit ae235b
Packit ae235b
  return g_string_free (escaped, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_regex_escape_string:
Packit ae235b
 * @string: (array length=length): the string to escape
Packit ae235b
 * @length: the length of @string, or -1 if @string is nul-terminated
Packit ae235b
 *
Packit ae235b
 * Escapes the special characters used for regular expressions
Packit ae235b
 * in @string, for instance "a.b*c" becomes "a\.b\*c". This
Packit ae235b
 * function is useful to dynamically generate regular expressions.
Packit ae235b
 *
Packit ae235b
 * @string can contain nul characters that are replaced with "\0",
Packit ae235b
 * in this case remember to specify the correct length of @string
Packit ae235b
 * in @length.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly-allocated escaped string
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_regex_escape_string (const gchar *string,
Packit ae235b
                       gint         length)
Packit ae235b
{
Packit ae235b
  GString *escaped;
Packit ae235b
  const char *p, *piece_start, *end;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (string != NULL, NULL);
Packit ae235b
Packit ae235b
  if (length < 0)
Packit ae235b
    length = strlen (string);
Packit ae235b
Packit ae235b
  end = string + length;
Packit ae235b
  p = piece_start = string;
Packit ae235b
  escaped = g_string_sized_new (length + 1);
Packit ae235b
Packit ae235b
  while (p < end)
Packit ae235b
    {
Packit ae235b
      switch (*p)
Packit ae235b
        {
Packit ae235b
        case '\0':
Packit ae235b
        case '\\':
Packit ae235b
        case '|':
Packit ae235b
        case '(':
Packit ae235b
        case ')':
Packit ae235b
        case '[':
Packit ae235b
        case ']':
Packit ae235b
        case '{':
Packit ae235b
        case '}':
Packit ae235b
        case '^':
Packit ae235b
        case '$':
Packit ae235b
        case '*':
Packit ae235b
        case '+':
Packit ae235b
        case '?':
Packit ae235b
        case '.':
Packit ae235b
          if (p != piece_start)
Packit ae235b
            /* copy the previous piece. */
Packit ae235b
            g_string_append_len (escaped, piece_start, p - piece_start);
Packit ae235b
          g_string_append_c (escaped, '\\');
Packit ae235b
          if (*p == '\0')
Packit ae235b
            g_string_append_c (escaped, '0');
Packit ae235b
          else
Packit ae235b
            g_string_append_c (escaped, *p);
Packit ae235b
          piece_start = ++p;
Packit ae235b
          break;
Packit ae235b
        default:
Packit ae235b
          p = g_utf8_next_char (p);
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
  }
Packit ae235b
Packit ae235b
  if (piece_start < end)
Packit ae235b
    g_string_append_len (escaped, piece_start, end - piece_start);
Packit ae235b
Packit ae235b
  return g_string_free (escaped, FALSE);
Packit ae235b
}