universe@39: /* universe@39: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@39: * universe@39: * Copyright 2015 Olaf Wintermann. All rights reserved. universe@39: * universe@39: * Redistribution and use in source and binary forms, with or without universe@39: * modification, are permitted provided that the following conditions are met: universe@39: * universe@39: * 1. Redistributions of source code must retain the above copyright universe@39: * notice, this list of conditions and the following disclaimer. universe@39: * universe@39: * 2. Redistributions in binary form must reproduce the above copyright universe@39: * notice, this list of conditions and the following disclaimer in the universe@39: * documentation and/or other materials provided with the distribution. universe@39: * universe@39: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@39: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@39: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@39: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@39: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@39: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@39: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@39: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@39: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@39: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@39: * POSSIBILITY OF SUCH DAMAGE. universe@39: */ universe@39: /** universe@39: * Bounded string implementation. universe@39: * universe@39: * The UCX strings (sstr_t) provide an alternative to C strings. universe@39: * The main difference to C strings is, that sstr_t does not universe@39: * need to be NULL-terminated. Instead the length is stored universe@39: * within the structure. universe@39: * universe@39: * When using sstr_t, developers must be full aware of what type universe@39: * of string (NULL-terminated) or not) they are using, when universe@39: * accessing the char* ptr directly. universe@39: * universe@39: * The UCX string module provides some common string functions, known from universe@39: * standard libc, working with sstr_t. universe@39: * universe@39: * @file string.h universe@39: * @author Mike Becker universe@39: * @author Olaf Wintermann universe@39: */ universe@39: universe@39: #ifndef UCX_STRING_H universe@39: #define UCX_STRING_H universe@39: universe@39: #include "ucx.h" universe@39: #include "allocator.h" universe@39: #include universe@39: universe@39: /** Shortcut for a sstr_t struct literal. */ universe@39: #define ST(s) { (char*)s, sizeof(s)-1 } universe@39: universe@39: /** Shortcut for the conversion of a C string to a sstr_t. */ universe@39: #define S(s) sstrn((char*)s, sizeof(s)-1) universe@39: universe@39: #ifdef __cplusplus universe@39: extern "C" { universe@39: #endif universe@39: universe@39: /** universe@39: * The UCX string structure. universe@39: */ universe@39: typedef struct { universe@39: /** A reference to the string (not necessarily NULL universe@39: * -terminated) */ universe@39: char *ptr; universe@39: /** The length of the string */ universe@39: size_t length; universe@39: } sstr_t; universe@39: universe@39: /** universe@39: * Creates a new sstr_t based on a C string. universe@39: * universe@39: * The length is implicitly inferred by using a call to strlen(). universe@39: * universe@39: * Note: the sstr_t will hold a reference to the C string. If you universe@39: * do want a copy, use sstrdup() on the return value of this function. universe@39: * universe@39: * @param cstring the C string to wrap universe@39: * @return a new sstr_t containing the C string universe@39: * universe@39: * @see sstrn() universe@39: */ universe@39: sstr_t sstr(char *cstring); universe@39: universe@39: /** universe@39: * Creates a new sstr_t of the specified length based on a C string. universe@39: * universe@39: * Note: the sstr_t will hold a reference to the C string. If you universe@39: * do want a copy, use sstrdup() on the return value of this function. universe@39: * universe@39: * @param cstring the C string to wrap universe@39: * @param length the length of the string universe@39: * @return a new sstr_t containing the C string universe@39: * universe@39: * @see sstr() universe@39: * @see S() universe@39: */ universe@39: sstr_t sstrn(char *cstring, size_t length); universe@39: universe@39: universe@39: /** universe@39: * Returns the cumulated length of all specified strings. universe@39: * universe@39: * At least one string must be specified. universe@39: * universe@39: * Attention: if the count argument does not match the count of the universe@39: * specified strings, the behavior is undefined. universe@39: * universe@39: * @param count the total number of specified strings (so at least 1) universe@39: * @param string the first string universe@39: * @param ... all other strings universe@39: * @return the cumulated length of all strings universe@39: */ universe@39: size_t sstrnlen(size_t count, sstr_t string, ...); universe@39: universe@39: /** universe@39: * Concatenates two or more strings. universe@39: * universe@39: * The resulting string will be allocated by standard malloc(). universe@39: * So developers MUST pass the sstr_t.ptr to free(). universe@39: * universe@39: * The sstr_t.ptr of the return value will always be NULL- universe@39: * terminated. universe@39: * universe@39: * @param count the total number of strings to concatenate universe@39: * @param s1 first string universe@39: * @param s2 second string universe@39: * @param ... all remaining strings universe@39: * @return the concatenated string universe@39: */ universe@39: sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); universe@39: universe@39: /** universe@39: * Concatenates two or more strings using an UcxAllocator. universe@39: * universe@39: * See sstrcat() for details. universe@39: * universe@39: * @param a the allocator to use universe@39: * @param count the total number of strings to concatenate universe@39: * @param s1 first string universe@39: * @param s2 second string universe@39: * @param ... all remaining strings universe@39: * @return the concatenated string universe@39: */ universe@39: sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); universe@39: universe@39: universe@39: /** universe@39: * Returns a substring starting at the specified location. universe@39: * universe@39: * Attention: the new string references the same memory area as the universe@39: * input string and will NOT be NULL-terminated. universe@39: * Use sstrdup() to get a copy. universe@39: * universe@39: * @param string input string universe@39: * @param start start location of the substring universe@39: * @return a substring of string starting at start universe@39: * universe@39: * @see sstrsubsl() universe@39: * @see sstrchr() universe@39: */ universe@39: sstr_t sstrsubs(sstr_t string, size_t start); universe@39: universe@39: /** universe@39: * Returns a substring with a maximum length starting at the specified location. universe@39: * universe@39: * Attention: the new string references the same memory area as the universe@39: * input string and will NOT be NULL-terminated. universe@39: * Use sstrdup() to get a copy. universe@39: * universe@39: * @param string input string universe@39: * @param start start location of the substring universe@39: * @param length the maximum length of the substring universe@39: * @return a substring of string starting at start universe@39: * with a maximum length of length universe@39: * universe@39: * @see sstrsubs() universe@39: * @see sstrchr() universe@39: */ universe@39: sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); universe@39: universe@39: /** universe@39: * Returns a substring starting at the location of the first occurrence of the universe@39: * specified character. universe@39: * universe@39: * If the string does not contain the character, an empty string is returned. universe@39: * universe@39: * @param string the string where to locate the character universe@39: * @param chr the character to locate universe@39: * @return a substring starting at the first location of chr universe@39: * universe@39: * @see sstrsubs() universe@39: */ universe@39: sstr_t sstrchr(sstr_t string, int chr); universe@39: universe@39: /** universe@39: * Returns a substring starting at the location of the last occurrence of the universe@39: * specified character. universe@39: * universe@39: * If the string does not contain the character, an empty string is returned. universe@39: * universe@39: * @param string the string where to locate the character universe@39: * @param chr the character to locate universe@39: * @return a substring starting at the last location of chr universe@39: * universe@39: * @see sstrsubs() universe@39: */ universe@39: sstr_t sstrrchr(sstr_t string, int chr); universe@39: universe@39: /** universe@39: * Returns a substring starting at the location of the first occurrence of the universe@39: * specified string. universe@39: * universe@39: * If the string does not contain the other string, an empty string is returned. universe@39: * universe@39: * If match is an empty string, the complete string is universe@39: * returned. universe@39: * universe@39: * @param string the string to be scanned universe@39: * @param match string containing the sequence of characters to match universe@39: * @return a substring starting at the first occurrence of universe@39: * match, or an empty string, if the sequence is not universe@39: * present in string universe@39: */ universe@39: sstr_t sstrstr(sstr_t string, sstr_t match); universe@39: universe@39: /** universe@39: * Splits a string into parts by using a delimiter string. universe@39: * universe@39: * This function will return NULL, if one of the following happens: universe@39: * universe@39: * universe@39: * The integer referenced by count is used as input and determines universe@39: * the maximum size of the resulting array, i.e. the maximum count of splits to universe@39: * perform + 1. universe@39: * universe@39: * The integer referenced by count is also used as output and is universe@39: * set to universe@39: * universe@39: * universe@39: * If the string starts with the delimiter, the first item of the resulting universe@39: * array will be an empty string. universe@39: * universe@39: * If the string ends with the delimiter and the maximum list size is not universe@39: * exceeded, the last array item will be an empty string. universe@39: * universe@39: * Attention: The array pointer AND all sstr_t.ptr of the array universe@39: * items must be manually passed to free(). Use sstrsplit_a() with universe@39: * an allocator to managed memory, to avoid this. universe@39: * universe@39: * @param string the string to split universe@39: * @param delim the delimiter string universe@39: * @param count IN: the maximum size of the resulting array (0 = no limit), universe@39: * OUT: the actual size of the array universe@39: * @return a sstr_t array containing the split strings or universe@39: * NULL on error universe@39: * universe@39: * @see sstrsplit_a() universe@39: */ universe@39: sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); universe@39: universe@39: /** universe@39: * Performing sstrsplit() using an UcxAllocator. universe@39: * universe@39: * Read the description of sstrsplit() for details. universe@39: * universe@39: * The memory for the sstr_t.ptr pointers of the array items and the memory for universe@39: * the sstr_t array itself are allocated by using the UcxAllocator.malloc() universe@39: * function. universe@39: * universe@39: * Note: the allocator is not used for memory that is freed within the universe@39: * same call of this function (locally scoped variables). universe@39: * universe@39: * @param allocator the UcxAllocator used for allocating memory universe@39: * @param string the string to split universe@39: * @param delim the delimiter string universe@39: * @param count IN: the maximum size of the resulting array (0 = no limit), universe@39: * OUT: the actual size of the array universe@39: * @return a sstr_t array containing the split strings or universe@39: * NULL on error universe@39: * universe@39: * @see sstrsplit() universe@39: */ universe@39: sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, universe@39: ssize_t *count); universe@39: universe@39: /** universe@39: * Compares two UCX strings with standard memcmp(). universe@39: * universe@39: * At first it compares the sstr_t.length attribute of the two strings. The universe@39: * memcmp() function is called, if and only if the lengths match. universe@39: * universe@39: * @param s1 the first string universe@39: * @param s2 the second string universe@39: * @return -1, if the length of s1 is less than the length of s2 or 1, if the universe@39: * length of s1 is greater than the length of s2 or the result of universe@39: * memcmp() otherwise (i.e. 0 if the strings match) universe@39: */ universe@39: int sstrcmp(sstr_t s1, sstr_t s2); universe@39: universe@39: /** universe@39: * Compares two UCX strings ignoring the case. universe@39: * universe@39: * At first it compares the sstr_t.length attribute of the two strings. If and universe@39: * only if the lengths match, both strings are compared char by char ignoring universe@39: * the case. universe@39: * universe@39: * @param s1 the first string universe@39: * @param s2 the second string universe@39: * @return -1, if the length of s1 is less than the length of s2 or 1, if the universe@39: * length of s1 is greater than the length of s2 or the difference between the universe@39: * first two differing characters otherwise (i.e. 0 if the strings match and universe@39: * no characters differ) universe@39: */ universe@39: int sstrcasecmp(sstr_t s1, sstr_t s2); universe@39: universe@39: /** universe@39: * Creates a duplicate of the specified string. universe@39: * universe@39: * The new sstr_t will contain a copy allocated by standard universe@39: * malloc(). So developers MUST pass the sstr_t.ptr to universe@39: * free(). universe@39: * universe@39: * The sstr_t.ptr of the return value will always be NULL- universe@39: * terminated. universe@39: * universe@39: * @param string the string to duplicate universe@39: * @return a duplicate of the string universe@39: * @see sstrdup_a() universe@39: */ universe@39: sstr_t sstrdup(sstr_t string); universe@39: universe@39: /** universe@39: * Creates a duplicate of the specified string using an UcxAllocator. universe@39: * universe@39: * The new sstr_t will contain a copy allocated by the allocators universe@39: * ucx_allocator_malloc function. So it is implementation depended, whether the universe@39: * returned sstr_t.ptr pointer must be passed to the allocators universe@39: * ucx_allocator_free function manually. universe@39: * universe@39: * The sstr_t.ptr of the return value will always be NULL- universe@39: * terminated. universe@39: * universe@39: * @param allocator a valid instance of an UcxAllocator universe@39: * @param string the string to duplicate universe@39: * @return a duplicate of the string universe@39: * @see sstrdup() universe@39: */ universe@39: sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string); universe@39: universe@39: /** universe@39: * Omits leading and trailing spaces. universe@39: * universe@39: * This function returns a new sstr_t containing a trimmed version of the universe@39: * specified string. universe@39: * universe@39: * Note: the new sstr_t references the same memory, thus you universe@39: * MUST NOT pass the sstr_t.ptr of the return value to universe@39: * free(). It is also highly recommended to avoid assignments like universe@39: * mystr = sstrtrim(mystr); as you lose the reference to the universe@39: * source string. Assignments of this type are only permitted, if the universe@39: * sstr_t.ptr of the source string does not need to be freed or if another universe@39: * reference to the source string exists. universe@39: * universe@39: * @param string the string that shall be trimmed universe@39: * @return a new sstr_t containing the trimmed string universe@39: */ universe@39: sstr_t sstrtrim(sstr_t string); universe@39: universe@39: /** universe@39: * Checks, if a string has a specific prefix. universe@39: * @param string the string to check universe@39: * @param prefix the prefix the string should have universe@39: * @return 1, if and only if the string has the specified prefix, 0 otherwise universe@39: */ universe@39: int sstrprefix(sstr_t string, sstr_t prefix); universe@39: universe@39: /** universe@39: * Checks, if a string has a specific suffix. universe@39: * @param string the string to check universe@39: * @param suffix the suffix the string should have universe@39: * @return 1, if and only if the string has the specified suffix, 0 otherwise universe@39: */ universe@39: int sstrsuffix(sstr_t string, sstr_t suffix); universe@39: universe@39: /** universe@39: * Returns a lower case version of a string. universe@39: * universe@39: * This function creates a duplicate of the input string, first. See the universe@39: * documentation of sstrdup() for the implications. universe@39: * universe@39: * @param string the input string universe@39: * @return the resulting lower case string universe@39: * @see sstrdup() universe@39: */ universe@39: sstr_t sstrlower(sstr_t string); universe@39: universe@39: /** universe@39: * Returns a lower case version of a string. universe@39: * universe@39: * This function creates a duplicate of the input string, first. See the universe@39: * documentation of sstrdup_a() for the implications. universe@39: * universe@39: * @param allocator the allocator used for duplicating the string universe@39: * @param string the input string universe@39: * @return the resulting lower case string universe@39: * @see sstrdup_a() universe@39: */ universe@39: sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string); universe@39: universe@39: /** universe@39: * Returns a upper case version of a string. universe@39: * universe@39: * This function creates a duplicate of the input string, first. See the universe@39: * documentation of sstrdup() for the implications. universe@39: * universe@39: * @param string the input string universe@39: * @return the resulting upper case string universe@39: * @see sstrdup() universe@39: */ universe@39: sstr_t sstrupper(sstr_t string); universe@39: universe@39: /** universe@39: * Returns a upper case version of a string. universe@39: * universe@39: * This function creates a duplicate of the input string, first. See the universe@39: * documentation of sstrdup_a() for the implications. universe@39: * universe@39: * @param allocator the allocator used for duplicating the string universe@39: * @param string the input string universe@39: * @return the resulting upper case string universe@39: * @see sstrdup_a() universe@39: */ universe@39: sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string); universe@39: universe@39: #ifdef __cplusplus universe@39: } universe@39: #endif universe@39: universe@39: #endif /* UCX_STRING_H */