From ff6b38033f0792141a499afdc52eded2cf45ae28 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 13 Jul 2019 11:11:09 +0200 Subject: [PATCH] string_replace_full gone on loop where replacing same occurrence. --- src/generic.c | 23 ++--------------------- tests/generic.c | 7 ++++--- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/generic.c b/src/generic.c index ea37b4e..1ff5b07 100644 --- a/src/generic.c +++ b/src/generic.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2018 Andrea Zagli + * Copyright (C) 2015-2019 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -92,29 +92,10 @@ gchar const gchar *origin, const gchar *replace) { - gchar *ret; - gchar *p; - if (string == NULL) return NULL; if (origin == NULL || replace == NULL) return g_strdup (string); - ret = g_strdup (string); - - do - { - p = g_strstr_len (ret, -1, origin); - - if (p == NULL) - { - return ret; - } - - ret = g_strndup (ret, p - ret); - - ret = g_strdup_printf ("%s%s%s", ret, replace, p + strlen (origin)); - } while (TRUE); - - return ret; + return g_strjoinv (replace, g_strsplit (string, origin, 0)); } /** diff --git a/tests/generic.c b/tests/generic.c index eae0aef..2466fd4 100644 --- a/tests/generic.c +++ b/tests/generic.c @@ -21,13 +21,14 @@ static void test_string_replace () { - g_assert_cmpstr (zak_utils_string_replace ("a string with double quotes \" to be escaped", "\"", "\\\\\""), ==, "a string with double quotes \\\\\" to be escaped"); + g_assert_cmpstr (zak_utils_string_replace ("a string with double quotes \" to be escaped \"", "\"", "\\\\\""), ==, "a string with double quotes \\\\\" to be escaped \""); } static void test_string_replace_full () { - g_assert_cmpstr (zak_utils_string_replace_full ("a string with double quotes \" to be escaped", "\"", "\\\\\""), ==, "a string with double quotes \\\\\" to be escaped"); + g_assert_cmpstr (zak_utils_string_replace_full ("a string with \" double quotes \" to be escaped", "\"", "\\\\\""), ==, "a string with \\\\\" double quotes \\\\\" to be escaped"); + g_assert_cmpstr (zak_utils_string_replace_full ("a string with \" double quotes \" to be escaped", "\"", "OO"), ==, "a string with OO double quotes OO to be escaped"); } int @@ -36,7 +37,7 @@ main (int argc, char *argv[]) g_test_init (&argc, &argv, NULL); g_test_add_func ("/generic/string_replace", test_string_replace); - /*g_test_add_func ("/generic/string_replace_full", test_string_replace_full);*/ + g_test_add_func ("/generic/string_replace_full", test_string_replace_full); return g_test_run (); } -- 2.49.0