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
--- a/src/string.c	Tue Jan 23 19:23:34 2018 +0100
+++ b/src/string.c	Sun Mar 11 13:43:07 2018 +0100
@@ -50,6 +50,21 @@
     return string;
 }
 
+scstr_t scstr(const char *cstring) {
+    scstr_t string;
+    string.ptr = cstring;
+    string.length = strlen(cstring);
+    return string;
+}
+
+scstr_t scstrn(const char *cstring, size_t length) {
+    scstr_t string;
+    string.ptr = cstring;
+    string.length = length;
+    return string;
+}
+
+
 size_t sstrnlen(size_t n, sstr_t s, ...) {
     va_list ap;
     size_t size = s.length;
@@ -391,11 +406,11 @@
     }
 }
 
-sstr_t sstrdup(sstr_t s) {
+sstr_t scstrdup(scstr_t s) {
     return sstrdup_a(ucx_default_allocator(), s);
 }
 
-sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
+sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t s) {
     sstr_t newstring;
     newstring.ptr = (char*)almalloc(allocator, s.length + 1);
     if (newstring.ptr) {
@@ -424,7 +439,7 @@
     return newstr;
 }
 
-int sstrprefix(sstr_t string, sstr_t prefix) {
+int ucx_strprefix(scstr_t string, scstr_t prefix) {
     if (string.length == 0) {
         return prefix.length == 0;
     }
@@ -439,7 +454,7 @@
     }
 }
 
-int sstrsuffix(sstr_t string, sstr_t suffix) {
+int ucx_strsuffix(scstr_t string, scstr_t suffix) {
     if (string.length == 0) {
         return suffix.length == 0;
     }
@@ -455,7 +470,7 @@
     }
 }
 
-sstr_t sstrlower(sstr_t string) {
+sstr_t ucx_strlower(scstr_t string) {
     sstr_t ret = sstrdup(string);
     for (size_t i = 0; i < ret.length ; i++) {
         ret.ptr[i] = tolower(ret.ptr[i]);
@@ -463,7 +478,7 @@
     return ret;
 }
 
-sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) {
+sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string) {
     sstr_t ret = sstrdup_a(allocator, string);
     for (size_t i = 0; i < ret.length ; i++) {
         ret.ptr[i] = tolower(ret.ptr[i]);
@@ -471,7 +486,7 @@
     return ret;
 }
 
-sstr_t sstrupper(sstr_t string) {
+sstr_t ucx_strupper(scstr_t string) {
     sstr_t ret = sstrdup(string);
     for (size_t i = 0; i < ret.length ; i++) {
         ret.ptr[i] = toupper(ret.ptr[i]);
@@ -479,10 +494,24 @@
     return ret;
 }
 
-sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) {
+sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string) {
     sstr_t ret = sstrdup_a(allocator, string);
     for (size_t i = 0; i < ret.length ; i++) {
         ret.ptr[i] = toupper(ret.ptr[i]);
     }
     return ret;
 }
+
+// private string conversion functions
+scstr_t ucx_sc2sc(scstr_t c) {
+    return c;
+}
+scstr_t ucx_ss2sc(sstr_t str) {
+    scstr_t cs;
+    cs.ptr = str.ptr;
+    cs.length = str.length;
+    return cs;
+}
+scstr_t ucx_ss2c_s(scstr_t c) {
+    return c;
+}
--- 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
 }

mercurial