add tk_utf8_from_utf16_ex and wstr_set_utf8_with_len

This commit is contained in:
lixianjing 2021-03-19 11:39:32 +08:00
parent 8645a001be
commit a2e1c8c68d
5 changed files with 73 additions and 1 deletions

View File

@ -242,12 +242,20 @@ char* tk_utf8_from_utf16(const wchar_t* str, char* out, uint32_t size) {
}
wchar_t* tk_utf8_to_utf16(const char* str, wchar_t* out, uint32_t size) {
return_value_if_fail(str != NULL && out != NULL, NULL);
return tk_utf8_to_utf16_ex(str, strlen(str), out, size);
}
wchar_t* tk_utf8_to_utf16_ex(const char* str, uint32_t size, wchar_t* out, uint32_t out_size) {
uint32_t i = 0;
const char* p = str;
const char* end = NULL;
const char* next = NULL;
return_value_if_fail(str != NULL && out != NULL, NULL);
while (p != NULL && *p && (i + 1) < size) {
end = str + size;
while (p != NULL && p < end && (i + 1) < out_size) {
out[i++] = utf8_get_char(p, &next);
p = next;
}
@ -255,3 +263,4 @@ wchar_t* tk_utf8_to_utf16(const char* str, wchar_t* out, uint32_t size) {
return out;
}

View File

@ -86,6 +86,20 @@ char* tk_utf8_from_utf16_ex(const wchar_t* in, uint32_t in_size, char* out, uint
*/
wchar_t* tk_utf8_to_utf16(const char* str, wchar_t* out, uint32_t size);
/**
* @method tk_utf8_to_utf16_ex
*
* char类型转换为wchar_t类型
*
* @param {const char*} str str
* @param {uint32_t} size
* @param {const wchar_t*} out
* @param {uint32_t} out_size
*
* @return {wchar_t*}
*/
wchar_t* tk_utf8_to_utf16_ex(const char* str, uint32_t size, wchar_t* out, uint32_t out_size);
END_C_DECLS
#endif /*TK_UTF8_H*/

View File

@ -153,6 +153,16 @@ ret_t wstr_clear(wstr_t* str) {
return RET_OK;
}
ret_t wstr_set_utf8_with_len(wstr_t* str, const char* text, uint32_t len) {
return_value_if_fail(str != NULL && text != NULL, RET_BAD_PARAMS);
return_value_if_fail(wstr_extend(str, len + 2) == RET_OK, RET_OOM);
tk_utf8_to_utf16_ex(text, len, str->str, str->capacity - 1);
str->size = wcslen(str->str);
return RET_OK;
}
ret_t wstr_set_utf8(wstr_t* str, const char* text) {
return_value_if_fail(str != NULL && text != NULL, RET_BAD_PARAMS);
return_value_if_fail(wstr_extend(str, strlen(text) + 2) == RET_OK, RET_OOM);

View File

@ -106,6 +106,17 @@ ret_t wstr_clear(wstr_t* str);
*/
ret_t wstr_set_utf8(wstr_t* str, const char* text);
/**
* @method wstr_set_utf8_with_len
* UTF8字符串
* @param {wstr_t*} str str对象
* @param {char*} text
* @param {uint32_t} len
*
* @return {ret_t} RET_OK表示成功
*/
ret_t wstr_set_utf8_with_len(wstr_t* str, const char* text, uint32_t len);
/**
* @method wstr_get_utf8
* UTF8字符串

View File

@ -353,3 +353,31 @@ TEST(WStr, count) {
ASSERT_EQ(wstr_count_char(&str, '1'), 2);
wstr_reset(&str);
}
TEST(WStr, set_utf8_with_len) {
wstr_t str;
wstr_init(&str, 0);
ASSERT_EQ(wstr_set_utf8_with_len(&str, "123123", 0), RET_OK);
ASSERT_EQ(str.size, 0);
ASSERT_EQ(wcscmp(str.str, L""), 0);
ASSERT_EQ(wstr_set_utf8_with_len(&str, "123123", 1), RET_OK);
ASSERT_EQ(str.size, 1);
ASSERT_EQ(wcscmp(str.str, L"1"), 0);
ASSERT_EQ(wstr_set_utf8_with_len(&str, "123123", 3), RET_OK);
ASSERT_EQ(str.size, 3);
ASSERT_EQ(wcscmp(str.str, L"123"), 0);
ASSERT_EQ(wstr_set_utf8_with_len(&str, "123123", 6), RET_OK);
ASSERT_EQ(str.size, 6);
ASSERT_EQ(wcscmp(str.str, L"123123"), 0);
ASSERT_EQ(wstr_set_utf8_with_len(&str, "123123", 6), RET_OK);
ASSERT_EQ(str.size, 6);
ASSERT_EQ(wcscmp(str.str, L"123123"), 0);
wstr_reset(&str);
}