From 3dae62387990746f26cc3d931bf5532652791a97 Mon Sep 17 00:00:00 2001 From: djcb Date: Sun, 26 May 2013 11:16:02 -0700 Subject: [PATCH] * add mu_str_replace --- lib/mu-str.c | 27 +++++++++++++++++++++++++++ lib/mu-str.h | 16 ++++++++++++++++ lib/tests/test-mu-str.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/lib/mu-str.c b/lib/mu-str.c index 28d4f317..803c7fb7 100644 --- a/lib/mu-str.c +++ b/lib/mu-str.c @@ -199,6 +199,33 @@ mu_str_size_parse_bkm (const char* str) } +char* +mu_str_replace (const char *str, const char *substr, const char *repl) +{ + GString *gstr; + const char *cur; + + g_return_val_if_fail (str, NULL); + g_return_val_if_fail (substr, NULL); + g_return_val_if_fail (repl, NULL); + + gstr = g_string_sized_new (2 * strlen (str)); + + for (cur = str; *cur; ++cur) { + if (g_str_has_prefix (cur, substr)) { + g_string_append (gstr, repl); + cur += strlen (substr) - 1; + } else + g_string_append_c (gstr, *cur); + } + + return g_string_free (gstr, FALSE); +} + + + + + char* mu_str_from_list (const GSList *lst, char sepa) diff --git a/lib/mu-str.h b/lib/mu-str.h index 99684d83..23adb39e 100644 --- a/lib/mu-str.h +++ b/lib/mu-str.h @@ -75,6 +75,21 @@ char *mu_str_display_contact (const char *str) G_GNUC_WARN_UNUSED_RESULT; const char* mu_str_size_s (size_t s) G_GNUC_CONST; char* mu_str_size (size_t s) G_GNUC_WARN_UNUSED_RESULT; + + + +/** + * Replace all occurences of substr in str with repl + * + * @param str a string + * @param substr some string to replace + * @param repl a replacement string + * + * @return a newly allocated string with the substr replaced by repl; free with g_free + */ +char *mu_str_replace (const char *str, const char *substr, const char *repl); + + /** * get a display string for a given set of flags, OR'ed in * @param flags; one character per flag: @@ -326,6 +341,7 @@ const gchar* mu_str_subject_normalize (const gchar* str); */ gchar* mu_str_quoted_from_strv (const gchar **params); + /** @} */ G_END_DECLS diff --git a/lib/tests/test-mu-str.c b/lib/tests/test-mu-str.c index d6a87a0b..cfdf23a7 100644 --- a/lib/tests/test-mu-str.c +++ b/lib/tests/test-mu-str.c @@ -431,6 +431,35 @@ test_mu_term_fixups (void) + + +static void +test_mu_str_replace (void) +{ + unsigned u; + struct { + const char* str; + const char* sub; + const char *repl; + const char *exp; + } strings [] = { + { "hello", "ll", "xx", "hexxo" }, + { "hello", "hello", "hi", "hi" }, + { "hello", "foo", "bar", "hello" } + }; + + for (u = 0; u != G_N_ELEMENTS(strings); ++u) { + char *res; + res = mu_str_replace (strings[u].str, + strings[u].sub, + strings[u].repl); + g_assert_cmpstr (res,==,strings[u].exp); + g_free (res); + } +} + + + int main (int argc, char *argv[]) { @@ -471,6 +500,9 @@ main (int argc, char *argv[]) g_test_add_func ("/mu-str/mu-str-esc-to-list", test_parse_arglist); + g_test_add_func ("/mu-str/mu-str-replace", + test_mu_str_replace); + g_test_add_func ("/mu-str/mu-str-esc-to-list", test_mu_str_esc_to_list);