[gtksourceview/wip/chergert/pcre2] pcre2: start on pcre2 implementation
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/chergert/pcre2] pcre2: start on pcre2 implementation
- Date: Fri, 25 Sep 2020 19:28:28 +0000 (UTC)
commit a045f91249371b75d59f2c3da22ff93ff907ca84
Author: Christian Hergert <chergert redhat com>
Date: Fri Sep 25 10:23:18 2020 -0700
pcre2: start on pcre2 implementation
gtksourceview/implregex-private.h | 4 +
gtksourceview/implregex.c | 399 ++++++++++++++++++++++++++++++--------
2 files changed, 324 insertions(+), 79 deletions(-)
---
diff --git a/gtksourceview/implregex-private.h b/gtksourceview/implregex-private.h
index da52474e..785d060a 100644
--- a/gtksourceview/implregex-private.h
+++ b/gtksourceview/implregex-private.h
@@ -41,6 +41,7 @@ gboolean impl_regex_match (const ImplRegex *regex,
const char *string,
GRegexMatchFlags match_options,
ImplMatchInfo **match_info);
+ImplRegex *impl_regex_ref (ImplRegex *regex);
void impl_regex_unref (ImplRegex *regex);
void impl_match_info_free (ImplMatchInfo *match_info);
char *impl_match_info_fetch (const ImplMatchInfo *match_info,
@@ -70,6 +71,9 @@ gboolean impl_match_info_fetch_named_pos (const ImplMatchInfo *match_info,
const char *name,
int *start_pos,
int *end_pos);
+gboolean impl_match_info_matches (const ImplMatchInfo *match_info);
+gboolean impl_match_info_next (ImplMatchInfo *match_info,
+ GError **error);
const char *impl_regex_get_pattern (const ImplRegex *regex);
G_END_DECLS
diff --git a/gtksourceview/implregex.c b/gtksourceview/implregex.c
index 56a12799..1c5e8408 100644
--- a/gtksourceview/implregex.c
+++ b/gtksourceview/implregex.c
@@ -21,27 +21,47 @@
#include "config.h"
+#define PCRE2_CODE_UNIT_WIDTH 8
+
+#include <pcre2.h>
+#include <string.h>
+
#include "implregex-private.h"
struct _ImplRegex
{
- int ref_count;
- char *pattern;
- GRegex *re;
+ int ref_count;
+ char *pattern;
+ pcre2_code *code;
+ GRegexMatchFlags match_options;
+ PCRE2_SPTR name_table;
+ int name_count;
+ int name_entry_size;
};
struct _ImplMatchInfo
{
- GMatchInfo *match_info;
+ ImplRegex *regex;
+ const char *string;
+ gsize string_len;
+ pcre2_match_data *match_data;
+ guint flags;
+ int n_matches;
+ gssize begin;
+ gssize end;
};
-#if 0
static void
set_regex_error (GError **error,
int errnum)
{
guchar errstr[128];
+ if (error == NULL)
+ {
+ return;
+ }
+
pcre2_get_error_message (errnum, errstr, sizeof errstr - 1);
errstr[sizeof errstr - 1] = 0;
@@ -50,15 +70,15 @@ set_regex_error (GError **error,
G_REGEX_ERROR_COMPILE,
(const gchar *)errstr);
}
-#endif
static ImplMatchInfo *
-impl_match_info_new (const ImplRegex *regex)
+impl_match_info_new (ImplRegex *regex)
{
ImplMatchInfo *match_info;
match_info = g_slice_new0 (ImplMatchInfo);
- match_info->match_info = NULL;
+ match_info->regex = impl_regex_ref (regex);
+ match_info->match_data = pcre2_match_data_create_from_pattern (regex->code, NULL);
return match_info;
}
@@ -69,22 +89,50 @@ impl_regex_new (const char *pattern,
GRegexMatchFlags match_options,
GError **error)
{
- GRegex *re;
+ pcre2_code *code;
ImplRegex *regex;
+ PCRE2_SIZE erroffset;
+ guint flags = 0;
+ int errnumber;
g_return_val_if_fail (pattern != NULL, NULL);
- re = g_regex_new (pattern, compile_options, match_options, error);
+ if (compile_options & G_REGEX_CASELESS)
+ flags |= PCRE2_CASELESS;
+
+ if (compile_options & G_REGEX_NEWLINE_LF)
+ flags |= PCRE2_NEWLINE_LF;
- if (re == NULL)
+ code = pcre2_compile ((PCRE2_SPTR)pattern,
+ PCRE2_ZERO_TERMINATED | flags,
+ 0,
+ &errnumber,
+ &erroffset,
+ NULL);
+
+ if (code == NULL)
{
+ set_regex_error (error, errnumber);
return NULL;
}
regex = g_slice_new0 (ImplRegex);
regex->ref_count = 1;
regex->pattern = g_strdup (pattern);
- regex->re = re;
+ regex->match_options = match_options;
+ regex->code = code;
+
+ (void)pcre2_pattern_info (code, PCRE2_INFO_NAMECOUNT, ®ex->name_count);
+
+ if (regex->name_count > 0)
+ {
+ (void)pcre2_pattern_info (code,
+ PCRE2_INFO_NAMEENTRYSIZE,
+ ®ex->name_entry_size);
+ (void)pcre2_pattern_info (code,
+ PCRE2_INFO_NAMETABLE,
+ ®ex->name_table);
+ }
return regex;
}
@@ -97,6 +145,17 @@ impl_regex_get_pattern (const ImplRegex *regex)
return regex->pattern;
}
+ImplRegex *
+impl_regex_ref (ImplRegex *regex)
+{
+ g_return_val_if_fail (regex != NULL, NULL);
+ g_return_val_if_fail (regex->ref_count > 0, NULL);
+
+ regex->ref_count++;
+
+ return regex;
+}
+
void
impl_regex_unref (ImplRegex *regex)
{
@@ -108,7 +167,7 @@ impl_regex_unref (ImplRegex *regex)
if (regex->ref_count == 0)
{
g_clear_pointer (®ex->pattern, g_free);
- g_clear_pointer (®ex->re, g_regex_unref);
+ g_clear_pointer (®ex->code, pcre2_code_free);
g_slice_free (ImplRegex, regex);
}
}
@@ -116,8 +175,12 @@ impl_regex_unref (ImplRegex *regex)
void
impl_match_info_free (ImplMatchInfo *match_info)
{
- g_clear_pointer (&match_info->match_info, g_match_info_free);
- g_slice_free (ImplMatchInfo, match_info);
+ if (match_info != NULL)
+ {
+ g_clear_pointer (&match_info->match_data, pcre2_match_data_free);
+ g_clear_pointer (&match_info->regex, impl_regex_unref);
+ g_slice_free (ImplMatchInfo, match_info);
+ }
}
gboolean
@@ -127,51 +190,65 @@ impl_regex_match (const ImplRegex *regex,
ImplMatchInfo **match_info)
{
g_return_val_if_fail (regex != NULL, FALSE);
- g_return_val_if_fail (regex->re != NULL, FALSE);
-
- if (match_info != NULL)
- {
- *match_info = impl_match_info_new (regex);
- }
-
- return g_regex_match (regex->re,
- string,
- match_options,
- match_info ? &(*match_info)->match_info : NULL);
+ g_return_val_if_fail (regex->code != NULL, FALSE);
+
+ return impl_regex_match_full (regex,
+ string,
+ strlen (string),
+ 0,
+ match_options,
+ match_info,
+ NULL);
}
char *
impl_match_info_fetch (const ImplMatchInfo *match_info,
int match_num)
{
+ int begin = 0;
+ int end = 0;
+
g_return_val_if_fail (match_info != NULL, NULL);
- return g_match_info_fetch (match_info->match_info, match_num);
+ if (!impl_match_info_fetch_pos (match_info, match_num, &begin, &end))
+ {
+ return NULL;
+ }
+
+ if (match_info->string == NULL)
+ {
+ return NULL;
+ }
+
+ g_assert (begin >= 0);
+ g_assert (end >= 0);
+
+ return g_strndup (match_info->string + begin, end - begin);
}
char *
impl_match_info_fetch_named (const ImplMatchInfo *match_info,
const char *name)
{
+ int begin;
+ int end;
+
g_return_val_if_fail (match_info != NULL, NULL);
- return g_match_info_fetch_named (match_info->match_info, name);
-}
+ if (!impl_match_info_fetch_named_pos (match_info, name, &begin, &end))
+ {
+ return NULL;
+ }
-static gboolean
-wrapper_eval (const GMatchInfo *match_info,
- GString *result,
- gpointer user_data)
-{
- struct {
- ImplRegexEvalCallback callback;
- gpointer user_data;
- } *wrapper = user_data;
- ImplMatchInfo wrapped = {
- .match_info = (GMatchInfo *)match_info,
- };
-
- return wrapper->callback (&wrapped, result, wrapper->user_data);
+ if (match_info->string == NULL)
+ {
+ return NULL;
+ }
+
+ g_assert (begin >= 0);
+ g_assert (end >= 0);
+
+ return g_strndup (match_info->string + begin, end - begin);
}
char *
@@ -184,25 +261,64 @@ impl_regex_replace_eval (const ImplRegex *regex,
gpointer user_data,
GError **error)
{
- struct {
- ImplRegexEvalCallback callback;
- gpointer user_data;
- } wrapper;
+ ImplMatchInfo *match_info;
+ GString *out_string;
+ gboolean done;
+ int str_pos;
g_return_val_if_fail (regex != NULL, NULL);
- g_return_val_if_fail (regex->re != NULL, NULL);
-
- wrapper.callback = eval;
- wrapper.user_data = user_data;
-
- return g_regex_replace_eval (regex->re,
- string,
- string_len,
- start_position,
- match_options,
- wrapper_eval,
- &wrapper,
- error);
+ g_return_val_if_fail (regex->code != NULL, NULL);
+
+ if (string_len < 0)
+ {
+ string_len = strlen (string);
+ }
+
+ if (start_position < 0)
+ {
+ start_position = 0;
+ }
+
+ match_info = NULL;
+
+ if (!impl_regex_match_full (regex, string, string_len, start_position, match_options, &match_info,
error))
+ {
+ impl_match_info_free (match_info);
+ return g_strndup (string, string_len);
+ }
+
+ g_assert (match_info != NULL);
+ g_assert (match_info->n_matches > 0);
+
+ str_pos = 0;
+ out_string = g_string_sized_new (string_len);
+ done = FALSE;
+
+ while (!done && impl_match_info_matches (match_info))
+ {
+ g_assert (match_info->begin >= 0);
+ g_assert (match_info->end >= 0);
+
+ g_string_append_len (out_string,
+ string + str_pos,
+ match_info->begin - str_pos);
+ str_pos = match_info->end;
+
+ done = eval (match_info, out_string, user_data);
+
+ if (!impl_match_info_next (match_info, NULL))
+ {
+ break;
+ }
+ }
+
+ g_string_append_len (out_string,
+ string + str_pos,
+ string_len - str_pos);
+
+ impl_match_info_free (match_info);
+
+ return g_string_free (out_string, FALSE);
}
gboolean
@@ -214,28 +330,64 @@ impl_regex_match_full (const ImplRegex *regex,
ImplMatchInfo **match_info,
GError **error)
{
- GMatchInfo *wrapped = NULL;
- gboolean ret;
+ ImplMatchInfo *fallback = NULL;
+ guint flags = PCRE2_NO_UTF_CHECK;
+ gboolean ret = FALSE;
+ PCRE2_SIZE *ovector;
+ int rc;
g_return_val_if_fail (regex != NULL, FALSE);
- g_return_val_if_fail (regex->re != NULL, FALSE);
+ g_return_val_if_fail (regex->code != NULL, FALSE);
+ g_return_val_if_fail (match_options == 0, FALSE);
- ret = g_regex_match_full (regex->re,
- string,
- string_len,
- start_position,
- match_options,
- &wrapped,
- error);
+ if (match_info == NULL)
+ {
+ match_info = &fallback;
+ }
- if (match_info != NULL)
+ if (string_len < 0)
{
- *match_info = g_slice_new0 (ImplMatchInfo);
- (*match_info)->match_info = wrapped;
+ string_len = strlen (string);
}
- else
+
+ *match_info = impl_match_info_new ((ImplRegex *)regex);
+
+ if ((match_options | regex->match_options) & G_REGEX_ANCHORED)
{
- g_match_info_free (wrapped);
+ flags |= PCRE2_ANCHORED;
+ }
+
+ (*match_info)->flags = flags;
+ (*match_info)->string = string;
+ (*match_info)->string_len = string_len;
+
+ rc = pcre2_match (regex->code,
+ (PCRE2_SPTR)string,
+ (PCRE2_SIZE)string_len,
+ start_position,
+ flags,
+ (*match_info)->match_data,
+ NULL);
+
+ if (rc > 0)
+ {
+ (*match_info)->n_matches = rc;
+ ret = TRUE;
+ }
+ else if (rc < 0)
+ {
+ set_regex_error (error, rc);
+ }
+
+ ovector = pcre2_get_ovector_pointer ((*match_info)->match_data);
+
+ (*match_info)->n_matches = rc;
+ (*match_info)->begin = ovector[0];
+ (*match_info)->end = ovector[1];
+
+ if (fallback != NULL)
+ {
+ impl_match_info_free (fallback);
}
return ret;
@@ -247,10 +399,26 @@ impl_match_info_fetch_pos (const ImplMatchInfo *match_info,
int *start_pos,
int *end_pos)
{
+ PCRE2_SIZE *ovector;
+
g_return_val_if_fail (match_info != NULL, FALSE);
- g_return_val_if_fail (match_info->match_info != NULL, FALSE);
+ g_return_val_if_fail (match_info->match_data != NULL, FALSE);
+ g_return_val_if_fail (start_pos != NULL, FALSE);
+ g_return_val_if_fail (end_pos != NULL, FALSE);
+
+ ovector = pcre2_get_ovector_pointer (match_info->match_data);
+
+ if (match_num >= match_info->n_matches)
+ {
+ *start_pos = -1;
+ *end_pos = -1;
+ return FALSE;
+ }
- return g_match_info_fetch_pos (match_info->match_info, match_num, start_pos, end_pos);
+ *start_pos = ovector[2*match_num];
+ *end_pos = ovector[2*match_num+1];
+
+ return TRUE;
}
gboolean
@@ -258,9 +426,82 @@ impl_match_info_fetch_named_pos (const ImplMatchInfo *match_info,
const char *name,
int *start_pos,
int *end_pos)
+{
+ PCRE2_SPTR tabptr;
+
+ g_return_val_if_fail (match_info != NULL, FALSE);
+ g_return_val_if_fail (match_info->match_data != NULL, FALSE);
+ g_return_val_if_fail (match_info->regex != NULL, FALSE);
+ g_return_val_if_fail (start_pos != NULL, FALSE);
+ g_return_val_if_fail (end_pos != NULL, FALSE);
+
+ tabptr = match_info->regex->name_table;
+
+ for (int i = 0; i < match_info->regex->name_count; i++)
+ {
+ int n = (tabptr[0] << 8) | tabptr[1];
+
+ if (g_strcmp0 (name, (const char *)(tabptr+2)) == 0)
+ {
+ return impl_match_info_fetch_pos (match_info, n, start_pos, end_pos);
+ }
+
+ tabptr += match_info->regex->name_entry_size;
+ }
+
+ *start_pos = -1;
+ *end_pos = -1;
+
+ return FALSE;
+}
+
+gboolean
+impl_match_info_matches (const ImplMatchInfo *match_info)
{
g_return_val_if_fail (match_info != NULL, FALSE);
- g_return_val_if_fail (match_info->match_info != NULL, FALSE);
- return g_match_info_fetch_named_pos (match_info->match_info, name, start_pos, end_pos);
+ return match_info->n_matches > 0;
+}
+
+gboolean
+impl_match_info_next (ImplMatchInfo *match_info,
+ GError **error)
+{
+ PCRE2_SIZE *ovector;
+ int rc;
+
+ g_return_val_if_fail (match_info != NULL, FALSE);
+ g_return_val_if_fail (match_info->regex != NULL, FALSE);
+
+ rc = pcre2_match (match_info->regex->code,
+ (PCRE2_SPTR)match_info->string,
+ (PCRE2_SIZE)match_info->string_len,
+ match_info->end,
+ match_info->flags,
+ match_info->match_data,
+ NULL);
+
+ if (rc < 0)
+ {
+ match_info->n_matches = -1;
+ set_regex_error (error, rc);
+ return FALSE;
+ }
+ else if (rc == 0)
+ {
+ match_info->n_matches = -1;
+ g_set_error (error,
+ G_REGEX_ERROR,
+ G_REGEX_ERROR_MATCH,
+ "No match");
+ return FALSE;
+ }
+
+ ovector = pcre2_get_ovector_pointer (match_info->match_data);
+
+ match_info->n_matches = rc;
+ match_info->begin = ovector[0];
+ match_info->end = ovector[1];
+
+ return TRUE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]