object: unify stringstream functionality
This commit is contained in:
117
object/string.c
117
object/string.c
@@ -347,123 +347,6 @@ static void string_to_string(struct b_object *obj, struct b_stringstream *out)
|
||||
b_stringstream_add(out, b_string_ptr(str));
|
||||
}
|
||||
|
||||
void b_strv_builder_begin(b_strv_builder *strv, char *buf, size_t max)
|
||||
{
|
||||
strv->strv_buf = buf;
|
||||
strv->strv_max = max;
|
||||
strv->strv_len = 0;
|
||||
strv->strv_alloc = 0;
|
||||
}
|
||||
|
||||
void b_strv_builder_begin_dynamic(b_strv_builder *strv)
|
||||
{
|
||||
strv->strv_buf = NULL;
|
||||
strv->strv_max = 0;
|
||||
strv->strv_len = 0;
|
||||
strv->strv_alloc = 1;
|
||||
}
|
||||
|
||||
static b_status strv_builder_push_string(
|
||||
b_strv_builder *strv, const char *s, size_t len)
|
||||
{
|
||||
if (strv->strv_len + len >= strv->strv_max && strv->strv_alloc == 1) {
|
||||
char *new_buf = realloc(strv->strv_buf, strv->strv_len + len + 1);
|
||||
if (!new_buf) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
strv->strv_buf = new_buf;
|
||||
strv->strv_max = strv->strv_len + len + 1;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (strv->strv_len < strv->strv_max) {
|
||||
strv->strv_buf[strv->strv_len++] = s[i];
|
||||
strv->strv_buf[strv->strv_len] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_strv_builder_add(struct b_strv_builder *strv, const char *str)
|
||||
{
|
||||
return strv_builder_push_string(strv, str, strlen(str));
|
||||
}
|
||||
|
||||
b_status b_strv_builder_addf(struct b_strv_builder *strv, const char *format, ...)
|
||||
{
|
||||
char str[1024];
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
size_t len = vsnprintf(str, sizeof str, format, arg);
|
||||
va_end(arg);
|
||||
|
||||
return strv_builder_push_string(strv, str, len);
|
||||
}
|
||||
|
||||
b_status b_strv_builder_addv(b_strv_builder *strv, const char **strs)
|
||||
{
|
||||
for (size_t i = 0; strs[i]; i++) {
|
||||
size_t len = strlen(strs[i]);
|
||||
b_status status = strv_builder_push_string(strv, strs[i], len);
|
||||
if (B_ERR(status)) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_strv_builder_addvl(b_strv_builder *strv, const char **strs, size_t count)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (!strs[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t len = strlen(strs[i]);
|
||||
b_status status = strv_builder_push_string(strv, strs[i], len);
|
||||
if (B_ERR(status)) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_strv_builder_add_many(b_strv_builder *strv, ...)
|
||||
{
|
||||
va_list arg;
|
||||
va_start(arg, strv);
|
||||
while (1) {
|
||||
const char *s = va_arg(arg, const char *);
|
||||
if (!s) {
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
size_t len = strlen(s);
|
||||
b_status status = strv_builder_push_string(strv, s, len);
|
||||
if (B_ERR(status)) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
char *b_strv_builder_end(b_strv_builder *strv)
|
||||
{
|
||||
char *out = strv->strv_buf;
|
||||
|
||||
strv->strv_alloc = 0;
|
||||
strv->strv_len = 0;
|
||||
strv->strv_max = 0;
|
||||
strv->strv_buf = NULL;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
char *b_strdup(const char *s)
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
|
||||
Reference in New Issue
Block a user