diff options
-rw-r--r-- | cstring/CMakeLists.txt | 5 | ||||
-rw-r--r-- | cstring/include/cstring.h | 35 | ||||
-rw-r--r-- | cstring/src/cstring.c | 8 |
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 | ||
16 | target_link_libraries(cstring PUBLIC | 16 | target_link_libraries(cstring PUBLIC |
17 | cassert) | 17 | cassert) |
18 | |||
19 | if(LINUX) | ||
20 | target_link_libraries(cstring PUBLIC | ||
21 | -lbsd) | ||
22 | endif() | ||
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. | ||
15 | uint64_t cstring_hash(const char* str); | ||
16 | |||
17 | /// Copy 'count' characters from the source to the destination, null-terminating | ||
18 | /// the destination. | ||
19 | static 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. | ||
138 | static 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 | |||
146 | DEF_STRING(sstring, 32) // Small. | 147 | DEF_STRING(sstring, 32) // Small. |
147 | DEF_STRING(mstring, 256) // Medium. | 148 | DEF_STRING(mstring, 256) // Medium. |
148 | DEF_STRING(lstring, 1024) // Large. | 149 | DEF_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 | |||
105 | uint64_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 | } | ||