diff -r ae5a98f0545c -r 88625853ae74 ucx/string.c --- a/ucx/string.c Sat Dec 01 20:34:55 2012 +0100 +++ b/ucx/string.c Mon Aug 12 14:40:19 2013 +0200 @@ -1,8 +1,29 @@ /* - * File: sstring.c - * Author: olaf + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * Created on 17. Juni 2010, 13:27 + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ #include @@ -10,18 +31,19 @@ #include #include "string.h" +#include "allocator.h" -sstr_t sstr(char *s) { +sstr_t sstr(char *cstring) { sstr_t string; - string.ptr = s; - string.length = strlen(s); + string.ptr = cstring; + string.length = strlen(cstring); return string; } -sstr_t sstrn(char *s, size_t n) { +sstr_t sstrn(char *cstring, size_t length) { sstr_t string; - string.ptr = s; - string.length = n; + string.ptr = cstring; + string.length = length; return string; } @@ -30,7 +52,7 @@ size_t size = s.length; va_start(ap, s); - for (int i=0;i len ? len : str.length; if(cplen <= 0) { @@ -78,6 +85,7 @@ ptr += cplen; } va_end(ap); + s.length = ptr - s.ptr; return s; } @@ -88,7 +96,7 @@ sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { sstr_t new_sstr; - if (start < 0 || start >= s.length || length < 0) { + if (start >= s.length) { return s; } if (length > s.length-start) { @@ -99,8 +107,25 @@ return new_sstr; } +sstr_t sstrchr(sstr_t s, int c) { + for(size_t i=0;imalloc(allocator->pool, sizeof(sstr_t)*(*n)); - char *pptr = sv.ptr; - for (int i = 0 ; i < *n ; i++) { - size_t l = strlen(pptr); - char* ptr = (char*) malloc(l + 1); - memcpy(ptr, pptr, l); - ptr[l] = 0; + if (result) { + char *pptr = sv.ptr; + for (size_t i = 0 ; i < *n ; i++) { + size_t l = strlen(pptr); + char* ptr = (char*) allocator->malloc(allocator->pool, l + 1); + memcpy(ptr, pptr, l); + ptr[l] = 0; - result[i] = sstrn(ptr, l); - pptr += l + d.length; + result[i] = sstrn(ptr, l); + pptr += l + d.length; + } + } else { + *n = -2; } - + free(sv.ptr); return result; } int sstrcmp(sstr_t s1, sstr_t s2) { - return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length); + if (s1.length == s2.length) { + return memcmp(s1.ptr, s2.ptr, s1.length); + } else if (s1.length > s2.length) { + return 1; + } else { + return -1; + } } sstr_t sstrdup(sstr_t s) { + return sstrdup_a(ucx_default_allocator(), s); +} + +sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) { sstr_t newstring; - newstring.ptr = (char*) malloc(s.length + 1); - if (newstring.ptr != NULL) { + newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1); + if (newstring.ptr) { newstring.length = s.length; newstring.ptr[newstring.length] = 0; - + memcpy(newstring.ptr, s.ptr, s.length); + } else { + newstring.length = 0; } - + return newstring; } + +sstr_t sstrtrim(sstr_t string) { + sstr_t newstr = string; + if (string.length == 0) { + return newstr; + } + + size_t i; + for(i=0;i 32) { + break; + } + } + newstr.ptr = &string.ptr[i]; + newstr.length = string.length - i; + + if(newstr.length == 0) { + return newstr; + } + + i = newstr.length - 1; + for(;;) { + char c = newstr.ptr[i]; + if(c > 32) { + break; + } + if(i > 0) { + i--; + } else { + break; + } + } + newstr.length = i + 1; + + return newstr; +}