diff -r 0923c036b913 -r 96f643d30ff1 src/ucx/string.h --- a/src/ucx/string.h Tue Jan 23 19:23:34 2018 +0100 +++ b/src/ucx/string.h Sun Mar 11 13:43:07 2018 +0100 @@ -61,7 +61,6 @@ #ifdef __cplusplus extern "C" { #endif - /** * The UCX string structure. */ @@ -73,6 +72,55 @@ size_t length; } sstr_t; +typedef struct { + const char *ptr; + size_t length; +} scstr_t; +#ifdef __cplusplus +} +#endif + + +#ifdef __cplusplus +inline scstr_t s2scstr(sstr_t s) { + scstr_t c; + c.ptr = s.ptr; + c.length = s.ptr; + return c; +} +inline scstr_t s2scstr(scstr_t c) { + return c; +} +#define SCSTR s2scstr +#else + +scstr_t ucx_sc2sc(scstr_t c); +scstr_t ucx_ss2sc(sstr_t str); +#if __STDC_VERSION__ >= 201112L +#define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str) +#elif defined(__GNUC__) || defined(__clang__) +#define SCSTR(str) __builtin_choose_expr( \ + __builtin_types_compatible_p(typeof(str), sstr_t), \ + ucx_ss2sc, \ + ucx_sc2sc)(str) +#elif defined(__sun) +#define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \ + scstr_t ucx_tmp_var_c; \ + ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\ + ucx_tmp_var_c.length = ucx_tmp_var_str.length;\ + ucx_tmp_var_c; }) +#else +scstr_t ucx_ss2c_s(); +#define SCSTR ucx_ss2c_s +#endif /* C11 feature test */ + +#endif /* C++ */ + +#ifdef __cplusplus +extern "C" { +#endif + + /** * Creates a new sstr_t based on a C string. * @@ -104,6 +152,9 @@ sstr_t sstrn(char *cstring, size_t length); +scstr_t scstr(const char *cstring); +scstr_t scstrn(const char *cstring, size_t length); + /** * Returns the cumulated length of all specified strings. * @@ -348,7 +399,9 @@ * @return a duplicate of the string * @see sstrdup_a() */ -sstr_t sstrdup(sstr_t string); +sstr_t scstrdup(scstr_t string); + +#define sstrdup(s) scstrdup(SCSTR(s)) /** * Creates a duplicate of the specified string using a UcxAllocator. @@ -366,7 +419,9 @@ * @return a duplicate of the string * @see sstrdup() */ -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string); +sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string); + +#define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s)) /** * Omits leading and trailing spaces. @@ -393,7 +448,9 @@ * @param prefix the prefix the string should have * @return 1, if and only if the string has the specified prefix, 0 otherwise */ -int sstrprefix(sstr_t string, sstr_t prefix); +int ucx_strprefix(scstr_t string, scstr_t prefix); + +#define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix)) /** * Checks, if a string has a specific suffix. @@ -401,7 +458,9 @@ * @param suffix the suffix the string should have * @return 1, if and only if the string has the specified suffix, 0 otherwise */ -int sstrsuffix(sstr_t string, sstr_t suffix); +int ucx_strsuffix(scstr_t string, scstr_t suffix); + +#define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix)) /** * Returns a lower case version of a string. @@ -413,7 +472,9 @@ * @return the resulting lower case string * @see sstrdup() */ -sstr_t sstrlower(sstr_t string); +sstr_t ucx_strlower(scstr_t string); + +#define sstrlower(string) ucx_strlower(SCSTR(string)) /** * Returns a lower case version of a string. @@ -426,7 +487,9 @@ * @return the resulting lower case string * @see sstrdup_a() */ -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string); +sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string); + +#define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string)) /** * Returns a upper case version of a string. @@ -438,7 +501,9 @@ * @return the resulting upper case string * @see sstrdup() */ -sstr_t sstrupper(sstr_t string); +sstr_t ucx_strupper(scstr_t string); + +#define sstrupper(string) ucx_strupper(SCSTR(string)) /** * Returns a upper case version of a string. @@ -451,7 +516,9 @@ * @return the resulting upper case string * @see sstrdup_a() */ -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string); +sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string); + +#define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string) #ifdef __cplusplus }