diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-05-18 14:38:41 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-05-18 14:38:41 -0400 |
| commit | a1d18fd4225f67275ea4173532969e4ddf55b39f (patch) | |
| tree | 6473f7cee0e829783bd64cc3d52af53f5089340f /builtins | |
| parent | ee9a40f9102f12357e7a46c3b48a486c62c2311e (diff) | |
Add C string type
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/c_string.c | 52 | ||||
| -rw-r--r-- | builtins/c_string.h | 18 | ||||
| -rw-r--r-- | builtins/functions.c | 20 | ||||
| -rw-r--r-- | builtins/functions.h | 5 | ||||
| -rw-r--r-- | builtins/tomo.h | 1 |
5 files changed, 96 insertions, 0 deletions
diff --git a/builtins/c_string.c b/builtins/c_string.c new file mode 100644 index 00000000..f74d6da2 --- /dev/null +++ b/builtins/c_string.c @@ -0,0 +1,52 @@ +// Type info and methods for CString datatype (char*) +#include <ctype.h> +#include <err.h> +#include <gc.h> +#include <gc/cord.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#include "functions.h" +#include "halfsiphash.h" +#include "text.h" +#include "types.h" +#include "util.h" + +public CORD CString$as_text(const void *c_string, bool colorize, const TypeInfo *info) +{ + (void)info; + if (!c_string) return "CString"; + CORD text = CORD_from_char_star(*(char**)c_string); + return CORD_all(colorize ? "\x1b[34mCString\x1b[m(" : "CString(", Text$quoted(text, colorize), ")"); +} + +public int CString$compare(const char **x, const char **y) +{ + if (!*x != !*y) + return (!*y) - (!*x); + return strcmp(*x, *y); +} + +public bool CString$equal(const char **x, const char **y) +{ + return CString$compare(x, y) == 0; +} + +public uint32_t CString$hash(const char **c_str) +{ + if (!*c_str) return 0; + + uint32_t hash; + halfsiphash(*c_str, strlen(*c_str), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + return hash; +} + +public const TypeInfo $CString = { + .size=sizeof(char*), + .align=__alignof__(char*), + .tag=CustomInfo, + .CustomInfo={.as_text=(void*)CString$as_text, .compare=(void*)CString$compare, .equal=(void*)CString$equal, .hash=(void*)CString$hash}, +}; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/c_string.h b/builtins/c_string.h new file mode 100644 index 00000000..6b4b0aad --- /dev/null +++ b/builtins/c_string.h @@ -0,0 +1,18 @@ +#pragma once + +// Type info and methods for CString datatype, which represents C's `char*` + +#include <gc/cord.h> +#include <stdbool.h> +#include <stdint.h> + +#include "types.h" + +CORD CString$as_text(const void *str, bool colorize, const TypeInfo *info); +int CString$compare(const char **x, const char **y); +bool CString$equal(const char **x, const char **y); +uint32_t CString$hash(const char **str); + +extern const TypeInfo $CString; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/functions.c b/builtins/functions.c index f668b253..eedcc9fb 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -240,4 +240,24 @@ public bool pop_flag(char **argv, int *i, const char *flag, CORD *result) } } +public void *xfopen(CORD path, CORD flags) +{ + return fopen(CORD_to_const_char_star(path), CORD_to_const_char_star(flags)); +} + +public CORD xfread_all(void *fp) +{ + return CORD_from_file_eager(fp); +} + +public void xfputs(CORD text, void *fp) +{ + CORD_put(text, fp); +} + +public void xfclose(void *fp) +{ + fclose(fp); +} + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/functions.h b/builtins/functions.h index f1a2867e..e62fc441 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -26,4 +26,9 @@ bool generic_equal(const void *x, const void *y, const TypeInfo *type); CORD generic_as_text(const void *obj, bool colorize, const TypeInfo *type); bool pop_flag(char **argv, int *i, const char *flag, CORD *result); +void *xfopen(CORD path, CORD flags); +CORD xfread_all(void *fp); +void xfputs(CORD text, void *fp); +void xfclose(void *fp); + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/tomo.h b/builtins/tomo.h index f0455ad1..ded82945 100644 --- a/builtins/tomo.h +++ b/builtins/tomo.h @@ -17,6 +17,7 @@ #include "array.h" #include "bool.h" +#include "c_string.h" #include "datatypes.h" #include "functions.h" #include "halfsiphash.h" |
