ucx/string.c

Tue, 03 Sep 2013 12:08:35 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 03 Sep 2013 12:08:35 +0200
changeset 38
b855f76e965b
parent 17
11dffb40cd91
child 39
3e55bed345f9
permissions
-rw-r--r--

code cleanup + experimental login prompt

/*
 * 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 *cstring) {
    sstr_t string;
    string.ptr = cstring;
    string.length = strlen(cstring);
    return string;
}

sstr_t sstrn(char *cstring, size_t length) {
    sstr_t string;
    string.ptr = cstring;
    string.length = length;
    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 = 1 ; i < n ; i++) {
        sstr_t str = va_arg(ap, sstr_t);
        size += str.length;
    }
    va_end(ap);

    return size;
}

sstr_t sstrncat(sstr_t s, size_t n, 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 = 1 ; i < n ; 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) {
    return sstrsplit_a(ucx_default_allocator(), s, d, n);
}

sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) {
    if (s.length == 0 || d.length == 0) {
        *n = -1;
        return NULL;
    }

    sstr_t* result;
    size_t nmax = *n;
    *n = 1;

    /* special case: exact match - no processing needed */
    if (sstrcmp(s, d) == 0) {
        *n = 0;
        return NULL;
    }
    sstr_t sv = sstrdup(s);
    if (sv.length == 0) {
        *n = -2;
        return NULL;
    }

    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*) allocator->malloc(allocator->pool, sizeof(sstr_t)*(*n));

    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;
        }
    } else {
        *n = -2;
    }
    
    free(sv.ptr);

    return result;
}

int sstrcmp(sstr_t s1, sstr_t s2) {
    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*)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;
}

int sstrprefix(sstr_t string, sstr_t prefix) {
    if (string.length == 0) {
        return prefix.length == 0;
    }
    if (prefix.length == 0) {
        return 1;
    }
    
    if (prefix.length > string.length) {
        return 0;
    } else {
        return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
    }
}

int sstrsuffix(sstr_t string, sstr_t suffix) {
    if (string.length == 0) {
        return suffix.length == 0;
    }
    if (suffix.length == 0) {
        return 1;
    }
    
    if (suffix.length > string.length) {
        return 0;
    } else {
        return memcmp(string.ptr+string.length-suffix.length,
            suffix.ptr, suffix.length) == 0;
    }
}

mercurial