mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 02:58:26 +08:00
add tk_utf8_from_utf16_ex and wstr_set_utf8_with_len
This commit is contained in:
parent
8645a001be
commit
a2e1c8c68d
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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*/
|
||||
|
@ -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);
|
||||
|
@ -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字符串。
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user