bstr is very similar to b_stringstream, with the key difference being it can be stack-allocated. this allows you to write a complex formatted string to a stack-allocated character buffer with no heap memory allocation required. bstr also supports automatically-managed dynamic string buffers for unbounded string construction.
72 lines
2.2 KiB
C
72 lines
2.2 KiB
C
#ifndef BLUE_CORE_BSTR_H_
|
|
#define BLUE_CORE_BSTR_H_
|
|
|
|
#include <blue/core/misc.h>
|
|
#include <blue/core/status.h>
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
|
|
#define B_BSTR_MAGIC 0x5005500550055005ULL
|
|
|
|
struct b_rope;
|
|
|
|
enum b_bstr_flags {
|
|
B_BSTR_F_NONE = 0x00u,
|
|
B_BSTR_F_ALLOC = 0x01u,
|
|
};
|
|
|
|
typedef struct b_bstr {
|
|
uint64_t bstr_magic;
|
|
enum b_bstr_flags bstr_flags;
|
|
char *bstr_buf;
|
|
/* total number of characters in bstr_buf, not including null terminator */
|
|
size_t bstr_len;
|
|
/* number of bytes allocated for bstr_buf (includes space for the null
|
|
* terminator) */
|
|
size_t bstr_capacity;
|
|
int *bstr_istack;
|
|
int bstr_add_indent;
|
|
size_t bstr_istack_ptr, bstr_istack_size;
|
|
} b_bstr;
|
|
|
|
BLUE_API void b_bstr_begin(b_bstr *strv, char *buf, size_t max);
|
|
BLUE_API void b_bstr_begin_dynamic(b_bstr *strv);
|
|
BLUE_API char *b_bstr_end(b_bstr *strv);
|
|
BLUE_API b_status b_bstr_reserve(b_bstr *strv, size_t len);
|
|
|
|
static inline size_t b_bstr_get_size(const b_bstr *str)
|
|
{
|
|
return str->bstr_len;
|
|
}
|
|
|
|
static inline size_t b_bstr_get_capacity(const b_bstr *str)
|
|
{
|
|
return str->bstr_capacity;
|
|
}
|
|
|
|
BLUE_API b_status b_bstr_push_indent(b_bstr *strv, int indent);
|
|
BLUE_API b_status b_bstr_pop_indent(b_bstr *strv);
|
|
|
|
BLUE_API b_status b_bstr_write_char(b_bstr *strv, char c);
|
|
BLUE_API b_status b_bstr_write_chars(
|
|
b_bstr *strv, const char *cs, size_t len, size_t *nr_written);
|
|
BLUE_API b_status b_bstr_write_cstr(
|
|
b_bstr *strv, const char *str, size_t *nr_written);
|
|
BLUE_API b_status b_bstr_write_cstr_list(
|
|
b_bstr *strv, const char **strs, size_t *nr_written);
|
|
BLUE_API b_status b_bstr_write_cstr_array(
|
|
b_bstr *strv, const char **strs, size_t count, size_t *nr_written);
|
|
BLUE_API b_status b_bstr_write_cstr_varg(b_bstr *strv, size_t *nr_written, ...);
|
|
BLUE_API b_status b_bstr_write_rope(
|
|
b_bstr *strv, const struct b_rope *rope, size_t *nr_written);
|
|
BLUE_API b_status b_bstr_write_fmt(
|
|
b_bstr *strv, size_t *nr_written, const char *format, ...);
|
|
BLUE_API b_status b_bstr_write_vfmt(
|
|
b_bstr *strv, size_t *nr_written, const char *format, va_list arg);
|
|
|
|
BLUE_API char *b_bstr_rope(const struct b_rope *rope, size_t *nr_written);
|
|
BLUE_API char *b_bstr_fmt(size_t *nr_written, const char *format, ...);
|
|
BLUE_API char *b_bstr_vfmt(size_t *nr_written, const char *format, va_list arg);
|
|
|
|
#endif
|