Add functions to dynamically allocate strings
This commit is contained in:
parent
86fe867011
commit
023c74d8d9
@ -1,7 +1,73 @@
|
|||||||
#include "str8.h"
|
#include "str8.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
Str8 *wapp_str8_buf_alloc(const Allocator *allocator, u64 capacity) {
|
||||||
|
Str8 *str = NULL;
|
||||||
|
|
||||||
|
if (!allocator) {
|
||||||
|
goto RETURN_STR8;
|
||||||
|
}
|
||||||
|
|
||||||
|
str = wapp_mem_allocator_alloc(allocator, sizeof(Str8));
|
||||||
|
if (!str) {
|
||||||
|
goto RETURN_STR8;
|
||||||
|
}
|
||||||
|
|
||||||
|
c8 *buf = wapp_mem_allocator_alloc(allocator, sizeof(c8) * capacity);
|
||||||
|
if (!buf) {
|
||||||
|
wapp_mem_allocator_free(allocator, (void **)&str);
|
||||||
|
goto RETURN_STR8;
|
||||||
|
}
|
||||||
|
|
||||||
|
str->buf = buf;
|
||||||
|
str->size = 0;
|
||||||
|
str->capacity = capacity;
|
||||||
|
|
||||||
|
RETURN_STR8:
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str) {
|
||||||
|
Str8 *output = NULL;
|
||||||
|
|
||||||
|
if (!allocator || !str) {
|
||||||
|
goto RETURN_ALLOC_CSTR;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(str);
|
||||||
|
output = wapp_str8_buf_alloc(allocator, length * 2);
|
||||||
|
if (!output) {
|
||||||
|
goto RETURN_ALLOC_CSTR;
|
||||||
|
}
|
||||||
|
|
||||||
|
output->size = length;
|
||||||
|
memcpy(output->buf, str, length);
|
||||||
|
|
||||||
|
RETURN_ALLOC_CSTR:
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str) {
|
||||||
|
Str8 *output = NULL;
|
||||||
|
|
||||||
|
if (!allocator || !str) {
|
||||||
|
goto RETURN_ALLOC_STR8;
|
||||||
|
}
|
||||||
|
|
||||||
|
output = wapp_str8_buf_alloc(allocator, str->capacity);
|
||||||
|
if (!output) {
|
||||||
|
goto RETURN_ALLOC_STR8;
|
||||||
|
}
|
||||||
|
|
||||||
|
output->size = str->size;
|
||||||
|
memcpy(output->buf, str->buf, str->size);
|
||||||
|
|
||||||
|
RETURN_ALLOC_STR8:
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
c8 wapp_str8_get(const Str8 *str, u64 index) {
|
c8 wapp_str8_get(const Str8 *str, u64 index) {
|
||||||
if (index >= str->size) {
|
if (index >= str->size) {
|
||||||
return '\0';
|
return '\0';
|
||||||
@ -18,6 +84,22 @@ void wapp_str8_set(Str8 *str, u64 index, c8 c) {
|
|||||||
str->buf[index] = c;
|
str->buf[index] = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end) {
|
||||||
|
if (start >= str->size || start >= end) {
|
||||||
|
return (Str8RO){0};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end > str->size) {
|
||||||
|
end = str->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (Str8RO){
|
||||||
|
.capacity = end - start,
|
||||||
|
.size = end - start,
|
||||||
|
.buf = str->buf + start,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define STR8_H
|
#define STR8_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@ -32,8 +33,13 @@ typedef const Str8 Str8RO;
|
|||||||
.size = sizeof(STRING) - 1, \
|
.size = sizeof(STRING) - 1, \
|
||||||
.buf = (c8 *)STRING})
|
.buf = (c8 *)STRING})
|
||||||
|
|
||||||
|
Str8 *wapp_str8_buf_alloc(const Allocator *allocator, u64 capacity);
|
||||||
|
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str);
|
||||||
|
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str);
|
||||||
|
|
||||||
c8 wapp_str8_get(Str8RO *str, u64 index);
|
c8 wapp_str8_get(Str8RO *str, u64 index);
|
||||||
void wapp_str8_set(Str8 *str, u64 index, c8 c);
|
void wapp_str8_set(Str8 *str, u64 index, c8 c);
|
||||||
|
Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end);
|
||||||
|
|
||||||
i64 wapp_str8_find(Str8RO *str, Str8RO substr);
|
i64 wapp_str8_find(Str8RO *str, Str8RO substr);
|
||||||
i64 wapp_str8_rfind(Str8RO *str, Str8RO substr);
|
i64 wapp_str8_rfind(Str8RO *str, Str8RO substr);
|
||||||
|
Loading…
Reference in New Issue
Block a user