From 6aea96466ee881fa16116de6380ca693c95165e2 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 13 Apr 2024 12:22:01 -0700 Subject: Hard asserts. --- cstring/CMakeLists.txt | 1 + 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 include) target_link_libraries(cstring INTERFACE + cassert -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 @@ /// Fixed-size strings with value semantics. #pragma once -#include #include +#include #include #include #include /// A fixed-size string. /// The string is null-terminated so that it can be used with the usual C APIs. -// -// TODO: The asserts on length should be hard asserts, not just asserts in debug -// builds. #define DEF_STRING(STRING, SIZE) \ typedef struct STRING { \ size_t length; \ @@ -55,19 +52,19 @@ \ static inline void STRING##_append_cstr(STRING* a, const char* b) { \ size_t b_length = strlen(b); \ - assert(a->length + b_length + 1 < SIZE); \ + ASSERT(a->length + b_length + 1 < SIZE); \ strlcpy(a->str + a->length, b, SIZE); \ a->length = a->length + b_length; \ } \ \ static inline void STRING##_append(STRING* a, STRING b) { \ - assert(a->length + b.length + 1 < SIZE); \ + ASSERT(a->length + b.length + 1 < SIZE); \ strlcpy(a->str + a->length, b.str, SIZE); \ a->length = a->length + b.length; \ } \ \ static inline STRING STRING##_concat(STRING a, STRING b) { \ - assert(a.length + b.length + 1 < SIZE); \ + ASSERT(a.length + b.length + 1 < SIZE); \ STRING str = {0}; \ strlcpy(str.str, a.str, SIZE); \ strlcpy(str.str + a.length, b.str, SIZE); \ @@ -99,7 +96,7 @@ static inline STRING STRING##_itoa(int n) { \ STRING str = (STRING){0}; \ const int written = snprintf(str.str, SIZE, "%d", n); \ - assert(written >= 0); \ + ASSERT(written >= 0); \ str.length = (size_t)written; \ return str; \ } \ -- cgit v1.2.3