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