From bbf38499cad6a0aab0fb54ea3d5ba37b4168965f Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 9 Feb 2025 14:29:54 +0000 Subject: [PATCH] Implement read-only string literals --- src/core/strings/str8/str8.h | 13 ++++++++---- tests/str8/test_str8.c | 41 ++++++++++++++++++++++++++++++------ tests/str8/test_str8.h | 1 + tests/wapptest.c | 8 +++---- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h index 8c80081..523006d 100644 --- a/src/core/strings/str8/str8.h +++ b/src/core/strings/str8/str8.h @@ -15,15 +15,20 @@ struct str8 { c8 *buf; }; +typedef const Str8 Str8RO; + #define wapp_str8_buf(CAPACITY) ((Str8){.capacity = CAPACITY, .size = 0, .buf = (c8[CAPACITY]){0}}) // Utilises the fact that memcpy returns pointer to dest buffer and that getting // address of compound literals is valid in C to create a string on the stack -#define wapp_str8_lit(STRING) ((Str8){.capacity = sizeof(STRING) - 1, \ - .size = sizeof(STRING) - 1, \ - .buf = memcpy(&((c8 [sizeof(STRING)]){0}), STRING, sizeof(STRING))}) +#define wapp_str8_lit(STRING) ((Str8){.capacity = (sizeof(STRING) - 1) * 2, \ + .size = sizeof(STRING) - 1, \ + .buf = memcpy(&((c8 [sizeof(STRING) * 2]){0}), STRING, sizeof(STRING))}) +#define wapp_str8_lit_ro(STRING) ((Str8RO){.capacity = sizeof(STRING) - 1, \ + .size = sizeof(STRING) - 1, \ + .buf = (c8 *)STRING}) -c8 wapp_str8_get(const Str8 *str, u64 index); +c8 wapp_str8_get(Str8RO *str, u64 index); void wapp_str8_set(Str8 *str, u64 index, c8 c); #ifdef __cplusplus diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 1fa5f7b..2445e83 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -7,25 +7,52 @@ TestFuncResult test_str8_lit(void) { bool result; Str8 s1 = wapp_str8_lit("Hello world"); - result = s1.capacity == 11 && s1.capacity == s1.size; + result = s1.capacity == 22 && s1.capacity != s1.size; Str8 s2 = wapp_str8_lit("Different strokes for different folks"); - result = result && s2.capacity == 37 && s2.capacity == s2.size; + result = result && s2.capacity == 74 && s2.capacity != s2.size; Str8 s3 = wapp_str8_lit("Discretion is the better part of valour"); - result = result && s3.capacity == 39 && s3.capacity == s3.size; + result = result && s3.capacity == 78 && s3.capacity != s3.size; Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view"); - result = result && s4.capacity == 38 && s4.capacity == s4.size; + result = result && s4.capacity == 76 && s4.capacity != s4.size; Str8 s5 = wapp_str8_lit("Do as I say, not as I do"); - result = result && s5.capacity == 24 && s5.capacity == s5.size; + result = result && s5.capacity == 48 && s5.capacity != s5.size; Str8 s6 = wapp_str8_lit("Do as you would be done by"); - result = result && s6.capacity == 26 && s6.capacity == s6.size; + result = result && s6.capacity == 52 && s6.capacity != s6.size; Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you"); - result = result && s7.capacity == 47 && s7.capacity == s7.size; + result = result && s7.capacity == 94 && s7.capacity != s7.size; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_lit_ro(void) { + bool result; + + Str8RO s1 = wapp_str8_lit_ro("Hello world"); + result = s1.capacity == 11 && s1.capacity == s1.size; + + Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks"); + result = result && s2.capacity == 37 && s2.capacity == s2.size; + + Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour"); + result = result && s3.capacity == 39 && s3.capacity == s3.size; + + Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view"); + result = result && s4.capacity == 38 && s4.capacity == s4.size; + + Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do"); + result = result && s5.capacity == 24 && s5.capacity == s5.size; + + Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by"); + result = result && s6.capacity == 26 && s6.capacity == s6.size; + + Str8RO s7 = wapp_str8_lit_ro("Do unto others as you would have them do to you"); + result = result && s7.capacity == 47 && s7.capacity == s7.size; return wapp_tester_result(result); } diff --git a/tests/str8/test_str8.h b/tests/str8/test_str8.h index 2563410..84c7ebb 100644 --- a/tests/str8/test_str8.h +++ b/tests/str8/test_str8.h @@ -8,6 +8,7 @@ BEGIN_C_LINKAGE #endif // __cplusplus TestFuncResult test_str8_lit(void); +TestFuncResult test_str8_lit_ro(void); TestFuncResult test_str8_buf(void); TestFuncResult test_str8_get_index_within_bounds(void); TestFuncResult test_str8_get_index_out_of_bounds(void); diff --git a/tests/wapptest.c b/tests/wapptest.c index 8e5352a..49f6703 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -1,4 +1,4 @@ -#include "str8/test_str8.h" +#include "test_str8.h" #include "test_allocator.h" #include "test_arena.h" #include "test_cpath.h" @@ -10,9 +10,9 @@ int main(void) { wapp_tester_run_tests(test_arena_allocator, test_arena_init, test_arena_init_succeeds_when_reserving_very_large_size, - test_str8_lit, test_str8_buf, test_str8_get_index_within_bounds, test_str8_get_index_out_of_bounds, - test_str8_set, test_cpath_join_path, test_cpath_dirname, test_cpath_dirup, - test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_fails_when_over_capacity, + test_str8_lit, test_str8_lit_ro, test_str8_buf, test_str8_get_index_within_bounds, + test_str8_get_index_out_of_bounds, test_str8_set, test_cpath_join_path, test_cpath_dirname, + test_cpath_dirup, test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_fails_when_over_capacity, test_arena_realloc_bigger_size, test_arena_realloc_smaller_size, test_arena_clear, test_arena_destroy, test_commander_cmd_success, test_commander_cmd_failure, test_commander_cmd_out_buf_success, test_commander_cmd_out_buf_failure);