ucx/string.c

Fri, 21 Jun 2013 10:27:03 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 21 Jun 2013 10:27:03 +0200
changeset 104
9d3dea320d8e
parent 103
08018864fb91
child 108
d2b1e67b2b48
permissions
-rw-r--r--

fixed sstrtrim and some warnings

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 Olaf Wintermann. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include <stdlib.h>
    30 #include <string.h>
    31 #include <stdarg.h>
    33 #include "string.h"
    35 sstr_t sstr(char *s) {
    36     sstr_t string;
    37     string.ptr = s;
    38     string.length = strlen(s);
    39     return string;
    40 }
    42 sstr_t sstrn(char *s, size_t n) {
    43     sstr_t string;
    44     string.ptr = s;
    45     string.length = n;
    46     return string;
    47 }
    49 size_t sstrnlen(size_t n, sstr_t s, ...) {
    50     va_list ap;
    51     size_t size = s.length;
    52     va_start(ap, s);
    54     for (size_t i = 0 ; i < n-1 ; i++) {
    55         sstr_t str = va_arg(ap, sstr_t);
    56         size += str.length;
    57     }
    58     va_end(ap);
    60     return size;
    61 }
    63 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...) {
    64     va_list ap;
    65     va_start(ap, c1);
    66     s.ptr[0] = 0;
    68     size_t len = s.length;
    69     size_t cplen = c1.length > len ? len : c1.length;
    70     char   *ptr = s.ptr;
    72     memcpy(ptr, c1.ptr, cplen);
    73     len -= cplen;
    74     ptr += cplen;
    75     for (size_t i = 0 ; i < n-1 ; i++) {
    76         sstr_t str = va_arg (ap, sstr_t);
    77         cplen = str.length > len ? len : str.length;
    78         if(cplen <= 0) {
    79             va_end(ap);
    80             return s;
    81         }
    82         memcpy(ptr, str.ptr, cplen);
    83         len -= cplen;
    84         ptr += cplen;
    85     }
    86     va_end(ap);
    87     s.length = ptr - s.ptr;
    89     return s;
    90 }
    92 sstr_t sstrsubs(sstr_t s, size_t start) {
    93     return sstrsubsl (s, start, s.length-start);
    94 }
    96 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
    97     sstr_t new_sstr;
    98     if (start >= s.length) {
    99         return s;
   100     }
   101     if (length > s.length-start) {
   102         length = s.length-start;
   103     }
   104     new_sstr.ptr = &s.ptr[start];
   105     new_sstr.length = length;
   106     return new_sstr;
   107 }
   109 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
   110     if (d.length == 0) {
   111         return NULL;
   112     }
   114     sstr_t* result;
   115     size_t nmax = *n;
   116     *n = 1;
   118     /* special case: exact match - no processing needed */
   119     if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) {
   120         *n = 0;
   121         return NULL;
   122     }
   123     sstr_t sv = sstrdup(s);
   125     for (size_t i = 0 ; i < s.length ; i++) {
   126         if (sv.ptr[i] == d.ptr[0]) {
   127             _Bool match = 1;
   128             for (size_t j = 1 ; j < d.length ; j++) {
   129                 if (j+i < s.length) {
   130                     match &= (sv.ptr[i+j] == d.ptr[j]);
   131                 } else {
   132                     match = 0;
   133                     break;
   134                 }
   135             }
   136             if (match) {
   137                 (*n)++;
   138                 for (size_t j = 0 ; j < d.length ; j++) {
   139                     sv.ptr[i+j] = 0;
   140                 }
   141                 i += d.length;
   142             }
   143         }
   144         if ((*n) == nmax) break;
   145     }
   146     result = (sstr_t*) malloc(sizeof(sstr_t) * (*n));
   148     char *pptr = sv.ptr;
   149     for (size_t i = 0 ; i < *n ; i++) {
   150         size_t l = strlen(pptr);
   151         char* ptr = (char*) malloc(l + 1);
   152         memcpy(ptr, pptr, l);
   153         ptr[l] = 0;
   155         result[i] = sstrn(ptr, l);
   156         pptr += l + d.length;
   157     }
   159     free(sv.ptr);
   161     return result;
   162 }
   164 int sstrcmp(sstr_t s1, sstr_t s2) {
   165     return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
   166 }
   168 sstr_t sstrdup(sstr_t s) {
   169     sstr_t newstring;
   170     newstring.ptr = (char*) malloc(s.length + 1);
   171     newstring.length = 0;
   172     if (newstring.ptr) {
   173         newstring.length = s.length;
   174         newstring.ptr[newstring.length] = 0;
   176         memcpy(newstring.ptr, s.ptr, s.length);
   177     } else {
   178         newstring.length = 0;
   179     }
   181     return newstring;
   182 }
   184 sstr_t sstrtrim(sstr_t string) {
   185     sstr_t newstr = string;
   186     if (string.length == 0) {
   187         return newstr;
   188     }
   190     size_t i;
   191     for(i=0;i<string.length;i++) {
   192         char c = string.ptr[i];
   193         if(c > 32) {
   194             break;
   195         }
   196     }
   197     newstr.ptr = &string.ptr[i];
   198     newstr.length = string.length - i;
   200     if(newstr.length == 0) {
   201         return newstr;
   202     }
   204     i = newstr.length - 1;
   205     for(;;) {
   206         char c = newstr.ptr[i];
   207         if(c > 32) {
   208             break;
   209         }
   210         if(i > 0) {
   211             i--;
   212         } else {
   213             break;
   214         }
   215     }
   216     newstr.length = i + 1;
   218     return newstr;
   219 }

mercurial