add str_from_xxx

This commit is contained in:
lixianjing 2022-10-15 11:38:25 +08:00
parent 5d529cb464
commit b62a5c5cfa
4 changed files with 118 additions and 32 deletions

View File

@ -1,4 +1,6 @@
# 最新动态
2022/10/15
* 增加str\_from\_xxx函数。
2022/10/13
* slide_menu load后如果得到焦点则让选中子控件得到焦点(感谢兆坤提供补丁)

View File

@ -123,26 +123,40 @@ ret_t str_append_more(str_t* str, const char* text, ...) {
}
ret_t str_append_int(str_t* str, int32_t value) {
char num[32];
tk_snprintf(num, sizeof(num), "%d", value);
char num[32] = {0};
tk_snprintf(num, sizeof(num) - 1, "%d", value);
return str_append(str, num);
}
ret_t str_append_int64(str_t* str, int64_t value) {
char num[32];
tk_snprintf(num, sizeof(num), "%" PRId64, value);
char num[64] = {0};
tk_snprintf(num, sizeof(num) - 1, "%" PRId64, value);
return str_append(str, num);
}
ret_t str_append_uint64(str_t* str, uint64_t value) {
char num[32];
tk_snprintf(num, sizeof(num), "%" PRIu64, value);
char num[64] = {0};
tk_snprintf(num, sizeof(num) - 1, "%" PRIu64, value);
return str_append(str, num);
}
ret_t str_from_int64(str_t* str, int64_t value) {
char num[64] = {0};
tk_snprintf(num, sizeof(num) - 1, "%" PRId64, value);
return str_set(str, num);
}
ret_t str_from_uint64(str_t* str, uint64_t value) {
char num[64] = {0};
tk_snprintf(num, sizeof(num) - 1, "%" PRIu64, value);
return str_set(str, num);
}
ret_t str_append_char(str_t* str, char c) {
return_value_if_fail(str != NULL, RET_BAD_PARAMS);
return_value_if_fail(str_extend(str, str->size + 2) == RET_OK, RET_BAD_PARAMS);
@ -347,34 +361,41 @@ bool_t str_eq(str_t* str, const char* text) {
return strcmp(str->str, text) == 0;
}
ret_t str_from_int(str_t* str, int32_t v) {
ret_t str_from_int(str_t* str, int32_t value) {
char buff[TK_NUM_MAX_LEN + 1];
return_value_if_fail(str != NULL, RET_BAD_PARAMS);
return str_set(str, tk_itoa(buff, sizeof(buff), v));
return str_set(str, tk_itoa(buff, sizeof(buff), value));
}
ret_t str_from_float(str_t* str, double v) {
ret_t str_from_uint32(str_t* str, uint32_t value) {
char num[32] = {0};
tk_snprintf(num, sizeof(num) - 1, "%u", value);
return str_set(str, num);
}
ret_t str_from_float(str_t* str, double value) {
char buff[TK_NUM_MAX_LEN + 1];
return_value_if_fail(str != NULL, RET_BAD_PARAMS);
return str_set(str, tk_ftoa(buff, sizeof(buff), v));
return str_set(str, tk_ftoa(buff, sizeof(buff), value));
}
ret_t str_from_value(str_t* str, const value_t* v) {
return_value_if_fail(str != NULL && v != NULL, RET_BAD_PARAMS);
ret_t str_from_value(str_t* str, const value_t* value) {
return_value_if_fail(str != NULL && value != NULL, RET_BAD_PARAMS);
if (v->type == VALUE_TYPE_STRING) {
return str_set(str, value_str(v));
} else if (v->type == VALUE_TYPE_WSTRING) {
return str_from_wstr(str, value_wstr(v));
} else if (v->type == VALUE_TYPE_FLOAT || v->type == VALUE_TYPE_FLOAT32 ||
v->type == VALUE_TYPE_DOUBLE) {
return str_from_float(str, value_float(v));
} else if (v->type == VALUE_TYPE_BOOL) {
return str_set(str, value_bool(v) ? "true" : "false");
if (value->type == VALUE_TYPE_STRING) {
return str_set(str, value_str(value));
} else if (value->type == VALUE_TYPE_WSTRING) {
return str_from_wstr(str, value_wstr(value));
} else if (value->type == VALUE_TYPE_FLOAT || value->type == VALUE_TYPE_FLOAT32 ||
value->type == VALUE_TYPE_DOUBLE) {
return str_from_float(str, value_float(value));
} else if (value->type == VALUE_TYPE_BOOL) {
return str_set(str, value_bool(value) ? "true" : "false");
} else {
return str_from_int(str, value_int(v));
return str_from_int(str, value_int(value));
}
}

View File

@ -386,31 +386,61 @@ ret_t str_encode_xml_entity_with_len(str_t* str, const char* text, uint32_t len)
* @method str_from_int
*
* @param {str_t*} str str对象
* @param {int32_t} v
* @param {int32_t} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_int(str_t* str, int32_t v);
ret_t str_from_int(str_t* str, int32_t value);
/**
* @method str_from_uint32
*
* @param {str_t*} str str对象
* @param {uint32_t} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_uint32(str_t* str, uint32_t value);
/**
* @method str_from_int64
*
* @param {str_t*} str str对象
* @param {int64_t} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_int64(str_t* str, int64_t value);
/**
* @method str_from_uint64
*
* @param {str_t*} str str对象
* @param {uint64_t} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_uint64(str_t* str, uint64_t value);
/**
* @method str_from_float
*
* @param {str_t*} str str对象
* @param {double} v
* @param {double} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_float(str_t* str, double v);
ret_t str_from_float(str_t* str, double value);
/**
* @method str_from_value
* value初始化字符串
* @param {str_t*} str str对象
* @param {value_t} v value
* @param {value_t} value value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_from_value(str_t* str, const value_t* v);
ret_t str_from_value(str_t* str, const value_t* value);
/**
* @method str_from_wstr
@ -437,21 +467,21 @@ ret_t str_from_wstr_with_len(str_t* str, const wchar_t* wstr, uint32_t len);
* @method str_to_int
*
* @param {str_t*} str str对象
* @param {int32_t*} v
* @param {int32_t*} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_to_int(str_t* str, int32_t* v);
ret_t str_to_int(str_t* str, int32_t* value);
/**
* @method str_to_float
*
* @param {str_t*} str str对象
* @param {double*} v
* @param {double*} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t str_to_float(str_t* str, double* v);
ret_t str_to_float(str_t* str, double* value);
/**
* @method str_encode_hex

View File

@ -1,4 +1,5 @@
#include "tkc/str.h"
#include "tkc/utils.h"
#include "tkc/object_default.h"
#include "gtest/gtest.h"
#include <string>
@ -610,3 +611,35 @@ TEST(Str, encode_xml_entity) {
str_reset(s);
}
TEST(Str, from) {
str_t str;
str_t* s = NULL;
s = str_init(&str, 0);
ASSERT_EQ(str_from_int(s, 123), RET_OK);
ASSERT_EQ(str_eq(s, "123"), TRUE);
ASSERT_EQ(str_from_int(s, -123), RET_OK);
ASSERT_EQ(str_eq(s, "-123"), TRUE);
ASSERT_EQ(str_from_uint32(s, 123), RET_OK);
ASSERT_EQ(str_eq(s, "123"), TRUE);
ASSERT_EQ(str_from_uint32(s, 0xffffffff), RET_OK);
ASSERT_EQ(tk_atoul(s->str), 0xffffffff);
ASSERT_EQ(str_from_int64(s, 1234), RET_OK);
ASSERT_EQ(str_eq(s, "1234"), TRUE);
ASSERT_EQ(str_from_int64(s, -1234), RET_OK);
ASSERT_EQ(str_eq(s, "-1234"), TRUE);
ASSERT_EQ(str_from_uint64(s, 12345), RET_OK);
ASSERT_EQ(str_eq(s, "12345"), TRUE);
ASSERT_EQ(str_from_float(s, 1.1), RET_OK);
ASSERT_STREQ(s->str, "1.100000");
str_reset(s);
}