Skip to content

Commit

Permalink
add str_append_format_padding
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Nov 19, 2024
1 parent ceb68c4 commit d9d4bcb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 最新动态

2024/11/19
* 增加函数 str_append_format_padding

2024/11/18
* 完善 atomic(感谢兆坤提供补丁)
* 修复拼写错误(感谢兆坤提供补丁)
Expand Down
23 changes: 23 additions & 0 deletions src/tkc/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/tkc/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -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键值对。
Expand Down
43 changes: 42 additions & 1 deletion tests/str_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}

0 comments on commit d9d4bcb

Please sign in to comment.