/**
* 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;
}
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);
#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);
+ }
}
}