completes conversion to scstr constsstr

Sun, 13 May 2018 07:13:09 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 13 May 2018 07:13:09 +0200
branch
constsstr
changeset 300
d1f814633049
parent 288
6af5798342e8
child 306
90b6d69bb499
child 307
e2b2b9a5b5ea

completes conversion to scstr

src/string.c file | annotate | diff | comparison | revisions
src/ucx/string.h file | annotate | diff | comparison | revisions
--- a/src/string.c	Tue May 08 12:49:56 2018 +0200
+++ b/src/string.c	Sun May 13 07:13:09 2018 +0200
@@ -166,49 +166,107 @@
     return s;
 }
 
+static int ucx_substring(
+        size_t str_length,
+        size_t start,
+        size_t length,
+        size_t *newlen,
+        size_t *newpos)
+{
+    *newlen = 0;
+    *newpos = 0;
+    
+    if(start > str_length) {
+        return 0;
+    }
+    
+    if(length > str_length - start) {
+        length = str_length - start;
+    }
+    *newlen = length;
+    *newpos = start;
+    return 1;
+}
+
 sstr_t sstrsubs(sstr_t s, size_t start) {
     return sstrsubsl (s, start, s.length-start);
 }
 
 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
-    sstr_t new_sstr;
-    if (start >= s.length) {
-        new_sstr.ptr = NULL;
-        new_sstr.length = 0;
-    } else {
-        if (length > s.length-start) {
-            length = s.length-start;
+    size_t pos;
+    sstr_t ret = { NULL, 0 };
+    if(ucx_substring(s.length, start, length, &ret.length, &pos)) {
+        ret.ptr = s.ptr + pos;
+    }
+    return ret;
+}
+
+scstr_t scstrsubs(scstr_t s, size_t start) {
+    return scstrsubsl (s, start, s.length-start);
+}
+
+scstr_t scstrsubsl(scstr_t s, size_t start, size_t length) {
+    size_t pos;
+    scstr_t ret = { NULL, 0 };
+    if(ucx_substring(s.length, start, length, &ret.length, &pos)) {
+        ret.ptr = s.ptr + pos;
+    }
+    return ret;
+}
+
+
+int ucx_strchr(const char *string, size_t length, int chr, size_t *pos) {
+    for(size_t i=0;i<length;i++) {
+        if(string[i] == chr) {
+            *pos = i;
+            return 1;
         }
-        new_sstr.ptr = &s.ptr[start];
-        new_sstr.length = length;
     }
-    return new_sstr;
+    return 0;
+}
+
+int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos) {
+    if(length > 0) {
+        for(size_t i=length-1;i>=0;i--) {
+            if(string[i] == chr) {
+                *pos = i;
+                return 1;
+            }
+        }
+    }
+    return 0;
 }
 
 sstr_t sstrchr(sstr_t s, int c) {
-    for(size_t i=0;i<s.length;i++) {
-        if(s.ptr[i] == c) {
-            return sstrsubs(s, i);
-        }
+    size_t pos = 0;
+    if(ucx_strchr(s.ptr, s.length, c, &pos)) {
+        return sstrsubs(s, pos);
     }
-    sstr_t n;
-    n.ptr = NULL;
-    n.length = 0;
-    return n;
+    return sstrn(NULL, 0);
 }
 
 sstr_t sstrrchr(sstr_t s, int c) {
-    if (s.length > 0) {
-        for(size_t i=s.length;i>0;i--) {
-            if(s.ptr[i-1] == c) {
-                return sstrsubs(s, i-1);
-            }
-        }
+    size_t pos = 0;
+    if(ucx_strrchr(s.ptr, s.length, c, &pos)) {
+        return sstrsubs(s, pos);
     }
-    sstr_t n;
-    n.ptr = NULL;
-    n.length = 0;
-    return n;
+    return sstrn(NULL, 0);
+}
+
+scstr_t scstrchr(scstr_t s, int c) {
+    size_t pos = 0;
+    if(ucx_strchr(s.ptr, s.length, c, &pos)) {
+        return scstrsubs(s, pos);
+    }
+    return scstrn(NULL, 0);
+}
+
+scstr_t scstrrchr(scstr_t s, int c) {
+    size_t pos = 0;
+    if(ucx_strrchr(s.ptr, s.length, c, &pos)) {
+        return scstrsubs(s, pos);
+    }
+    return scstrn(NULL, 0);
 }
 
 #define ptable_r(dest, useheap, ptable, index) (dest = useheap ? \
--- a/src/ucx/string.h	Tue May 08 12:49:56 2018 +0200
+++ b/src/ucx/string.h	Sun May 13 07:13:09 2018 +0200
@@ -184,7 +184,6 @@
  *
  * @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
  */
@@ -200,7 +199,6 @@
  * @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
  */
@@ -242,6 +240,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.
@@ -271,6 +276,9 @@
 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,

mercurial