sv: SV_AUTO macro (for literal strings/literal byte arrays really).

This commit is contained in:
2026-02-10 16:33:11 +00:00
committed by oreodave
parent daa1d3d565
commit 818d4da850
3 changed files with 9 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ typedef struct
// String view macro constructor
#define SV(DATA, SIZE) ((sv_t){.data = (DATA), .size = (SIZE)})
#define SV_AUTO(DATA) ((sv_t){.data = (void *)(DATA), .size = sizeof(DATA) - 1})
// Pretty printers
#define SV_FMT(SV) (int)(SV).size, (SV).data
#define PR_SV "%.*s"

View File

@@ -84,10 +84,10 @@ void sym_unique_test(void)
sys_init(&system);
sv_t symbols[] = {
SV("hello", 5),
SV("goodbye", 7),
SV("display", 7),
SV("@xs'a_sh;d::a-h]", 16),
SV_AUTO("hello"),
SV_AUTO("goodbye"),
SV_AUTO("display"),
SV_AUTO("@xs'a_sh;d::a-h]"),
};
lisp_t *ptrs[ARRSIZE(symbols)];
@@ -159,7 +159,7 @@ void sys_test(void)
"Making integers doesn't affect system memory size");
// Creating symbols won't affect memory size, but does affect the symbol table
(void)intern(&sys, SV("hello world!", 12));
(void)intern(&sys, SV_AUTO("hello world!"));
TEST(sys.memory.size == old_memory_size,
"Interning doesn't affect system memory size");
TEST(sys.symtable.count > 0, "Interning affects symbol table");
@@ -169,7 +169,7 @@ void sys_test(void)
TEST(sys.memory.size > 0, "Creating conses affects memory size");
old_memory_size = sys.memory.size;
(void)cons(&sys, intern(&sys, SV("test", 4)), NIL);
(void)cons(&sys, intern(&sys, SV_AUTO("test")), NIL);
TEST(sys.memory.size > old_memory_size,
"Creating conses back to back affects memory size");
old_memory_size = sys.memory.size;

View File

@@ -64,8 +64,8 @@ void stream_test_string(void)
{
TEST_START();
sv_t test_strings[] = {
SV("hello, world!", 13),
SV("another string", 14),
SV_AUTO("hello, world!"),
SV_AUTO("another string"),
SV((char *)text, ARRSIZE(text) / 2),
};