diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-11-17 05:24:04 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-11-17 05:24:04 +0100 |
commit | d5a727a062e4d727491f4e7f047269d60a93a8cf (patch) | |
tree | 565aec155ab056f1bef8ddc7dc78d52e65aa76b7 /kit/string_ref.h | |
parent | 4ba961dcbf60f7c12f53f46baa261757e15d5931 (diff) | |
download | saw-d5a727a062e4d727491f4e7f047269d60a93a8cf.zip |
Remove lots of dependencies
Diffstat (limited to 'kit/string_ref.h')
-rw-r--r-- | kit/string_ref.h | 137 |
1 files changed, 0 insertions, 137 deletions
diff --git a/kit/string_ref.h b/kit/string_ref.h deleted file mode 100644 index e7ea809..0000000 --- a/kit/string_ref.h +++ /dev/null @@ -1,137 +0,0 @@ -#ifndef KIT_STRING_REF_H -#define KIT_STRING_REF_H - -#include "array_ref.h" - -#include <assert.h> -#include <string.h> - -#ifdef __cplusplus -extern "C" { -#endif - -typedef KIT_AR(char) kit_str_t; - -#if defined(__GNUC__) || defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-function" -# pragma GCC diagnostic ignored "-Wunknown-pragmas" -# pragma GCC push_options -# pragma GCC optimize("O3") -#endif - -static kit_str_t kit_str(i64 size, char const *static_string) { - kit_str_t s = { .size = size, .values = (char *) static_string }; - return s; -} - -enum { - KIT_STR_NOT_FOUND = -1, -}; - -static i64 kit_str_find(kit_str_t a, kit_str_t b) { - assert(a.size >= 0 && (a.size == 0 || a.values != NULL) && - b.size >= 0 && (b.size == 0 || b.values != NULL)); - if (a.size < 0 || (a.size != 0 && a.values == NULL) || b.size < 0 || - (b.size != 0 && b.values == NULL)) - return -1; - for (i64 index = 0; index + b.size <= a.size; index++) - if (KIT_AR_EQUAL(kit_str(b.size, a.values + index), b)) - return index; - return -1; -} - -static i64 kit_str_find_back(kit_str_t a, kit_str_t b) { - assert(a.size >= 0 && (a.size == 0 || a.values != NULL) && - b.size >= 0 && (b.size == 0 || b.values != NULL)); - if (a.size < 0 || (a.size != 0 && a.values == NULL) || b.size < 0 || - (b.size != 0 && b.values == NULL)) - return -1; - for (i64 index = a.size - b.size; index >= 0; index--) - if (KIT_AR_EQUAL(kit_str(b.size, a.values + index), b)) - return index; - return -1; -} - -static i64 kit_str_find_n(kit_str_t a, kit_str_t b, i64 n) { - assert(a.size >= 0 && (a.size == 0 || a.values != NULL) && - b.size >= 0 && (b.size == 0 || b.values != NULL)); - if (a.size < 0 || (a.size != 0 && a.values == NULL) || b.size < 0 || - (b.size != 0 && b.values == NULL)) - return -1; - i64 count = 0; - for (i64 index = 0; index + b.size <= a.size; index++) - if (KIT_AR_EQUAL(kit_str(b.size, a.values + index), b)) { - if (count == n) - return index; - else - count++; - } - return -1; -} - -static i64 kit_str_find_back_n(kit_str_t a, kit_str_t b, i64 n) { - assert(a.size >= 0 && (a.size == 0 || a.values != NULL) && - b.size >= 0 && (b.size == 0 || b.values != NULL)); - if (a.size < 0 || (a.size != 0 && a.values == NULL) || b.size < 0 || - (b.size != 0 && b.values == NULL)) - return -1; - i64 count = 0; - for (i64 index = a.size - b.size; index >= 0; index--) - if (KIT_AR_EQUAL(kit_str(b.size, a.values + index), b)) { - if (count == n) - return index; - else - count++; - } - return -1; -} - -// Make a barbarian string for C standard library functions. -// Not thread safe. -// Use with caution. -// -static char *kit_make_bs(kit_str_t s) { - static char buf[8][4096]; - static i32 index = 0; - i64 n = s.size; - if (n > 4095) - n = 4095; - if (n > 0) - memcpy(buf[index], s.values, n); - buf[index][n] = '\0'; - char *result = buf[index]; - index = (index + 1) % 8; - return result; -} - -#if defined(__GNUC__) || defined(__clang__) -# pragma GCC pop_options -# pragma GCC diagnostic pop -#endif - -#define KIT_SZ(static_str_) \ - kit_str(sizeof(static_str_) - 1, (static_str_)) - -#define KIT_WRAP_BS(string_) kit_str(strlen(string_), (string_)) - -#define KIT_WRAP_STR(...) \ - kit_str((__VA_ARGS__).size, (__VA_ARGS__).values) - -#ifdef __cplusplus -} -#endif - -#define BS(...) kit_make_bs(KIT_WRAP_STR(__VA_ARGS__)) -#define str_t kit_str_t -#define str kit_str -#define str_find kit_str_find -#define str_find_back kit_str_find_back -#define str_find_n kit_str_find_n -#define str_find_back_n kit_str_find_back_n -#define STR_NOT_FOUND KIT_STR_NOT_FOUND -#define SZ KIT_SZ -#define WRAP_BS KIT_WRAP_BS -#define WRAP_STR KIT_WRAP_STR - -#endif |