src/ucx/string.h

branch
constsstr
changeset 275
96f643d30ff1
parent 259
2f5dea574a75
child 276
f1b2146d4805
     1.1 --- a/src/ucx/string.h	Tue Jan 23 19:23:34 2018 +0100
     1.2 +++ b/src/ucx/string.h	Sun Mar 11 13:43:07 2018 +0100
     1.3 @@ -61,7 +61,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 @@ -73,6 +72,55 @@
    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 +#ifdef	__cplusplus
    1.20 +}
    1.21 +#endif
    1.22 +
    1.23 +
    1.24 +#ifdef __cplusplus
    1.25 +inline scstr_t s2scstr(sstr_t s) {
    1.26 +    scstr_t c;
    1.27 +    c.ptr = s.ptr;
    1.28 +    c.length = s.ptr;
    1.29 +    return c;
    1.30 +}
    1.31 +inline scstr_t s2scstr(scstr_t c) {
    1.32 +    return c;
    1.33 +}
    1.34 +#define SCSTR s2scstr
    1.35 +#else
    1.36 +
    1.37 +scstr_t ucx_sc2sc(scstr_t c);
    1.38 +scstr_t ucx_ss2sc(sstr_t str);
    1.39 +#if __STDC_VERSION__ >= 201112L
    1.40 +#define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
    1.41 +#elif defined(__GNUC__) || defined(__clang__)
    1.42 +#define SCSTR(str) __builtin_choose_expr( \
    1.43 +        __builtin_types_compatible_p(typeof(str), sstr_t), \
    1.44 +        ucx_ss2sc, \
    1.45 +        ucx_sc2sc)(str)
    1.46 +#elif defined(__sun)
    1.47 +#define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
    1.48 +	scstr_t ucx_tmp_var_c; \
    1.49 +	ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
    1.50 +	ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
    1.51 +	ucx_tmp_var_c; })
    1.52 +#else
    1.53 +scstr_t ucx_ss2c_s();
    1.54 +#define SCSTR ucx_ss2c_s
    1.55 +#endif /* C11 feature test */
    1.56 +
    1.57 +#endif /* C++ */
    1.58 +
    1.59 +#ifdef	__cplusplus
    1.60 +extern "C" {
    1.61 +#endif
    1.62 +
    1.63 +
    1.64  /**
    1.65   * Creates a new sstr_t based on a C string.
    1.66   * 
    1.67 @@ -104,6 +152,9 @@
    1.68  sstr_t sstrn(char *cstring, size_t length);
    1.69  
    1.70  
    1.71 +scstr_t scstr(const char *cstring);
    1.72 +scstr_t scstrn(const char *cstring, size_t length);
    1.73 +
    1.74  /**
    1.75   * Returns the cumulated length of all specified strings.
    1.76   *
    1.77 @@ -348,7 +399,9 @@
    1.78   * @return a duplicate of the string
    1.79   * @see sstrdup_a()
    1.80   */
    1.81 -sstr_t sstrdup(sstr_t string);
    1.82 +sstr_t scstrdup(scstr_t string);
    1.83 +
    1.84 +#define sstrdup(s) scstrdup(SCSTR(s))
    1.85  
    1.86  /**
    1.87   * Creates a duplicate of the specified string using a UcxAllocator.
    1.88 @@ -366,7 +419,9 @@
    1.89   * @return a duplicate of the string
    1.90   * @see sstrdup()
    1.91   */
    1.92 -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
    1.93 +sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
    1.94 +
    1.95 +#define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
    1.96  
    1.97  /**
    1.98   * Omits leading and trailing spaces.
    1.99 @@ -393,7 +448,9 @@
   1.100   * @param prefix the prefix the string should have
   1.101   * @return 1, if and only if the string has the specified prefix, 0 otherwise
   1.102   */
   1.103 -int sstrprefix(sstr_t string, sstr_t prefix);
   1.104 +int ucx_strprefix(scstr_t string, scstr_t prefix);
   1.105 +
   1.106 +#define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
   1.107  
   1.108  /**
   1.109   * Checks, if a string has a specific suffix.
   1.110 @@ -401,7 +458,9 @@
   1.111   * @param suffix the suffix the string should have
   1.112   * @return 1, if and only if the string has the specified suffix, 0 otherwise
   1.113   */
   1.114 -int sstrsuffix(sstr_t string, sstr_t suffix);
   1.115 +int ucx_strsuffix(scstr_t string, scstr_t suffix);
   1.116 +
   1.117 +#define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
   1.118  
   1.119  /**
   1.120   * Returns a lower case version of a string.
   1.121 @@ -413,7 +472,9 @@
   1.122   * @return the resulting lower case string
   1.123   * @see sstrdup()
   1.124   */
   1.125 -sstr_t sstrlower(sstr_t string);
   1.126 +sstr_t ucx_strlower(scstr_t string);
   1.127 +
   1.128 +#define sstrlower(string) ucx_strlower(SCSTR(string))
   1.129  
   1.130  /**
   1.131   * Returns a lower case version of a string.
   1.132 @@ -426,7 +487,9 @@
   1.133   * @return the resulting lower case string
   1.134   * @see sstrdup_a()
   1.135   */
   1.136 -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string);
   1.137 +sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
   1.138 +
   1.139 +#define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
   1.140  
   1.141  /**
   1.142   * Returns a upper case version of a string.
   1.143 @@ -438,7 +501,9 @@
   1.144   * @return the resulting upper case string
   1.145   * @see sstrdup()
   1.146   */
   1.147 -sstr_t sstrupper(sstr_t string);
   1.148 +sstr_t ucx_strupper(scstr_t string);
   1.149 +
   1.150 +#define sstrupper(string) ucx_strupper(SCSTR(string))
   1.151  
   1.152  /**
   1.153   * Returns a upper case version of a string.
   1.154 @@ -451,7 +516,9 @@
   1.155   * @return the resulting upper case string
   1.156   * @see sstrdup_a()
   1.157   */
   1.158 -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string);
   1.159 +sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
   1.160 +
   1.161 +#define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
   1.162  
   1.163  #ifdef	__cplusplus
   1.164  }

mercurial