1 #include <kiba/format/buffer.h>
5 b8 format_buffer_create_static(
format_buffer *buf, format_buffer_mode mode,
char *buffer, usize length) {
19 KB_ERROR(
"can not allocate initial memory for the format buf");
22 if (!format_buffer_create_static(buf, FORMAT_BUFFER_WRITE, data, initial_length)) {
36 static b8 format_buffer_resize(
format_buffer *buf, usize min_new_size) {
37 KB_ASSERT_DEBUG(buf->mode == FORMAT_BUFFER_WRITE,
"only writing format_buffer may call resize");
39 KB_WARN(
"static format_buffer will not be resized");
42 const usize new_length = (min_new_size / buf->length + 1) * buf->length;
45 KB_WARN(
"could not resize dynamic format_buffer");
49 buf->length = new_length;
53 b8 format_buffer_advance(
format_buffer *buf) {
return buf->head++ < buf->length; }
55 void format_buffer_reset(
format_buffer *buf) { buf->head = 0; }
58 KB_ASSERT_DEBUG(buf->mode == FORMAT_BUFFER_WRITE,
"only writing format_buffer may add content to the buffer");
59 if (buf->head == buf->length && !format_buffer_resize(buf, buf->length + 1)) {
62 buf->data[buf->head++] = c;
66 b8 format_buffer_add_char_n(
format_buffer *buf,
char c, usize n) {
67 KB_ASSERT_DEBUG(buf->mode == FORMAT_BUFFER_WRITE,
"only writing format_buffer may add content to the buffer");
68 usize remaining_length = buf->length - buf->head;
69 if (remaining_length < n && !format_buffer_resize(buf, buf->length + n - remaining_length)) {
77 b8 format_buffer_add_chars(
format_buffer *buf,
const char *chars, usize length) {
78 KB_ASSERT_DEBUG(buf->mode == FORMAT_BUFFER_WRITE,
"only writing format_buffer may add content to the buffer");
79 usize remaining_length = buf->length - buf->head;
80 if (remaining_length < length && !format_buffer_resize(buf, buf->length + length - remaining_length)) {
83 memory_copy(buf->data + buf->head, chars,
sizeof(
char) * length);
105 b8 format_buffer_peak_char(
const format_buffer *buf,
char *c) {
106 KB_ASSERT_DEBUG(buf->mode == FORMAT_BUFFER_READ,
"only reading format_buffer may read content from the buffer");
107 if (buf->head < buf->length) {
108 *c = buf->data[buf->head];
115 return format_buffer_peak_char(buf, c) && format_buffer_advance(buf);
120 return format_buffer_peak_char(buf, &actual) && c == actual && format_buffer_advance(buf);
123 b8 format_buffer_match_chars(
format_buffer *buf,
const char *c, usize length) {
124 for (usize i = 0; i < length; ++i) {
125 if (!format_buffer_match_char(buf, c[i])) {
void * allocator_allocate(allocator *alloc, usize size)
Allocate unaligned memory.
void * allocator_reallocate(allocator *alloc, void *original, usize new_size)
Reallocate memory to unaligned block.
void allocator_free(allocator *alloc, void *mem)
Give back memory to the allocator.
void * memory_copy(void *dst, const void *src, usize size)
Copy memory.
void * memory_zero(void *mem, usize size)
Zero out memory.
void * memory_set(void *mem, u8 byte, usize size)
Set content of memory block.
Lightweight layer between platform and other engine components to enable tracing/monitoring.
const char * string_view_data(const string_view view)
Get raw pointer to beginning of the string_view data.
string_view string_view_from_string(const string str)
Create a new string view of a whole string.
#define KB_NULL
Value of an invalid ptr (nullptr).
#define KB_ASSERT(expr,...)
Perform runtime assertion and log failures.
#define KB_WARN(...)
Log entry with warn log level.
#define KB_ERROR(...)
Log entry with error log level.
#define KB_ASSERT_DEBUG(expr,...)
Perform runtime debug assertion and log failures.
Central allocator structure.
Non owning views on actual strings.
b8 is_valid
Indicates if the view is valid and can be used/accessed.
usize length
The length of the view.