code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(95 lines)
1 // Type info and methods for CString datatype (char*)
3 #include <err.h>
4 #include <gc.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 #include "integers.h"
11 #include "siphash.h"
12 #include "stdlib.h"
13 #include "text.h"
14 #include "util.h"
16 public
17 Text_t CString$as_text(const void *c_string, bool colorize, const TypeInfo_t *info) {
18 (void)info;
19 if (!c_string) return Text("CString");
20 Text_t text = Text$from_str(*(const char **)c_string);
21 return Text$concat(colorize ? Text("\x1b[34mCString\x1b[m(") : Text("CString("),
22 Text$quoted(text, colorize, Text("\"")), Text(")"));
25 PUREFUNC public int32_t CString$compare(const void *x, const void *y, const TypeInfo_t *info) {
26 (void)info;
27 if (x == y) return 0;
29 if (!*(const char **)x != !*(const char **)y) return (!*(const char **)y) - (!*(const char **)x);
31 const char *x_str = *(const char **)x, *y_str = *(const char **)y;
32 return (x_str == NULL) || (y_str == NULL) ? (x_str != NULL) - (y_str != NULL) : strcmp(x_str, y_str);
35 PUREFUNC public bool CString$equal(const void *x, const void *y, const TypeInfo_t *type) {
36 return CString$compare(x, y, type) == 0;
39 PUREFUNC public uint64_t CString$hash(const void *c_str, const TypeInfo_t *info) {
40 (void)info;
41 if (!*(const char **)c_str) return 0;
42 return siphash24(*(void **)c_str, strlen(*(const char **)c_str));
45 PUREFUNC public bool CString$is_none(const void *c_str, const TypeInfo_t *info) {
46 (void)info;
47 return *(const char **)c_str == NULL;
50 public
51 void CString$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *info) {
52 (void)info;
53 const char *str = *(const char **)obj;
54 int64_t len = (int64_t)strlen(str);
55 Int64$serialize(&len, out, pointers, &Int64$info);
56 fwrite(str, sizeof(char), (size_t)len, out);
59 public
60 void CString$deserialize(FILE *in, void *out, List_t *pointers, const TypeInfo_t *info) {
61 (void)info;
62 int64_t len = -1;
63 Int64$deserialize(in, &len, pointers, &Int64$info);
64 char *str = GC_MALLOC_ATOMIC((size_t)len + 1);
65 if (fread(str, sizeof(char), (size_t)len, in) != (size_t)len) fail("Not enough data in stream to deserialize");
66 str[len + 1] = '\0';
67 *(const char **)out = str;
70 public
71 const char *CString$join(const char *glue, List_t strings) {
72 Text_t ret = EMPTY_TEXT;
73 Text_t glue_text = Text$from_str(glue);
74 for (int64_t i = 0; i < (int64_t)strings.length; i++) {
75 if (i > 0) ret = Text$concat(ret, glue_text);
76 ret = Text$concat(ret, Text$from_str(*(const char **)(strings.data + i * strings.stride)));
78 return Text$as_c_string(ret);
81 public
82 const TypeInfo_t CString$info = {
83 .size = sizeof(const char *),
84 .align = __alignof__(const char *),
85 .metamethods =
87 .hash = CString$hash,
88 .compare = CString$compare,
89 .equal = CString$equal,
90 .as_text = CString$as_text,
91 .is_none = CString$is_none,
92 .serialize = CString$serialize,
93 .deserialize = CString$deserialize,
94 },
95 };