diff options
-rw-r--r-- | cstring/CMakeLists.txt | 1 | ||||
-rw-r--r-- | cstring/include/cstring.h | 13 |
2 files changed, 6 insertions, 8 deletions
diff --git a/cstring/CMakeLists.txt b/cstring/CMakeLists.txt index 3283cea..df2fa45 100644 --- a/cstring/CMakeLists.txt +++ b/cstring/CMakeLists.txt | |||
@@ -8,4 +8,5 @@ target_include_directories(cstring INTERFACE | |||
8 | include) | 8 | include) |
9 | 9 | ||
10 | target_link_libraries(cstring INTERFACE | 10 | target_link_libraries(cstring INTERFACE |
11 | cassert | ||
11 | -lbsd) | 12 | -lbsd) |
diff --git a/cstring/include/cstring.h b/cstring/include/cstring.h index 991d742..134e68e 100644 --- a/cstring/include/cstring.h +++ b/cstring/include/cstring.h | |||
@@ -1,17 +1,14 @@ | |||
1 | /// Fixed-size strings with value semantics. | 1 | /// Fixed-size strings with value semantics. |
2 | #pragma once | 2 | #pragma once |
3 | 3 | ||
4 | #include <assert.h> | ||
5 | #include <bsd/string.h> | 4 | #include <bsd/string.h> |
5 | #include <cassert.h> | ||
6 | #include <stdbool.h> | 6 | #include <stdbool.h> |
7 | #include <stdint.h> | 7 | #include <stdint.h> |
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | 9 | ||
10 | /// A fixed-size string. | 10 | /// A fixed-size string. |
11 | /// The string is null-terminated so that it can be used with the usual C APIs. | 11 | /// The string is null-terminated so that it can be used with the usual C APIs. |
12 | // | ||
13 | // TODO: The asserts on length should be hard asserts, not just asserts in debug | ||
14 | // builds. | ||
15 | #define DEF_STRING(STRING, SIZE) \ | 12 | #define DEF_STRING(STRING, SIZE) \ |
16 | typedef struct STRING { \ | 13 | typedef struct STRING { \ |
17 | size_t length; \ | 14 | size_t length; \ |
@@ -55,19 +52,19 @@ | |||
55 | \ | 52 | \ |
56 | static inline void STRING##_append_cstr(STRING* a, const char* b) { \ | 53 | static inline void STRING##_append_cstr(STRING* a, const char* b) { \ |
57 | size_t b_length = strlen(b); \ | 54 | size_t b_length = strlen(b); \ |
58 | assert(a->length + b_length + 1 < SIZE); \ | 55 | ASSERT(a->length + b_length + 1 < SIZE); \ |
59 | strlcpy(a->str + a->length, b, SIZE); \ | 56 | strlcpy(a->str + a->length, b, SIZE); \ |
60 | a->length = a->length + b_length; \ | 57 | a->length = a->length + b_length; \ |
61 | } \ | 58 | } \ |
62 | \ | 59 | \ |
63 | static inline void STRING##_append(STRING* a, STRING b) { \ | 60 | static inline void STRING##_append(STRING* a, STRING b) { \ |
64 | assert(a->length + b.length + 1 < SIZE); \ | 61 | ASSERT(a->length + b.length + 1 < SIZE); \ |
65 | strlcpy(a->str + a->length, b.str, SIZE); \ | 62 | strlcpy(a->str + a->length, b.str, SIZE); \ |
66 | a->length = a->length + b.length; \ | 63 | a->length = a->length + b.length; \ |
67 | } \ | 64 | } \ |
68 | \ | 65 | \ |
69 | static inline STRING STRING##_concat(STRING a, STRING b) { \ | 66 | static inline STRING STRING##_concat(STRING a, STRING b) { \ |
70 | assert(a.length + b.length + 1 < SIZE); \ | 67 | ASSERT(a.length + b.length + 1 < SIZE); \ |
71 | STRING str = {0}; \ | 68 | STRING str = {0}; \ |
72 | strlcpy(str.str, a.str, SIZE); \ | 69 | strlcpy(str.str, a.str, SIZE); \ |
73 | strlcpy(str.str + a.length, b.str, SIZE); \ | 70 | strlcpy(str.str + a.length, b.str, SIZE); \ |
@@ -99,7 +96,7 @@ | |||
99 | static inline STRING STRING##_itoa(int n) { \ | 96 | static inline STRING STRING##_itoa(int n) { \ |
100 | STRING str = (STRING){0}; \ | 97 | STRING str = (STRING){0}; \ |
101 | const int written = snprintf(str.str, SIZE, "%d", n); \ | 98 | const int written = snprintf(str.str, SIZE, "%d", n); \ |
102 | assert(written >= 0); \ | 99 | ASSERT(written >= 0); \ |
103 | str.length = (size_t)written; \ | 100 | str.length = (size_t)written; \ |
104 | return str; \ | 101 | return str; \ |
105 | } \ | 102 | } \ |