summaryrefslogtreecommitdiff
path: root/source/kit/string_ref.h
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-12-29 06:21:33 +0100
committerMitya Selivanov <automainint@guattari.tech>2023-12-29 06:21:33 +0100
commit2d6c8fec45b23a8a28668ecf3ef281139ab778a7 (patch)
tree75d2a8538992129a83c0c2b83688289443d697e5 /source/kit/string_ref.h
parent820b171245f2f14766f3accdb0246a4e2c0d596a (diff)
downloadsaw-2d6c8fec45b23a8a28668ecf3ef281139ab778a7.zip
refactor dependencies; include dependencies source code
Diffstat (limited to 'source/kit/string_ref.h')
-rw-r--r--source/kit/string_ref.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h
new file mode 100644
index 0000000..38ade80
--- /dev/null
+++ b/source/kit/string_ref.h
@@ -0,0 +1,72 @@
+#ifndef KIT_STRING_REF_H
+#define KIT_STRING_REF_H
+
+#include "array_ref.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;
+}
+
+// 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)
+
+#ifndef KIT_DISABLE_SHORT_NAMES
+# define BS(...) kit_make_bs(KIT_WRAP_STR(__VA_ARGS__))
+
+# define str_t kit_str_t
+
+# define SZ KIT_SZ
+# define WRAP_BS KIT_WRAP_BS
+# define WRAP_STR KIT_WRAP_STR
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif