Simplify logic of mu_str_remove_ctrl_in_place. Add tests.
This commit is contained in:
committed by
djcb
parent
6017ac46ce
commit
588d227171
31
lib/mu-str.c
31
lib/mu-str.c
@ -1043,35 +1043,26 @@ errexit:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char*
|
||||||
char *
|
|
||||||
mu_str_remove_ctrl_in_place (char *str)
|
mu_str_remove_ctrl_in_place (char *str)
|
||||||
{
|
{
|
||||||
char *cur;
|
char *orig, *cur;
|
||||||
|
|
||||||
g_return_val_if_fail (str, NULL);
|
g_return_val_if_fail (str, NULL);
|
||||||
|
|
||||||
for (cur = str; *cur; ++cur) {
|
orig = str;
|
||||||
|
|
||||||
GString *gstr;
|
|
||||||
|
|
||||||
if (!iscntrl(*cur))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
|
for (cur = orig; *cur; ++cur) {
|
||||||
if (isspace(*cur)) {
|
if (isspace(*cur)) {
|
||||||
/* squash special white space into a simple space */
|
/* squash special white space into a simple space */
|
||||||
*cur = ' ';
|
*orig++ = ' ';
|
||||||
} else {
|
} else if (iscntrl(*cur)) {
|
||||||
/* remove other control characters */
|
/* eat it */
|
||||||
gstr = g_string_sized_new (strlen (str));
|
} else
|
||||||
for (cur = str; *cur; ++cur)
|
*orig++ = *cur;
|
||||||
if (!iscntrl (*cur))
|
|
||||||
g_string_append_c (gstr, *cur);
|
|
||||||
memcpy (str, gstr->str, gstr->len); /* fits */
|
|
||||||
g_string_free (gstr, TRUE);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*orig = '\0'; /* ensure the updated string has a NULL */
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -502,6 +502,30 @@ test_mu_str_replace (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_mu_str_remove_ctrl_in_place (void)
|
||||||
|
{
|
||||||
|
unsigned u;
|
||||||
|
struct {
|
||||||
|
char *str;
|
||||||
|
const char *exp;
|
||||||
|
} strings [] = {
|
||||||
|
{ g_strdup(""), ""},
|
||||||
|
{ g_strdup("hello, world!"), "hello, world!" },
|
||||||
|
{ g_strdup("hello,\tworld!"), "hello, world!" },
|
||||||
|
{ g_strdup("hello,\n\nworld!"), "hello, world!", },
|
||||||
|
{ g_strdup("hello,\x1f\x1e\x1ew\nor\nld!"), "hello,w or ld!" },
|
||||||
|
{ g_strdup("\x1ehello, world!\x1f"), "hello, world!" }
|
||||||
|
};
|
||||||
|
|
||||||
|
for (u = 0; u != G_N_ELEMENTS(strings); ++u) {
|
||||||
|
char *res;
|
||||||
|
res = mu_str_remove_ctrl_in_place (strings[u].str);
|
||||||
|
g_assert_cmpstr (res,==,strings[u].exp);
|
||||||
|
g_free (strings[u].str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
@ -559,6 +583,8 @@ main (int argc, char *argv[])
|
|||||||
g_test_add_func ("/mu-str/mu_term_fixups",
|
g_test_add_func ("/mu-str/mu_term_fixups",
|
||||||
test_mu_term_fixups);
|
test_mu_term_fixups);
|
||||||
|
|
||||||
|
g_test_add_func ("/mu-str/mu_str_remove_ctrl_in_place",
|
||||||
|
test_mu_str_remove_ctrl_in_place);
|
||||||
|
|
||||||
/* FIXME: add tests for mu_str_flags; but note the
|
/* FIXME: add tests for mu_str_flags; but note the
|
||||||
* function simply calls mu_msg_field_str */
|
* function simply calls mu_msg_field_str */
|
||||||
|
|||||||
Reference in New Issue
Block a user