[glib/wip/3v1n0/regex-pcre2-flags-fixes: 10/11] regex: Compute the offsets size based on match results
- From: Marco Trevisan <marcotrevi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/3v1n0/regex-pcre2-flags-fixes: 10/11] regex: Compute the offsets size based on match results
- Date: Tue, 6 Sep 2022 17:24:13 +0000 (UTC)
commit c8a619f79be56164d7655ca9f941eeb37cb2b1dd
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date: Tue Sep 6 19:05:24 2022 +0200
regex: Compute the offsets size based on match results
While the ovector count would include all the allocated space, we only
care about the actual match values, so avoid wasting allocations and
just use the ones we need to hold the offsets.
glib/gregex.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
---
diff --git a/glib/gregex.c b/glib/gregex.c
index ba09904185..b757638556 100644
--- a/glib/gregex.c
+++ b/glib/gregex.c
@@ -857,10 +857,20 @@ recalc_match_offsets (GMatchInfo *match_info,
GError **error)
{
PCRE2_SIZE *ovector;
+ uint32_t ovector_size = 0;
uint32_t pre_n_offset;
uint32_t i;
- if (pcre2_get_ovector_count (match_info->match_data) > G_MAXUINT32 / 2)
+ g_assert (!IS_PCRE2_ERROR (match_info->matches));
+
+ if (match_info->matches == PCRE2_ERROR_PARTIAL)
+ ovector_size = 1;
+ else if (match_info->matches > 0)
+ ovector_size = match_info->matches;
+
+ g_assert (ovector_size != 0);
+
+ if (pcre2_get_ovector_count (match_info->match_data) < ovector_size)
{
g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
_("Error while matching regular expression %s: %s"),
@@ -869,7 +879,7 @@ recalc_match_offsets (GMatchInfo *match_info,
}
pre_n_offset = match_info->n_offsets;
- match_info->n_offsets = pcre2_get_ovector_count (match_info->match_data) * 2;
+ match_info->n_offsets = ovector_size * 2;
ovector = pcre2_get_ovector_pointer (match_info->match_data);
if (match_info->n_offsets != pre_n_offset)
@@ -2404,7 +2414,7 @@ g_regex_match_all_full (const GRegex *regex,
_("Error while matching regular expression %s: %s"),
regex->pattern, match_error (info->matches));
}
- else if (info->matches > 0)
+ else if (info->matches != PCRE2_ERROR_NOMATCH)
{
if (!recalc_match_offsets (info, error))
info->matches = PCRE2_ERROR_NOMATCH;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]