src/ucx/string.h

changeset 306
90b6d69bb499
parent 283
c3b6ff227481
parent 300
d1f814633049
child 315
5b97de37aada
     1.1 --- a/src/ucx/string.h	Mon May 14 15:58:51 2018 +0200
     1.2 +++ b/src/ucx/string.h	Mon May 14 17:56:03 2018 +0200
     1.3 @@ -67,7 +67,6 @@
     1.4  #ifdef	__cplusplus
     1.5  extern "C" {
     1.6  #endif
     1.7 -
     1.8  /**
     1.9   * The UCX string structure.
    1.10   */
    1.11 @@ -79,6 +78,56 @@
    1.12      size_t length;
    1.13  } sstr_t;
    1.14  
    1.15 +typedef struct {
    1.16 +    const char *ptr;
    1.17 +    size_t     length;
    1.18 +} scstr_t;
    1.19 +
    1.20 +#ifdef	__cplusplus
    1.21 +}
    1.22 +#endif
    1.23 +
    1.24 +
    1.25 +#ifdef __cplusplus
    1.26 +inline scstr_t s2scstr(sstr_t s) {
    1.27 +    scstr_t c;
    1.28 +    c.ptr = s.ptr;
    1.29 +    c.length = s.ptr;
    1.30 +    return c;
    1.31 +}
    1.32 +inline scstr_t s2scstr(scstr_t c) {
    1.33 +    return c;
    1.34 +}
    1.35 +#define SCSTR s2scstr
    1.36 +#else
    1.37 +
    1.38 +scstr_t ucx_sc2sc(scstr_t c);
    1.39 +scstr_t ucx_ss2sc(sstr_t str);
    1.40 +#if __STDC_VERSION__ >= 201112L
    1.41 +#define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
    1.42 +#elif defined(__GNUC__) || defined(__clang__)
    1.43 +#define SCSTR(str) __builtin_choose_expr( \
    1.44 +        __builtin_types_compatible_p(typeof(str), sstr_t), \
    1.45 +        ucx_ss2sc, \
    1.46 +        ucx_sc2sc)(str)
    1.47 +#elif defined(__sun)
    1.48 +#define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
    1.49 +	scstr_t ucx_tmp_var_c; \
    1.50 +	ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
    1.51 +	ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
    1.52 +	ucx_tmp_var_c; })
    1.53 +#else
    1.54 +scstr_t ucx_ss2c_s();
    1.55 +#define SCSTR ucx_ss2c_s
    1.56 +#endif /* C11 feature test */
    1.57 +
    1.58 +#endif /* C++ */
    1.59 +
    1.60 +#ifdef	__cplusplus
    1.61 +extern "C" {
    1.62 +#endif
    1.63 +
    1.64 +
    1.65  /**
    1.66   * Creates a new sstr_t based on a C string.
    1.67   * 
    1.68 @@ -110,6 +159,9 @@
    1.69  sstr_t sstrn(char *cstring, size_t length);
    1.70  
    1.71  
    1.72 +scstr_t scstr(const char *cstring);
    1.73 +scstr_t scstrn(const char *cstring, size_t length);
    1.74 +
    1.75  /**
    1.76   * Returns the cumulated length of all specified strings.
    1.77   *
    1.78 @@ -123,7 +175,9 @@
    1.79   * @param ...      all other strings
    1.80   * @return the cumulated length of all strings
    1.81   */
    1.82 -size_t sstrnlen(size_t count, sstr_t string, ...);
    1.83 +size_t ucx_strnlen(size_t count, ...);
    1.84 +
    1.85 +#define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__)
    1.86  
    1.87  /**
    1.88   * Concatenates two or more strings.
    1.89 @@ -136,11 +190,12 @@
    1.90   *
    1.91   * @param count   the total number of strings to concatenate
    1.92   * @param s1      first string
    1.93 - * @param s2      second string
    1.94   * @param ...     all remaining strings
    1.95   * @return the concatenated string
    1.96   */
    1.97 -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...);
    1.98 +sstr_t ucx_strcat(size_t count, scstr_t s1, ...);
    1.99 +
   1.100 +#define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__)
   1.101  
   1.102  /**
   1.103   * Concatenates two or more strings using a UcxAllocator.
   1.104 @@ -150,12 +205,12 @@
   1.105   * @param a       the allocator to use
   1.106   * @param count   the total number of strings to concatenate
   1.107   * @param s1      first string
   1.108 - * @param s2      second string
   1.109   * @param ...     all remaining strings
   1.110   * @return the concatenated string
   1.111   */
   1.112 -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...);
   1.113 +sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
   1.114  
   1.115 +#define sstrcat_a(count, s1, ...) ucx_strcat_a(count, SCSTR(s1), __VA_ARGS__)
   1.116  
   1.117  /**
   1.118   * Returns a substring starting at the specified location.
   1.119 @@ -191,6 +246,13 @@
   1.120   */
   1.121  sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
   1.122  
   1.123 +scstr_t scstrsubs(scstr_t s, size_t start);
   1.124 +scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
   1.125 +
   1.126 +
   1.127 +int ucx_strchr(const char *string, size_t length, int chr, size_t *pos);
   1.128 +int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos);
   1.129 +
   1.130  /**
   1.131   * Returns a substring starting at the location of the first occurrence of the
   1.132   * specified character.
   1.133 @@ -219,6 +281,17 @@
   1.134   */
   1.135  sstr_t sstrrchr(sstr_t string, int chr);
   1.136  
   1.137 +
   1.138 +scstr_t scstrchr(scstr_t string, int chr);
   1.139 +scstr_t scstrrchr(scstr_t string, int chr);
   1.140 +
   1.141 +const char* ucx_strstr(
   1.142 +        const char *str,
   1.143 +        size_t length,
   1.144 +        const char *match,
   1.145 +        size_t matchlen,
   1.146 +        size_t *newlen);
   1.147 +
   1.148  /**
   1.149   * Returns a substring starting at the location of the first occurrence of the
   1.150   * specified string.
   1.151 @@ -234,7 +307,11 @@
   1.152   *               <code>match</code>, or an empty string, if the sequence is not
   1.153   *               present in <code>string</code>
   1.154   */
   1.155 -sstr_t sstrstr(sstr_t string, sstr_t match);
   1.156 +sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
   1.157 +#define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
   1.158 +
   1.159 +scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
   1.160 +#define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
   1.161  
   1.162  /**
   1.163   * Splits a string into parts by using a delimiter string.
   1.164 @@ -283,7 +360,9 @@
   1.165   * 
   1.166   * @see sstrsplit_a()
   1.167   */
   1.168 -sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count);
   1.169 +sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
   1.170 +
   1.171 +#define sstrsplit(s, delim, count) ucx_strsplit(SCSTR(s), SCSTR(delim), count)
   1.172  
   1.173  /**
   1.174   * Performing sstrsplit() using a UcxAllocator.
   1.175 @@ -307,9 +386,11 @@
   1.176   * 
   1.177   * @see sstrsplit()
   1.178   */
   1.179 -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
   1.180 +sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
   1.181          ssize_t *count);
   1.182  
   1.183 +#define sstrsplit_a(a, s, d, c) ucx_strsplit_a(a, SCSTR(s), SCSTR(d, c))
   1.184 +
   1.185  /**
   1.186   * Compares two UCX strings with standard <code>memcmp()</code>.
   1.187   * 
   1.188 @@ -322,7 +403,9 @@
   1.189   * length of s1 is greater than the length of s2 or the result of
   1.190   * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
   1.191   */
   1.192 -int sstrcmp(sstr_t s1, sstr_t s2);
   1.193 +int ucx_str_cmp(scstr_t s1, scstr_t s2);
   1.194 +
   1.195 +#define sstrcmp(s1, s2) ucx_str_cmp(SCSTR(s1), SCSTR(s2))
   1.196  
   1.197  /**
   1.198   * Compares two UCX strings ignoring the case.
   1.199 @@ -338,7 +421,9 @@
   1.200   * first two differing characters otherwise (i.e. 0 if the strings match and
   1.201   * no characters differ)
   1.202   */
   1.203 -int sstrcasecmp(sstr_t s1, sstr_t s2);
   1.204 +int ucx_str_casecmp(scstr_t s1, scstr_t s2);
   1.205 +
   1.206 +#define sstrcasecmp(s1, s2) ucx_str_casecmp(SCSTR(s1), SCSTR(s2))
   1.207  
   1.208  /**
   1.209   * Creates a duplicate of the specified string.
   1.210 @@ -354,7 +439,9 @@
   1.211   * @return a duplicate of the string
   1.212   * @see sstrdup_a()
   1.213   */
   1.214 -sstr_t sstrdup(sstr_t string);
   1.215 +sstr_t scstrdup(scstr_t string);
   1.216 +
   1.217 +#define sstrdup(s) scstrdup(SCSTR(s))
   1.218  
   1.219  /**
   1.220   * Creates a duplicate of the specified string using a UcxAllocator.
   1.221 @@ -372,7 +459,12 @@
   1.222   * @return a duplicate of the string
   1.223   * @see sstrdup()
   1.224   */
   1.225 -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
   1.226 +sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
   1.227 +
   1.228 +#define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
   1.229 +
   1.230 +
   1.231 +size_t ucx_strtrim(const char *str, size_t length, size_t *newlen);
   1.232  
   1.233  /**
   1.234   * Omits leading and trailing spaces.
   1.235 @@ -393,13 +485,17 @@
   1.236   */
   1.237  sstr_t sstrtrim(sstr_t string);
   1.238  
   1.239 +scstr_t scstrtrim(scstr_t string);
   1.240 +
   1.241  /**
   1.242   * Checks, if a string has a specific prefix.
   1.243   * @param string the string to check
   1.244   * @param prefix the prefix the string should have
   1.245   * @return 1, if and only if the string has the specified prefix, 0 otherwise
   1.246   */
   1.247 -int sstrprefix(sstr_t string, sstr_t prefix);
   1.248 +int ucx_strprefix(scstr_t string, scstr_t prefix);
   1.249 +
   1.250 +#define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
   1.251  
   1.252  /**
   1.253   * Checks, if a string has a specific suffix.
   1.254 @@ -407,7 +503,9 @@
   1.255   * @param suffix the suffix the string should have
   1.256   * @return 1, if and only if the string has the specified suffix, 0 otherwise
   1.257   */
   1.258 -int sstrsuffix(sstr_t string, sstr_t suffix);
   1.259 +int ucx_strsuffix(scstr_t string, scstr_t suffix);
   1.260 +
   1.261 +#define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
   1.262  
   1.263  /**
   1.264   * Returns a lower case version of a string.
   1.265 @@ -419,7 +517,9 @@
   1.266   * @return the resulting lower case string
   1.267   * @see sstrdup()
   1.268   */
   1.269 -sstr_t sstrlower(sstr_t string);
   1.270 +sstr_t ucx_strlower(scstr_t string);
   1.271 +
   1.272 +#define sstrlower(string) ucx_strlower(SCSTR(string))
   1.273  
   1.274  /**
   1.275   * Returns a lower case version of a string.
   1.276 @@ -432,7 +532,9 @@
   1.277   * @return the resulting lower case string
   1.278   * @see sstrdup_a()
   1.279   */
   1.280 -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string);
   1.281 +sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
   1.282 +
   1.283 +#define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
   1.284  
   1.285  /**
   1.286   * Returns a upper case version of a string.
   1.287 @@ -444,7 +546,9 @@
   1.288   * @return the resulting upper case string
   1.289   * @see sstrdup()
   1.290   */
   1.291 -sstr_t sstrupper(sstr_t string);
   1.292 +sstr_t ucx_strupper(scstr_t string);
   1.293 +
   1.294 +#define sstrupper(string) ucx_strupper(SCSTR(string))
   1.295  
   1.296  /**
   1.297   * Returns a upper case version of a string.
   1.298 @@ -457,7 +561,9 @@
   1.299   * @return the resulting upper case string
   1.300   * @see sstrdup_a()
   1.301   */
   1.302 -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string);
   1.303 +sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
   1.304 +
   1.305 +#define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
   1.306  
   1.307  #ifdef	__cplusplus
   1.308  }

mercurial