From 12b8d218d93a487b478f66a2bc9cb6ef6458dd63 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 5 Oct 2024 15:03:34 -0700 Subject: Add slice and make_empty. --- cstring/include/cstring.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cstring/include/cstring.h b/cstring/include/cstring.h index b07dad6..8ed4e93 100644 --- a/cstring/include/cstring.h +++ b/cstring/include/cstring.h @@ -43,6 +43,8 @@ } \ } \ \ + static inline STRING STRING##_make_empty() { return (STRING){0}; } \ + \ static inline STRING STRING##_dirname(const STRING path) { \ STRING str = path; \ for (int i = str.length - 1; i >= 0; --i) { \ @@ -79,9 +81,8 @@ const STRING a, const char* b, const size_t b_length) { \ ASSERT(a.length + b_length + 1 <= SIZE); \ STRING str = {0}; \ - strlcpy(str.str, a.str, SIZE); \ - strlcpy(str.str + a.length, b, SIZE - a.length); \ - str.length = a.length + b_length; \ + STRING##_append_cstr_len(&str, a.str, a.length); \ + STRING##_append_cstr_len(&str, b, b_length); \ return str; \ } \ \ @@ -97,6 +98,15 @@ return STRING##_concat(STRING##_concat_cstr(a, "/"), b); \ } \ \ + static inline STRING STRING##_slice( \ + const STRING a, const size_t start, const size_t end) { \ + ASSERT(start < a.length); \ + ASSERT(end <= a.length); \ + STRING str = {0}; \ + strlcpy(str.str, &a.str[start], end - start); \ + return str; \ + } \ + \ static inline bool STRING##_eq_cstr_len( \ const STRING a, const char* b, size_t b_length) { \ return (a.length == b_length) && strncmp(a.str, b, a.length) == 0; \ -- cgit v1.2.3