Add string formatting to Str8
This commit is contained in:
parent
cda7e413e2
commit
80bd68f313
@ -1,7 +1,9 @@
|
|||||||
#include "str8.h"
|
#include "str8.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
#include "mem_allocator.h"
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
@ -150,7 +152,7 @@ Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Str8 *wapp_str8_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) {
|
Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) {
|
||||||
if (!allocator || !dst || !src) {
|
if (!allocator || !dst || !src) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -226,6 +228,26 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) {
|
|||||||
memcpy(dst, src->buf, to_copy);
|
memcpy(dst, src->buf, to_copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wapp_str8_format(Str8 *dst, const char *format, ...) {
|
||||||
|
if (!dst || !format) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
va_list args1;
|
||||||
|
va_list args2;
|
||||||
|
|
||||||
|
va_start(args1, format);
|
||||||
|
va_copy(args2, args1);
|
||||||
|
|
||||||
|
u64 total_size = vsnprintf(NULL, 0, format, args1);
|
||||||
|
dst->size = total_size <= dst->capacity ? total_size : dst->capacity;
|
||||||
|
|
||||||
|
vsnprintf((char *)(dst->buf), dst->capacity, format, args2);
|
||||||
|
|
||||||
|
va_end(args1);
|
||||||
|
va_end(args2);
|
||||||
|
}
|
||||||
|
|
||||||
i64 wapp_str8_find(Str8RO *str, Str8RO substr) {
|
i64 wapp_str8_find(Str8RO *str, Str8RO substr) {
|
||||||
if (substr.size > str->size) {
|
if (substr.size > str->size) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -67,6 +67,7 @@ Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity);
|
|||||||
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str);
|
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str);
|
||||||
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str);
|
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str);
|
||||||
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end);
|
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end);
|
||||||
|
Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src);
|
||||||
// Only needed for allocators like malloc where each allocation has to be freed on its own.
|
// Only needed for allocators like malloc where each allocation has to be freed on its own.
|
||||||
// No need to use it for allocators like Arena.
|
// No need to use it for allocators like Arena.
|
||||||
void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str);
|
void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str);
|
||||||
@ -79,11 +80,11 @@ void wapp_str8_set(Str8 *str, u64 index, c8 c);
|
|||||||
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
|
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
|
||||||
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
|
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
|
||||||
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end);
|
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end);
|
||||||
Str8 *wapp_str8_concat(const Allocator *allocator, Str8 *dst, Str8RO *src);
|
|
||||||
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
|
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
|
||||||
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src);
|
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src);
|
||||||
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src);
|
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src);
|
||||||
void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
|
void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
|
||||||
|
void wapp_str8_format(Str8 *dst, const char *format, ...);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Str8 find functions
|
* Str8 find functions
|
||||||
|
@ -32,7 +32,7 @@ CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_bu
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Redirect output
|
// Redirect output
|
||||||
cmd_str = wapp_str8_concat(&arena, cmd_str, &wapp_str8_lit_ro(" 2>&1"));
|
cmd_str = wapp_str8_alloc_concat(&arena, cmd_str, &wapp_str8_lit_ro(" 2>&1"));
|
||||||
|
|
||||||
CMDResult output = execute_command(cmd_str, out_handling, out_buf);
|
CMDResult output = execute_command(cmd_str, out_handling, out_buf);
|
||||||
|
|
||||||
|
@ -270,7 +270,7 @@ TestFuncResult test_str8_slice(void) {
|
|||||||
return wapp_tester_result(result);
|
return wapp_tester_result(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
TestFuncResult test_str8_concat(void) {
|
TestFuncResult test_str8_alloc_concat(void) {
|
||||||
bool result;
|
bool result;
|
||||||
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
|
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
|
||||||
|
|
||||||
@ -282,10 +282,10 @@ TestFuncResult test_str8_concat(void) {
|
|||||||
|
|
||||||
Str8 *output;
|
Str8 *output;
|
||||||
|
|
||||||
output = wapp_str8_concat(&arena, &str, &suffix1);
|
output = wapp_str8_alloc_concat(&arena, &str, &suffix1);
|
||||||
result = output->size == concat1.size && wapp_str8_equal(output, &concat1);
|
result = output->size == concat1.size && wapp_str8_equal(output, &concat1);
|
||||||
|
|
||||||
output = wapp_str8_concat(&arena, output, &suffix2);
|
output = wapp_str8_alloc_concat(&arena, output, &suffix2);
|
||||||
result = result && output->size == concat2.size && wapp_str8_equal(output, &concat2);
|
result = result && output->size == concat2.size && wapp_str8_equal(output, &concat2);
|
||||||
|
|
||||||
wapp_mem_arena_allocator_destroy(&arena);
|
wapp_mem_arena_allocator_destroy(&arena);
|
||||||
|
@ -14,12 +14,12 @@ TestFuncResult test_str8_alloc_buf(void);
|
|||||||
TestFuncResult test_str8_alloc_cstr(void);
|
TestFuncResult test_str8_alloc_cstr(void);
|
||||||
TestFuncResult test_str8_alloc_str8(void);
|
TestFuncResult test_str8_alloc_str8(void);
|
||||||
TestFuncResult test_str8_alloc_substr(void);
|
TestFuncResult test_str8_alloc_substr(void);
|
||||||
|
TestFuncResult test_str8_alloc_concat(void);
|
||||||
TestFuncResult test_str8_get_index_within_bounds(void);
|
TestFuncResult test_str8_get_index_within_bounds(void);
|
||||||
TestFuncResult test_str8_get_index_out_of_bounds(void);
|
TestFuncResult test_str8_get_index_out_of_bounds(void);
|
||||||
TestFuncResult test_str8_set(void);
|
TestFuncResult test_str8_set(void);
|
||||||
TestFuncResult test_str8_equal(void);
|
TestFuncResult test_str8_equal(void);
|
||||||
TestFuncResult test_str8_slice(void);
|
TestFuncResult test_str8_slice(void);
|
||||||
TestFuncResult test_str8_concat(void);
|
|
||||||
TestFuncResult test_str8_concat_capped(void);
|
TestFuncResult test_str8_concat_capped(void);
|
||||||
TestFuncResult test_str8_copy_cstr_capped(void);
|
TestFuncResult test_str8_copy_cstr_capped(void);
|
||||||
TestFuncResult test_str8_copy_str8_capped(void);
|
TestFuncResult test_str8_copy_str8_capped(void);
|
||||||
|
@ -20,12 +20,12 @@ int main(void) {
|
|||||||
test_str8_alloc_cstr,
|
test_str8_alloc_cstr,
|
||||||
test_str8_alloc_str8,
|
test_str8_alloc_str8,
|
||||||
test_str8_alloc_substr,
|
test_str8_alloc_substr,
|
||||||
|
test_str8_alloc_concat,
|
||||||
test_str8_get_index_within_bounds,
|
test_str8_get_index_within_bounds,
|
||||||
test_str8_get_index_out_of_bounds,
|
test_str8_get_index_out_of_bounds,
|
||||||
test_str8_set,
|
test_str8_set,
|
||||||
test_str8_equal,
|
test_str8_equal,
|
||||||
test_str8_slice,
|
test_str8_slice,
|
||||||
test_str8_concat,
|
|
||||||
test_str8_concat_capped,
|
test_str8_concat_capped,
|
||||||
test_str8_copy_cstr_capped,
|
test_str8_copy_cstr_capped,
|
||||||
test_str8_copy_str8_capped,
|
test_str8_copy_str8_capped,
|
||||||
|
Loading…
Reference in New Issue
Block a user