From 9f3bb5797206c06d0ec6456e8e4ca8e9b1b4f969 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Tue, 10 Feb 2026 17:40:51 +0000 Subject: [PATCH] sv: major refactor - Internal data pointer is read only by default => any uses that require use of the pointer itself (freeing, mutating) must perform a cast. - refactor tests to use some new sv macros and functions, and clean them up. - slight cleanup of sv.c --- include/alisp/sv.h | 2 +- src/stream.c | 2 +- src/sv.c | 4 ---- test/test_stream.c | 11 ++++++----- test/test_sv.c | 4 ++-- test/test_vec.c | 4 ++-- 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/include/alisp/sv.h b/include/alisp/sv.h index 2925f85..aac6777 100644 --- a/include/alisp/sv.h +++ b/include/alisp/sv.h @@ -14,7 +14,7 @@ typedef struct { u64 size; - char *data; + const char *data; } sv_t; // String view macro constructor diff --git a/src/stream.c b/src/stream.c index b3b3684..416b744 100644 --- a/src/stream.c +++ b/src/stream.c @@ -107,7 +107,7 @@ void stream_free(stream_t *stream) switch (stream->type) { case STREAM_TYPE_STRING: - free(stream->string.data); + free((char *)stream->string.data); break; case STREAM_TYPE_FILE: case STREAM_TYPE_PIPE: diff --git a/src/sv.c b/src/sv.c index eda21b0..e9e8073 100644 --- a/src/sv.c +++ b/src/sv.c @@ -58,8 +58,6 @@ sv_t sv_till(sv_t sv, const char *reject) ++offset) continue; - if (offset == sv.size) - return sv; return sv_truncate(sv, offset); } @@ -73,8 +71,6 @@ sv_t sv_while(sv_t sv, const char *accept) ++offset) continue; - if (offset == sv.size) - return sv; return sv_truncate(sv, offset); } diff --git a/test/test_stream.c b/test/test_stream.c index 8b15db1..1e7998d 100644 --- a/test/test_stream.c +++ b/test/test_stream.c @@ -66,7 +66,7 @@ void stream_test_string(void) sv_t test_strings[] = { SV_AUTO("hello, world!"), SV_AUTO("another string"), - SV((char *)text, ARRSIZE(text) / 2), + sv_truncate(SV_AUTO(text), ARRSIZE(text) / 2), }; for (u64 i = 0; i < ARRSIZE(test_strings); ++i) @@ -87,7 +87,8 @@ void stream_test_string(void) "Freeing a stream does not free the underlying memory it was derived " "from"); - free(copy.data); + // NOTE: Okay to free since we own copy. + free((void *)copy.data); } stream_t stream = {0}; @@ -319,7 +320,7 @@ void stream_test_substr(void) TEST(result.size == size, "Substring has right size (%lu)", result.size); - sv_t expected = SV((char *)words_text + position, size); + sv_t expected = sv_substr(SV_AUTO(words_text), position, size); TEST(strncmp(result.data, expected.data, result.size) == 0, "Expect the substring to be the same as the data we put in"); } @@ -332,7 +333,7 @@ void stream_test_substr(void) TEST(result.size == size, "Substring has right size (%lu)", result.size); - sv_t expected = SV((char *)words_text, size); + sv_t expected = sv_truncate(SV_AUTO(words_text), size); TEST(strncmp(result.data, expected.data, result.size) == 0, "Expect the substring to be the same as the data we put in"); } @@ -349,7 +350,7 @@ void stream_test_substr(void) TEST(result.size == size, "Substring has right size (%lu)", result.size); - sv_t expected = SV((char *)words_text + position, size); + sv_t expected = sv_substr(SV_AUTO(words_text), position, size); TEST(strncmp(result.data, expected.data, result.size) == 0, "Expect the substring to be the same as the data we put in"); diff --git a/test/test_sv.c b/test/test_sv.c index ec6b6cf..955adee 100644 --- a/test/test_sv.c +++ b/test/test_sv.c @@ -24,8 +24,8 @@ void sv_copy_test(void) TEST(strncmp(word.data, copy.data, copy.size) == 0, "`%s` == `%s`", word.data, copy.data); - // Obviously we can't just have this lying around. - free(copy.data); + // NOTE: Okay to free since we own copy. + free((void *)copy.data); } } diff --git a/test/test_vec.c b/test/test_vec.c index 0b8300e..9067b19 100644 --- a/test/test_vec.c +++ b/test/test_vec.c @@ -50,13 +50,13 @@ void vec_test_gen_substr(void) {0, 16}, {0, 32}, {32, 64}, - {0, ARRSIZE(text)}, + {0, ARRSIZE(text) - 1}, }; for (u64 i = 0; i < ARRSIZE(tests); ++i) { struct Test test = tests[i]; - const sv_t substr = SV((char *)text + test.start, test.size); + const sv_t substr = sv_substr(SV_AUTO(text), test.start, test.size); const u64 size = test.size / 2; lisp_t *lvec = make_vec(&system, size);