#ifndef GUF_CSTR_H #define GUF_CSTR_H #include #include "guf_common.h" typedef const char* guf_const_cstr; typedef char* guf_heap_cstr; #define GUF_OBJ_TYPE guf_const_cstr #include "guf_obj.h" #define GUF_OBJ_TYPE guf_heap_cstr #include "guf_obj.h" static inline guf_heap_cstr *guf_heap_cstr_cpy_init(guf_heap_cstr *dst, const guf_heap_cstr *src) { GUF_ASSERT_RELEASE(dst && src); if (*src == NULL) { *dst = NULL; return dst; } *dst = strdup(*src); GUF_ASSERT_RELEASE(*dst); return dst; } static inline guf_heap_cstr *guf_heap_cstr_move_init(guf_heap_cstr *dst, guf_heap_cstr *src) { GUF_ASSERT_RELEASE(dst && src); *dst = *src; *src = NULL; return dst; } static inline void guf_heap_cstr_free(guf_heap_cstr *a) { GUF_ASSERT_RELEASE(a); free(*a); } static inline int guf_heap_cstr_cmp(const guf_heap_cstr *a, const guf_heap_cstr *b) { GUF_ASSERT_RELEASE(a && b); GUF_ASSERT_RELEASE(*a && *b); return strcmp(*a, *b); } static inline GUF_OBJ_OPS_DEFINE_CMP_INVERSE(guf_heap_cstr, guf_heap_cstr_cmp, guf_heap_cstr_cmp_inv) static inline bool guf_heap_cstr_eq(const guf_heap_cstr *a, const guf_heap_cstr *b) { GUF_ASSERT_RELEASE(a && b); GUF_ASSERT_RELEASE(*a && *b); return 0 == strcmp(*a, *b); } static guf_heap_cstr_ops guf_heap_cstr_operations = { .copy_init = guf_heap_cstr_cpy_init, .move_init = guf_heap_cstr_move_init, .free = guf_heap_cstr_free, .cmp = guf_heap_cstr_cmp, .cmp_inv = guf_heap_cstr_cmp_inv, .eq = guf_heap_cstr_eq }; static inline int guf_const_cstr_cmp(const guf_const_cstr *a, const guf_const_cstr *b) { GUF_ASSERT_RELEASE(a && b); GUF_ASSERT_RELEASE(*a && *b); return strcmp(*a, *b); } static inline GUF_OBJ_OPS_DEFINE_CMP_INVERSE(guf_const_cstr, guf_const_cstr_cmp, guf_const_cstr_cmp_inv) static inline bool guf_const_cstr_eq(const guf_const_cstr *a, const guf_const_cstr *b) { GUF_ASSERT_RELEASE(a && b); GUF_ASSERT_RELEASE(*a && *b); return 0 == strcmp(*a, *b); } static guf_const_cstr_ops guf_const_cstr_operations = { .copy_init = NULL, .move_init = NULL, .free = NULL, .cmp = guf_const_cstr_cmp, .cmp_inv = guf_const_cstr_cmp_inv, .eq = guf_const_cstr_eq }; #endif