codebased/include/utils/string.h
2023-04-13 02:29:49 +02:00

38 lines
694 B
C

#ifndef UTILS_STRING_H
#define UTILS_STRING_H
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
const char* data;
size_t length;
} StringView;
typedef struct {
char* data;
size_t length;
} HeapString;
static inline HeapString heapstring_from(const char* text, size_t length)
{
char* allocated = malloc(sizeof(char) * length + 1);
strncpy(allocated, text, length);
HeapString string = {
.data = allocated,
.length = length,
};
return string;
}
static inline HeapString heapstring_from_cstring(const char* text)
{
return heapstring_from(text, strlen(text));
}
static inline void heapstring_destroy(HeapString* string)
{
free(string->data);
}
#endif