src/server/ucx/string.c

Wed, 22 Feb 2012 23:20:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 22 Feb 2012 23:20:39 +0100
changeset 23
a2c8fc23c90e
parent 21
627b09ee74e4
child 25
5dee29c7c530
permissions
-rw-r--r--

Added basic authentication

/*
 * File:   sstring.c
 * Author: olaf
 *
 * Created on 17. Juni 2010, 13:27
 */

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "string.h"
#include "mempool.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 (int i=0;i<n-1;i++) {
        sstr_t str = va_arg(ap, sstr_t);
        size += str.length;
    }

    return size;
}

sstr_t sstrcat (sstr_t s, ...) {
    va_list ap;
    va_start(ap, s);
    s.ptr[0] = 0;

    sstr_t str = va_arg (ap, sstr_t);
    while (str.ptr != NULL) {
        s.ptr = strncat (s.ptr, str.ptr, s.length);
        str = va_arg (ap, sstr_t);
    }

    return s;
}

sstr_t sstrncat (size_t n, sstr_t s, sstr_t c1, ...) {
    va_list ap;
    va_start(ap, c1);
    s.ptr[0] = 0;

    s.ptr = strncat (s.ptr, c1.ptr, s.length);
    for (int i=0;i<n-1;i++) {
        sstr_t str = va_arg (ap, sstr_t);
        s.ptr = strncat (s.ptr, str.ptr, s.length);
    }

    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 < 0 || start >= s.length || length < 0) {
        return s;
    }
    if (length > s.length-start) {
        length = s.length-start;
    }
    new_sstr.ptr = &s.ptr[start];
    new_sstr.length = length;
    return new_sstr;
}

int sstrcmp(sstr_t s1, sstr_t s2) {
    return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
}

sstr_t sstrdub(sstr_t s) {
    sstr_t newstring;
    newstring.ptr = malloc(s.length + 1);
    newstring.length = s.length;
    newstring.ptr[newstring.length] = 0;

    memcpy(newstring.ptr, s.ptr, s.length);

    return newstring;
}


// webserver extension
sstr_t sstrtrim(sstr_t string) {
    sstr_t newstr = string;
    int nsoff = 0;
    int l = 1;
    for(int i=0;i<string.length;i++) {
        char c = string.ptr[i];
        if(l) {
            /* leading whitespace */
            if(c > 32) {
                l = 0;
                nsoff = i;
                newstr.ptr = &string.ptr[i];
                newstr.length = string.length - nsoff;
            }
        } else {
            /* trailing whitespace */
            if(c > 32) {
                newstr.length = (i - nsoff) + 1;
            }
        }
    }
    return newstr;
}

sstr_t sstrdub_mp(UcxMempool *mp, sstr_t s) {
    sstr_t newstring;
    newstring.ptr = ucx_mempool_malloc(mp, s.length + 1);
    newstring.length = s.length;
    newstring.ptr[newstring.length] = 0;

    /* TODO: sometimes memcpy and/or memmove destroy the source */
    //memcpy(newstring.ptr, s.ptr, s.length);
    for(int i=0;i<s.length;i++) {
        newstring.ptr[i] = s.ptr[i];
    }

    return newstring;
}

mercurial