ucx/string.c

Tue, 10 Jun 2014 15:43:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 10 Jun 2014 15:43:13 +0200
changeset 173
31a8682fffb7
parent 152
3238f65db163
child 177
11ad03783baf
permissions
-rw-r--r--

fixed some sstring issues + added allocator macros

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@109 34 #include "allocator.h"
olaf@20 35
universe@116 36 sstr_t sstr(char *cstring) {
olaf@20 37 sstr_t string;
universe@116 38 string.ptr = cstring;
universe@116 39 string.length = strlen(cstring);
olaf@20 40 return string;
olaf@20 41 }
olaf@20 42
universe@116 43 sstr_t sstrn(char *cstring, size_t length) {
olaf@20 44 sstr_t string;
universe@116 45 string.ptr = cstring;
universe@116 46 string.length = length;
olaf@20 47 return string;
olaf@20 48 }
olaf@20 49
olaf@68 50 size_t sstrnlen(size_t n, sstr_t s, ...) {
olaf@20 51 va_list ap;
olaf@20 52 size_t size = s.length;
olaf@20 53 va_start(ap, s);
olaf@20 54
universe@116 55 for (size_t i = 1 ; i < n ; i++) {
olaf@20 56 sstr_t str = va_arg(ap, sstr_t);
olaf@20 57 size += str.length;
olaf@20 58 }
universe@24 59 va_end(ap);
olaf@20 60
olaf@20 61 return size;
olaf@20 62 }
olaf@20 63
universe@123 64 sstr_t sstrncat(sstr_t s, size_t n, sstr_t c1, ...) {
olaf@20 65 va_list ap;
olaf@20 66 va_start(ap, c1);
olaf@20 67 s.ptr[0] = 0;
olaf@47 68
olaf@47 69 size_t len = s.length;
olaf@47 70 size_t cplen = c1.length > len ? len : c1.length;
olaf@47 71 char *ptr = s.ptr;
olaf@47 72
olaf@47 73 memcpy(ptr, c1.ptr, cplen);
olaf@47 74 len -= cplen;
olaf@47 75 ptr += cplen;
universe@119 76 for (size_t i = 1 ; i < n ; i++) {
olaf@20 77 sstr_t str = va_arg (ap, sstr_t);
olaf@47 78 cplen = str.length > len ? len : str.length;
olaf@47 79 if(cplen <= 0) {
olaf@47 80 va_end(ap);
olaf@47 81 return s;
olaf@47 82 }
olaf@47 83 memcpy(ptr, str.ptr, cplen);
olaf@47 84 len -= cplen;
olaf@47 85 ptr += cplen;
olaf@20 86 }
universe@24 87 va_end(ap);
universe@100 88 s.length = ptr - s.ptr;
olaf@20 89
olaf@20 90 return s;
olaf@20 91 }
olaf@20 92
olaf@68 93 sstr_t sstrsubs(sstr_t s, size_t start) {
olaf@20 94 return sstrsubsl (s, start, s.length-start);
olaf@20 95 }
olaf@20 96
olaf@68 97 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
olaf@20 98 sstr_t new_sstr;
olaf@104 99 if (start >= s.length) {
universe@173 100 new_sstr.ptr = NULL;
universe@173 101 new_sstr.length = 0;
universe@173 102 } else {
universe@173 103 if (length > s.length-start) {
universe@173 104 length = s.length-start;
universe@173 105 }
universe@173 106 new_sstr.ptr = &s.ptr[start];
universe@173 107 new_sstr.length = length;
olaf@20 108 }
olaf@20 109 return new_sstr;
olaf@20 110 }
olaf@20 111
olaf@108 112 sstr_t sstrchr(sstr_t s, int c) {
olaf@108 113 for(size_t i=0;i<s.length;i++) {
olaf@108 114 if(s.ptr[i] == c) {
olaf@108 115 return sstrsubs(s, i);
olaf@108 116 }
olaf@108 117 }
olaf@108 118 sstr_t n;
olaf@108 119 n.ptr = NULL;
olaf@108 120 n.length = 0;
olaf@108 121 return n;
olaf@108 122 }
olaf@108 123
universe@148 124 sstr_t sstrrchr(sstr_t s, int c) {
universe@148 125 if (s.length > 0) {
universe@152 126 for(size_t i=s.length;i>0;i--) {
universe@152 127 if(s.ptr[i-1] == c) {
universe@152 128 return sstrsubs(s, i-1);
universe@148 129 }
universe@148 130 }
universe@148 131 }
universe@148 132 sstr_t n;
universe@148 133 n.ptr = NULL;
universe@148 134 n.length = 0;
universe@148 135 return n;
universe@148 136 }
universe@148 137
universe@173 138 sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) {
universe@125 139 return sstrsplit_a(ucx_default_allocator(), s, d, n);
universe@119 140 }
universe@119 141
universe@173 142 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, ssize_t *n) {
universe@119 143 if (s.length == 0 || d.length == 0) {
universe@119 144 *n = -1;
universe@39 145 return NULL;
universe@39 146 }
universe@39 147
universe@39 148 sstr_t* result;
universe@173 149 ssize_t nmax = *n;
universe@39 150 *n = 1;
universe@39 151
universe@39 152 /* special case: exact match - no processing needed */
universe@119 153 if (sstrcmp(s, d) == 0) {
universe@71 154 *n = 0;
universe@71 155 return NULL;
universe@39 156 }
universe@39 157 sstr_t sv = sstrdup(s);
universe@119 158 if (sv.length == 0) {
universe@119 159 *n = -2;
universe@119 160 return NULL;
universe@119 161 }
universe@39 162
universe@95 163 for (size_t i = 0 ; i < s.length ; i++) {
universe@39 164 if (sv.ptr[i] == d.ptr[0]) {
universe@39 165 _Bool match = 1;
universe@95 166 for (size_t j = 1 ; j < d.length ; j++) {
universe@39 167 if (j+i < s.length) {
universe@39 168 match &= (sv.ptr[i+j] == d.ptr[j]);
universe@39 169 } else {
universe@39 170 match = 0;
universe@39 171 break;
universe@39 172 }
universe@39 173 }
universe@39 174 if (match) {
universe@39 175 (*n)++;
universe@95 176 for (size_t j = 0 ; j < d.length ; j++) {
universe@39 177 sv.ptr[i+j] = 0;
universe@39 178 }
universe@39 179 i += d.length;
universe@39 180 }
universe@39 181 }
universe@39 182 if ((*n) == nmax) break;
universe@39 183 }
universe@173 184 result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)*(*n));
universe@39 185
universe@119 186 if (result) {
universe@119 187 char *pptr = sv.ptr;
universe@173 188 for (ssize_t i = 0 ; i < *n ; i++) {
universe@119 189 size_t l = strlen(pptr);
universe@173 190 char* ptr = (char*) almalloc(allocator, l + 1);
universe@173 191 if (ptr) {
universe@173 192 memcpy(ptr, pptr, l);
universe@173 193 ptr[l] = 0;
universe@39 194
universe@173 195 result[i] = sstrn(ptr, l);
universe@173 196 pptr += l + d.length;
universe@173 197 } else {
universe@173 198 for (ssize_t j = i-1 ; j >= 0 ; j--) {
universe@173 199 alfree(allocator, result[j].ptr);
universe@173 200 }
universe@173 201 alfree(allocator, result);
universe@173 202 *n = -2;
universe@173 203 break;
universe@173 204 }
universe@119 205 }
universe@119 206 } else {
universe@119 207 *n = -2;
universe@39 208 }
universe@119 209
universe@39 210 free(sv.ptr);
universe@39 211
universe@39 212 return result;
universe@39 213 }
universe@39 214
olaf@68 215 int sstrcmp(sstr_t s1, sstr_t s2) {
universe@116 216 if (s1.length == s2.length) {
universe@116 217 return memcmp(s1.ptr, s2.ptr, s1.length);
universe@116 218 } else if (s1.length > s2.length) {
universe@116 219 return 1;
universe@116 220 } else {
universe@116 221 return -1;
universe@116 222 }
olaf@20 223 }
olaf@20 224
universe@149 225 int sstrcasecmp(sstr_t s1, sstr_t s2) {
universe@149 226 if (s1.length == s2.length) {
universe@149 227 #ifdef _WIN32
universe@149 228 return _strnicmp(s1.ptr, s2.ptr, s1.length);
universe@149 229 #else
universe@149 230 return strncasecmp(s1.ptr, s2.ptr, s1.length);
universe@149 231 #endif
universe@149 232 } else if (s1.length > s2.length) {
universe@149 233 return 1;
universe@149 234 } else {
universe@149 235 return -1;
universe@149 236 }
universe@149 237 }
universe@149 238
olaf@68 239 sstr_t sstrdup(sstr_t s) {
universe@125 240 return sstrdup_a(ucx_default_allocator(), s);
olaf@109 241 }
olaf@20 242
universe@125 243 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
olaf@109 244 sstr_t newstring;
universe@173 245 newstring.ptr = (char*)almalloc(allocator, s.length + 1);
olaf@109 246 if (newstring.ptr) {
olaf@109 247 newstring.length = s.length;
olaf@109 248 newstring.ptr[newstring.length] = 0;
olaf@109 249
olaf@109 250 memcpy(newstring.ptr, s.ptr, s.length);
olaf@109 251 } else {
olaf@109 252 newstring.length = 0;
olaf@109 253 }
olaf@109 254
olaf@20 255 return newstring;
olaf@20 256 }
olaf@96 257
olaf@96 258 sstr_t sstrtrim(sstr_t string) {
olaf@96 259 sstr_t newstr = string;
universe@98 260 if (string.length == 0) {
universe@98 261 return newstr;
universe@98 262 }
universe@98 263
universe@97 264 size_t i;
olaf@96 265 for(i=0;i<string.length;i++) {
olaf@96 266 char c = string.ptr[i];
olaf@96 267 if(c > 32) {
olaf@96 268 break;
olaf@96 269 }
olaf@96 270 }
olaf@96 271 newstr.ptr = &string.ptr[i];
olaf@96 272 newstr.length = string.length - i;
olaf@96 273
olaf@104 274 if(newstr.length == 0) {
olaf@104 275 return newstr;
olaf@104 276 }
olaf@104 277
olaf@104 278 i = newstr.length - 1;
olaf@104 279 for(;;) {
olaf@96 280 char c = newstr.ptr[i];
olaf@96 281 if(c > 32) {
olaf@96 282 break;
olaf@96 283 }
olaf@104 284 if(i > 0) {
olaf@104 285 i--;
olaf@104 286 } else {
olaf@104 287 break;
olaf@104 288 }
olaf@96 289 }
olaf@96 290 newstr.length = i + 1;
olaf@96 291
olaf@96 292 return newstr;
olaf@96 293 }
universe@146 294
universe@146 295 int sstrprefix(sstr_t string, sstr_t prefix) {
universe@146 296 if (string.length == 0) {
universe@146 297 return prefix.length == 0;
universe@146 298 }
universe@146 299 if (prefix.length == 0) {
universe@146 300 return 1;
universe@146 301 }
universe@146 302
universe@146 303 if (prefix.length > string.length) {
universe@146 304 return 0;
universe@146 305 } else {
universe@146 306 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
universe@146 307 }
universe@146 308 }
universe@146 309
universe@146 310 int sstrsuffix(sstr_t string, sstr_t suffix) {
universe@146 311 if (string.length == 0) {
universe@146 312 return suffix.length == 0;
universe@146 313 }
universe@146 314 if (suffix.length == 0) {
universe@146 315 return 1;
universe@146 316 }
universe@146 317
universe@146 318 if (suffix.length > string.length) {
universe@146 319 return 0;
universe@146 320 } else {
universe@146 321 return memcmp(string.ptr+string.length-suffix.length,
universe@146 322 suffix.ptr, suffix.length) == 0;
universe@146 323 }
universe@146 324 }

mercurial