From: Richard Russon Date: Sun, 26 May 2019 15:02:23 +0000 (+0100) Subject: test: improve test_mutt_str_is_ascii() X-Git-Tag: 2019-10-25~187^2~31 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0ac3c293edc380131c3f6109f674117ef1443119;p=neomutt test: improve test_mutt_str_is_ascii() --- diff --git a/mutt/string.c b/mutt/string.c index d372fa87e..e3e40870b 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -1003,19 +1003,19 @@ int mutt_str_word_casecmp(const char *a, const char *b) /** * mutt_str_is_ascii - Is a string ASCII (7-bit)? - * @param p String to examine - * @param len Length of string + * @param str String to examine + * @param len Length of string to examine * @retval true There are no 8-bit chars */ -bool mutt_str_is_ascii(const char *p, size_t len) +bool mutt_str_is_ascii(const char *str, size_t len) { - const char *s = p; - while (s && ((unsigned int) (s - p) < len)) - { - if ((*s & 0x80) != 0) + if (!str) + return true; + + for (; (*str != '\0') && (len > 0); str++, len--) + if ((*str & 0x80) != 0) return false; - s++; - } + return true; } diff --git a/mutt/string2.h b/mutt/string2.h index a0edd243e..89df8cb45 100644 --- a/mutt/string2.h +++ b/mutt/string2.h @@ -81,7 +81,7 @@ void mutt_str_dequote_comment(char *s); const char *mutt_str_find_word(const char *src); const char *mutt_str_getenv(const char *name); bool mutt_str_inline_replace(char *buf, size_t buflen, size_t xlen, const char *rstr); -bool mutt_str_is_ascii(const char *p, size_t len); +bool mutt_str_is_ascii(const char *str, size_t len); bool mutt_str_is_email_wsp(char c); size_t mutt_str_lws_len(const char *s, size_t n); size_t mutt_str_lws_rlen(const char *s, size_t n); diff --git a/test/string/mutt_str_is_ascii.c b/test/string/mutt_str_is_ascii.c index 333f1497b..e78648159 100644 --- a/test/string/mutt_str_is_ascii.c +++ b/test/string/mutt_str_is_ascii.c @@ -25,11 +25,37 @@ #include "config.h" #include "mutt/mutt.h" +struct IsAsciiTest +{ + const char *str; + int len; + bool result; +}; + void test_mutt_str_is_ascii(void) { // bool mutt_str_is_ascii(const char *p, size_t len); + struct IsAsciiTest ascii_tests[] = + { + { NULL, 10, true }, + { "apple", 0, true }, + { "", 10, true }, + { "apple", 5, true }, + + { "\200apple", 6, false }, + { "ap\200ple", 6, false }, + { "apple\200", 6, false }, + { "apple\200", 5, true }, + }; + { - TEST_CHECK(mutt_str_is_ascii(NULL, 10) == true); + for (size_t i = 0; i < mutt_array_size(ascii_tests); i++) + { + struct IsAsciiTest *t = &ascii_tests[i]; + TEST_CASE_("%s", NONULL(t->str)); + bool result = mutt_str_is_ascii(t->str, t->len); + TEST_CHECK(result == t->result); + } } }