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: #ifndef _SSTRING_H olaf@20: #define _SSTRING_H olaf@20: universe@69: #include "ucx.h" olaf@109: #include "allocator.h" universe@38: #include universe@38: universe@42: /* use macros for literals only */ universe@74: #define S(s) { (char*)s, sizeof(s)-1 } universe@74: #define ST(s) sstrn((char*)s, sizeof(s)-1) olaf@20: olaf@20: #ifdef __cplusplus olaf@20: extern "C" { olaf@20: #endif olaf@20: olaf@20: typedef struct sstring { olaf@20: char *ptr; olaf@20: size_t length; olaf@20: } sstr_t; olaf@20: olaf@20: /* olaf@20: * creates a new sstr_t from a null terminated string olaf@20: * olaf@20: * s null terminated string olaf@20: */ olaf@68: sstr_t sstr(char *s); olaf@20: olaf@20: /* olaf@20: * creates a new sstr_t from a string and length olaf@20: * olaf@20: * s string olaf@20: * n length of string olaf@20: */ olaf@68: sstr_t sstrn(char *s, size_t n); olaf@20: olaf@20: olaf@20: /* olaf@20: * gets the length of n sstr_t strings olaf@20: * olaf@20: * n number of strings olaf@20: * s string olaf@20: * ... strings olaf@20: */ olaf@68: size_t sstrnlen(size_t n, sstr_t s, ...); olaf@20: olaf@20: olaf@20: /* olaf@20: * concatenates n strings olaf@20: * olaf@20: * n number of strings olaf@20: * s new string with enough memory allocated olaf@20: * ... strings olaf@20: */ olaf@68: sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...); olaf@20: olaf@20: olaf@20: /* olaf@20: * olaf@20: */ olaf@68: sstr_t sstrsubs(sstr_t s, size_t start); olaf@20: olaf@20: /* olaf@20: * olaf@20: */ olaf@68: sstr_t sstrsubsl(sstr_t s, size_t start, size_t length); olaf@20: universe@39: /* olaf@108: * olaf@108: */ olaf@108: sstr_t sstrchr(sstr_t s, int c); olaf@108: olaf@108: /* universe@39: * splits s into n parts universe@39: * universe@39: * s the string to split universe@39: * d the delimiter string universe@39: * n the maximum size of the resulting list universe@39: * a size of 0 indicates an unbounded list size universe@39: * the actual size of the list will be stored here universe@39: * universe@39: * Hint: use this value to avoid dynamic reallocation of the result list universe@39: * universe@39: * Returns a list of the split strings universe@39: * NOTE: this list needs to be freed manually after usage universe@39: * universe@39: * Returns NULL on error universe@39: */ olaf@68: sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n); olaf@20: olaf@68: int sstrcmp(sstr_t s1, sstr_t s2); olaf@20: olaf@68: sstr_t sstrdup(sstr_t s); olaf@109: sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s); olaf@20: olaf@96: sstr_t sstrtrim(sstr_t string); olaf@96: olaf@20: #ifdef __cplusplus olaf@20: } olaf@20: #endif olaf@20: olaf@20: #endif /* _SSTRING_H */