src/server/ucx/string.c

changeset 95
74a81d9e19d0
parent 94
6b15a094d996
child 96
0185b13bf41f
--- a/src/server/ucx/string.c	Mon Oct 14 13:36:28 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,283 +0,0 @@
-/*
- * 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.
- *
- *   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 <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-
-#include "string.h"
-#include "allocator.h"
-
-sstr_t sstr(char *s) {
-    sstr_t string;
-    string.ptr = s;
-    string.length = strlen(s);
-    return string;
-}
-
-sstr_t sstrn(char *s, size_t n) {
-    sstr_t string;
-    string.ptr = s;
-    string.length = n;
-    return string;
-}
-
-size_t sstrnlen(size_t n, sstr_t s, ...) {
-    va_list ap;
-    size_t size = s.length;
-    va_start(ap, s);
-
-    for (size_t i = 0 ; i < n-1 ; i++) {
-        sstr_t str = va_arg(ap, sstr_t);
-        size += str.length;
-    }
-    va_end(ap);
-
-    return size;
-}
-
-sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...) {
-    va_list ap;
-    va_start(ap, c1);
-    s.ptr[0] = 0;
-    
-    size_t len = s.length;
-    size_t cplen = c1.length > len ? len : c1.length;
-    char   *ptr = s.ptr;
-    
-    memcpy(ptr, c1.ptr, cplen);
-    len -= cplen;
-    ptr += cplen;
-    for (size_t i = 0 ; i < n-1 ; i++) {
-        sstr_t str = va_arg (ap, sstr_t);
-        cplen = str.length > len ? len : str.length;
-        if(cplen <= 0) {
-            va_end(ap);
-            return s;
-        }
-        memcpy(ptr, str.ptr, cplen);
-        len -= cplen;
-        ptr += cplen;
-    }
-    va_end(ap);
-    s.length = ptr - s.ptr;
-
-    return s;
-}
-
-sstr_t sstrsubs(sstr_t s, size_t start) {
-    return sstrsubsl (s, start, s.length-start);
-}
-
-sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
-    sstr_t new_sstr;
-    if (start >= s.length) {
-        return s;
-    }
-    if (length > s.length-start) {
-        length = s.length-start;
-    }
-    new_sstr.ptr = &s.ptr[start];
-    new_sstr.length = length;
-    return new_sstr;
-}
-
-sstr_t sstrchr(sstr_t s, int c) {
-    for(size_t i=0;i<s.length;i++) {
-        if(s.ptr[i] == c) {
-            return sstrsubs(s, i);
-        }
-    }
-    sstr_t n;
-    n.ptr = NULL;
-    n.length = 0;
-    return n;
-}
-
-sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
-    if (d.length == 0) {
-        return NULL;
-    }
-
-    sstr_t* result;
-    size_t nmax = *n;
-    *n = 1;
-
-    /* special case: exact match - no processing needed */
-    if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) {
-        *n = 0;
-        return NULL;
-    }
-    sstr_t sv = sstrdup(s);
-
-    for (size_t i = 0 ; i < s.length ; i++) {
-        if (sv.ptr[i] == d.ptr[0]) {
-            _Bool match = 1;
-            for (size_t j = 1 ; j < d.length ; j++) {
-                if (j+i < s.length) {
-                    match &= (sv.ptr[i+j] == d.ptr[j]);
-                } else {
-                    match = 0;
-                    break;
-                }
-            }
-            if (match) {
-                (*n)++;
-                for (size_t j = 0 ; j < d.length ; j++) {
-                    sv.ptr[i+j] = 0;
-                }
-                i += d.length;
-            }
-        }
-        if ((*n) == nmax) break;
-    }
-    result = (sstr_t*) malloc(sizeof(sstr_t) * (*n));
-
-    char *pptr = sv.ptr;
-    for (size_t i = 0 ; i < *n ; i++) {
-        size_t l = strlen(pptr);
-        char* ptr = (char*) malloc(l + 1);
-        memcpy(ptr, pptr, l);
-        ptr[l] = 0;
-
-        result[i] = sstrn(ptr, l);
-        pptr += l + d.length;
-    }
-
-    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);
-}
-
-sstr_t sstrdup(sstr_t s) {
-    sstr_t newstring;
-    newstring.ptr = (char*) malloc(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 sstrdup_alloc(UcxAllocator *allocator, sstr_t s) {
-    sstr_t newstring;
-    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<string.length;i++) {
-        char c = string.ptr[i];
-        if(c > 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;
-}
-
-// webserver extension
-
-int sstr_startswith(sstr_t string, sstr_t cmp) {
-    sstr_t sub = sstrsubsl(string, 0, cmp.length);
-    if(!sstrcmp(sub, cmp)) {
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) {
-    sstr_t newstring;
-    newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1);
-    if (newstring.ptr != NULL) {
-        newstring.length = s.length;
-        newstring.ptr[newstring.length] = 0;
-
-        memcpy(newstring.ptr, s.ptr, s.length);
-    }
-
-    return newstring;
-}
-
-sstr_t sstrdup_pool(pool_handle_t *pool, sstr_t s) {
-    sstr_t newstring;
-    newstring.ptr = (char*)pool_malloc(pool, s.length + 1);
-    if (newstring.ptr != NULL) {
-        newstring.length = s.length;
-        newstring.ptr[newstring.length] = 0;
-
-        memcpy(newstring.ptr, s.ptr, s.length);
-    }
-
-    return newstring;
-}

mercurial