From 4b5e4cd1f21582f5e5fa682ab4e4bff252963468 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 29 Nov 2024 12:55:14 -0500 Subject: Change how types handle metamethods --- stdlib/c_strings.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'stdlib/c_strings.c') diff --git a/stdlib/c_strings.c b/stdlib/c_strings.c index 5ac7d4b4..99632a53 100644 --- a/stdlib/c_strings.c +++ b/stdlib/c_strings.c @@ -10,11 +10,11 @@ #include "siphash.h" #include "util.h" -public Text_t CString$as_text(const char **c_string, bool colorize, const TypeInfo_t *info) +public Text_t CString$as_text(const void *c_string, bool colorize, const TypeInfo_t *info) { (void)info; if (!c_string) return Text("CString"); - Text_t text = Text$from_str(*c_string); + Text_t text = Text$from_str(*(char**)c_string); return Text$concat(colorize ? Text("\x1b[34mCString\x1b[m(") : Text("CString("), Text$quoted(text, colorize), Text(")")); } @@ -23,33 +23,43 @@ public Text_t CString$as_text_simple(const char *str) return Text$format("%s", str); } -PUREFUNC public int32_t CString$compare(const char **x, const char **y) +PUREFUNC public int32_t CString$compare(const void *x, const void *y, const TypeInfo_t*) { if (x == y) return 0; - if (!*x != !*y) - return (!*y) - (!*x); + if (!*(char**)x != !*(char**)y) + return (!*(char**)y) - (!*(char**)x); - return strcmp(*x, *y); + return strcmp(*(char**)x, *(char**)y); } -PUREFUNC public bool CString$equal(const char **x, const char **y) +PUREFUNC public bool CString$equal(const void *x, const void *y, const TypeInfo_t *type) { - return CString$compare(x, y) == 0; + return CString$compare(x, y, type) == 0; } -PUREFUNC public uint64_t CString$hash(const char **c_str) +PUREFUNC public uint64_t CString$hash(const void *c_str, const TypeInfo_t*) { - if (!*c_str) return 0; - return siphash24((void*)*c_str, strlen(*c_str)); + if (!*(char**)c_str) return 0; + return siphash24(*(void**)c_str, strlen(*(char**)c_str)); +} + +PUREFUNC public bool CString$is_none(const void *c_str, const TypeInfo_t*) +{ + return *(char**)c_str == NULL; } public const TypeInfo_t CString$info = { .size=sizeof(char*), .align=__alignof__(char*), - .tag=CStringInfo, - .CustomInfo={.as_text=(void*)CString$as_text, .compare=(void*)CString$compare, .equal=(void*)CString$equal, .hash=(void*)CString$hash}, + .metamethods={ + .hash=CString$hash, + .compare=CString$compare, + .equal=CString$equal, + .as_text=CString$as_text, + .is_none=CString$is_none, + }, }; // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 -- cgit v1.2.3