aboutsummaryrefslogtreecommitdiff
path: root/stdlib/c_strings.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-11-29 12:55:14 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-11-29 12:55:14 -0500
commit4b5e4cd1f21582f5e5fa682ab4e4bff252963468 (patch)
treee8fecb01f444c1d392c09255adba5cf6b312b326 /stdlib/c_strings.c
parent0b0e0a0a1d41e9574de8dc17c688a4894c5e7f92 (diff)
Change how types handle metamethods
Diffstat (limited to 'stdlib/c_strings.c')
-rw-r--r--stdlib/c_strings.c36
1 files changed, 23 insertions, 13 deletions
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