mirror of https://github.com/djcb/mu.git
Simplify logic of mu_str_remove_ctrl_in_place. Add tests.
This commit is contained in:
parent
6017ac46ce
commit
588d227171
33
lib/mu-str.c
33
lib/mu-str.c
|
@ -588,7 +588,7 @@ process_str (const char *str, gboolean xapian_esc, gboolean query_esc)
|
|||
|
||||
if (!is_range_field)
|
||||
uc = g_unichar_tolower (uc);
|
||||
|
||||
|
||||
g_string_append_unichar (gstr, uc);
|
||||
}
|
||||
|
||||
|
@ -1043,35 +1043,26 @@ errexit:
|
|||
}
|
||||
|
||||
|
||||
|
||||
char *
|
||||
char*
|
||||
mu_str_remove_ctrl_in_place (char *str)
|
||||
{
|
||||
char *cur;
|
||||
char *orig, *cur;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
|
||||
for (cur = str; *cur; ++cur) {
|
||||
|
||||
GString *gstr;
|
||||
|
||||
if (!iscntrl(*cur))
|
||||
continue;
|
||||
orig = str;
|
||||
|
||||
for (cur = orig; *cur; ++cur) {
|
||||
if (isspace(*cur)) {
|
||||
/* squash special white space into a simple space */
|
||||
*cur = ' ';
|
||||
} else {
|
||||
/* remove other control characters */
|
||||
gstr = g_string_sized_new (strlen (str));
|
||||
for (cur = str; *cur; ++cur)
|
||||
if (!iscntrl (*cur))
|
||||
g_string_append_c (gstr, *cur);
|
||||
memcpy (str, gstr->str, gstr->len); /* fits */
|
||||
g_string_free (gstr, TRUE);
|
||||
break;
|
||||
}
|
||||
*orig++ = ' ';
|
||||
} else if (iscntrl(*cur)) {
|
||||
/* eat it */
|
||||
} else
|
||||
*orig++ = *cur;
|
||||
}
|
||||
|
||||
*orig = '\0'; /* ensure the updated string has a NULL */
|
||||
|
||||
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
|
||||
main (int argc, char *argv[])
|
||||
|
@ -559,6 +583,8 @@ main (int argc, char *argv[])
|
|||
g_test_add_func ("/mu-str/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
|
||||
* function simply calls mu_msg_field_str */
|
||||
|
|
Loading…
Reference in New Issue