add tk_utf8_trim_invalid_char

This commit is contained in:
lixianjing 2021-12-11 19:11:09 +08:00
parent 35149fde61
commit d5827b7070
4 changed files with 69 additions and 0 deletions

View File

@ -2,6 +2,7 @@
2021/12/11
* 增加tk\_str\_is\_in\_array。
* 增加tk\_utf8\_trim\_invalid\_char。
* 完善project.json感谢俊杰提供补丁
2021/12/09

View File

@ -289,3 +289,25 @@ char* tk_utf8_dup_utf16(const wchar_t* in, int32_t size) {
return tk_utf8_from_utf16_ex(in, size, out, out_size - 1);
}
char* tk_utf8_trim_invalid_char(char* str) {
int32_t i = 0;
int32_t n = 0;
char* p = str;
return_value_if_fail(str != NULL, str);
while (*p) {
n = tk_utf8_get_bytes_of_leading(*p);
if (n > 0) {
for (i = 0; i < n; i++) {
if (p[i] == '\0') {
*p = '\0';
return str;
}
}
}
p += n;
}
return str;
}

View File

@ -122,6 +122,21 @@ char* tk_utf8_dup_utf16(const wchar_t* in, int32_t size);
*/
uint32_t tk_utf8_get_bytes_of_leading(uint8_t c);
/**
* @method tk_utf8_trim_invalid_char
*
*
*
* > strncpy/snprintf等生成字符串时
* > assert
* >
*
* @param {const char*} str
*
* @return {char*} UTF8字符串
*/
char* tk_utf8_trim_invalid_char(char* str);
END_C_DECLS
#endif /*TK_UTF8_H*/

View File

@ -56,3 +56,34 @@ TEST(Utf8, dup) {
ASSERT_STREQ(str, text);
TKMEM_FREE(text);
}
TEST(Utf8, trim_invalid) {
char text[32] = {0};
const char* str = "中文";
memset(text, 0x00, sizeof(text));
strncpy(text, str, 1);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "");
strncpy(text, str, 2);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "");
strncpy(text, str, 3);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "");
strncpy(text, str, 4);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "");
strncpy(text, str, 5);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "");
strncpy(text, str, 6);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "中文");
strncpy(text, str, 7);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "中文");
strncpy(text, "abc", 4);
ASSERT_STREQ(tk_utf8_trim_invalid_char(text), "abc");
}