object: unify stringstream functionality
This commit is contained in:
@@ -18,13 +18,6 @@ typedef enum b_strlen_flags {
|
||||
B_STRLEN_IGNORE_MOD = 0x02u,
|
||||
} b_strlen_flags;
|
||||
|
||||
typedef struct b_strv_builder {
|
||||
char *strv_buf;
|
||||
size_t strv_len;
|
||||
size_t strv_max;
|
||||
unsigned char strv_alloc;
|
||||
} b_strv_builder;
|
||||
|
||||
BLUE_API b_string *b_string_create(void);
|
||||
BLUE_API b_string *b_string_create_from_cstr(const char *s);
|
||||
BLUE_API b_string *b_string_create_from_c(char c, size_t count);
|
||||
@@ -60,17 +53,6 @@ BLUE_API size_t b_string_get_capacity(const b_string *str);
|
||||
|
||||
BLUE_API const char *b_string_ptr(const b_string *str);
|
||||
|
||||
BLUE_API void b_strv_builder_begin(b_strv_builder *strv, char *buf, size_t max);
|
||||
BLUE_API void b_strv_builder_begin_dynamic(b_strv_builder *strv);
|
||||
|
||||
BLUE_API b_status b_strv_builder_add(b_strv_builder *strv, const char *str);
|
||||
BLUE_API b_status b_strv_builder_addf(b_strv_builder *strv, const char *format, ...);
|
||||
BLUE_API b_status b_strv_builder_addv(b_strv_builder *strv, const char **strs);
|
||||
BLUE_API b_status b_strv_builder_addvl(
|
||||
b_strv_builder *strv, const char **strs, size_t count);
|
||||
BLUE_API b_status b_strv_builder_add_many(b_strv_builder *strv, ...);
|
||||
BLUE_API char *b_strv_builder_end(b_strv_builder *strv);
|
||||
|
||||
BLUE_API char *b_strdup(const char *s);
|
||||
BLUE_API size_t b_strlen(const char *s, b_strlen_flags flags);
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
#ifndef BLUELIB_STRING_FORMATTER_H
|
||||
#define BLUELIB_STRING_FORMATTER_H
|
||||
|
||||
#include <blue/object/string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef struct b_stringstream b_stringstream;
|
||||
|
||||
BLUE_API b_stringstream *b_stringstream_create(void);
|
||||
BLUE_API b_string *b_stringstream_end(b_stringstream *f);
|
||||
BLUE_API void b_stringstream_destroy(b_stringstream *f);
|
||||
BLUE_API const b_string *b_stringstream_str(b_stringstream *f);
|
||||
BLUE_API const char *b_stringstream_cstr(b_stringstream *f);
|
||||
BLUE_API void b_stringstream_clear(b_stringstream *f);
|
||||
|
||||
BLUE_API void b_stringstream_push_indent(b_stringstream *f, int indent);
|
||||
BLUE_API void b_stringstream_push_indent_abs(b_stringstream *f, int indent);
|
||||
BLUE_API void b_stringstream_pop_indent(b_stringstream *f);
|
||||
|
||||
BLUE_API void b_stringstream_add(b_stringstream *f, const char *s);
|
||||
BLUE_API void b_stringstream_addf(b_stringstream *f, const char *format, ...);
|
||||
BLUE_API void b_stringstream_addvf(b_stringstream *f, const char *format, va_list arg);
|
||||
BLUE_API void b_stringstream_add_str(b_stringstream *f, const b_string *str);
|
||||
|
||||
#endif
|
||||
@@ -11,7 +11,7 @@
|
||||
#define B_UUID_STRING_MAX 37
|
||||
|
||||
struct b_string;
|
||||
struct b_strv_builder;
|
||||
struct b_stringstream;
|
||||
|
||||
typedef struct b_uuid b_uuid;
|
||||
|
||||
@@ -42,8 +42,8 @@ static inline void b_uuid_release(b_uuid *uuid)
|
||||
|
||||
BLUE_API b_status b_uuid_to_string(const b_uuid *uuid, struct b_string *out);
|
||||
BLUE_API b_status b_uuid_to_cstr(const b_uuid *uuid, char out[B_UUID_STRING_MAX]);
|
||||
BLUE_API b_status b_uuid_to_strv_builder(
|
||||
const b_uuid *uuid, struct b_strv_builder *out);
|
||||
BLUE_API b_status b_uuid_to_stringstream(
|
||||
const b_uuid *uuid, struct b_stringstream *out);
|
||||
BLUE_API void b_uuid_get_bytes(
|
||||
const b_uuid *uuid, unsigned char bytes[B_UUID_NBYTES]);
|
||||
BLUE_API void b_uuid_get_uuid_bytes(const b_uuid *uuid, b_uuid_bytes *bytes);
|
||||
|
||||
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);
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "uuid.h"
|
||||
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/object/string.h>
|
||||
#include <blue/object/type.h>
|
||||
#include <blue/object/uuid.h>
|
||||
#include <blue/object/string.h>
|
||||
#include <ctype.h>
|
||||
#include "uuid.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static struct b_object_type uuid_type = {
|
||||
.t_name = "corelib::uuid",
|
||||
@@ -16,7 +18,8 @@ static struct b_object_type uuid_type = {
|
||||
|
||||
struct b_uuid *b_uuid_create(void)
|
||||
{
|
||||
struct b_uuid *out = (struct b_uuid *)b_object_type_instantiate(&uuid_type);
|
||||
struct b_uuid *out
|
||||
= (struct b_uuid *)b_object_type_instantiate(&uuid_type);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -25,9 +28,10 @@ struct b_uuid *b_uuid_create(void)
|
||||
}
|
||||
|
||||
struct b_uuid *b_uuid_create_from_bytes(
|
||||
unsigned char u00, unsigned char u01, unsigned char u02, unsigned char u03,
|
||||
unsigned char u04, unsigned char u05, unsigned char u06, unsigned char u07,
|
||||
unsigned char u08, unsigned char u09, unsigned char u10, unsigned char u11,
|
||||
unsigned char u00, unsigned char u01, unsigned char u02,
|
||||
unsigned char u03, unsigned char u04, unsigned char u05,
|
||||
unsigned char u06, unsigned char u07, unsigned char u08,
|
||||
unsigned char u09, unsigned char u10, unsigned char u11,
|
||||
unsigned char u12, unsigned char u13, unsigned char u14, unsigned char u15)
|
||||
{
|
||||
struct b_uuid *uuid = b_uuid_create();
|
||||
@@ -148,20 +152,27 @@ b_status b_uuid_to_string(const struct b_uuid *uuid, struct b_string *out)
|
||||
|
||||
b_status b_uuid_to_cstr(const struct b_uuid *uuid, char out[B_UUID_STRING_MAX])
|
||||
{
|
||||
snprintf(out, B_UUID_STRING_MAX,
|
||||
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
||||
uuid->uuid_bytes.uuid_bytes[ 0], uuid->uuid_bytes.uuid_bytes[ 1], uuid->uuid_bytes.uuid_bytes[ 2], uuid->uuid_bytes.uuid_bytes[ 3],
|
||||
uuid->uuid_bytes.uuid_bytes[ 4], uuid->uuid_bytes.uuid_bytes[ 5], uuid->uuid_bytes.uuid_bytes[ 6], uuid->uuid_bytes.uuid_bytes[ 7],
|
||||
uuid->uuid_bytes.uuid_bytes[ 8], uuid->uuid_bytes.uuid_bytes[ 9], uuid->uuid_bytes.uuid_bytes[10], uuid->uuid_bytes.uuid_bytes[11],
|
||||
uuid->uuid_bytes.uuid_bytes[12], uuid->uuid_bytes.uuid_bytes[13], uuid->uuid_bytes.uuid_bytes[14], uuid->uuid_bytes.uuid_bytes[15]);
|
||||
snprintf(
|
||||
out, B_UUID_STRING_MAX,
|
||||
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%"
|
||||
"02x%02x",
|
||||
uuid->uuid_bytes.uuid_bytes[0], uuid->uuid_bytes.uuid_bytes[1],
|
||||
uuid->uuid_bytes.uuid_bytes[2], uuid->uuid_bytes.uuid_bytes[3],
|
||||
uuid->uuid_bytes.uuid_bytes[4], uuid->uuid_bytes.uuid_bytes[5],
|
||||
uuid->uuid_bytes.uuid_bytes[6], uuid->uuid_bytes.uuid_bytes[7],
|
||||
uuid->uuid_bytes.uuid_bytes[8], uuid->uuid_bytes.uuid_bytes[9],
|
||||
uuid->uuid_bytes.uuid_bytes[10], uuid->uuid_bytes.uuid_bytes[11],
|
||||
uuid->uuid_bytes.uuid_bytes[12], uuid->uuid_bytes.uuid_bytes[13],
|
||||
uuid->uuid_bytes.uuid_bytes[14], uuid->uuid_bytes.uuid_bytes[15]);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_uuid_to_strv_builder(const struct b_uuid *uuid, struct b_strv_builder *out)
|
||||
b_status b_uuid_to_stringstream(
|
||||
const struct b_uuid *uuid, struct b_stringstream *out)
|
||||
{
|
||||
char str[B_UUID_STRING_MAX];
|
||||
b_uuid_to_cstr(uuid, str);
|
||||
b_strv_builder_add(out, str);
|
||||
b_stringstream_add(out, str);
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user