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
     1.1 --- a/src/string.c	Tue May 08 12:49:56 2018 +0200
     1.2 +++ b/src/string.c	Sun May 13 07:13:09 2018 +0200
     1.3 @@ -166,49 +166,107 @@
     1.4      return s;
     1.5  }
     1.6  
     1.7 +static int ucx_substring(
     1.8 +        size_t str_length,
     1.9 +        size_t start,
    1.10 +        size_t length,
    1.11 +        size_t *newlen,
    1.12 +        size_t *newpos)
    1.13 +{
    1.14 +    *newlen = 0;
    1.15 +    *newpos = 0;
    1.16 +    
    1.17 +    if(start > str_length) {
    1.18 +        return 0;
    1.19 +    }
    1.20 +    
    1.21 +    if(length > str_length - start) {
    1.22 +        length = str_length - start;
    1.23 +    }
    1.24 +    *newlen = length;
    1.25 +    *newpos = start;
    1.26 +    return 1;
    1.27 +}
    1.28 +
    1.29  sstr_t sstrsubs(sstr_t s, size_t start) {
    1.30      return sstrsubsl (s, start, s.length-start);
    1.31  }
    1.32  
    1.33  sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
    1.34 -    sstr_t new_sstr;
    1.35 -    if (start >= s.length) {
    1.36 -        new_sstr.ptr = NULL;
    1.37 -        new_sstr.length = 0;
    1.38 -    } else {
    1.39 -        if (length > s.length-start) {
    1.40 -            length = s.length-start;
    1.41 +    size_t pos;
    1.42 +    sstr_t ret = { NULL, 0 };
    1.43 +    if(ucx_substring(s.length, start, length, &ret.length, &pos)) {
    1.44 +        ret.ptr = s.ptr + pos;
    1.45 +    }
    1.46 +    return ret;
    1.47 +}
    1.48 +
    1.49 +scstr_t scstrsubs(scstr_t s, size_t start) {
    1.50 +    return scstrsubsl (s, start, s.length-start);
    1.51 +}
    1.52 +
    1.53 +scstr_t scstrsubsl(scstr_t s, size_t start, size_t length) {
    1.54 +    size_t pos;
    1.55 +    scstr_t ret = { NULL, 0 };
    1.56 +    if(ucx_substring(s.length, start, length, &ret.length, &pos)) {
    1.57 +        ret.ptr = s.ptr + pos;
    1.58 +    }
    1.59 +    return ret;
    1.60 +}
    1.61 +
    1.62 +
    1.63 +int ucx_strchr(const char *string, size_t length, int chr, size_t *pos) {
    1.64 +    for(size_t i=0;i<length;i++) {
    1.65 +        if(string[i] == chr) {
    1.66 +            *pos = i;
    1.67 +            return 1;
    1.68          }
    1.69 -        new_sstr.ptr = &s.ptr[start];
    1.70 -        new_sstr.length = length;
    1.71      }
    1.72 -    return new_sstr;
    1.73 +    return 0;
    1.74 +}
    1.75 +
    1.76 +int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos) {
    1.77 +    if(length > 0) {
    1.78 +        for(size_t i=length-1;i>=0;i--) {
    1.79 +            if(string[i] == chr) {
    1.80 +                *pos = i;
    1.81 +                return 1;
    1.82 +            }
    1.83 +        }
    1.84 +    }
    1.85 +    return 0;
    1.86  }
    1.87  
    1.88  sstr_t sstrchr(sstr_t s, int c) {
    1.89 -    for(size_t i=0;i<s.length;i++) {
    1.90 -        if(s.ptr[i] == c) {
    1.91 -            return sstrsubs(s, i);
    1.92 -        }
    1.93 +    size_t pos = 0;
    1.94 +    if(ucx_strchr(s.ptr, s.length, c, &pos)) {
    1.95 +        return sstrsubs(s, pos);
    1.96      }
    1.97 -    sstr_t n;
    1.98 -    n.ptr = NULL;
    1.99 -    n.length = 0;
   1.100 -    return n;
   1.101 +    return sstrn(NULL, 0);
   1.102  }
   1.103  
   1.104  sstr_t sstrrchr(sstr_t s, int c) {
   1.105 -    if (s.length > 0) {
   1.106 -        for(size_t i=s.length;i>0;i--) {
   1.107 -            if(s.ptr[i-1] == c) {
   1.108 -                return sstrsubs(s, i-1);
   1.109 -            }
   1.110 -        }
   1.111 +    size_t pos = 0;
   1.112 +    if(ucx_strrchr(s.ptr, s.length, c, &pos)) {
   1.113 +        return sstrsubs(s, pos);
   1.114      }
   1.115 -    sstr_t n;
   1.116 -    n.ptr = NULL;
   1.117 -    n.length = 0;
   1.118 -    return n;
   1.119 +    return sstrn(NULL, 0);
   1.120 +}
   1.121 +
   1.122 +scstr_t scstrchr(scstr_t s, int c) {
   1.123 +    size_t pos = 0;
   1.124 +    if(ucx_strchr(s.ptr, s.length, c, &pos)) {
   1.125 +        return scstrsubs(s, pos);
   1.126 +    }
   1.127 +    return scstrn(NULL, 0);
   1.128 +}
   1.129 +
   1.130 +scstr_t scstrrchr(scstr_t s, int c) {
   1.131 +    size_t pos = 0;
   1.132 +    if(ucx_strrchr(s.ptr, s.length, c, &pos)) {
   1.133 +        return scstrsubs(s, pos);
   1.134 +    }
   1.135 +    return scstrn(NULL, 0);
   1.136  }
   1.137  
   1.138  #define ptable_r(dest, useheap, ptable, index) (dest = useheap ? \
     2.1 --- a/src/ucx/string.h	Tue May 08 12:49:56 2018 +0200
     2.2 +++ b/src/ucx/string.h	Sun May 13 07:13:09 2018 +0200
     2.3 @@ -184,7 +184,6 @@
     2.4   *
     2.5   * @param count   the total number of strings to concatenate
     2.6   * @param s1      first string
     2.7 - * @param s2      second string
     2.8   * @param ...     all remaining strings
     2.9   * @return the concatenated string
    2.10   */
    2.11 @@ -200,7 +199,6 @@
    2.12   * @param a       the allocator to use
    2.13   * @param count   the total number of strings to concatenate
    2.14   * @param s1      first string
    2.15 - * @param s2      second string
    2.16   * @param ...     all remaining strings
    2.17   * @return the concatenated string
    2.18   */
    2.19 @@ -242,6 +240,13 @@
    2.20   */
    2.21  sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
    2.22  
    2.23 +scstr_t scstrsubs(scstr_t s, size_t start);
    2.24 +scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
    2.25 +
    2.26 +
    2.27 +int ucx_strchr(const char *string, size_t length, int chr, size_t *pos);
    2.28 +int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos);
    2.29 +
    2.30  /**
    2.31   * Returns a substring starting at the location of the first occurrence of the
    2.32   * specified character.
    2.33 @@ -271,6 +276,9 @@
    2.34  sstr_t sstrrchr(sstr_t string, int chr);
    2.35  
    2.36  
    2.37 +scstr_t scstrchr(scstr_t string, int chr);
    2.38 +scstr_t scstrrchr(scstr_t string, int chr);
    2.39 +
    2.40  const char* ucx_strstr(
    2.41          const char *str,
    2.42          size_t length,

mercurial