gpattern: Implement copy function

Add copy ability for pattern spec, so that it can be used as a boxed
type.
This commit is contained in:
Marco Trevisan (Treviño) 2021-04-25 02:30:57 +02:00
parent 232d008ec8
commit 474ece6d61
4 changed files with 53 additions and 0 deletions

View File

@ -1425,6 +1425,7 @@ GPatternSpec
g_pattern_spec_new g_pattern_spec_new
g_pattern_spec_free g_pattern_spec_free
g_pattern_spec_equal g_pattern_spec_equal
g_pattern_spec_copy
g_pattern_match g_pattern_match
g_pattern_match_string g_pattern_match_string
g_pattern_match_simple g_pattern_match_simple

View File

@ -24,6 +24,7 @@
#include "gmacros.h" #include "gmacros.h"
#include "gmessages.h" #include "gmessages.h"
#include "gmem.h" #include "gmem.h"
#include "gstrfuncs.h"
#include "gunicode.h" #include "gunicode.h"
#include "gutils.h" #include "gutils.h"
@ -353,6 +354,30 @@ g_pattern_spec_new (const gchar *pattern)
return pspec; 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: * g_pattern_spec_free:
* @pspec: a #GPatternSpec * @pspec: a #GPatternSpec

View File

@ -33,6 +33,8 @@ GLIB_AVAILABLE_IN_ALL
GPatternSpec* g_pattern_spec_new (const gchar *pattern); GPatternSpec* g_pattern_spec_new (const gchar *pattern);
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
void g_pattern_spec_free (GPatternSpec *pspec); void g_pattern_spec_free (GPatternSpec *pspec);
GLIB_AVAILABLE_IN_2_70
GPatternSpec *g_pattern_spec_copy (GPatternSpec *pspec);
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
gboolean g_pattern_spec_equal (GPatternSpec *pspec1, gboolean g_pattern_spec_equal (GPatternSpec *pspec1,
GPatternSpec *pspec2); GPatternSpec *pspec2);

View File

@ -84,6 +84,24 @@ test_compilation (gconstpointer d)
g_pattern_spec_free (spec); 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; typedef struct _MatchTest MatchTest;
struct _MatchTest struct _MatchTest
@ -222,6 +240,13 @@ main (int argc, char** argv)
g_free (path); 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++) for (i = 0; i < G_N_ELEMENTS (match_tests); i++)
{ {
path = g_strdup_printf ("/pattern/match/%" G_GSIZE_FORMAT, i); path = g_strdup_printf ("/pattern/match/%" G_GSIZE_FORMAT, i);