ucx/string.c

Sun, 14 Jul 2013 17:11:34 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 14 Jul 2013 17:11:34 +0200
changeset 109
75cb6590358b
parent 108
d2b1e67b2b48
child 116
234920008754
permissions
-rw-r--r--

added properties load/store functions

     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"
    34 #include "allocator.h"
    36 sstr_t sstr(char *s) {
    37     sstr_t string;
    38     string.ptr = s;
    39     string.length = strlen(s);
    40     return string;
    41 }
    43 sstr_t sstrn(char *s, size_t n) {
    44     sstr_t string;
    45     string.ptr = s;
    46     string.length = n;
    47     return string;
    48 }
    50 size_t sstrnlen(size_t n, sstr_t s, ...) {
    51     va_list ap;
    52     size_t size = s.length;
    53     va_start(ap, s);
    55     for (size_t i = 0 ; i < n-1 ; i++) {
    56         sstr_t str = va_arg(ap, sstr_t);
    57         size += str.length;
    58     }
    59     va_end(ap);
    61     return size;
    62 }
    64 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...) {
    65     va_list ap;
    66     va_start(ap, c1);
    67     s.ptr[0] = 0;
    69     size_t len = s.length;
    70     size_t cplen = c1.length > len ? len : c1.length;
    71     char   *ptr = s.ptr;
    73     memcpy(ptr, c1.ptr, cplen);
    74     len -= cplen;
    75     ptr += cplen;
    76     for (size_t i = 0 ; i < n-1 ; i++) {
    77         sstr_t str = va_arg (ap, sstr_t);
    78         cplen = str.length > len ? len : str.length;
    79         if(cplen <= 0) {
    80             va_end(ap);
    81             return s;
    82         }
    83         memcpy(ptr, str.ptr, cplen);
    84         len -= cplen;
    85         ptr += cplen;
    86     }
    87     va_end(ap);
    88     s.length = ptr - s.ptr;
    90     return s;
    91 }
    93 sstr_t sstrsubs(sstr_t s, size_t start) {
    94     return sstrsubsl (s, start, s.length-start);
    95 }
    97 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
    98     sstr_t new_sstr;
    99     if (start >= s.length) {
   100         return s;
   101     }
   102     if (length > s.length-start) {
   103         length = s.length-start;
   104     }
   105     new_sstr.ptr = &s.ptr[start];
   106     new_sstr.length = length;
   107     return new_sstr;
   108 }
   110 sstr_t sstrchr(sstr_t s, int c) {
   111     for(size_t i=0;i<s.length;i++) {
   112         if(s.ptr[i] == c) {
   113             return sstrsubs(s, i);
   114         }
   115     }
   116     sstr_t n;
   117     n.ptr = NULL;
   118     n.length = 0;
   119     return n;
   120 }
   122 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
   123     if (d.length == 0) {
   124         return NULL;
   125     }
   127     sstr_t* result;
   128     size_t nmax = *n;
   129     *n = 1;
   131     /* special case: exact match - no processing needed */
   132     if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) {
   133         *n = 0;
   134         return NULL;
   135     }
   136     sstr_t sv = sstrdup(s);
   138     for (size_t i = 0 ; i < s.length ; i++) {
   139         if (sv.ptr[i] == d.ptr[0]) {
   140             _Bool match = 1;
   141             for (size_t j = 1 ; j < d.length ; j++) {
   142                 if (j+i < s.length) {
   143                     match &= (sv.ptr[i+j] == d.ptr[j]);
   144                 } else {
   145                     match = 0;
   146                     break;
   147                 }
   148             }
   149             if (match) {
   150                 (*n)++;
   151                 for (size_t j = 0 ; j < d.length ; j++) {
   152                     sv.ptr[i+j] = 0;
   153                 }
   154                 i += d.length;
   155             }
   156         }
   157         if ((*n) == nmax) break;
   158     }
   159     result = (sstr_t*) malloc(sizeof(sstr_t) * (*n));
   161     char *pptr = sv.ptr;
   162     for (size_t i = 0 ; i < *n ; i++) {
   163         size_t l = strlen(pptr);
   164         char* ptr = (char*) malloc(l + 1);
   165         memcpy(ptr, pptr, l);
   166         ptr[l] = 0;
   168         result[i] = sstrn(ptr, l);
   169         pptr += l + d.length;
   170     }
   172     free(sv.ptr);
   174     return result;
   175 }
   177 int sstrcmp(sstr_t s1, sstr_t s2) {
   178     return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
   179 }
   181 sstr_t sstrdup(sstr_t s) {
   182     sstr_t newstring;
   183     newstring.ptr = (char*) malloc(s.length + 1);
   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 sstrdup_alloc(UcxAllocator *allocator, sstr_t s) {
   197     sstr_t newstring;
   198     newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1);
   199     if (newstring.ptr) {
   200         newstring.length = s.length;
   201         newstring.ptr[newstring.length] = 0;
   203         memcpy(newstring.ptr, s.ptr, s.length);
   204     } else {
   205         newstring.length = 0;
   206     }
   208     return newstring;
   209 }
   211 sstr_t sstrtrim(sstr_t string) {
   212     sstr_t newstr = string;
   213     if (string.length == 0) {
   214         return newstr;
   215     }
   217     size_t i;
   218     for(i=0;i<string.length;i++) {
   219         char c = string.ptr[i];
   220         if(c > 32) {
   221             break;
   222         }
   223     }
   224     newstr.ptr = &string.ptr[i];
   225     newstr.length = string.length - i;
   227     if(newstr.length == 0) {
   228         return newstr;
   229     }
   231     i = newstr.length - 1;
   232     for(;;) {
   233         char c = newstr.ptr[i];
   234         if(c > 32) {
   235             break;
   236         }
   237         if(i > 0) {
   238             i--;
   239         } else {
   240             break;
   241         }
   242     }
   243     newstr.length = i + 1;
   245     return newstr;
   246 }

mercurial