olaf@20: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@20: * universe@103: * Copyright 2013 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 olaf@20: olaf@20: #include "string.h" olaf@109: #include "allocator.h" olaf@20: olaf@68: sstr_t sstr(char *s) { olaf@20: sstr_t string; olaf@20: string.ptr = s; olaf@20: string.length = strlen(s); olaf@20: return string; olaf@20: } olaf@20: olaf@68: sstr_t sstrn(char *s, size_t n) { olaf@20: sstr_t string; olaf@20: string.ptr = s; olaf@20: string.length = n; 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@95: for (size_t i = 0 ; i < n-1 ; 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@68: sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...) { olaf@20: va_list ap; olaf@20: va_start(ap, c1); olaf@20: s.ptr[0] = 0; olaf@47: olaf@47: size_t len = s.length; olaf@47: size_t cplen = c1.length > len ? len : c1.length; olaf@47: char *ptr = s.ptr; olaf@47: olaf@47: memcpy(ptr, c1.ptr, cplen); olaf@47: len -= cplen; olaf@47: ptr += cplen; universe@95: for (size_t i = 0 ; i < n-1 ; i++) { olaf@20: sstr_t str = va_arg (ap, sstr_t); olaf@47: cplen = str.length > len ? len : str.length; olaf@47: if(cplen <= 0) { olaf@47: va_end(ap); olaf@47: return s; olaf@47: } olaf@47: memcpy(ptr, str.ptr, cplen); olaf@47: len -= cplen; olaf@47: ptr += cplen; olaf@20: } universe@24: va_end(ap); universe@100: s.length = ptr - s.ptr; olaf@20: olaf@20: return s; olaf@20: } olaf@20: olaf@68: sstr_t sstrsubs(sstr_t s, size_t start) { olaf@20: return sstrsubsl (s, start, s.length-start); olaf@20: } olaf@20: olaf@68: sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { olaf@20: sstr_t new_sstr; olaf@104: if (start >= s.length) { olaf@20: return s; olaf@20: } olaf@20: if (length > s.length-start) { olaf@20: length = s.length-start; olaf@20: } olaf@20: new_sstr.ptr = &s.ptr[start]; olaf@20: new_sstr.length = length; 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;is2.length ? s2.length: s1.length); olaf@20: } olaf@20: olaf@68: sstr_t sstrdup(sstr_t s) { olaf@20: sstr_t newstring; universe@24: newstring.ptr = (char*) malloc(s.length + 1); universe@87: if (newstring.ptr) { universe@24: newstring.length = s.length; universe@24: newstring.ptr[newstring.length] = 0; olaf@109: universe@24: memcpy(newstring.ptr, s.ptr, s.length); universe@87: } else { universe@87: newstring.length = 0; universe@24: } olaf@109: olaf@109: return newstring; olaf@109: } olaf@20: olaf@109: sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s) { olaf@109: sstr_t newstring; olaf@109: newstring.ptr = (char*)allocator->malloc(allocator->pool, 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@98: if (string.length == 0) { universe@98: return newstr; universe@98: } universe@98: universe@97: size_t i; olaf@96: for(i=0;i 32) { olaf@96: break; olaf@96: } olaf@96: } olaf@96: newstr.ptr = &string.ptr[i]; olaf@96: newstr.length = string.length - i; olaf@96: olaf@104: if(newstr.length == 0) { olaf@104: return newstr; olaf@104: } olaf@104: olaf@104: i = newstr.length - 1; olaf@104: for(;;) { olaf@96: char c = newstr.ptr[i]; olaf@96: if(c > 32) { olaf@96: break; olaf@96: } olaf@104: if(i > 0) { olaf@104: i--; olaf@104: } else { olaf@104: break; olaf@104: } olaf@96: } olaf@96: newstr.length = i + 1; olaf@96: olaf@96: return newstr; olaf@96: }