ucx/string.c

Mon, 19 Aug 2013 11:54:54 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 19 Aug 2013 11:54:54 +0200
changeset 149
3bf87676d42d
parent 148
c27c2425c0b1
child 152
3238f65db163
permissions
-rw-r--r--

added sstrcasecmp

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) {
olaf@20 100 return s;
olaf@20 101 }
olaf@20 102 if (length > s.length-start) {
olaf@20 103 length = s.length-start;
olaf@20 104 }
olaf@20 105 new_sstr.ptr = &s.ptr[start];
olaf@20 106 new_sstr.length = length;
olaf@20 107 return new_sstr;
olaf@20 108 }
olaf@20 109
olaf@108 110 sstr_t sstrchr(sstr_t s, int c) {
olaf@108 111 for(size_t i=0;i<s.length;i++) {
olaf@108 112 if(s.ptr[i] == c) {
olaf@108 113 return sstrsubs(s, i);
olaf@108 114 }
olaf@108 115 }
olaf@108 116 sstr_t n;
olaf@108 117 n.ptr = NULL;
olaf@108 118 n.length = 0;
olaf@108 119 return n;
olaf@108 120 }
olaf@108 121
universe@148 122 sstr_t sstrrchr(sstr_t s, int c) {
universe@148 123 if (s.length > 0) {
universe@148 124 for(size_t i=s.length-1;i>=0;i--) {
universe@148 125 if(s.ptr[i] == c) {
universe@148 126 return sstrsubs(s, i);
universe@148 127 }
universe@148 128 }
universe@148 129 }
universe@148 130 sstr_t n;
universe@148 131 n.ptr = NULL;
universe@148 132 n.length = 0;
universe@148 133 return n;
universe@148 134 }
universe@148 135
olaf@68 136 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
universe@125 137 return sstrsplit_a(ucx_default_allocator(), s, d, n);
universe@119 138 }
universe@119 139
universe@125 140 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) {
universe@119 141 if (s.length == 0 || d.length == 0) {
universe@119 142 *n = -1;
universe@39 143 return NULL;
universe@39 144 }
universe@39 145
universe@39 146 sstr_t* result;
universe@39 147 size_t nmax = *n;
universe@39 148 *n = 1;
universe@39 149
universe@39 150 /* special case: exact match - no processing needed */
universe@119 151 if (sstrcmp(s, d) == 0) {
universe@71 152 *n = 0;
universe@71 153 return NULL;
universe@39 154 }
universe@39 155 sstr_t sv = sstrdup(s);
universe@119 156 if (sv.length == 0) {
universe@119 157 *n = -2;
universe@119 158 return NULL;
universe@119 159 }
universe@39 160
universe@95 161 for (size_t i = 0 ; i < s.length ; i++) {
universe@39 162 if (sv.ptr[i] == d.ptr[0]) {
universe@39 163 _Bool match = 1;
universe@95 164 for (size_t j = 1 ; j < d.length ; j++) {
universe@39 165 if (j+i < s.length) {
universe@39 166 match &= (sv.ptr[i+j] == d.ptr[j]);
universe@39 167 } else {
universe@39 168 match = 0;
universe@39 169 break;
universe@39 170 }
universe@39 171 }
universe@39 172 if (match) {
universe@39 173 (*n)++;
universe@95 174 for (size_t j = 0 ; j < d.length ; j++) {
universe@39 175 sv.ptr[i+j] = 0;
universe@39 176 }
universe@39 177 i += d.length;
universe@39 178 }
universe@39 179 }
universe@39 180 if ((*n) == nmax) break;
universe@39 181 }
olaf@120 182 result = (sstr_t*) allocator->malloc(allocator->pool, sizeof(sstr_t)*(*n));
universe@39 183
universe@119 184 if (result) {
universe@119 185 char *pptr = sv.ptr;
universe@119 186 for (size_t i = 0 ; i < *n ; i++) {
universe@119 187 size_t l = strlen(pptr);
olaf@120 188 char* ptr = (char*) allocator->malloc(allocator->pool, l + 1);
universe@119 189 memcpy(ptr, pptr, l);
universe@119 190 ptr[l] = 0;
universe@39 191
universe@119 192 result[i] = sstrn(ptr, l);
universe@119 193 pptr += l + d.length;
universe@119 194 }
universe@119 195 } else {
universe@119 196 *n = -2;
universe@39 197 }
universe@119 198
universe@39 199 free(sv.ptr);
universe@39 200
universe@39 201 return result;
universe@39 202 }
universe@39 203
olaf@68 204 int sstrcmp(sstr_t s1, sstr_t s2) {
universe@116 205 if (s1.length == s2.length) {
universe@116 206 return memcmp(s1.ptr, s2.ptr, s1.length);
universe@116 207 } else if (s1.length > s2.length) {
universe@116 208 return 1;
universe@116 209 } else {
universe@116 210 return -1;
universe@116 211 }
olaf@20 212 }
olaf@20 213
universe@149 214 int sstrcasecmp(sstr_t s1, sstr_t s2) {
universe@149 215 if (s1.length == s2.length) {
universe@149 216 #ifdef _WIN32
universe@149 217 return _strnicmp(s1.ptr, s2.ptr, s1.length);
universe@149 218 #else
universe@149 219 return strncasecmp(s1.ptr, s2.ptr, s1.length);
universe@149 220 #endif
universe@149 221 } else if (s1.length > s2.length) {
universe@149 222 return 1;
universe@149 223 } else {
universe@149 224 return -1;
universe@149 225 }
universe@149 226 }
universe@149 227
olaf@68 228 sstr_t sstrdup(sstr_t s) {
universe@125 229 return sstrdup_a(ucx_default_allocator(), s);
olaf@109 230 }
olaf@20 231
universe@125 232 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
olaf@109 233 sstr_t newstring;
olaf@109 234 newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1);
olaf@109 235 if (newstring.ptr) {
olaf@109 236 newstring.length = s.length;
olaf@109 237 newstring.ptr[newstring.length] = 0;
olaf@109 238
olaf@109 239 memcpy(newstring.ptr, s.ptr, s.length);
olaf@109 240 } else {
olaf@109 241 newstring.length = 0;
olaf@109 242 }
olaf@109 243
olaf@20 244 return newstring;
olaf@20 245 }
olaf@96 246
olaf@96 247 sstr_t sstrtrim(sstr_t string) {
olaf@96 248 sstr_t newstr = string;
universe@98 249 if (string.length == 0) {
universe@98 250 return newstr;
universe@98 251 }
universe@98 252
universe@97 253 size_t i;
olaf@96 254 for(i=0;i<string.length;i++) {
olaf@96 255 char c = string.ptr[i];
olaf@96 256 if(c > 32) {
olaf@96 257 break;
olaf@96 258 }
olaf@96 259 }
olaf@96 260 newstr.ptr = &string.ptr[i];
olaf@96 261 newstr.length = string.length - i;
olaf@96 262
olaf@104 263 if(newstr.length == 0) {
olaf@104 264 return newstr;
olaf@104 265 }
olaf@104 266
olaf@104 267 i = newstr.length - 1;
olaf@104 268 for(;;) {
olaf@96 269 char c = newstr.ptr[i];
olaf@96 270 if(c > 32) {
olaf@96 271 break;
olaf@96 272 }
olaf@104 273 if(i > 0) {
olaf@104 274 i--;
olaf@104 275 } else {
olaf@104 276 break;
olaf@104 277 }
olaf@96 278 }
olaf@96 279 newstr.length = i + 1;
olaf@96 280
olaf@96 281 return newstr;
olaf@96 282 }
universe@146 283
universe@146 284 int sstrprefix(sstr_t string, sstr_t prefix) {
universe@146 285 if (string.length == 0) {
universe@146 286 return prefix.length == 0;
universe@146 287 }
universe@146 288 if (prefix.length == 0) {
universe@146 289 return 1;
universe@146 290 }
universe@146 291
universe@146 292 if (prefix.length > string.length) {
universe@146 293 return 0;
universe@146 294 } else {
universe@146 295 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
universe@146 296 }
universe@146 297 }
universe@146 298
universe@146 299 int sstrsuffix(sstr_t string, sstr_t suffix) {
universe@146 300 if (string.length == 0) {
universe@146 301 return suffix.length == 0;
universe@146 302 }
universe@146 303 if (suffix.length == 0) {
universe@146 304 return 1;
universe@146 305 }
universe@146 306
universe@146 307 if (suffix.length > string.length) {
universe@146 308 return 0;
universe@146 309 } else {
universe@146 310 return memcmp(string.ptr+string.length-suffix.length,
universe@146 311 suffix.ptr, suffix.length) == 0;
universe@146 312 }
universe@146 313 }

mercurial