olaf@20: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@20: * universe@225: * Copyright 2016 Olaf Wintermann. All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@20: */ olaf@20: olaf@20: #include universe@69: #include olaf@20: #include universe@189: #include olaf@20: olaf@20: #include "string.h" olaf@109: #include "allocator.h" olaf@20: universe@116: sstr_t sstr(char *cstring) { olaf@20: sstr_t string; universe@116: string.ptr = cstring; universe@116: string.length = strlen(cstring); olaf@20: return string; olaf@20: } olaf@20: universe@116: sstr_t sstrn(char *cstring, size_t length) { olaf@20: sstr_t string; universe@116: string.ptr = cstring; universe@116: string.length = length; olaf@20: return string; olaf@20: } olaf@20: olaf@68: size_t sstrnlen(size_t n, sstr_t s, ...) { olaf@20: va_list ap; olaf@20: size_t size = s.length; olaf@20: va_start(ap, s); olaf@20: universe@116: for (size_t i = 1 ; i < n ; i++) { olaf@20: sstr_t str = va_arg(ap, sstr_t); olaf@20: size += str.length; olaf@20: } universe@24: va_end(ap); olaf@20: olaf@20: return size; olaf@20: } olaf@20: olaf@180: static sstr_t sstrvcat_a( olaf@180: UcxAllocator *a, olaf@180: size_t count, olaf@180: sstr_t s1, olaf@180: sstr_t s2, olaf@180: va_list ap) { olaf@180: sstr_t str; olaf@180: str.ptr = NULL; olaf@180: str.length = 0; olaf@180: if(count < 2) { olaf@180: return str; olaf@180: } olaf@180: universe@185: sstr_t *strings = (sstr_t*) calloc(count, sizeof(sstr_t)); olaf@180: if(!strings) { olaf@180: return str; olaf@180: } olaf@180: olaf@180: // get all args and overall length olaf@180: strings[0] = s1; olaf@180: strings[1] = s2; olaf@180: size_t strlen = s1.length + s2.length; olaf@180: for (size_t i=2;i= s.length) { universe@173: new_sstr.ptr = NULL; universe@173: new_sstr.length = 0; universe@173: } else { universe@173: if (length > s.length-start) { universe@173: length = s.length-start; universe@173: } universe@173: new_sstr.ptr = &s.ptr[start]; universe@173: new_sstr.length = length; olaf@20: } olaf@20: return new_sstr; olaf@20: } olaf@20: olaf@108: sstr_t sstrchr(sstr_t s, int c) { olaf@108: for(size_t i=0;i 0) { universe@152: for(size_t i=s.length;i>0;i--) { universe@152: if(s.ptr[i-1] == c) { universe@152: return sstrsubs(s, i-1); universe@148: } universe@148: } universe@148: } universe@148: sstr_t n; universe@148: n.ptr = NULL; universe@148: n.length = 0; universe@148: return n; universe@148: } universe@148: universe@214: sstr_t sstrstr(sstr_t string, sstr_t match) { universe@214: if (match.length == 0) { universe@214: return string; universe@214: } universe@214: universe@214: for (size_t i = 0 ; i < string.length ; i++) { universe@214: sstr_t substr = sstrsubs(string, i); universe@214: if (sstrprefix(substr, match)) { universe@214: return substr; universe@214: } universe@214: } universe@214: universe@214: sstr_t emptystr; universe@214: emptystr.length = 0; universe@214: emptystr.ptr = NULL; universe@214: return emptystr; universe@214: } universe@214: universe@173: sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) { universe@125: return sstrsplit_a(ucx_default_allocator(), s, d, n); universe@119: } universe@119: universe@173: sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, ssize_t *n) { universe@119: if (s.length == 0 || d.length == 0) { universe@119: *n = -1; universe@39: return NULL; universe@39: } universe@231: universe@231: /* special cases: delimiter is at least as large as the string */ universe@231: if (d.length >= s.length) { universe@231: /* exact match */ universe@231: if (sstrcmp(s, d) == 0) { universe@231: *n = 0; universe@231: return NULL; universe@231: } else /* no match possible */ { universe@231: *n = 1; universe@231: sstr_t *result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)); universe@231: result->ptr = (char*) almalloc(allocator, 1+s.length); universe@231: memcpy(result->ptr, s.ptr, s.length); universe@231: result->ptr[s.length] = '\0'; universe@231: result->length = s.length; universe@231: return result; universe@231: } universe@231: } universe@231: universe@39: sstr_t* result; universe@173: ssize_t nmax = *n; universe@39: *n = 1; universe@231: universe@39: sstr_t sv = sstrdup(s); universe@119: if (sv.length == 0) { universe@119: *n = -2; universe@119: return NULL; universe@119: } universe@39: universe@95: for (size_t i = 0 ; i < s.length ; i++) { universe@214: sstr_t substr = sstrsubs(sv, i); universe@214: if (sstrprefix(substr, d)) { universe@214: (*n)++; universe@214: for (size_t j = 0 ; j < d.length ; j++) { universe@214: sv.ptr[i+j] = 0; universe@39: } universe@214: i += d.length - 1; // -1, because the loop will do a i++ universe@39: } universe@39: if ((*n) == nmax) break; universe@39: } universe@173: result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)*(*n)); universe@39: universe@119: if (result) { universe@119: char *pptr = sv.ptr; universe@173: for (ssize_t i = 0 ; i < *n ; i++) { universe@119: size_t l = strlen(pptr); universe@173: char* ptr = (char*) almalloc(allocator, l + 1); universe@173: if (ptr) { universe@173: memcpy(ptr, pptr, l); universe@231: ptr[l] = '\0'; universe@39: universe@173: result[i] = sstrn(ptr, l); universe@173: pptr += l + d.length; universe@173: } else { universe@173: for (ssize_t j = i-1 ; j >= 0 ; j--) { universe@173: alfree(allocator, result[j].ptr); universe@173: } universe@173: alfree(allocator, result); universe@173: *n = -2; universe@173: break; universe@173: } universe@119: } universe@119: } else { universe@119: *n = -2; universe@39: } universe@119: universe@39: free(sv.ptr); universe@39: universe@39: return result; universe@39: } universe@39: olaf@68: int sstrcmp(sstr_t s1, sstr_t s2) { universe@116: if (s1.length == s2.length) { universe@116: return memcmp(s1.ptr, s2.ptr, s1.length); universe@116: } else if (s1.length > s2.length) { universe@116: return 1; universe@116: } else { universe@116: return -1; universe@116: } olaf@20: } olaf@20: universe@149: int sstrcasecmp(sstr_t s1, sstr_t s2) { universe@149: if (s1.length == s2.length) { universe@149: #ifdef _WIN32 universe@149: return _strnicmp(s1.ptr, s2.ptr, s1.length); universe@149: #else universe@149: return strncasecmp(s1.ptr, s2.ptr, s1.length); universe@149: #endif universe@149: } else if (s1.length > s2.length) { universe@149: return 1; universe@149: } else { universe@149: return -1; universe@149: } universe@149: } universe@149: olaf@68: sstr_t sstrdup(sstr_t s) { universe@125: return sstrdup_a(ucx_default_allocator(), s); olaf@109: } olaf@20: universe@125: sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) { olaf@109: sstr_t newstring; universe@173: newstring.ptr = (char*)almalloc(allocator, s.length + 1); olaf@109: if (newstring.ptr) { olaf@109: newstring.length = s.length; olaf@109: newstring.ptr[newstring.length] = 0; olaf@109: olaf@109: memcpy(newstring.ptr, s.ptr, s.length); olaf@109: } else { olaf@109: newstring.length = 0; olaf@109: } olaf@109: olaf@20: return newstring; olaf@20: } olaf@96: olaf@96: sstr_t sstrtrim(sstr_t string) { olaf@96: sstr_t newstr = string; universe@189: universe@189: while (newstr.length > 0 && isspace(*newstr.ptr)) { universe@189: newstr.ptr++; universe@189: newstr.length--; universe@98: } universe@189: while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) { universe@189: newstr.length--; olaf@96: } olaf@96: olaf@96: return newstr; olaf@96: } universe@146: universe@146: int sstrprefix(sstr_t string, sstr_t prefix) { universe@146: if (string.length == 0) { universe@146: return prefix.length == 0; universe@146: } universe@146: if (prefix.length == 0) { universe@146: return 1; universe@146: } universe@146: universe@146: if (prefix.length > string.length) { universe@146: return 0; universe@146: } else { universe@146: return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; universe@146: } universe@146: } universe@146: universe@146: int sstrsuffix(sstr_t string, sstr_t suffix) { universe@146: if (string.length == 0) { universe@146: return suffix.length == 0; universe@146: } universe@146: if (suffix.length == 0) { universe@146: return 1; universe@146: } universe@146: universe@146: if (suffix.length > string.length) { universe@146: return 0; universe@146: } else { universe@146: return memcmp(string.ptr+string.length-suffix.length, universe@146: suffix.ptr, suffix.length) == 0; universe@146: } universe@146: } universe@210: universe@210: sstr_t sstrlower(sstr_t string) { universe@210: sstr_t ret = sstrdup(string); universe@210: for (size_t i = 0; i < ret.length ; i++) { universe@210: ret.ptr[i] = tolower(ret.ptr[i]); universe@210: } universe@210: return ret; universe@210: } universe@210: universe@210: sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) { universe@210: sstr_t ret = sstrdup_a(allocator, string); universe@210: for (size_t i = 0; i < ret.length ; i++) { universe@210: ret.ptr[i] = tolower(ret.ptr[i]); universe@210: } universe@210: return ret; universe@210: } universe@210: universe@210: sstr_t sstrupper(sstr_t string) { universe@210: sstr_t ret = sstrdup(string); universe@210: for (size_t i = 0; i < ret.length ; i++) { universe@210: ret.ptr[i] = toupper(ret.ptr[i]); universe@210: } universe@210: return ret; universe@210: } universe@210: universe@210: sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) { universe@210: sstr_t ret = sstrdup_a(allocator, string); universe@210: for (size_t i = 0; i < ret.length ; i++) { universe@210: ret.ptr[i] = toupper(ret.ptr[i]); universe@210: } universe@210: return ret; universe@210: }