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 // String view macro constructor
#define SV(DATA, SIZE) ((sv_t){.data = (DATA), .size = (SIZE)}) #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 // Pretty printers
#define SV_FMT(SV) (int)(SV).size, (SV).data #define SV_FMT(SV) (int)(SV).size, (SV).data
#define PR_SV "%.*s" #define PR_SV "%.*s"

View File

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

View File

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