ucx/string.c

Fri, 12 Jul 2013 20:50:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Jul 2013 20:50:18 +0200
changeset 108
d2b1e67b2b48
parent 104
9d3dea320d8e
child 109
75cb6590358b
permissions
-rw-r--r--

new properties parser

     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 sstrchr(sstr_t s, int c) {
   110     for(size_t i=0;i<s.length;i++) {
   111         if(s.ptr[i] == c) {
   112             return sstrsubs(s, i);
   113         }
   114     }
   115     sstr_t n;
   116     n.ptr = NULL;
   117     n.length = 0;
   118     return n;
   119 }
   121 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
   122     if (d.length == 0) {
   123         return NULL;
   124     }
   126     sstr_t* result;
   127     size_t nmax = *n;
   128     *n = 1;
   130     /* special case: exact match - no processing needed */
   131     if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) {
   132         *n = 0;
   133         return NULL;
   134     }
   135     sstr_t sv = sstrdup(s);
   137     for (size_t i = 0 ; i < s.length ; i++) {
   138         if (sv.ptr[i] == d.ptr[0]) {
   139             _Bool match = 1;
   140             for (size_t j = 1 ; j < d.length ; j++) {
   141                 if (j+i < s.length) {
   142                     match &= (sv.ptr[i+j] == d.ptr[j]);
   143                 } else {
   144                     match = 0;
   145                     break;
   146                 }
   147             }
   148             if (match) {
   149                 (*n)++;
   150                 for (size_t j = 0 ; j < d.length ; j++) {
   151                     sv.ptr[i+j] = 0;
   152                 }
   153                 i += d.length;
   154             }
   155         }
   156         if ((*n) == nmax) break;
   157     }
   158     result = (sstr_t*) malloc(sizeof(sstr_t) * (*n));
   160     char *pptr = sv.ptr;
   161     for (size_t i = 0 ; i < *n ; i++) {
   162         size_t l = strlen(pptr);
   163         char* ptr = (char*) malloc(l + 1);
   164         memcpy(ptr, pptr, l);
   165         ptr[l] = 0;
   167         result[i] = sstrn(ptr, l);
   168         pptr += l + d.length;
   169     }
   171     free(sv.ptr);
   173     return result;
   174 }
   176 int sstrcmp(sstr_t s1, sstr_t s2) {
   177     return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
   178 }
   180 sstr_t sstrdup(sstr_t s) {
   181     sstr_t newstring;
   182     newstring.ptr = (char*) malloc(s.length + 1);
   183     newstring.length = 0;
   184     if (newstring.ptr) {
   185         newstring.length = s.length;
   186         newstring.ptr[newstring.length] = 0;
   188         memcpy(newstring.ptr, s.ptr, s.length);
   189     } else {
   190         newstring.length = 0;
   191     }
   193     return newstring;
   194 }
   196 sstr_t sstrtrim(sstr_t string) {
   197     sstr_t newstr = string;
   198     if (string.length == 0) {
   199         return newstr;
   200     }
   202     size_t i;
   203     for(i=0;i<string.length;i++) {
   204         char c = string.ptr[i];
   205         if(c > 32) {
   206             break;
   207         }
   208     }
   209     newstr.ptr = &string.ptr[i];
   210     newstr.length = string.length - i;
   212     if(newstr.length == 0) {
   213         return newstr;
   214     }
   216     i = newstr.length - 1;
   217     for(;;) {
   218         char c = newstr.ptr[i];
   219         if(c > 32) {
   220             break;
   221         }
   222         if(i > 0) {
   223             i--;
   224         } else {
   225             break;
   226         }
   227     }
   228     newstr.length = i + 1;
   230     return newstr;
   231 }

mercurial