diff --git a/docs/changes.md b/docs/changes.md index b1d935c28..6646c2cb3 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,5 +1,8 @@ # 最新动态 +2024/11/19 + * 增加函数 str_append_format_padding + 2024/11/18 * 完善 atomic(感谢兆坤提供补丁) * 修复拼写错误(感谢兆坤提供补丁) diff --git a/src/tkc/str.c b/src/tkc/str.c index f3271575d..ea5ec1776 100644 --- a/src/tkc/str.c +++ b/src/tkc/str.c @@ -1135,6 +1135,29 @@ ret_t str_append_format(str_t* str, uint32_t size, const char* format, ...) { return RET_OK; } +ret_t str_append_format_padding(str_t* str, uint32_t size, const char* format, ...) { + va_list va; + int32_t ret = 0; + uint32_t old_size = 0; + return_value_if_fail(str != NULL && format != NULL, RET_BAD_PARAMS); + return_value_if_fail(str_extend(str, str->size + size + 1) == RET_OK, RET_OOM); + + old_size = str->size; + va_start(va, format); + ret = tk_vsnprintf(str->str + str->size, size, format, va); + va_end(va); + + return_value_if_fail(ret >= 0, RET_BAD_PARAMS); + str->size += ret; + + if (str->size < (old_size + size)) { + uint32_t n = (old_size + size) - str->size; + str_append_n_chars(str, ' ', n); + } + + return RET_OK; +} + ret_t str_append_json_pair(str_t* str, const char* key, const value_t* value) { ret_t ret; return_value_if_fail(str != NULL && key != NULL && value, RET_BAD_PARAMS); diff --git a/src/tkc/str.h b/src/tkc/str.h index bdd00a6d1..6ef9de5a8 100644 --- a/src/tkc/str.h +++ b/src/tkc/str.h @@ -810,6 +810,17 @@ ret_t str_format(str_t* str, uint32_t size, const char* format, ...); */ ret_t str_append_format(str_t* str, uint32_t size, const char* format, ...); +/** + * @method str_append_format_padding + * 通过格式追加字符串(字符串不够size长时,自动填充空格)。 + * @param {str_t*} str str对象。 + * @param {uint32_t} size format生成的字符串的最大长度(用于预先分配内存)。 + * @param {const char*} format 格式。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t str_append_format_padding(str_t* str, uint32_t size, const char* format, ...); + /** * @method str_append_json_pair * 追加json键值对。 diff --git a/tests/str_test.cc b/tests/str_test.cc index 47676c71f..6630bc2da 100644 --- a/tests/str_test.cc +++ b/tests/str_test.cc @@ -48,7 +48,7 @@ TEST(Str, shrink) { str_append(&s, "1234"); ASSERT_EQ(str_shrink(&s, 2), RET_OK); ASSERT_STREQ(s.str, "12"); - + ASSERT_EQ(str_shrink(&s, 20), RET_OK); ASSERT_STREQ(s.str, "12"); @@ -1080,4 +1080,45 @@ TEST(Str, str_append_json_pair) { ASSERT_STREQ(s->str, "\"x\":\"abc\""); str_reset(s); +} + +TEST(Str, append_format_ex) { + str_t s; + str_init(&s, 0); + str_append(&s, "Name Date Temperature Humidity\n"); + + str_append_format_padding(&s, 14, "Device%d", 1); + str_append_format_padding(&s, 14, "2024-11-01"); + str_append_format_padding(&s, 16, "%2.2f \xC2\xB0\x43", 26.5); + str_append_format(&s, 14, "50\n"); + + str_append_format_padding(&s, 14, "Device%d", 2); + str_append_format_padding(&s, 14, "2024-11-02"); + str_append_format_padding(&s, 16, "%2.2f \xC2\xB0\x43", (float)27); + str_append_format(&s, 14, "60\n"); + + log_debug("\n%s\n", s.str); + + str_clear(&s); + str_append_format_padding(&s, 14, "Device%d", 2); + ASSERT_STREQ(s.str, "Device2 "); + + str_clear(&s); + str_append_format_padding(&s, 14, "%2.4f", 2.5); + ASSERT_STREQ(s.str, "2.5000 "); + + str_clear(&s); + str_append_format_padding(&s, 14, "%2.4f", 27.0); + ASSERT_STREQ(s.str, "27.0000 "); + + str_clear(&s); + str_append_format_padding(&s, 14, "%2.4f", (float)27); + ASSERT_STREQ(s.str, "27.0000 "); + + str_clear(&s); + float v = 27; + str_append_format_padding(&s, 14, "%2.4f", v); + ASSERT_STREQ(s.str, "27.0000 "); + + str_reset(&s); } \ No newline at end of file