diff options
author | Anton Kling <anton@kling.gg> | 2024-10-13 13:33:29 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-10-13 13:34:01 +0200 |
commit | a67d4744d78fc58846f29667ae49cee70fa0bf16 (patch) | |
tree | 848dc06f4cf19d4a6560b7bea604d8177f06e7f7 /userland/libc/include | |
parent | edca64134a9873f32379b2b83bd925ecca1c19b1 (diff) |
libc/sb: Refactor string builder to allow for none malloc allocations
A lot of code written usually has means of doing allocations in a more
optimal way than having the string builder library doing it itself.
For example a temporary buffer can be allocated on the stack and the
string builder functions can then make use of this buffer without ever
having to run malloc/free(which would be expensive)
Diffstat (limited to 'userland/libc/include')
-rw-r--r-- | userland/libc/include/tb/sb.h | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/userland/libc/include/tb/sb.h b/userland/libc/include/tb/sb.h index c2fa1cf..8493997 100644 --- a/userland/libc/include/tb/sb.h +++ b/userland/libc/include/tb/sb.h @@ -2,25 +2,29 @@ #define SB_H #include "sv.h" #include <stddef.h> +#include <stdint.h> struct sb { char *string; size_t length; size_t capacity; + uint8_t prebuffer; }; struct sv; -void sb_init(struct sb *ctx, size_t starting_capacity); +void sb_init(struct sb *ctx); +int sb_init_capacity(struct sb *ctx, size_t starting_capacity); +void sb_init_buffer(struct sb *ctx, char *buffer, size_t size); void sb_free(struct sb *ctx); void sb_reset(struct sb *ctx); int sb_isempty(const struct sb *ctx); -void sb_append_char(struct sb *ctx, char c); +int sb_append_char(struct sb *ctx, char c); int sb_delete_right(struct sb *ctx, size_t n); -void sb_append(struct sb *ctx, const char *s); -void sb_append_buffer(struct sb *ctx, const char *buffer, +int sb_append(struct sb *ctx, const char *s); +int sb_append_buffer(struct sb *ctx, const char *buffer, size_t length); -void sb_append_sv(struct sb *ctx, struct sv sv); -void sb_prepend_sv(struct sb *ctx, struct sv sv); -void sb_prepend_buffer(struct sb *ctx, const char *buffer, size_t length); +int sb_append_sv(struct sb *ctx, struct sv sv); +int sb_prepend_sv(struct sb *ctx, struct sv sv); +int sb_prepend_buffer(struct sb *ctx, const char *buffer, size_t length); #endif |