gen/
.clang-format
570 B
.gitignore
30 B
.gitsigners
112 B
LICENSE
1.1 KiB
Makefile
911 B
README
1.8 KiB
ast.c
5.0 KiB
ast.h
15.1 KiB
desugar.c
23.1 KiB
desugar.h
286 B
gen.c
108.5 KiB
gen.h
4.9 KiB
io.c
1.1 KiB
io.h
444 B
limits.h
1.3 KiB
module.c
10.0 KiB
module.h
2.2 KiB
options.c
1.4 KiB
options.h
472 B
parser.c
68.3 KiB
parser.h
942 B
radiance.c
3.7 KiB
ralloc.c
2.0 KiB
ralloc.h
1.1 KiB
resolver.c
109.7 KiB
resolver.h
5.6 KiB
riscv.c
12.0 KiB
riscv.h
12.0 KiB
scanner.c
10.2 KiB
scanner.h
3.2 KiB
strings.c
2.6 KiB
strings.h
407 B
symtab.c
5.7 KiB
symtab.h
4.6 KiB
types.h
1.0 KiB
util.h
1.5 KiB
util.h
raw
| 1 | #ifndef UTIL_H |
| 2 | #define UTIL_H |
| 3 | |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include "types.h" |
| 8 | |
| 9 | /** |
| 10 | * Concatenate string src to the end of dst. |
| 11 | */ |
| 12 | static inline usize strlcat(char *dst, const char *src, usize dsize) { |
| 13 | usize dst_len = strlen(dst); |
| 14 | usize src_len = strlen(src); |
| 15 | |
| 16 | /* If destination buffer is already full or too small, can't append */ |
| 17 | if (dst_len >= dsize) { |
| 18 | return dst_len + src_len; /* Return what length would be */ |
| 19 | } |
| 20 | |
| 21 | /* Calculate remaining space in destination */ |
| 22 | usize remaining = dsize - dst_len - 1; /* -1 for null terminator */ |
| 23 | |
| 24 | if (remaining > 0) { |
| 25 | /* Use strncat to append, but limit to remaining space */ |
| 26 | strncat(dst, src, remaining); |
| 27 | } |
| 28 | /* Return total length that would be created */ |
| 29 | return dst_len + src_len; |
| 30 | } |
| 31 | |
| 32 | /* Copy a string safely */ |
| 33 | static inline void strndup(char *dst, const char *src, size_t maxlen) { |
| 34 | if (!dst || !src) |
| 35 | return; |
| 36 | |
| 37 | size_t srclen = strlen(src); |
| 38 | size_t copylen = srclen < maxlen - 1 ? srclen : maxlen - 1; |
| 39 | |
| 40 | memcpy(dst, src, copylen); |
| 41 | dst[copylen] = '\0'; |
| 42 | } |
| 43 | |
| 44 | /* Like `strstr` but find the _last_ occurrence. */ |
| 45 | static inline char *strrstr(const char *haystack, const char *needle) { |
| 46 | if (*needle == '\0') { |
| 47 | return (char *)haystack + strlen(haystack); |
| 48 | } |
| 49 | char *result = NULL; |
| 50 | char *p = strstr(haystack, needle); |
| 51 | |
| 52 | while (p != NULL) { |
| 53 | result = p; |
| 54 | p = strstr(p + 1, needle); |
| 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | #endif |