Add str8_copy functions
This commit is contained in:
parent
6078e54087
commit
0569fca193
@ -151,6 +151,31 @@ void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) {
|
|||||||
dst->size += to_copy;
|
dst->size += to_copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src) {
|
||||||
|
if (!dst || !src) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(src);
|
||||||
|
u64 to_copy = length <= dst->capacity ? length : dst->capacity;
|
||||||
|
|
||||||
|
memset(dst->buf, 0, dst->size);
|
||||||
|
memcpy(dst->buf, src, to_copy);
|
||||||
|
dst->size = to_copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) {
|
||||||
|
if (!dst || !src) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 to_copy = src->size <= dst->capacity ? src->size : dst->capacity;
|
||||||
|
|
||||||
|
memset(dst->buf, 0, dst->size);
|
||||||
|
memcpy(dst->buf, src->buf, to_copy);
|
||||||
|
dst->size = to_copy;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
@ -70,6 +70,8 @@ bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
|
|||||||
Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end);
|
Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end);
|
||||||
Str8 *wapp_str8_concat(const Allocator *allocator, Str8 *dst, Str8RO *src);
|
Str8 *wapp_str8_concat(const Allocator *allocator, Str8 *dst, Str8RO *src);
|
||||||
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
|
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
|
||||||
|
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src);
|
||||||
|
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Str8 find functions
|
* Str8 find functions
|
||||||
|
@ -278,6 +278,41 @@ TestFuncResult test_str8_concat_capped(void) {
|
|||||||
return wapp_tester_result(result);
|
return wapp_tester_result(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_str8_copy_cstr_capped(void) {
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
Str8 buf = wapp_str8_buf(32);
|
||||||
|
const char *src1 = "Hello world";
|
||||||
|
const char *src2 = "Hello world from the Wizard Apprentice standard library";
|
||||||
|
Str8RO src1_cp = wapp_str8_lit_ro("Hello world");
|
||||||
|
Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr");
|
||||||
|
|
||||||
|
wapp_str8_copy_cstr_capped(&buf, src1);
|
||||||
|
result = buf.size == src1_cp.size && wapp_str8_equal(&buf, &src1_cp);
|
||||||
|
|
||||||
|
wapp_str8_copy_cstr_capped(&buf, src2);
|
||||||
|
result = result && buf.size == src2_cp.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp);
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_str8_copy_str8_capped(void) {
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
Str8 buf = wapp_str8_buf(32);
|
||||||
|
Str8RO src1 = wapp_str8_lit_ro("Hello world");
|
||||||
|
Str8RO src2 = wapp_str8_lit_ro("Hello world from the Wizard Apprentice standard library");
|
||||||
|
Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr");
|
||||||
|
|
||||||
|
wapp_str8_copy_str8_capped(&buf, &src1);
|
||||||
|
result = buf.size == src1.size && wapp_str8_equal(&buf, &src1);
|
||||||
|
|
||||||
|
wapp_str8_copy_str8_capped(&buf, &src2);
|
||||||
|
result = result && buf.size < src2.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp);
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
|
|
||||||
TestFuncResult test_str8_find(void) {
|
TestFuncResult test_str8_find(void) {
|
||||||
bool result;
|
bool result;
|
||||||
Str8RO s = wapp_str8_lit("Do as I say, not as I do");
|
Str8RO s = wapp_str8_lit("Do as I say, not as I do");
|
||||||
|
@ -20,6 +20,8 @@ TestFuncResult test_str8_equal(void);
|
|||||||
TestFuncResult test_str8_substr(void);
|
TestFuncResult test_str8_substr(void);
|
||||||
TestFuncResult test_str8_concat(void);
|
TestFuncResult test_str8_concat(void);
|
||||||
TestFuncResult test_str8_concat_capped(void);
|
TestFuncResult test_str8_concat_capped(void);
|
||||||
|
TestFuncResult test_str8_copy_cstr_capped(void);
|
||||||
|
TestFuncResult test_str8_copy_str8_capped(void);
|
||||||
TestFuncResult test_str8_find(void);
|
TestFuncResult test_str8_find(void);
|
||||||
TestFuncResult test_str8_rfind(void);
|
TestFuncResult test_str8_rfind(void);
|
||||||
TestFuncResult test_str8_split(void);
|
TestFuncResult test_str8_split(void);
|
||||||
|
@ -26,6 +26,8 @@ int main(void) {
|
|||||||
test_str8_substr,
|
test_str8_substr,
|
||||||
test_str8_concat,
|
test_str8_concat,
|
||||||
test_str8_concat_capped,
|
test_str8_concat_capped,
|
||||||
|
test_str8_copy_cstr_capped,
|
||||||
|
test_str8_copy_str8_capped,
|
||||||
test_str8_find,
|
test_str8_find,
|
||||||
test_str8_rfind,
|
test_str8_rfind,
|
||||||
test_str8_split,
|
test_str8_split,
|
||||||
|
Loading…
Reference in New Issue
Block a user