/* The functions of api.h over the vendored StringZilla (vendor/). Pure C: * nothing here sees a Louise value, so nothing here can unwind into one; * the generated wrappers (csrc/bindings.c) do the conversion and own the * panics. No StringZilla function called here throws, longjmps or * unwinds: they are leaf computations over borrowed memory, and the * sort's allocation failure is a status code, not an exception. */ #include "api.h" #include #include #include /* Declare the API as the library TU exports it: with dynamic dispatch * on, the `SZ_DYNAMIC` declarations are extern and resolve to * stringzilla_lib.c's runtime-dispatched symbols; without it this TU * would get its own inline, serial-only copies. */ #define SZ_DYNAMIC_DISPATCH 1 #include int64_t szl_find(const char *haystack, size_t haystack_len, const char *needle, size_t needle_len) { /* StringZilla reports an empty needle as "not found"; Louise follows * Rust's `str::find`, where it matches at 0. */ if (!needle_len) return 0; sz_cptr_t p = sz_find(haystack, haystack_len, needle, needle_len); return p ? (int64_t)(p - haystack) : -1; } int64_t szl_rfind(const char *haystack, size_t haystack_len, const char *needle, size_t needle_len) { if (!needle_len) return (int64_t)haystack_len; sz_cptr_t p = sz_rfind(haystack, haystack_len, needle, needle_len); return p ? (int64_t)(p - haystack) : -1; } size_t szl_count(const char *haystack, size_t haystack_len, const char *needle, size_t needle_len) { /* StringZilla never matches an empty needle; Louise counts its * matches at every gap, as Rust's `str::matches("").count()` does. */ if (!needle_len) return haystack_len + 1; size_t count = 0; size_t pos = 0; while (pos <= haystack_len) { sz_cptr_t p = sz_find(haystack + pos, haystack_len - pos, needle, needle_len); if (!p) break; count++; pos += (size_t)(p - (haystack + pos)) + needle_len; } return count; } int64_t szl_find_byte(const char *s, size_t len, uint8_t byte) { sz_cptr_t p = sz_find_byte(s, len, (sz_cptr_t)&byte); return p ? (int64_t)(p - s) : -1; } int64_t szl_rfind_byte(const char *s, size_t len, uint8_t byte) { sz_cptr_t p = sz_rfind_byte(s, len, (sz_cptr_t)&byte); return p ? (int64_t)(p - s) : -1; } int szl_order(const char *a, size_t a_len, const char *b, size_t b_len) { return (int)sz_order(a, a_len, b, b_len); } /* The top two bits are cut: a Louise `Int` is a 62-bit small value or a * big integer, and literals reach 2^62 - 1, so every result stays a * literal (ADR 0014). */ #define SZL_INT64_MASK 0x3FFFFFFFFFFFFFFFull int64_t szl_hash(const char *s, size_t len, int64_t seed) { return (int64_t)(sz_hash(s, len, (sz_u64_t)seed) & SZL_INT64_MASK); } int64_t szl_bytesum(const char *s, size_t len) { return (int64_t)(sz_bytesum(s, len) & SZL_INT64_MASK); } #define SZL_STR_(x) #x #define SZL_STR(x) SZL_STR_(x) const char *szl_version(void) { return SZL_STR(STRINGZILLA_H_VERSION_MAJOR) "." SZL_STR(STRINGZILLA_H_VERSION_MINOR) "." SZL_STR( STRINGZILLA_H_VERSION_PATCH); } /* sz_capabilities_to_string writes a static buffer: build it once, so * concurrent actors never race it. */ static char szl_caps[256]; static once_flag szl_caps_once = ONCE_FLAG_INIT; static void szl_caps_init(void) { strncpy(szl_caps, sz_capabilities_to_string(sz_capabilities()), sizeof(szl_caps) - 1); szl_caps[sizeof(szl_caps) - 1] = '\0'; } const char *szl_capabilities(void) { call_once(&szl_caps_once, szl_caps_init); return szl_caps; } struct szl_sort { char **strings; size_t *lengths; size_t count; size_t capacity; sz_sorted_idx_t *order; }; szl_sort *szl_sort_new(void) { szl_sort *s = malloc(sizeof(szl_sort)); if (!s) return NULL; s->strings = NULL; s->lengths = NULL; s->count = 0; s->capacity = 0; s->order = NULL; return s; } void szl_sort_free(szl_sort *s) { if (!s) return; for (size_t i = 0; i != s->count; ++i) free(s->strings[i]); free(s->strings); free(s->lengths); free(s->order); free(s); } bool szl_sort_push(szl_sort *s, const char *text, size_t text_len) { if (s->count == s->capacity) { size_t capacity = s->capacity ? s->capacity * 2 : 16; char **strings = realloc(s->strings, capacity * sizeof(*strings)); if (!strings) return false; s->strings = strings; size_t *lengths = realloc(s->lengths, capacity * sizeof(*lengths)); if (!lengths) return false; s->lengths = lengths; s->capacity = capacity; } /* malloc(0) may return NULL; an empty string still needs a slot. */ char *copy = malloc(text_len ? text_len : 1); if (!copy) return false; memcpy(copy, text, text_len); s->strings[s->count] = copy; s->lengths[s->count] = text_len; s->count++; return true; } size_t szl_sort_count(const szl_sort *s) { return s->count; } static sz_cptr_t szl_sort_get_start(void const *handle, sz_sorted_idx_t i) { return ((const szl_sort *)handle)->strings[i]; } static sz_size_t szl_sort_get_length(void const *handle, sz_sorted_idx_t i) { return ((const szl_sort *)handle)->lengths[i]; } static void *szl_allocate_(sz_size_t length, void *handle) { (void)handle; return malloc(length ? length : 1); } static void szl_free_(void *p, sz_size_t length, void *handle) { (void)length; (void)handle; free(p); } bool szl_sort_argsort(szl_sort *s) { free(s->order); s->order = NULL; if (s->count == 0) return true; s->order = malloc(s->count * sizeof(*s->order)); if (!s->order) return false; sz_sequence_t sequence; sequence.handle = s; sequence.count = s->count; sequence.get_start = szl_sort_get_start; sequence.get_length = szl_sort_get_length; /* StringZilla's default allocator calls its `free` through a * `char *`-typed function cast to the `void *` typedef: fine on every * real ABI, but UBSan's function check rejects it (v5 kept the * pattern). Our own allocator has the exact signatures. */ sz_memory_allocator_t alloc = {szl_allocate_, szl_free_, NULL}; return sz_sequence_argsort(&sequence, &alloc, s->order) == sz_success_k; } int64_t szl_sort_order(const szl_sort *s, size_t i) { return (int64_t)s->order[i]; }