diff -r 353d71349e61 -r 90b6d69bb499 src/ucx/string.h --- a/src/ucx/string.h Mon May 14 15:58:51 2018 +0200 +++ b/src/ucx/string.h Mon May 14 17:56:03 2018 +0200 @@ -67,7 +67,6 @@ #ifdef __cplusplus extern "C" { #endif - /** * The UCX string structure. */ @@ -79,6 +78,56 @@ 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. * @@ -110,6 +159,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. * @@ -123,7 +175,9 @@ * @param ... all other strings * @return the cumulated length of all strings */ -size_t sstrnlen(size_t count, sstr_t string, ...); +size_t ucx_strnlen(size_t count, ...); + +#define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__) /** * Concatenates two or more strings. @@ -136,11 +190,12 @@ * * @param count the total number of strings to concatenate * @param s1 first string - * @param s2 second string * @param ... all remaining strings * @return the concatenated string */ -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); +sstr_t ucx_strcat(size_t count, scstr_t s1, ...); + +#define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__) /** * Concatenates two or more strings using a UcxAllocator. @@ -150,12 +205,12 @@ * @param a the allocator to use * @param count the total number of strings to concatenate * @param s1 first string - * @param s2 second string * @param ... all remaining strings * @return the concatenated string */ -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); +sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...); +#define sstrcat_a(count, s1, ...) ucx_strcat_a(count, SCSTR(s1), __VA_ARGS__) /** * Returns a substring starting at the specified location. @@ -191,6 +246,13 @@ */ sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); +scstr_t scstrsubs(scstr_t s, size_t start); +scstr_t scstrsubsl(scstr_t string, size_t start, size_t length); + + +int ucx_strchr(const char *string, size_t length, int chr, size_t *pos); +int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos); + /** * Returns a substring starting at the location of the first occurrence of the * specified character. @@ -219,6 +281,17 @@ */ sstr_t sstrrchr(sstr_t string, int chr); + +scstr_t scstrchr(scstr_t string, int chr); +scstr_t scstrrchr(scstr_t string, int chr); + +const char* ucx_strstr( + const char *str, + size_t length, + const char *match, + size_t matchlen, + size_t *newlen); + /** * Returns a substring starting at the location of the first occurrence of the * specified string. @@ -234,7 +307,11 @@ * match, or an empty string, if the sequence is not * present in string */ -sstr_t sstrstr(sstr_t string, sstr_t match); +sstr_t ucx_sstrstr(sstr_t string, scstr_t match); +#define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match)) + +scstr_t ucx_scstrstr(scstr_t string, scstr_t match); +#define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match)) /** * Splits a string into parts by using a delimiter string. @@ -283,7 +360,9 @@ * * @see sstrsplit_a() */ -sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); +sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count); + +#define sstrsplit(s, delim, count) ucx_strsplit(SCSTR(s), SCSTR(delim), count) /** * Performing sstrsplit() using a UcxAllocator. @@ -307,9 +386,11 @@ * * @see sstrsplit() */ -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, +sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim, ssize_t *count); +#define sstrsplit_a(a, s, d, c) ucx_strsplit_a(a, SCSTR(s), SCSTR(d, c)) + /** * Compares two UCX strings with standard memcmp(). * @@ -322,7 +403,9 @@ * length of s1 is greater than the length of s2 or the result of * memcmp() otherwise (i.e. 0 if the strings match) */ -int sstrcmp(sstr_t s1, sstr_t s2); +int ucx_str_cmp(scstr_t s1, scstr_t s2); + +#define sstrcmp(s1, s2) ucx_str_cmp(SCSTR(s1), SCSTR(s2)) /** * Compares two UCX strings ignoring the case. @@ -338,7 +421,9 @@ * first two differing characters otherwise (i.e. 0 if the strings match and * no characters differ) */ -int sstrcasecmp(sstr_t s1, sstr_t s2); +int ucx_str_casecmp(scstr_t s1, scstr_t s2); + +#define sstrcasecmp(s1, s2) ucx_str_casecmp(SCSTR(s1), SCSTR(s2)) /** * Creates a duplicate of the specified string. @@ -354,7 +439,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. @@ -372,7 +459,12 @@ * @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)) + + +size_t ucx_strtrim(const char *str, size_t length, size_t *newlen); /** * Omits leading and trailing spaces. @@ -393,13 +485,17 @@ */ sstr_t sstrtrim(sstr_t string); +scstr_t scstrtrim(scstr_t string); + /** * Checks, if a string has a specific prefix. * @param string the string to check * @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. @@ -407,7 +503,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. @@ -419,7 +517,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. @@ -432,7 +532,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. @@ -444,7 +546,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. @@ -457,7 +561,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 }