From 24bc8d5a1512646bfbb8464d20977f9c57d72395 Mon Sep 17 00:00:00 2001 From: jun <83899451+zeichensystem@users.noreply.github.com> Date: Tue, 4 Mar 2025 08:15:21 +0100 Subject: [PATCH] Add guf_str_view functions --- src/guf_str.h | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/guf_str.h b/src/guf_str.h index 4005b64..1854ae2 100644 --- a/src/guf_str.h +++ b/src/guf_str.h @@ -36,7 +36,6 @@ typedef struct guf_str { guf_str_state state; } guf_str; - #define GUF_CSTR_TO_VIEW(CSTR) ((guf_str_view){.str = (CSTR), .len = strlen((CSTR))}) #define GUF_STR_TO_VIEW(GUF_STR_PTR) ((guf_str_view){.str = guf_str_const_cstr((GUF_STR_PTR)), .len = guf_str_len((GUF_STR_PTR))}) #define GUF_CSTR_TO_READONLY_STR(CSTR) ((guf_str){.state = GUF_STR_STATE_VIEW, .allocator = NULL, .data.heap.c_str = CSTR, .data.heap.len = strlen(CSTR), .data.heap.capacity = 0}) @@ -91,13 +90,17 @@ GUF_STR_KWRDS bool guf_str_is_valid(const guf_str *str); GUF_STR_KWRDS bool guf_str_alloc_success(const guf_str *str); GUF_STR_KWRDS guf_hash_size_t guf_str_view_hash(const guf_str_view *sv); +GUF_STR_KWRDS uint64_t guf_str_view_hash64(const guf_str_view *sv); +GUF_STR_KWRDS uint32_t guf_str_view_hash32(const guf_str_view *sv); // Comparison: GUF_STR_KWRDS bool guf_str_view_equal(const guf_str_view* a, const guf_str_view* b); +GUF_STR_KWRDS bool guf_str_view_equal_val_arg(guf_str_view a_val, guf_str_view b_val); +GUF_STR_KWRDS int guf_str_view_cmp(const void *str_view_a, const void *str_view_b); // For qsort etc. + GUF_STR_KWRDS bool guf_str_equal(const guf_str *a, const guf_str *b); GUF_STR_KWRDS bool guf_str_equals_cstr(const guf_str *a, const char *c_str); GUF_STR_KWRDS bool guf_str_equals_strview(const guf_str *a, guf_str_view b); -GUF_STR_KWRDS int guf_str_view_cmp(const void *str_view_a, const void *str_view_b); // For qsort etc. #endif @@ -112,6 +115,8 @@ GUF_STR_KWRDS int guf_str_view_cmp(const void *str_view_a, const void *str_view_ #endif #include "guf_utf8.h" +// TODO: find_first_of + GUF_STR_KWRDS guf_str_view guf_str_next_tok(guf_str_view *input, const guf_str_view *delims, ptrdiff_t num_delims, const guf_str_view *preserved_delims, ptrdiff_t num_preserved_delims) { if (input->len <= 0 || input->str == NULL) { @@ -182,8 +187,6 @@ GUF_STR_KWRDS guf_str_view guf_str_next_tok(guf_str_view *input, const guf_str_v return tok; } -// TODO: find_first_of and tokenise -> for parsing, see aoclib. - GUF_STR_KWRDS guf_str_view guf_substr_view(guf_str_view str, ptrdiff_t pos, ptrdiff_t count) { GUF_ASSERT(str.str); @@ -207,14 +210,31 @@ GUF_STR_KWRDS guf_hash_size_t guf_str_view_hash(const guf_str_view *sv) if (!sv->str || sv->len <= 0) { return GUF_HASH_INIT; } - return guf_hash(sv->str, sv->len, GUF_HASH_INIT); } +GUF_STR_KWRDS uint64_t guf_str_view_hash64(const guf_str_view *sv) +{ + GUF_ASSERT(sv); + if (!sv->str || sv->len <= 0) { + return GUF_HASH64_INIT; + } + return guf_hash64(sv->str, sv->len, GUF_HASH64_INIT); +} + +GUF_STR_KWRDS uint32_t guf_str_view_hash32(const guf_str_view *sv) +{ + GUF_ASSERT(sv); + if (!sv->str || sv->len <= 0) { + return GUF_HASH32_INIT; + } + return guf_hash32(sv->str, sv->len, GUF_HASH32_INIT); +} + // Comparison: GUF_STR_KWRDS bool guf_str_view_equal(const guf_str_view* a, const guf_str_view* b) { - GUF_ASSERT_RELEASE(a && b); + GUF_ASSERT(a && b); if (a->len != b->len) { return false; } @@ -224,12 +244,19 @@ GUF_STR_KWRDS bool guf_str_view_equal(const guf_str_view* a, const guf_str_view* } else if (!a->str && !b->str) { return a->len == b->len; } + GUF_ASSERT(a->str && b->str); - GUF_ASSERT_RELEASE(a->len >= 0); + if (a->len <= 0) { + return true; + } return 0 == memcmp(a->str, b->str, a->len); } +GUF_STR_KWRDS bool guf_str_view_equal_val_arg(guf_str_view a_val, guf_str_view b_val) +{ + return guf_str_view_equal(&a_val, &b_val); +} #undef GUF_STR_IMPL #undef GUF_STR_IMPL_STATIC