aboutsummaryrefslogtreecommitdiff
path: root/cstring
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-06-27 09:27:53 -0700
committer3gg <3gg@shellblade.net>2025-06-27 09:27:53 -0700
commit8e245a6c9a8287178b2d0853dec24f442c52ce95 (patch)
tree2728a073f7c9e7babf23aec6df3db20cfdf30e7a /cstring
parent1cb585ea456d61213ec2f2faa08bfd6a4fa39228 (diff)
Remove bsd string dependencyHEADmain
Diffstat (limited to 'cstring')
-rw-r--r--cstring/CMakeLists.txt5
-rw-r--r--cstring/include/cstring.h35
-rw-r--r--cstring/src/cstring.c8
3 files changed, 26 insertions, 22 deletions
diff --git a/cstring/CMakeLists.txt b/cstring/CMakeLists.txt
index a1abde2..92fe5a7 100644
--- a/cstring/CMakeLists.txt
+++ b/cstring/CMakeLists.txt
@@ -15,8 +15,3 @@ target_include_directories(cstring PUBLIC
15 15
16target_link_libraries(cstring PUBLIC 16target_link_libraries(cstring PUBLIC
17 cassert) 17 cassert)
18
19if(LINUX)
20 target_link_libraries(cstring PUBLIC
21 -lbsd)
22endif()
diff --git a/cstring/include/cstring.h b/cstring/include/cstring.h
index 8ed4e93..3e693e1 100644
--- a/cstring/include/cstring.h
+++ b/cstring/include/cstring.h
@@ -3,14 +3,23 @@
3 3
4#include <cassert.h> 4#include <cassert.h>
5 5
6#ifdef __linux__
7#include <bsd/string.h>
8#else
9#include <string.h>
10#endif
11#include <stdbool.h> 6#include <stdbool.h>
12#include <stdint.h> 7#include <stdint.h>
13#include <stdio.h> 8#include <stdio.h>
9#include <string.h>
10
11// -----------------------------------------------------------------------------
12// C-string helpers.
13
14/// Return a hash of the given string.
15uint64_t cstring_hash(const char* str);
16
17/// Copy 'count' characters from the source to the destination, null-terminating
18/// the destination.
19static inline void cstring_copy(char* dst, const char* src, size_t count) {
20 memcpy(dst, src, count * sizeof(char));
21 dst[count] = '\0';
22}
14 23
15// ----------------------------------------------------------------------------- 24// -----------------------------------------------------------------------------
16// Fix-sized strings. 25// Fix-sized strings.
@@ -38,7 +47,8 @@
38 return (STRING){0}; \ 47 return (STRING){0}; \
39 } else { \ 48 } else { \
40 STRING str = (STRING){0}; \ 49 STRING str = (STRING){0}; \
41 str.length = strlcpy(str.str, cstr, SIZE); \ 50 str.length = strlen(cstr); \
51 cstring_copy(str.str, cstr, str.length); \
42 return str; \ 52 return str; \
43 } \ 53 } \
44 } \ 54 } \
@@ -65,7 +75,7 @@
65 static inline void STRING##_append_cstr_len( \ 75 static inline void STRING##_append_cstr_len( \
66 STRING* a, const char* b, const size_t b_length) { \ 76 STRING* a, const char* b, const size_t b_length) { \
67 ASSERT(a->length + b_length + 1 <= SIZE); \ 77 ASSERT(a->length + b_length + 1 <= SIZE); \
68 strlcpy(a->str + a->length, b, SIZE - a->length); \ 78 cstring_copy(a->str + a->length, b, SIZE - a->length); \
69 a->length += b_length; \ 79 a->length += b_length; \
70 } \ 80 } \
71 \ 81 \
@@ -103,7 +113,7 @@
103 ASSERT(start < a.length); \ 113 ASSERT(start < a.length); \
104 ASSERT(end <= a.length); \ 114 ASSERT(end <= a.length); \
105 STRING str = {0}; \ 115 STRING str = {0}; \
106 strlcpy(str.str, &a.str[start], end - start); \ 116 cstring_copy(str.str, &a.str[start], end - start); \
107 return str; \ 117 return str; \
108 } \ 118 } \
109 \ 119 \
@@ -134,15 +144,6 @@
134 return cstring_hash(str.str); \ 144 return cstring_hash(str.str); \
135 } 145 }
136 146
137/// Return a hash of the given string.
138static inline uint64_t cstring_hash(const char* str) {
139 uint64_t hash = 0;
140 for (size_t i = 0; i < strlen(str); ++i) {
141 hash = (uint64_t)str[i] + (hash << 6) + (hash << 16) - hash;
142 }
143 return hash;
144}
145
146DEF_STRING(sstring, 32) // Small. 147DEF_STRING(sstring, 32) // Small.
147DEF_STRING(mstring, 256) // Medium. 148DEF_STRING(mstring, 256) // Medium.
148DEF_STRING(lstring, 1024) // Large. 149DEF_STRING(lstring, 1024) // Large.
diff --git a/cstring/src/cstring.c b/cstring/src/cstring.c
index 832cb85..e308589 100644
--- a/cstring/src/cstring.c
+++ b/cstring/src/cstring.c
@@ -101,3 +101,11 @@ string string_format_size(size_t size) {
101 .length = length, 101 .length = length,
102 }; 102 };
103} 103}
104
105uint64_t cstring_hash(const char* str) {
106 uint64_t hash = 0;
107 for (size_t i = 0; i < strlen(str); ++i) {
108 hash = (uint64_t)str[i] + (hash << 6) + (hash << 16) - hash;
109 }
110 return hash;
111}