adds scstr_t struct for const strings and adapts some string functions constsstr

Sun, 11 Mar 2018 13:43:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Mar 2018 13:43:07 +0100
branch
constsstr
changeset 275
96f643d30ff1
parent 274
0923c036b913
child 276
f1b2146d4805

adds scstr_t struct for const strings and adapts some string functions

src/string.c file | annotate | diff | comparison | revisions
src/ucx/string.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/string.c	Tue Jan 23 19:23:34 2018 +0100
     1.2 +++ b/src/string.c	Sun Mar 11 13:43:07 2018 +0100
     1.3 @@ -50,6 +50,21 @@
     1.4      return string;
     1.5  }
     1.6  
     1.7 +scstr_t scstr(const char *cstring) {
     1.8 +    scstr_t string;
     1.9 +    string.ptr = cstring;
    1.10 +    string.length = strlen(cstring);
    1.11 +    return string;
    1.12 +}
    1.13 +
    1.14 +scstr_t scstrn(const char *cstring, size_t length) {
    1.15 +    scstr_t string;
    1.16 +    string.ptr = cstring;
    1.17 +    string.length = length;
    1.18 +    return string;
    1.19 +}
    1.20 +
    1.21 +
    1.22  size_t sstrnlen(size_t n, sstr_t s, ...) {
    1.23      va_list ap;
    1.24      size_t size = s.length;
    1.25 @@ -391,11 +406,11 @@
    1.26      }
    1.27  }
    1.28  
    1.29 -sstr_t sstrdup(sstr_t s) {
    1.30 +sstr_t scstrdup(scstr_t s) {
    1.31      return sstrdup_a(ucx_default_allocator(), s);
    1.32  }
    1.33  
    1.34 -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
    1.35 +sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t s) {
    1.36      sstr_t newstring;
    1.37      newstring.ptr = (char*)almalloc(allocator, s.length + 1);
    1.38      if (newstring.ptr) {
    1.39 @@ -424,7 +439,7 @@
    1.40      return newstr;
    1.41  }
    1.42  
    1.43 -int sstrprefix(sstr_t string, sstr_t prefix) {
    1.44 +int ucx_strprefix(scstr_t string, scstr_t prefix) {
    1.45      if (string.length == 0) {
    1.46          return prefix.length == 0;
    1.47      }
    1.48 @@ -439,7 +454,7 @@
    1.49      }
    1.50  }
    1.51  
    1.52 -int sstrsuffix(sstr_t string, sstr_t suffix) {
    1.53 +int ucx_strsuffix(scstr_t string, scstr_t suffix) {
    1.54      if (string.length == 0) {
    1.55          return suffix.length == 0;
    1.56      }
    1.57 @@ -455,7 +470,7 @@
    1.58      }
    1.59  }
    1.60  
    1.61 -sstr_t sstrlower(sstr_t string) {
    1.62 +sstr_t ucx_strlower(scstr_t string) {
    1.63      sstr_t ret = sstrdup(string);
    1.64      for (size_t i = 0; i < ret.length ; i++) {
    1.65          ret.ptr[i] = tolower(ret.ptr[i]);
    1.66 @@ -463,7 +478,7 @@
    1.67      return ret;
    1.68  }
    1.69  
    1.70 -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) {
    1.71 +sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string) {
    1.72      sstr_t ret = sstrdup_a(allocator, string);
    1.73      for (size_t i = 0; i < ret.length ; i++) {
    1.74          ret.ptr[i] = tolower(ret.ptr[i]);
    1.75 @@ -471,7 +486,7 @@
    1.76      return ret;
    1.77  }
    1.78  
    1.79 -sstr_t sstrupper(sstr_t string) {
    1.80 +sstr_t ucx_strupper(scstr_t string) {
    1.81      sstr_t ret = sstrdup(string);
    1.82      for (size_t i = 0; i < ret.length ; i++) {
    1.83          ret.ptr[i] = toupper(ret.ptr[i]);
    1.84 @@ -479,10 +494,24 @@
    1.85      return ret;
    1.86  }
    1.87  
    1.88 -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) {
    1.89 +sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string) {
    1.90      sstr_t ret = sstrdup_a(allocator, string);
    1.91      for (size_t i = 0; i < ret.length ; i++) {
    1.92          ret.ptr[i] = toupper(ret.ptr[i]);
    1.93      }
    1.94      return ret;
    1.95  }
    1.96 +
    1.97 +// private string conversion functions
    1.98 +scstr_t ucx_sc2sc(scstr_t c) {
    1.99 +    return c;
   1.100 +}
   1.101 +scstr_t ucx_ss2sc(sstr_t str) {
   1.102 +    scstr_t cs;
   1.103 +    cs.ptr = str.ptr;
   1.104 +    cs.length = str.length;
   1.105 +    return cs;
   1.106 +}
   1.107 +scstr_t ucx_ss2c_s(scstr_t c) {
   1.108 +    return c;
   1.109 +}
     2.1 --- a/src/ucx/string.h	Tue Jan 23 19:23:34 2018 +0100
     2.2 +++ b/src/ucx/string.h	Sun Mar 11 13:43:07 2018 +0100
     2.3 @@ -61,7 +61,6 @@
     2.4  #ifdef	__cplusplus
     2.5  extern "C" {
     2.6  #endif
     2.7 -
     2.8  /**
     2.9   * The UCX string structure.
    2.10   */
    2.11 @@ -73,6 +72,55 @@
    2.12      size_t length;
    2.13  } sstr_t;
    2.14  
    2.15 +typedef struct {
    2.16 +    const char *ptr;
    2.17 +    size_t     length;
    2.18 +} scstr_t;
    2.19 +#ifdef	__cplusplus
    2.20 +}
    2.21 +#endif
    2.22 +
    2.23 +
    2.24 +#ifdef __cplusplus
    2.25 +inline scstr_t s2scstr(sstr_t s) {
    2.26 +    scstr_t c;
    2.27 +    c.ptr = s.ptr;
    2.28 +    c.length = s.ptr;
    2.29 +    return c;
    2.30 +}
    2.31 +inline scstr_t s2scstr(scstr_t c) {
    2.32 +    return c;
    2.33 +}
    2.34 +#define SCSTR s2scstr
    2.35 +#else
    2.36 +
    2.37 +scstr_t ucx_sc2sc(scstr_t c);
    2.38 +scstr_t ucx_ss2sc(sstr_t str);
    2.39 +#if __STDC_VERSION__ >= 201112L
    2.40 +#define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
    2.41 +#elif defined(__GNUC__) || defined(__clang__)
    2.42 +#define SCSTR(str) __builtin_choose_expr( \
    2.43 +        __builtin_types_compatible_p(typeof(str), sstr_t), \
    2.44 +        ucx_ss2sc, \
    2.45 +        ucx_sc2sc)(str)
    2.46 +#elif defined(__sun)
    2.47 +#define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
    2.48 +	scstr_t ucx_tmp_var_c; \
    2.49 +	ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
    2.50 +	ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
    2.51 +	ucx_tmp_var_c; })
    2.52 +#else
    2.53 +scstr_t ucx_ss2c_s();
    2.54 +#define SCSTR ucx_ss2c_s
    2.55 +#endif /* C11 feature test */
    2.56 +
    2.57 +#endif /* C++ */
    2.58 +
    2.59 +#ifdef	__cplusplus
    2.60 +extern "C" {
    2.61 +#endif
    2.62 +
    2.63 +
    2.64  /**
    2.65   * Creates a new sstr_t based on a C string.
    2.66   * 
    2.67 @@ -104,6 +152,9 @@
    2.68  sstr_t sstrn(char *cstring, size_t length);
    2.69  
    2.70  
    2.71 +scstr_t scstr(const char *cstring);
    2.72 +scstr_t scstrn(const char *cstring, size_t length);
    2.73 +
    2.74  /**
    2.75   * Returns the cumulated length of all specified strings.
    2.76   *
    2.77 @@ -348,7 +399,9 @@
    2.78   * @return a duplicate of the string
    2.79   * @see sstrdup_a()
    2.80   */
    2.81 -sstr_t sstrdup(sstr_t string);
    2.82 +sstr_t scstrdup(scstr_t string);
    2.83 +
    2.84 +#define sstrdup(s) scstrdup(SCSTR(s))
    2.85  
    2.86  /**
    2.87   * Creates a duplicate of the specified string using a UcxAllocator.
    2.88 @@ -366,7 +419,9 @@
    2.89   * @return a duplicate of the string
    2.90   * @see sstrdup()
    2.91   */
    2.92 -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
    2.93 +sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
    2.94 +
    2.95 +#define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
    2.96  
    2.97  /**
    2.98   * Omits leading and trailing spaces.
    2.99 @@ -393,7 +448,9 @@
   2.100   * @param prefix the prefix the string should have
   2.101   * @return 1, if and only if the string has the specified prefix, 0 otherwise
   2.102   */
   2.103 -int sstrprefix(sstr_t string, sstr_t prefix);
   2.104 +int ucx_strprefix(scstr_t string, scstr_t prefix);
   2.105 +
   2.106 +#define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
   2.107  
   2.108  /**
   2.109   * Checks, if a string has a specific suffix.
   2.110 @@ -401,7 +458,9 @@
   2.111   * @param suffix the suffix the string should have
   2.112   * @return 1, if and only if the string has the specified suffix, 0 otherwise
   2.113   */
   2.114 -int sstrsuffix(sstr_t string, sstr_t suffix);
   2.115 +int ucx_strsuffix(scstr_t string, scstr_t suffix);
   2.116 +
   2.117 +#define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
   2.118  
   2.119  /**
   2.120   * Returns a lower case version of a string.
   2.121 @@ -413,7 +472,9 @@
   2.122   * @return the resulting lower case string
   2.123   * @see sstrdup()
   2.124   */
   2.125 -sstr_t sstrlower(sstr_t string);
   2.126 +sstr_t ucx_strlower(scstr_t string);
   2.127 +
   2.128 +#define sstrlower(string) ucx_strlower(SCSTR(string))
   2.129  
   2.130  /**
   2.131   * Returns a lower case version of a string.
   2.132 @@ -426,7 +487,9 @@
   2.133   * @return the resulting lower case string
   2.134   * @see sstrdup_a()
   2.135   */
   2.136 -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string);
   2.137 +sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
   2.138 +
   2.139 +#define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
   2.140  
   2.141  /**
   2.142   * Returns a upper case version of a string.
   2.143 @@ -438,7 +501,9 @@
   2.144   * @return the resulting upper case string
   2.145   * @see sstrdup()
   2.146   */
   2.147 -sstr_t sstrupper(sstr_t string);
   2.148 +sstr_t ucx_strupper(scstr_t string);
   2.149 +
   2.150 +#define sstrupper(string) ucx_strupper(SCSTR(string))
   2.151  
   2.152  /**
   2.153   * Returns a upper case version of a string.
   2.154 @@ -451,7 +516,9 @@
   2.155   * @return the resulting upper case string
   2.156   * @see sstrdup_a()
   2.157   */
   2.158 -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string);
   2.159 +sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
   2.160 +
   2.161 +#define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
   2.162  
   2.163  #ifdef	__cplusplus
   2.164  }

mercurial