packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | /* The functions of api.h over the vendored StringZilla (vendor/). Pure C: |
| 2 | * nothing here sees a Louise value, so nothing here can unwind into one; |
| 3 | * the generated wrappers (csrc/bindings.c) do the conversion and own the |
| 4 | * panics. No StringZilla function called here throws, longjmps or |
| 5 | * unwinds: they are leaf computations over borrowed memory, and the |
| 6 | * sort's allocation failure is a status code, not an exception. */ |
| 7 | #include "api.h" |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <threads.h> |
| 12 | |
| 13 | /* Declare the API as the library TU exports it: with dynamic dispatch |
| 14 | * on, the `SZ_DYNAMIC` declarations are extern and resolve to |
| 15 | * stringzilla_lib.c's runtime-dispatched symbols; without it this TU |
| 16 | * would get its own inline, serial-only copies. */ |
| 17 | #define SZ_DYNAMIC_DISPATCH 1 |
| 18 | #include <stringzilla/stringzilla.h> |
| 19 | |
| 20 | int64_t szl_find(const char *haystack, size_t haystack_len, const char *needle, size_t needle_len) { |
| 21 | /* StringZilla reports an empty needle as "not found"; Louise follows |
| 22 | * Rust's `str::find`, where it matches at 0. */ |
| 23 | if (!needle_len) return 0; |
| 24 | sz_cptr_t p = sz_find(haystack, haystack_len, needle, needle_len); |
| 25 | return p ? (int64_t)(p - haystack) : -1; |
| 26 | } |
| 27 | |
| 28 | int64_t szl_rfind(const char *haystack, size_t haystack_len, const char *needle, size_t needle_len) { |
| 29 | if (!needle_len) return (int64_t)haystack_len; |
| 30 | sz_cptr_t p = sz_rfind(haystack, haystack_len, needle, needle_len); |
| 31 | return p ? (int64_t)(p - haystack) : -1; |
| 32 | } |
| 33 | |
| 34 | size_t szl_count(const char *haystack, size_t haystack_len, const char *needle, size_t needle_len) { |
| 35 | /* StringZilla never matches an empty needle; Louise counts its |
| 36 | * matches at every gap, as Rust's `str::matches("").count()` does. */ |
| 37 | if (!needle_len) return haystack_len + 1; |
| 38 | size_t count = 0; |
| 39 | size_t pos = 0; |
| 40 | while (pos <= haystack_len) { |
| 41 | sz_cptr_t p = sz_find(haystack + pos, haystack_len - pos, needle, needle_len); |
| 42 | if (!p) break; |
| 43 | count++; |
| 44 | pos += (size_t)(p - (haystack + pos)) + needle_len; |
| 45 | } |
| 46 | return count; |
| 47 | } |
| 48 | |
| 49 | int64_t szl_find_byte(const char *s, size_t len, uint8_t byte) { |
| 50 | sz_cptr_t p = sz_find_byte(s, len, (sz_cptr_t)&byte); |
| 51 | return p ? (int64_t)(p - s) : -1; |
| 52 | } |
| 53 | |
| 54 | int64_t szl_rfind_byte(const char *s, size_t len, uint8_t byte) { |
| 55 | sz_cptr_t p = sz_rfind_byte(s, len, (sz_cptr_t)&byte); |
| 56 | return p ? (int64_t)(p - s) : -1; |
| 57 | } |
| 58 | |
| 59 | int szl_order(const char *a, size_t a_len, const char *b, size_t b_len) { |
| 60 | return (int)sz_order(a, a_len, b, b_len); |
| 61 | } |
| 62 | |
| 63 | /* The top two bits are cut: a Louise `Int` is a 62-bit small value or a |
| 64 | * big integer, and literals reach 2^62 - 1, so every result stays a |
| 65 | * literal (ADR 0014). */ |
| 66 | #define SZL_INT64_MASK 0x3FFFFFFFFFFFFFFFull |
| 67 | |
| 68 | int64_t szl_hash(const char *s, size_t len, int64_t seed) { |
| 69 | return (int64_t)(sz_hash(s, len, (sz_u64_t)seed) & SZL_INT64_MASK); |
| 70 | } |
| 71 | |
| 72 | int64_t szl_bytesum(const char *s, size_t len) { |
| 73 | return (int64_t)(sz_bytesum(s, len) & SZL_INT64_MASK); |
| 74 | } |
| 75 | |
| 76 | #define SZL_STR_(x) #x |
| 77 | #define SZL_STR(x) SZL_STR_(x) |
| 78 | |
| 79 | const char *szl_version(void) { |
| 80 | return SZL_STR(STRINGZILLA_H_VERSION_MAJOR) "." SZL_STR(STRINGZILLA_H_VERSION_MINOR) "." SZL_STR( |
| 81 | STRINGZILLA_H_VERSION_PATCH); |
| 82 | } |
| 83 | |
| 84 | /* sz_capabilities_to_string writes a static buffer: build it once, so |
| 85 | * concurrent actors never race it. */ |
| 86 | static char szl_caps[256]; |
| 87 | static once_flag szl_caps_once = ONCE_FLAG_INIT; |
| 88 | static void szl_caps_init(void) { |
| 89 | strncpy(szl_caps, sz_capabilities_to_string(sz_capabilities()), sizeof(szl_caps) - 1); |
| 90 | szl_caps[sizeof(szl_caps) - 1] = '\0'; |
| 91 | } |
| 92 | |
| 93 | const char *szl_capabilities(void) { |
| 94 | call_once(&szl_caps_once, szl_caps_init); |
| 95 | return szl_caps; |
| 96 | } |
| 97 | |
| 98 | struct szl_sort { |
| 99 | char **strings; |
| 100 | size_t *lengths; |
| 101 | size_t count; |
| 102 | size_t capacity; |
| 103 | sz_sorted_idx_t *order; |
| 104 | }; |
| 105 | |
| 106 | szl_sort *szl_sort_new(void) { |
| 107 | szl_sort *s = malloc(sizeof(szl_sort)); |
| 108 | if (!s) return NULL; |
| 109 | s->strings = NULL; |
| 110 | s->lengths = NULL; |
| 111 | s->count = 0; |
| 112 | s->capacity = 0; |
| 113 | s->order = NULL; |
| 114 | return s; |
| 115 | } |
| 116 | |
| 117 | void szl_sort_free(szl_sort *s) { |
| 118 | if (!s) return; |
| 119 | for (size_t i = 0; i != s->count; ++i) free(s->strings[i]); |
| 120 | free(s->strings); |
| 121 | free(s->lengths); |
| 122 | free(s->order); |
| 123 | free(s); |
| 124 | } |
| 125 | |
| 126 | bool szl_sort_push(szl_sort *s, const char *text, size_t text_len) { |
| 127 | if (s->count == s->capacity) { |
| 128 | size_t capacity = s->capacity ? s->capacity * 2 : 16; |
| 129 | char **strings = realloc(s->strings, capacity * sizeof(*strings)); |
| 130 | if (!strings) return false; |
| 131 | s->strings = strings; |
| 132 | size_t *lengths = realloc(s->lengths, capacity * sizeof(*lengths)); |
| 133 | if (!lengths) return false; |
| 134 | s->lengths = lengths; |
| 135 | s->capacity = capacity; |
| 136 | } |
| 137 | /* malloc(0) may return NULL; an empty string still needs a slot. */ |
| 138 | char *copy = malloc(text_len ? text_len : 1); |
| 139 | if (!copy) return false; |
| 140 | memcpy(copy, text, text_len); |
| 141 | s->strings[s->count] = copy; |
| 142 | s->lengths[s->count] = text_len; |
| 143 | s->count++; |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | size_t szl_sort_count(const szl_sort *s) { return s->count; } |
| 148 | |
| 149 | static sz_cptr_t szl_sort_get_start(void const *handle, sz_sorted_idx_t i) { |
| 150 | return ((const szl_sort *)handle)->strings[i]; |
| 151 | } |
| 152 | |
| 153 | static sz_size_t szl_sort_get_length(void const *handle, sz_sorted_idx_t i) { |
| 154 | return ((const szl_sort *)handle)->lengths[i]; |
| 155 | } |
| 156 | |
| 157 | static void *szl_allocate_(sz_size_t length, void *handle) { |
| 158 | (void)handle; |
| 159 | return malloc(length ? length : 1); |
| 160 | } |
| 161 | |
| 162 | static void szl_free_(void *p, sz_size_t length, void *handle) { |
| 163 | (void)length; |
| 164 | (void)handle; |
| 165 | free(p); |
| 166 | } |
| 167 | |
| 168 | bool szl_sort_argsort(szl_sort *s) { |
| 169 | free(s->order); |
| 170 | s->order = NULL; |
| 171 | if (s->count == 0) return true; |
| 172 | s->order = malloc(s->count * sizeof(*s->order)); |
| 173 | if (!s->order) return false; |
| 174 | sz_sequence_t sequence; |
| 175 | sequence.handle = s; |
| 176 | sequence.count = s->count; |
| 177 | sequence.get_start = szl_sort_get_start; |
| 178 | sequence.get_length = szl_sort_get_length; |
| 179 | /* StringZilla's default allocator calls its `free` through a |
| 180 | * `char *`-typed function cast to the `void *` typedef: fine on every |
| 181 | * real ABI, but UBSan's function check rejects it (v5 kept the |
| 182 | * pattern). Our own allocator has the exact signatures. */ |
| 183 | sz_memory_allocator_t alloc = {szl_allocate_, szl_free_, NULL}; |
| 184 | return sz_sequence_argsort(&sequence, &alloc, s->order) == sz_success_k; |
| 185 | } |
| 186 | |
| 187 | int64_t szl_sort_order(const szl_sort *s, size_t i) { return (int64_t)s->order[i]; } |