add str_from_wstr_with_len

This commit is contained in:
xianjimli 2019-12-27 18:43:14 +08:00
parent 689d7a27a5
commit b68f7667a0
3 changed files with 40 additions and 5 deletions

View File

@ -261,7 +261,7 @@ ret_t str_from_value(str_t* str, const value_t* v) {
}
}
ret_t str_from_wstr(str_t* str, const wchar_t* wstr) {
ret_t str_from_wstr_with_len(str_t* str, const wchar_t* wstr, uint32_t len) {
return_value_if_fail(str != NULL, RET_BAD_PARAMS);
str->size = 0;
@ -269,12 +269,12 @@ ret_t str_from_wstr(str_t* str, const wchar_t* wstr) {
str->str[0] = '\0';
}
if (wstr != NULL) {
uint32_t size = wcslen(wstr) * 4 + 1;
if (wstr != NULL && len > 0) {
uint32_t size = len * 4 + 1;
return_value_if_fail(str_extend(str, size + 1) == RET_OK, RET_OOM);
if (size > 0) {
tk_utf8_from_utf16(wstr, str->str, size);
tk_utf8_from_utf16_ex(wstr, len, str->str, size);
str->size = strlen(str->str);
} else {
str_set(str, "");
@ -284,6 +284,12 @@ ret_t str_from_wstr(str_t* str, const wchar_t* wstr) {
return RET_OK;
}
ret_t str_from_wstr(str_t* str, const wchar_t* wstr) {
return_value_if_fail(str != NULL && wstr != NULL, RET_BAD_PARAMS);
return str_from_wstr_with_len(str, wstr, wcslen(wstr));
}
ret_t str_to_int(str_t* str, int32_t* v) {
return_value_if_fail(str != NULL && v != NULL, RET_BAD_PARAMS);
*v = tk_atoi(str->str);

View File

@ -277,12 +277,23 @@ ret_t str_from_value(str_t* str, const value_t* v);
* @method str_from_wstr
* value初始化字符串
* @param {str_t*} str str对象
* @param {wchar_t*} wstr wstr
* @param {wchar_t*} wstr Unicode字符串
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_wstr(str_t* str, const wchar_t* wstr);
/**
* @method str_from_wstr_with_len
* value初始化字符串
* @param {str_t*} str str对象
* @param {wchar_t*} wstr Unicode字符串
* @param {uint32_t} len Unicode字符串的长度
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_wstr_with_len(str_t* str, const wchar_t* wstr, uint32_t len);
/**
* @method str_to_int
*

View File

@ -257,3 +257,21 @@ TEST(Str, expand_vars) {
object_unref(vars);
str_reset(s);
}
TEST(Str, from_wstr) {
str_t str;
str_t* s = NULL;
s = str_init(&str, 0);
ASSERT_EQ(s->size, 0);
str_from_wstr(s, L"123456");
ASSERT_STREQ(s->str, "123456");
str_from_wstr_with_len(s, L"123456", 3);
ASSERT_STREQ(s->str, "123");
str_from_wstr_with_len(s, L"123456", 0);
ASSERT_STREQ(s->str, "");
str_reset(s);
}