Compare commits
No commits in common. "3f9a9088605c3f14a5c7c41a5019e3f37654f9d8" and "970c588d660687212bb4e27e7050ab852ac10b8a" have entirely different histories.
3f9a908860
...
970c588d66
@ -2,7 +2,6 @@
|
|||||||
#define DSTR_H
|
#define DSTR_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -10,8 +9,8 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct dstr String;
|
typedef struct dstr String;
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity, Allocator *allocator);
|
String *wapp_dstr_with_capacity(u64 capacity);
|
||||||
String *wapp_dstr_from_string(const char *str, Allocator *allocator);
|
String *wapp_dstr_from_string(const char *str);
|
||||||
void wapp_dstr_update(String **dst, const char *src);
|
void wapp_dstr_update(String **dst, const char *src);
|
||||||
void wapp_dstr_free(String **str);
|
void wapp_dstr_free(String **str);
|
||||||
void wapp_dstr_concat(String **dst, const char *src);
|
void wapp_dstr_concat(String **dst, const char *src);
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
#include "dstr.h"
|
#include "dstr.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
|
||||||
#include "mem_libc.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -11,34 +9,24 @@
|
|||||||
#define CAPACITY_SCALAR 8
|
#define CAPACITY_SCALAR 8
|
||||||
|
|
||||||
struct dstr {
|
struct dstr {
|
||||||
Allocator allocator;
|
|
||||||
u64 capacity;
|
u64 capacity;
|
||||||
u64 size;
|
u64 size;
|
||||||
char buf[];
|
char buf[];
|
||||||
};
|
};
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity, Allocator *allocator) {
|
String *wapp_dstr_with_capacity(u64 capacity) {
|
||||||
Allocator alloc;
|
String *out = (String *)calloc(1, sizeof(String) + capacity + 1);
|
||||||
if (allocator) {
|
|
||||||
alloc = *allocator;
|
|
||||||
} else {
|
|
||||||
alloc = wapp_mem_libc_allocator();
|
|
||||||
}
|
|
||||||
|
|
||||||
String *out =
|
|
||||||
(String *)wapp_mem_allocator_alloc(&alloc, sizeof(String) + capacity + 1);
|
|
||||||
if (!out) {
|
if (!out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->allocator = alloc;
|
|
||||||
out->capacity = capacity;
|
out->capacity = capacity;
|
||||||
out->size = 0;
|
out->size = 0;
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
String *wapp_dstr_from_string(const char *str, Allocator *allocator) {
|
String *wapp_dstr_from_string(const char *str) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -47,13 +35,14 @@ String *wapp_dstr_from_string(const char *str, Allocator *allocator) {
|
|||||||
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
String *out = wapp_dstr_with_capacity(capacity, allocator);
|
String *out = wapp_dstr_with_capacity(capacity);
|
||||||
|
|
||||||
if (!out) {
|
if (!out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->size = length;
|
out->size = length;
|
||||||
strncpy(out->buf, str, length + 1);
|
strncpy(out->buf, str, length);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@ -67,24 +56,24 @@ void wapp_dstr_update(String **dst, const char *src) {
|
|||||||
|
|
||||||
String *str = *dst;
|
String *str = *dst;
|
||||||
|
|
||||||
if (length < str->capacity) {
|
if (length <= str->capacity) {
|
||||||
memset(str->buf, 0, str->capacity);
|
memset(str->buf, 0, str->capacity);
|
||||||
|
|
||||||
str->size = length;
|
str->size = length;
|
||||||
|
|
||||||
strncpy(str->buf, src, length + 1);
|
strncpy(str->buf, src, length);
|
||||||
} else {
|
} else {
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
String *tmp = (String *)wapp_mem_allocator_realloc(
|
String *tmp = (String *)realloc(*dst, sizeof(String) + capacity + 1);
|
||||||
&(str->allocator), *dst, sizeof(String) + capacity + 1);
|
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
tmp->capacity = capacity;
|
||||||
tmp->size = length;
|
tmp->size = length;
|
||||||
strncpy(tmp->buf, src, length + 1);
|
strncpy(tmp->buf, src, length);
|
||||||
|
|
||||||
*dst = tmp;
|
*dst = tmp;
|
||||||
}
|
}
|
||||||
@ -95,8 +84,8 @@ void wapp_dstr_free(String **str) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String *str_ptr = *str;
|
free(*str);
|
||||||
wapp_mem_allocator_free(&(str_ptr->allocator), (void **)str);
|
*str = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_dstr_concat(String **dst, const char *src) {
|
void wapp_dstr_concat(String **dst, const char *src) {
|
||||||
@ -105,6 +94,7 @@ void wapp_dstr_concat(String **dst, const char *src) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u64 src_length = strlen(src);
|
u64 src_length = strlen(src);
|
||||||
|
|
||||||
if (src_length == 0) {
|
if (src_length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -115,7 +105,7 @@ void wapp_dstr_concat(String **dst, const char *src) {
|
|||||||
memset(str, 0, new_length + 1);
|
memset(str, 0, new_length + 1);
|
||||||
|
|
||||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||||
strncat(str, src, new_length + 1 - (*dst)->size);
|
strncat(str, src, src_length);
|
||||||
|
|
||||||
wapp_dstr_update(dst, str);
|
wapp_dstr_update(dst, str);
|
||||||
}
|
}
|
||||||
@ -141,11 +131,10 @@ void wapp_dstr_resize(String **str) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String *str_ptr = *str;
|
|
||||||
u64 capacity = (*str)->size;
|
u64 capacity = (*str)->size;
|
||||||
|
|
||||||
String *tmp = (String *)wapp_mem_allocator_realloc(
|
String *tmp = (String *)realloc(*str, sizeof(String) + capacity + 1);
|
||||||
&(str_ptr->allocator), *str, sizeof(String) + capacity + 1);
|
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -169,7 +158,7 @@ void wapp_dstr_print(const String *str) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%.*s\n", (i32)str->size, str->buf);
|
printf("%s\n", str->buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
i64 wapp_dstr_find(const String *str, const char *substr) {
|
i64 wapp_dstr_find(const String *str, const char *substr) {
|
||||||
@ -178,6 +167,7 @@ i64 wapp_dstr_find(const String *str, const char *substr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u64 substr_length = strlen(substr);
|
u64 substr_length = strlen(substr);
|
||||||
|
|
||||||
if (substr_length == 0 || substr_length > str->size) {
|
if (substr_length == 0 || substr_length > str->size) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -185,15 +175,16 @@ i64 wapp_dstr_find(const String *str, const char *substr) {
|
|||||||
char buf[substr_length + 1];
|
char buf[substr_length + 1];
|
||||||
memset(buf, 0, substr_length + 1);
|
memset(buf, 0, substr_length + 1);
|
||||||
|
|
||||||
const char *s1;
|
|
||||||
for (u64 i = 0; i < str->size; ++i) {
|
for (u64 i = 0; i < str->size; ++i) {
|
||||||
if (i + substr_length > str->size) {
|
if (i + substr_length >= str->size) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
s1 = &(str->buf[i]);
|
for (u64 j = 0; j < substr_length; ++j) {
|
||||||
|
buf[j] = str->buf[i + j];
|
||||||
|
}
|
||||||
|
|
||||||
if (strncmp(s1, substr, substr_length) == 0) {
|
if (strcmp(buf, substr) == 0) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,12 @@ internal void *mem_libc_alloc_aligned(u64 size, u64 alignment,
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj) {
|
internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj) {
|
||||||
return realloc(ptr, size);
|
void *output = realloc(ptr, size);
|
||||||
|
if (output) {
|
||||||
|
memset(output, 0, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void mem_libc_free(void **ptr, void *alloc_obj) {
|
internal void mem_libc_free(void **ptr, void *alloc_obj) {
|
||||||
|
Loading…
Reference in New Issue
Block a user