From 97fa2dc0a55a49818b12f475e2c163fa201bf2d0 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Thu, 3 Feb 2022 13:29:24 +0100 Subject: Fix c++ compability in cstr. --- include/stc/cstr.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/stc/cstr.h b/include/stc/cstr.h index b99e5525..78fd4cce 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -32,14 +32,14 @@ #include #define cstr_npos (SIZE_MAX >> 1) -typedef struct { size_t size, cap; char str[]; } _cstr_rep_t; +typedef struct { size_t size, cap; char str[sizeof(size_t)]; } _cstr_rep_t; #define _cstr_rep(self) c_container_of((self)->str, _cstr_rep_t, str) #ifdef _i_static - static const cstr cstr_null; + static _cstr_rep_t _cstr_nullrep = {0, 0, {0}}; + static const cstr cstr_null = {_cstr_nullrep.str}; #else extern const cstr cstr_null; #endif - /* optimal memory: based on malloc_usable_size() sequence: 24, 40, 56, ... */ #define _cstr_opt_mem(cap) ((((offsetof(_cstr_rep_t, str) + (cap) + 8)>>4)<<4) + 8) /* optimal string capacity: 7, 23, 39, ... */ @@ -175,11 +175,10 @@ c_strncasecmp(const char* s1, const char* s2, size_t nmax) { /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(_i_implement) -static struct { size_t size, cap; char str[1]; } _cstr_nullrep = {0, 0, {0}}; -#ifdef _i_static -static -#endif +#ifndef _i_static +static struct _cstr_rep_t _cstr_nullrep = {0, 0, {0}}; const cstr cstr_null = {_cstr_nullrep.str}; +#endif STC_DEF size_t cstr_reserve(cstr* self, const size_t cap) { -- cgit v1.2.3