34 lines
665 B
C
34 lines
665 B
C
#include "error.h"
|
|
#include "vmstring.h"
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
int vm_string_init(struct vm_string *str, const char *text) {
|
|
if (text && *text) {
|
|
str->data = strdup(text);
|
|
if (!str->data) {
|
|
return -ERR_OUT_OF_MEMORY;
|
|
}
|
|
str->len = strlen(text);
|
|
str->cap = str->len + 1;
|
|
} else {
|
|
vm_string_empty(str);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void vm_string_empty(struct vm_string *str) {
|
|
str->data = NULL;
|
|
str->len = 0;
|
|
str->cap = 0;
|
|
}
|
|
|
|
const char *vm_cstr(const struct vm_string *str) {
|
|
return str->data;
|
|
}
|
|
|
|
size_t vm_strlen(const struct vm_string *str) {
|
|
return str->len;
|
|
}
|