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: */ universe@116: /** universe@116: * Bounded string implementation. universe@116: * universe@116: * The UCX strings (sstr_t) provide an alternative to C strings. universe@116: * The main difference to C strings is, that sstr_t does not universe@116: * need to be NULL-terminated. Instead the length is stored universe@116: * within the structure. universe@116: * universe@116: * When using sstr_t, developers must be full aware of what type universe@116: * of string (NULL-terminated) or not) they are using, when universe@116: * accessing the char* ptr directly. universe@116: * universe@116: * The UCX string module provides some common string functions, known from universe@116: * standard libc, working with sstr_t. universe@116: * universe@116: * @file string.h universe@116: * @author Mike Becker universe@116: * @author Olaf Wintermann universe@116: */ olaf@20: universe@116: #ifndef UCX_STRING_H universe@116: #define UCX_STRING_H olaf@20: universe@69: #include "ucx.h" olaf@109: #include "allocator.h" universe@38: #include universe@38: universe@116: /** Shortcut for a sstr_t struct literal. */ universe@116: #define ST(s) { (char*)s, sizeof(s)-1 } universe@116: /** Shortcut for the conversion of a C string to a sstr_t. */ universe@116: #define S(s) sstrn((char*)s, sizeof(s)-1) olaf@20: olaf@20: #ifdef __cplusplus olaf@20: extern "C" { olaf@20: #endif olaf@20: universe@116: /** universe@116: * The UCX string structure. universe@116: */ universe@116: typedef struct { universe@116: /** A reference to the string (not necessarily NULL universe@116: * -terminated) */ olaf@20: char *ptr; universe@116: /** The length of the string */ olaf@20: size_t length; olaf@20: } sstr_t; olaf@20: universe@116: /** universe@116: * Creates a new sstr_t based on a C string. universe@116: * universe@116: * The length is implicitly inferred by using a call to strlen(). olaf@20: * universe@116: * Note: the sstr_t will hold a reference to the C string. If you universe@116: * do want a copy, use sstrdup() on the return value of this function. universe@116: * universe@116: * @param cstring the C string to wrap universe@116: * @return a new sstr_t containing the C string universe@116: * universe@116: * @see sstrn() olaf@20: */ universe@116: sstr_t sstr(char *cstring); olaf@20: universe@116: /** universe@116: * Creates a new sstr_t of the specified length based on a C string. olaf@20: * universe@116: * Note: the sstr_t will hold a reference to the C string. If you universe@116: * do want a copy, use sstrdup() on the return value of this function. universe@116: * universe@116: * @param cstring the C string to wrap universe@116: * @param length the length of the string universe@116: * @return a new sstr_t containing the C string universe@116: * universe@116: * @see sstr() universe@116: * @see S() olaf@20: */ universe@116: sstr_t sstrn(char *cstring, size_t length); olaf@20: olaf@20: universe@116: /** universe@116: * Returns the cumulated length of all specified strings. olaf@20: * universe@116: * At least one string must be specified. universe@116: * universe@116: * Attention: if the count argument does not match the count of the universe@116: * specified strings, the behavior is undefined. universe@116: * universe@116: * @param count the total number of specified strings (so at least 1) universe@116: * @param string the first string universe@116: * @param ... all other strings universe@116: * @return the cumulated length of all strings olaf@20: */ universe@116: size_t sstrnlen(size_t count, sstr_t string, ...); 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: universe@116: /** universe@116: * Compares two UCX strings with standard memcmp(). universe@116: * universe@116: * At first it compares the sstr_t.length attribute of the two strings. The universe@116: * memcmp() function is called, if and only if the lengths match. universe@116: * universe@116: * @param s1 the first string universe@116: * @param s2 the second string universe@116: * @return -1, if the length of s1 is less than the length of s2 or 1, if the universe@116: * length of s1 is greater than the length of s2 or the result of universe@116: * memcmp() otherwise (i.e. 0 if the strings match) universe@116: */ olaf@68: int sstrcmp(sstr_t s1, sstr_t s2); olaf@20: universe@116: /** universe@116: * Creates a duplicate of the specified string. universe@116: * universe@116: * The new sstr_t will contain a copy allocated by standard universe@116: * malloc(). So developers MUST pass the sstr_t.ptr to universe@116: * free(). universe@116: * universe@116: * @param string the string to duplicate universe@116: * @return a duplicate of the argument universe@116: */ universe@116: sstr_t sstrdup(sstr_t string); universe@116: sstr_t sstrdupa(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: universe@116: #endif /* UCX_STRING_H */