From 474ece6d614a418ee21e3169bc453b6921347fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Sun, 25 Apr 2021 02:30:57 +0200 Subject: [PATCH] gpattern: Implement copy function Add copy ability for pattern spec, so that it can be used as a boxed type. --- docs/reference/glib/glib-sections.txt | 1 + glib/gpattern.c | 25 +++++++++++++++++++++++++ glib/gpattern.h | 2 ++ glib/tests/pattern.c | 25 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index dff212e4d..3a96ba789 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1425,6 +1425,7 @@ GPatternSpec g_pattern_spec_new g_pattern_spec_free g_pattern_spec_equal +g_pattern_spec_copy g_pattern_match g_pattern_match_string g_pattern_match_simple diff --git a/glib/gpattern.c b/glib/gpattern.c index 16a0bf5f7..24001002a 100644 --- a/glib/gpattern.c +++ b/glib/gpattern.c @@ -24,6 +24,7 @@ #include "gmacros.h" #include "gmessages.h" #include "gmem.h" +#include "gstrfuncs.h" #include "gunicode.h" #include "gutils.h" @@ -353,6 +354,30 @@ g_pattern_spec_new (const gchar *pattern) return pspec; } +/** + * g_pattern_spec_copy: + * @pspec: a #GPatternSpec + * + * Copies @pspec in a new #GPatternSpec. + * + * Returns: (transfer full): a copy of @pspec. + * + * Since: 2.70 + **/ +GPatternSpec * +g_pattern_spec_copy (GPatternSpec *pspec) +{ + GPatternSpec *pspec_copy; + + g_return_val_if_fail (pspec != NULL, NULL); + + pspec_copy = g_new (GPatternSpec, 1); + *pspec_copy = *pspec; + pspec_copy->pattern = g_strndup (pspec->pattern, pspec->pattern_length); + + return pspec_copy; +} + /** * g_pattern_spec_free: * @pspec: a #GPatternSpec diff --git a/glib/gpattern.h b/glib/gpattern.h index bd9e4a7af..d69799cdd 100644 --- a/glib/gpattern.h +++ b/glib/gpattern.h @@ -33,6 +33,8 @@ GLIB_AVAILABLE_IN_ALL GPatternSpec* g_pattern_spec_new (const gchar *pattern); GLIB_AVAILABLE_IN_ALL void g_pattern_spec_free (GPatternSpec *pspec); +GLIB_AVAILABLE_IN_2_70 +GPatternSpec *g_pattern_spec_copy (GPatternSpec *pspec); GLIB_AVAILABLE_IN_ALL gboolean g_pattern_spec_equal (GPatternSpec *pspec1, GPatternSpec *pspec2); diff --git a/glib/tests/pattern.c b/glib/tests/pattern.c index 70553a882..ef807fbde 100644 --- a/glib/tests/pattern.c +++ b/glib/tests/pattern.c @@ -84,6 +84,24 @@ test_compilation (gconstpointer d) g_pattern_spec_free (spec); } +static void +test_copy (gconstpointer d) +{ + const CompileTest *test = d; + GPatternSpec *p1, *p2; + + p1 = g_pattern_spec_new (test->src); + p2 = g_pattern_spec_copy (p1); + + g_assert_cmpint (p2->match_type, ==, test->match_type); + g_assert_cmpstr (p2->pattern, ==, test->pattern); + g_assert_cmpint (p2->pattern_length, ==, strlen (p1->pattern)); + g_assert_cmpint (p2->min_length, ==, test->min); + + g_pattern_spec_free (p1); + g_pattern_spec_free (p2); +} + typedef struct _MatchTest MatchTest; struct _MatchTest @@ -222,6 +240,13 @@ main (int argc, char** argv) g_free (path); } + for (i = 0; i < G_N_ELEMENTS (compile_tests); i++) + { + path = g_strdup_printf ("/pattern/copy/%" G_GSIZE_FORMAT, i); + g_test_add_data_func (path, &compile_tests[i], test_copy); + g_free (path); + } + for (i = 0; i < G_N_ELEMENTS (match_tests); i++) { path = g_strdup_printf ("/pattern/match/%" G_GSIZE_FORMAT, i);