Skip to content

Commit

Permalink
Simplify str2const() some more and make sure that type of the
Browse files Browse the repository at this point in the history
str2const((const str *)x) is "const str_const *", not just
"str_const *". This prevents "promoting" const str * into
[non-const]str_const * when passing it via that macro.
  • Loading branch information
sobomax committed Jan 25, 2021
1 parent 1d261f0 commit b134399
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions str.h
Expand Up @@ -21,7 +21,6 @@
#ifndef str_h
#define str_h

#include <assert.h>
#include <string.h>

/**
Expand Down Expand Up @@ -72,11 +71,12 @@ typedef struct __str_const str_const;
#define str_init(_string) (str){_string, sizeof(_string) - 1}
#define str_const_init(_string) (str_const){_string, sizeof(_string) - 1}

#define OBJ_IS_OF_TYPE(Type, Obj) _Generic((Obj), Type: 1, const Type: 1, default: 0)
#define str2const(_sp) ({ \
static_assert(OBJ_IS_OF_TYPE(str *, _sp), "str * expected"); \
/*return*/ (str_const *)(void *)(_sp); \
})
static inline const str_const *_cs2cc(const str *_sp) {return (const str_const *)(const void *)(_sp);}
static inline str_const *_s2c(str *_sp) {return (str_const *)(void *)(_sp);}

#define str2const(_sp) ( \
_Generic((_sp), str *: _s2c, const str *: _cs2cc)(_sp) \
)

static inline void init_str(str *dest, const char *src)
{
Expand Down

0 comments on commit b134399

Please sign in to comment.