diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2023-03-28 06:45:10 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2023-03-28 06:45:10 +0200 |
commit | 4126fa433baedc37c90f6984db5beb73d45ad3a6 (patch) | |
tree | 8966aaa364e407bf87c65c98d2bb98e17c41d2d3 /source | |
parent | 2d10c7cb0179af68271720376ae7ff1b2309fc81 (diff) | |
download | kit-4126fa433baedc37c90f6984db5beb73d45ad3a6.zip |
Barbarian strings
Diffstat (limited to 'source')
-rw-r--r-- | source/kit/string_ref.h | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h index 1497bb0..6c89292 100644 --- a/source/kit/string_ref.h +++ b/source/kit/string_ref.h @@ -2,6 +2,7 @@ #define KIT_STRING_REF_H #include "array_ref.h" +#include <string.h> #ifdef __cplusplus extern "C" { @@ -27,6 +28,19 @@ static kit_str_t kit_str(ptrdiff_t const size, return s; } +/* Make a barbarian string for C standard library functions. + * Not thread safe. + */ +static char const *kit_make_bs(kit_str_t const s) { + static char buf[4096]; + ptrdiff_t n = s.size; + if (n > 4095) + n = 4095; + memcpy(buf, s.values, n); + buf[n] = '\0'; + return buf; +} + #ifdef __GNUC__ # pragma GCC pop_options # pragma GCC diagnostic pop @@ -35,6 +49,8 @@ static kit_str_t kit_str(ptrdiff_t const size, #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(string_) \ kit_str((string_).size, (string_).values) @@ -45,6 +61,8 @@ static kit_str_t kit_str(ptrdiff_t const size, # define str_t kit_str_t # define SZ KIT_SZ +# define BS kit_make_bs +# define WRAP_BS KIT_WRAP_BS # define WRAP_STR KIT_WRAP_STR #endif |