mirror of https://github.com/djcb/mu.git
* add mu_str_replace
This commit is contained in:
parent
52d89bd1f8
commit
3dae623879
27
lib/mu-str.c
27
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)
|
||||
|
|
16
lib/mu-str.h
16
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
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue