blob: e7ef28c3327e4ac8a1e42dd501f9f37dc789196f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include "string.h"
#include "assert.h"
#include "src/lisiblestd/memory.h"
#include <string.h>
String String_new(Allocator *allocator, const char *str) {
LSTD_ASSERT(allocator != NULL);
LSTD_ASSERT(str != NULL);
usize length = strlen(str);
char *value = Allocator_allocate(allocator, length + 1);
LSTD_ASSERT(value != NULL);
strncpy(value, str, length + 1);
value[length] = '\0';
return (String){.value = value, .length = length};
}
void String_destroy(Allocator *allocator, String *string) {
LSTD_ASSERT(allocator != NULL);
LSTD_ASSERT(string != NULL);
Allocator_free(allocator, string->value);
}
bool String_eq(const String *lhs, const String *rhs) {
if (lhs == NULL || rhs == NULL || lhs->length != rhs->length) {
return false;
}
return strncmp(lhs->value, rhs->value, lhs->length) == 0;
}
StringView String_view(const String *str) {
return (StringView){.value = str->value, .length = str->length};
}
usize String_length(const String *string) {
LSTD_ASSERT(string != NULL);
return string->length;
}
StringView StringView_from_str(const char *data) {
LSTD_ASSERT(data != NULL);
return (StringView){.value = data, .length = strlen(data)};
}
|