diff --git a/cmdline/stress.c b/cmdline/stress.c index 8e15d543..6a7b517c 100644 --- a/cmdline/stress.c +++ b/cmdline/stress.c @@ -619,6 +619,9 @@ void stress_functions(dc_context_t* context) assert( !DC_EVENT_RETURNS_STRING(100) ); assert( !DC_EVENT_RETURNS_STRING(300) ); assert( !DC_EVENT_RETURNS_STRING(400) ); + + assert( dc_utf8_strlen("c")==1 && strlen("c")==1 ); + assert( dc_utf8_strlen("ä")==1 && strlen("ä")==2 ); } /* test dc_array_t diff --git a/src/dc_tools.c b/src/dc_tools.c index bef83f1a..9abe1222 100644 --- a/src/dc_tools.c +++ b/src/dc_tools.c @@ -420,9 +420,12 @@ error: } -#if 0 /* not needed at the moment */ -static size_t dc_utf8_strlen(const char* s) +size_t dc_utf8_strlen(const char* s) { + if (s==NULL) { + return 0; + } + size_t i = 0; size_t j = 0; while (s[i]) { @@ -432,11 +435,14 @@ static size_t dc_utf8_strlen(const char* s) } return j; } -#endif static size_t dc_utf8_strnlen(const char* s, size_t n) { + if (s==NULL) { + return 0; + } + size_t i = 0; size_t j = 0; while (i < n) { diff --git a/src/dc_tools.h b/src/dc_tools.h index 213647f6..4a7d5750 100644 --- a/src/dc_tools.h +++ b/src/dc_tools.h @@ -41,6 +41,7 @@ char* dc_binary_to_uc_hex (const uint8_t* buf, size_t bytes); void dc_remove_cr_chars (char*); /* remove all \r characters from string */ void dc_unify_lineends (char*); void dc_replace_bad_utf8_chars (char*); /* replace bad UTF-8 characters by sequences of `_` (to avoid problems in filenames, we do not use eg. `?`) the function is useful if strings are unexpectingly encoded eg. as ISO-8859-1 */ +size_t dc_utf8_strlen (const char*); void dc_truncate_str (char*, int approx_characters); void dc_truncate_n_unwrap_str (char*, int approx_characters, int do_unwrap); carray* dc_split_into_lines (const char* buf_terminated); /* split string into lines*/