olaf@20: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@20: * universe@259: * Copyright 2017 Mike Becker, 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@259: #include "ucx.h" universe@259: #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@146: 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: universe@316: /** Expands a sstr_t or scstr_t to printf arguments. */ universe@283: #define SFMT(s) (int) (s).length, (s).ptr universe@283: universe@316: /** Format specifier for a sstr_t or scstr_t. */ universe@283: #define PRIsstr ".*s" universe@283: olaf@20: #ifdef __cplusplus olaf@20: extern "C" { olaf@20: #endif universe@116: /** universe@116: * The UCX string structure. universe@116: */ universe@116: typedef struct { universe@316: /** A pointer to the string universe@316: * (not necessarily NULL-terminated) */ universe@316: char *ptr; universe@116: /** The length of the string */ olaf@20: size_t length; olaf@20: } sstr_t; olaf@20: universe@316: /** universe@316: * The UCX string structure for immutable (constant) strings. universe@316: */ olaf@275: typedef struct { universe@316: /** A constant pointer to the immutable string universe@316: * (not necessarily NULL-terminated) */ olaf@275: const char *ptr; universe@316: /** The length of the string */ universe@316: size_t length; olaf@275: } scstr_t; olaf@288: olaf@275: #ifdef __cplusplus olaf@275: } olaf@275: #endif olaf@275: olaf@275: olaf@275: #ifdef __cplusplus olaf@275: inline scstr_t s2scstr(sstr_t s) { olaf@275: scstr_t c; olaf@275: c.ptr = s.ptr; olaf@275: c.length = s.ptr; olaf@275: return c; olaf@275: } olaf@275: inline scstr_t s2scstr(scstr_t c) { olaf@275: return c; olaf@275: } olaf@275: #define SCSTR s2scstr olaf@275: #else olaf@275: universe@316: /** universe@316: * One of two type adjustment functions that return a scstr_t. universe@316: * universe@320: * Used internally to convert a UCX string to an immutable UCX string. universe@316: * This variant is used, when the string is already immutable and no operation universe@316: * needs to be performed. universe@316: * universe@316: * @param str some scstr_t universe@316: * @return the argument itself universe@316: */ universe@316: scstr_t ucx_sc2sc(scstr_t str); universe@316: universe@316: /** universe@316: * One of two type adjustment functions that return a scstr_t. universe@316: * universe@320: * Used internally to convert a UCX string to an immutable UCX string. universe@316: * universe@316: * @param str some sstr_t universe@316: * @return an immutable (scstr_t) version of the provided string. universe@316: */ olaf@275: scstr_t ucx_ss2sc(sstr_t str); universe@316: olaf@275: #if __STDC_VERSION__ >= 201112L universe@316: /** universe@316: * Casts a UCX string to an immutable UCX string (scstr_t). universe@316: * @param str some UCX string universe@316: * @return the an immutable version of the provided string universe@316: */ olaf@275: #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str) universe@316: olaf@275: #elif defined(__GNUC__) || defined(__clang__) universe@316: universe@316: /** universe@316: * Casts a UCX string to an immutable UCX string (scstr_t). universe@316: * @param str some UCX string universe@316: * @return the an immutable version of the provided string universe@316: */ olaf@275: #define SCSTR(str) __builtin_choose_expr( \ olaf@275: __builtin_types_compatible_p(typeof(str), sstr_t), \ olaf@275: ucx_ss2sc, \ olaf@275: ucx_sc2sc)(str) universe@316: olaf@275: #elif defined(__sun) universe@316: universe@316: /** universe@316: * Casts a UCX string to an immutable UCX string (scstr_t). universe@316: * @param str some UCX string universe@316: * @return the an immutable version of the provided string universe@316: */ olaf@275: #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \ olaf@275: scstr_t ucx_tmp_var_c; \ olaf@275: ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\ olaf@275: ucx_tmp_var_c.length = ucx_tmp_var_str.length;\ olaf@275: ucx_tmp_var_c; }) universe@316: #else /* no generics and no builtins */ universe@316: universe@316: /** universe@316: * Casts a UCX string to an immutable UCX string (scstr_t). universe@316: * universe@316: * This internal function (ab)uses the C standard an expects one single universe@320: * argument which is then implicitly converted to scstr_t without a warning. universe@316: * universe@316: * @return the an immutable version of the provided string universe@316: */ olaf@275: scstr_t ucx_ss2c_s(); universe@316: universe@316: /** universe@316: * Casts a UCX string to an immutable UCX string (scstr_t). universe@316: * @param str some UCX string universe@316: * @return the an immutable version of the provided string universe@316: */ universe@316: #define SCSTR(str) ucx_ss2c_s(str) olaf@275: #endif /* C11 feature test */ olaf@275: olaf@275: #endif /* C++ */ olaf@275: olaf@275: #ifdef __cplusplus olaf@275: extern "C" { olaf@275: #endif olaf@275: olaf@275: 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@316: * If you need to wrap a constant string, use scstr(). universe@316: * 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@316: * If you need to wrap a constant string, use scstrn(). universe@316: * 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: universe@316: /** universe@316: * Creates a new scstr_t based on a constant C string. universe@316: * universe@316: * The length is implicitly inferred by using a call to strlen(). universe@316: * universe@316: * Note: the scstr_t will hold a reference to the C string. If you universe@316: * do want a copy, use scstrdup() on the return value of this function. universe@316: * universe@316: * @param cstring the C string to wrap universe@316: * @return a new scstr_t containing the C string universe@316: * universe@316: * @see scstrn() universe@316: */ universe@316: scstr_t scstr(const char *cstring); olaf@20: universe@316: universe@316: /** universe@316: * Creates a new scstr_t of the specified length based on a constant C string. universe@316: * universe@316: * Note: the scstr_t will hold a reference to the C string. If you universe@316: * do want a copy, use scstrdup() on the return value of this function. universe@316: * universe@316: * universe@316: * @param cstring the C string to wrap universe@316: * @param length the length of the string universe@316: * @return a new scstr_t containing the C string universe@316: * universe@316: * @see scstr() universe@316: */ olaf@275: scstr_t scstrn(const char *cstring, size_t length); olaf@275: universe@116: /** universe@116: * Returns the cumulated length of all specified strings. universe@318: * 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@318: * @param ... all strings universe@116: * @return the cumulated length of all strings olaf@20: */ universe@319: size_t scstrnlen(size_t count, ...); olaf@288: universe@318: /** universe@320: * Alias for scstrnlen() which automatically converts the arguments. universe@318: * universe@318: * @param count the total number of specified strings (so at least 1) universe@318: * @param ... all strings universe@318: * @return the cumulated length of all strings universe@318: */ universe@319: #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__) olaf@20: universe@119: /** olaf@183: * Concatenates two or more strings. olaf@183: * olaf@183: * The resulting string will be allocated by standard malloc(). olaf@183: * So developers MUST pass the sstr_t.ptr to free(). olaf@183: * olaf@183: * The sstr_t.ptr of the return value will always be NULL- olaf@183: * terminated. olaf@180: * olaf@180: * @param count the total number of strings to concatenate olaf@183: * @param s1 first string olaf@183: * @param ... all remaining strings olaf@180: * @return the concatenated string olaf@180: */ universe@319: sstr_t scstrcat(size_t count, scstr_t s1, ...); olaf@288: universe@318: /** universe@320: * Alias for scstrcat() which automatically converts the arguments. universe@318: * universe@318: * @param count the total number of strings to concatenate universe@318: * @param s1 first string universe@318: * @param ... all remaining strings universe@318: * @return the concatenated string universe@318: */ universe@319: #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__) olaf@183: olaf@183: /** universe@225: * Concatenates two or more strings using a UcxAllocator. olaf@183: * universe@319: * See scstrcat() for details. olaf@183: * olaf@183: * @param a the allocator to use olaf@183: * @param count the total number of strings to concatenate olaf@183: * @param s1 first string olaf@183: * @param ... all remaining strings olaf@183: * @return the concatenated string olaf@183: */ universe@319: sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...); olaf@180: universe@318: /** universe@320: * Alias for scstrcat_a() which automatically converts the arguments. universe@318: * universe@318: * See sstrcat() for details. universe@318: * universe@318: * @param a the allocator to use universe@318: * @param count the total number of strings to concatenate universe@318: * @param s1 first string universe@318: * @param ... all remaining strings universe@318: * @return the concatenated string universe@318: */ universe@318: #define sstrcat_a(a, count, s1, ...) \ universe@319: scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__) olaf@180: olaf@180: /** universe@119: * Returns a substring starting at the specified location. universe@119: * universe@119: * Attention: the new string references the same memory area as the universe@119: * input string and will NOT be NULL-terminated. universe@119: * Use sstrdup() to get a copy. universe@119: * universe@119: * @param string input string universe@119: * @param start start location of the substring universe@119: * @return a substring of string starting at start universe@119: * universe@119: * @see sstrsubsl() universe@119: * @see sstrchr() universe@119: */ universe@119: sstr_t sstrsubs(sstr_t string, size_t start); universe@119: universe@119: /** universe@119: * Returns a substring with a maximum length starting at the specified location. universe@119: * universe@119: * Attention: the new string references the same memory area as the universe@119: * input string and will NOT be NULL-terminated. universe@119: * Use sstrdup() to get a copy. universe@119: * universe@119: * @param string input string universe@119: * @param start start location of the substring universe@119: * @param length the maximum length of the substring universe@119: * @return a substring of string starting at start universe@119: * with a maximum length of length universe@119: * universe@119: * @see sstrsubs() universe@119: * @see sstrchr() universe@119: */ universe@119: sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); universe@119: universe@318: /** universe@318: * Returns a substring of an immutable string starting at the specified universe@318: * location. universe@318: * universe@318: * Attention: the new string references the same memory area as the universe@318: * input string and will NOT be NULL-terminated. universe@318: * Use scstrdup() to get a copy. universe@318: * universe@318: * @param string input string universe@318: * @param start start location of the substring universe@318: * @return a substring of string starting at start universe@318: * universe@318: * @see scstrsubsl() universe@318: * @see scstrchr() universe@318: */ universe@318: scstr_t scstrsubs(scstr_t string, size_t start); universe@318: universe@318: /** universe@318: * Returns a substring of an immutable string with a maximum length starting universe@318: * at the specified location. universe@318: * universe@318: * Attention: the new string references the same memory area as the universe@318: * input string and will NOT be NULL-terminated. universe@318: * Use scstrdup() to get a copy. universe@318: * universe@318: * @param string input string universe@318: * @param start start location of the substring universe@318: * @param length the maximum length of the substring universe@318: * @return a substring of string starting at start universe@318: * with a maximum length of length universe@318: * universe@318: * @see scstrsubs() universe@318: * @see scstrchr() universe@318: */ olaf@300: scstr_t scstrsubsl(scstr_t string, size_t start, size_t length); olaf@300: universe@119: /** universe@119: * Returns a substring starting at the location of the first occurrence of the universe@119: * specified character. universe@119: * universe@119: * If the string does not contain the character, an empty string is returned. universe@119: * universe@119: * @param string the string where to locate the character universe@119: * @param chr the character to locate universe@148: * @return a substring starting at the first location of chr universe@119: * universe@119: * @see sstrsubs() universe@119: */ universe@119: sstr_t sstrchr(sstr_t string, int chr); universe@119: universe@119: /** universe@148: * Returns a substring starting at the location of the last occurrence of the universe@148: * specified character. universe@148: * universe@148: * If the string does not contain the character, an empty string is returned. universe@148: * universe@148: * @param string the string where to locate the character universe@148: * @param chr the character to locate universe@148: * @return a substring starting at the last location of chr universe@148: * universe@148: * @see sstrsubs() universe@148: */ universe@148: sstr_t sstrrchr(sstr_t string, int chr); universe@148: universe@318: /** universe@318: * Returns an immutable substring starting at the location of the first universe@318: * occurrence of the specified character. universe@318: * universe@318: * If the string does not contain the character, an empty string is returned. universe@318: * universe@318: * @param string the string where to locate the character universe@318: * @param chr the character to locate universe@318: * @return a substring starting at the first location of chr universe@318: * universe@318: * @see scstrsubs() universe@318: */ universe@318: scstr_t scstrchr(scstr_t string, int chr); olaf@276: universe@318: /** universe@318: * Returns an immutable substring starting at the location of the last universe@318: * occurrence of the specified character. universe@318: * universe@318: * If the string does not contain the character, an empty string is returned. universe@318: * universe@318: * @param string the string where to locate the character universe@318: * @param chr the character to locate universe@318: * @return a substring starting at the last location of chr universe@318: * universe@318: * @see scstrsubs() universe@318: */ olaf@300: scstr_t scstrrchr(scstr_t string, int chr); olaf@300: universe@148: /** universe@214: * Returns a substring starting at the location of the first occurrence of the universe@214: * specified string. universe@214: * universe@214: * If the string does not contain the other string, an empty string is returned. universe@214: * universe@214: * If match is an empty string, the complete string is universe@214: * returned. universe@214: * universe@214: * @param string the string to be scanned universe@214: * @param match string containing the sequence of characters to match universe@214: * @return a substring starting at the first occurrence of universe@214: * match, or an empty string, if the sequence is not universe@214: * present in string universe@214: */ universe@319: sstr_t scstrsstr(sstr_t string, scstr_t match); universe@318: universe@318: /** universe@320: * Alias for scstrsstr() which automatically converts the match string. universe@318: * universe@318: * @param string the string to be scanned universe@318: * @param match string containing the sequence of characters to match universe@318: * @return a substring starting at the first occurrence of universe@318: * match, or an empty string, if the sequence is not universe@318: * present in string universe@318: */ universe@319: #define sstrstr(string, match) scstrsstr(string, SCSTR(match)) olaf@276: universe@318: /** universe@318: * Returns an immutable substring starting at the location of the universe@318: * first occurrence of the specified immutable string. universe@318: * universe@318: * If the string does not contain the other string, an empty string is returned. universe@318: * universe@318: * If match is an empty string, the complete string is universe@318: * returned. universe@318: * universe@318: * @param string the string to be scanned universe@318: * @param match string containing the sequence of characters to match universe@318: * @return a substring starting at the first occurrence of universe@318: * match, or an empty string, if the sequence is not universe@318: * present in string universe@318: */ universe@319: scstr_t scstrscstr(scstr_t string, scstr_t match); universe@318: universe@318: /** universe@320: * Alias for scstrscstr() which automatically converts the match string. universe@318: * universe@318: * @param string the string to be scanned universe@318: * @param match string containing the sequence of characters to match universe@318: * @return a substring starting at the first occurrence of universe@318: * match, or an empty string, if the sequence is not universe@318: * present in string universe@318: */ universe@319: #define sstrscstr(string, match) scstrscstr(string, SCSTR(match)) universe@214: universe@214: /** universe@119: * Splits a string into parts by using a delimiter string. universe@119: * universe@119: * This function will return NULL, if one of the following happens: universe@119: * universe@119: * universe@119: * The integer referenced by count is used as input and determines universe@160: * the maximum size of the resulting array, i.e. the maximum count of splits to universe@119: * perform + 1. universe@119: * universe@119: * The integer referenced by count is also used as output and is universe@119: * set to universe@119: * universe@119: * universe@119: * If the string starts with the delimiter, the first item of the resulting universe@160: * array will be an empty string. universe@119: * universe@119: * If the string ends with the delimiter and the maximum list size is not universe@160: * exceeded, the last array item will be an empty string. universe@233: * In case the list size would be exceeded, the last array item will be the universe@233: * remaining string after the last split, including the terminating universe@233: * delimiter. universe@119: * universe@160: * Attention: The array pointer AND all sstr_t.ptr of the array universe@125: * items must be manually passed to free(). Use sstrsplit_a() with universe@119: * an allocator to managed memory, to avoid this. olaf@20: * universe@119: * @param string the string to split universe@119: * @param delim the delimiter string universe@160: * @param count IN: the maximum size of the resulting array (0 = no limit), universe@160: * OUT: the actual size of the array universe@160: * @return a sstr_t array containing the split strings or universe@318: * NULL on error universe@318: * universe@319: * @see scstrsplit_a() universe@318: */ universe@319: sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count); universe@318: universe@318: /** universe@320: * Alias for scstrsplit() which automatically converts the arguments. universe@318: * universe@318: * @param string the string to split universe@318: * @param delim the delimiter string universe@318: * @param count IN: the maximum size of the resulting array (0 = no limit), universe@318: * OUT: the actual size of the array universe@318: * @return a sstr_t array containing the split strings or universe@318: * NULL on error universe@119: * universe@125: * @see sstrsplit_a() olaf@20: */ universe@318: #define sstrsplit(string, delim, count) \ universe@319: scstrsplit(SCSTR(string), SCSTR(delim), count) olaf@20: universe@119: /** universe@319: * Performing scstrsplit() using a UcxAllocator. universe@119: * universe@319: * Read the description of scstrsplit() for details. universe@119: * universe@160: * The memory for the sstr_t.ptr pointers of the array items and the memory for universe@119: * the sstr_t array itself are allocated by using the UcxAllocator.malloc() universe@119: * function. universe@119: * universe@119: * Note: the allocator is not used for memory that is freed within the universe@119: * same call of this function (locally scoped variables). universe@119: * universe@125: * @param allocator the UcxAllocator used for allocating memory universe@119: * @param string the string to split universe@119: * @param delim the delimiter string universe@160: * @param count IN: the maximum size of the resulting array (0 = no limit), universe@160: * OUT: the actual size of the array universe@160: * @return a sstr_t array containing the split strings or universe@318: * NULL on error universe@119: * universe@319: * @see scstrsplit() olaf@20: */ universe@319: sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim, universe@173: ssize_t *count); olaf@20: universe@318: /** universe@320: * Alias for scstrsplit_a() which automatically converts the arguments. universe@318: * universe@318: * @param allocator the UcxAllocator used for allocating memory universe@318: * @param string the string to split universe@318: * @param delim the delimiter string universe@318: * @param count IN: the maximum size of the resulting array (0 = no limit), universe@318: * OUT: the actual size of the array universe@318: * @return a sstr_t array containing the split strings or universe@318: * NULL on error universe@318: * universe@318: * @see sstrsplit() universe@318: */ universe@318: #define sstrsplit_a(allocator, string, delim, count) \ universe@319: scstrsplit_a(allocator, SCSTR(string), SCSTR(delim, count)) olaf@276: universe@116: /** universe@116: * Compares two UCX strings with standard memcmp(). universe@116: * universe@318: * At first it compares the scstr_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: */ universe@319: int scstrcmp(scstr_t s1, scstr_t s2); olaf@276: universe@318: /** universe@320: * Alias for scstrcmp() which automatically converts its arguments. universe@318: * universe@318: * @param s1 the first string universe@318: * @param s2 the second string universe@318: * @return -1, if the length of s1 is less than the length of s2 or 1, if the universe@318: * length of s1 is greater than the length of s2 or the result of universe@318: * memcmp() otherwise (i.e. 0 if the strings match) universe@318: */ universe@319: #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2)) olaf@20: universe@116: /** universe@149: * Compares two UCX strings ignoring the case. universe@149: * universe@319: * At first it compares the scstr_t.length attribute of the two strings. If and universe@149: * only if the lengths match, both strings are compared char by char ignoring universe@149: * the case. universe@149: * universe@149: * @param s1 the first string universe@149: * @param s2 the second string universe@149: * @return -1, if the length of s1 is less than the length of s2 or 1, if the universe@318: * length of s1 is greater than the length of s2 or the result of the platform universe@318: * specific string comparison function ignoring the case. universe@149: */ universe@319: int scstrcasecmp(scstr_t s1, scstr_t s2); olaf@276: universe@318: /** universe@320: * Alias for scstrcasecmp() which automatically converts the arguments. universe@318: * universe@318: * @param s1 the first string universe@318: * @param s2 the second string universe@318: * @return -1, if the length of s1 is less than the length of s2 or 1, if the universe@318: * length of s1 is greater than the length of s2 or the result of the platform universe@318: * specific string comparison function ignoring the case. universe@318: */ universe@319: #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2)) universe@149: universe@149: /** 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@118: * The sstr_t.ptr of the return value will always be NULL- universe@318: * terminated and mutable, regardless of the argument. universe@318: * universe@318: * @param string the string to duplicate universe@318: * @return a duplicate of the string universe@319: * @see scstrdup_a() universe@318: */ universe@319: sstr_t scstrdup(scstr_t string); universe@318: universe@318: /** universe@320: * Alias for scstrdup() which automatically converts the argument. universe@118: * universe@116: * @param string the string to duplicate universe@118: * @return a duplicate of the string universe@125: * @see sstrdup_a() universe@116: */ universe@319: #define sstrdup(string) scstrdup(SCSTR(string)) olaf@20: universe@118: /** universe@225: * Creates a duplicate of the specified string using a UcxAllocator. universe@118: * universe@118: * The new sstr_t will contain a copy allocated by the allocators universe@319: * UcxAllocator.malloc() function. So it is implementation depended, whether the universe@118: * returned sstr_t.ptr pointer must be passed to the allocators universe@319: * UcxAllocator.free() function manually. universe@118: * universe@118: * The sstr_t.ptr of the return value will always be NULL- universe@318: * terminated and mutable, regardless of the argument. universe@118: * universe@225: * @param allocator a valid instance of a UcxAllocator universe@118: * @param string the string to duplicate universe@118: * @return a duplicate of the string universe@319: * @see scstrdup() universe@118: */ universe@319: sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string); olaf@275: universe@318: /** universe@320: * Alias for scstrdup_a() which automatically converts the argument. universe@318: * universe@318: * @param allocator a valid instance of a UcxAllocator universe@318: * @param string the string to duplicate universe@318: * @return a duplicate of the string universe@319: * @see scstrdup() universe@318: */ universe@319: #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string)) universe@118: olaf@276: universe@118: /** universe@118: * Omits leading and trailing spaces. universe@118: * universe@118: * This function returns a new sstr_t containing a trimmed version of the universe@118: * specified string. universe@118: * universe@118: * Note: the new sstr_t references the same memory, thus you universe@118: * MUST NOT pass the sstr_t.ptr of the return value to universe@118: * free(). It is also highly recommended to avoid assignments like universe@118: * mystr = sstrtrim(mystr); as you lose the reference to the universe@118: * source string. Assignments of this type are only permitted, if the universe@118: * sstr_t.ptr of the source string does not need to be freed or if another universe@118: * reference to the source string exists. universe@118: * universe@118: * @param string the string that shall be trimmed universe@118: * @return a new sstr_t containing the trimmed string universe@118: */ olaf@96: sstr_t sstrtrim(sstr_t string); olaf@96: universe@318: /** universe@318: * Omits leading and trailing spaces. universe@318: * universe@318: * This function returns a new scstr_t containing a trimmed version of the universe@318: * specified string. universe@318: * universe@318: * Note: the new scstr_t references the same memory, thus you universe@318: * MUST NOT pass the scstr_t.ptr of the return value to universe@318: * free(). It is also highly recommended to avoid assignments like universe@318: * mystr = scstrtrim(mystr); as you lose the reference to the universe@318: * source string. Assignments of this type are only permitted, if the universe@318: * scstr_t.ptr of the source string does not need to be freed or if another universe@318: * reference to the source string exists. universe@318: * universe@318: * @param string the string that shall be trimmed universe@318: * @return a new scstr_t containing the trimmed string universe@318: */ olaf@276: scstr_t scstrtrim(scstr_t string); olaf@276: universe@146: /** universe@146: * Checks, if a string has a specific prefix. universe@146: * @param string the string to check universe@146: * @param prefix the prefix the string should have universe@146: * @return 1, if and only if the string has the specified prefix, 0 otherwise universe@146: */ universe@319: int scstrprefix(scstr_t string, scstr_t prefix); olaf@275: universe@318: /** universe@320: * Alias for scstrprefix() which automatically converts the arguments. universe@318: * universe@318: * @param string the string to check universe@318: * @param prefix the prefix the string should have universe@318: * @return 1, if and only if the string has the specified prefix, 0 otherwise universe@318: */ universe@319: #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix)) universe@146: universe@146: /** universe@146: * Checks, if a string has a specific suffix. universe@146: * @param string the string to check universe@146: * @param suffix the suffix the string should have universe@146: * @return 1, if and only if the string has the specified suffix, 0 otherwise universe@146: */ universe@319: int scstrsuffix(scstr_t string, scstr_t suffix); olaf@275: universe@318: /** universe@320: * Alias for scstrsuffix() which automatically converts the arguments. universe@318: * universe@318: * @param string the string to check universe@318: * @param suffix the suffix the string should have universe@318: * @return 1, if and only if the string has the specified suffix, 0 otherwise universe@318: */ universe@319: #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix)) universe@146: universe@210: /** universe@210: * Returns a lower case version of a string. universe@210: * universe@210: * This function creates a duplicate of the input string, first. See the universe@318: * documentation of scstrdup() for the implications. universe@210: * universe@210: * @param string the input string universe@210: * @return the resulting lower case string universe@318: * @see scstrdup() universe@210: */ universe@319: sstr_t scstrlower(scstr_t string); olaf@275: universe@318: /** universe@320: * Alias for scstrlower() which automatically converts the argument. universe@318: * universe@318: * @param string the input string universe@318: * @return the resulting lower case string universe@318: */ universe@319: #define sstrlower(string) scstrlower(SCSTR(string)) universe@210: universe@210: /** universe@210: * Returns a lower case version of a string. universe@210: * universe@210: * This function creates a duplicate of the input string, first. See the universe@318: * documentation of scstrdup_a() for the implications. universe@210: * universe@210: * @param allocator the allocator used for duplicating the string universe@210: * @param string the input string universe@210: * @return the resulting lower case string universe@318: * @see scstrdup_a() universe@210: */ universe@319: sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string); olaf@275: universe@318: universe@318: /** universe@320: * Alias for scstrlower_a() which automatically converts the argument. universe@318: * universe@318: * @param allocator the allocator used for duplicating the string universe@318: * @param string the input string universe@318: * @return the resulting lower case string universe@318: */ universe@319: #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string)) universe@210: universe@210: /** universe@210: * Returns a upper case version of a string. universe@210: * universe@210: * This function creates a duplicate of the input string, first. See the universe@318: * documentation of scstrdup() for the implications. universe@210: * universe@210: * @param string the input string universe@210: * @return the resulting upper case string universe@318: * @see scstrdup() universe@210: */ universe@319: sstr_t scstrupper(scstr_t string); olaf@275: universe@318: /** universe@320: * Alias for scstrupper() which automatically converts the argument. universe@318: * universe@318: * @param string the input string universe@318: * @return the resulting upper case string universe@318: */ universe@319: #define sstrupper(string) scstrupper(SCSTR(string)) universe@210: universe@210: /** universe@210: * Returns a upper case version of a string. universe@210: * universe@210: * This function creates a duplicate of the input string, first. See the universe@318: * documentation of scstrdup_a() for the implications. universe@210: * universe@210: * @param allocator the allocator used for duplicating the string universe@210: * @param string the input string universe@210: * @return the resulting upper case string universe@318: * @see scstrdup_a() universe@210: */ universe@319: sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string); olaf@275: universe@318: /** universe@320: * Alias for scstrupper_a() which automatically converts the argument. universe@318: * universe@318: * @param allocator the allocator used for duplicating the string universe@318: * @param string the input string universe@318: * @return the resulting upper case string universe@318: */ universe@319: #define sstrupper_a(allocator, string) scstrupper_a(allocator, string) universe@210: olaf@20: #ifdef __cplusplus olaf@20: } olaf@20: #endif olaf@20: universe@116: #endif /* UCX_STRING_H */