Sun, 11 Mar 2018 13:43:07 +0100
adds scstr_t struct for const strings and adapts some string functions
olaf@20 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@20 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@20 | 27 | */ |
universe@116 | 28 | /** |
universe@116 | 29 | * Bounded string implementation. |
universe@116 | 30 | * |
universe@116 | 31 | * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings. |
universe@116 | 32 | * The main difference to C strings is, that <code>sstr_t</code> does <b>not |
universe@116 | 33 | * need to be <code>NULL</code>-terminated</b>. Instead the length is stored |
universe@116 | 34 | * within the structure. |
universe@116 | 35 | * |
universe@116 | 36 | * When using <code>sstr_t</code>, developers must be full aware of what type |
universe@116 | 37 | * of string (<code>NULL</code>-terminated) or not) they are using, when |
universe@116 | 38 | * accessing the <code>char* ptr</code> directly. |
universe@116 | 39 | * |
universe@116 | 40 | * The UCX string module provides some common string functions, known from |
universe@116 | 41 | * standard libc, working with <code>sstr_t</code>. |
universe@116 | 42 | * |
universe@116 | 43 | * @file string.h |
universe@116 | 44 | * @author Mike Becker |
universe@116 | 45 | * @author Olaf Wintermann |
universe@116 | 46 | */ |
olaf@20 | 47 | |
universe@116 | 48 | #ifndef UCX_STRING_H |
universe@116 | 49 | #define UCX_STRING_H |
olaf@20 | 50 | |
universe@259 | 51 | #include "ucx.h" |
universe@259 | 52 | #include "allocator.h" |
universe@38 | 53 | #include <stddef.h> |
universe@38 | 54 | |
universe@116 | 55 | /** Shortcut for a <code>sstr_t struct</code> literal. */ |
universe@116 | 56 | #define ST(s) { (char*)s, sizeof(s)-1 } |
universe@146 | 57 | |
universe@116 | 58 | /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */ |
universe@116 | 59 | #define S(s) sstrn((char*)s, sizeof(s)-1) |
olaf@20 | 60 | |
olaf@20 | 61 | #ifdef __cplusplus |
olaf@20 | 62 | extern "C" { |
olaf@20 | 63 | #endif |
universe@116 | 64 | /** |
universe@116 | 65 | * The UCX string structure. |
universe@116 | 66 | */ |
universe@116 | 67 | typedef struct { |
universe@116 | 68 | /** A reference to the string (<b>not necessarily <code>NULL</code> |
universe@116 | 69 | * -terminated</b>) */ |
olaf@20 | 70 | char *ptr; |
universe@116 | 71 | /** The length of the string */ |
olaf@20 | 72 | size_t length; |
olaf@20 | 73 | } sstr_t; |
olaf@20 | 74 | |
olaf@275 | 75 | typedef struct { |
olaf@275 | 76 | const char *ptr; |
olaf@275 | 77 | size_t length; |
olaf@275 | 78 | } scstr_t; |
olaf@275 | 79 | #ifdef __cplusplus |
olaf@275 | 80 | } |
olaf@275 | 81 | #endif |
olaf@275 | 82 | |
olaf@275 | 83 | |
olaf@275 | 84 | #ifdef __cplusplus |
olaf@275 | 85 | inline scstr_t s2scstr(sstr_t s) { |
olaf@275 | 86 | scstr_t c; |
olaf@275 | 87 | c.ptr = s.ptr; |
olaf@275 | 88 | c.length = s.ptr; |
olaf@275 | 89 | return c; |
olaf@275 | 90 | } |
olaf@275 | 91 | inline scstr_t s2scstr(scstr_t c) { |
olaf@275 | 92 | return c; |
olaf@275 | 93 | } |
olaf@275 | 94 | #define SCSTR s2scstr |
olaf@275 | 95 | #else |
olaf@275 | 96 | |
olaf@275 | 97 | scstr_t ucx_sc2sc(scstr_t c); |
olaf@275 | 98 | scstr_t ucx_ss2sc(sstr_t str); |
olaf@275 | 99 | #if __STDC_VERSION__ >= 201112L |
olaf@275 | 100 | #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str) |
olaf@275 | 101 | #elif defined(__GNUC__) || defined(__clang__) |
olaf@275 | 102 | #define SCSTR(str) __builtin_choose_expr( \ |
olaf@275 | 103 | __builtin_types_compatible_p(typeof(str), sstr_t), \ |
olaf@275 | 104 | ucx_ss2sc, \ |
olaf@275 | 105 | ucx_sc2sc)(str) |
olaf@275 | 106 | #elif defined(__sun) |
olaf@275 | 107 | #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \ |
olaf@275 | 108 | scstr_t ucx_tmp_var_c; \ |
olaf@275 | 109 | ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\ |
olaf@275 | 110 | ucx_tmp_var_c.length = ucx_tmp_var_str.length;\ |
olaf@275 | 111 | ucx_tmp_var_c; }) |
olaf@275 | 112 | #else |
olaf@275 | 113 | scstr_t ucx_ss2c_s(); |
olaf@275 | 114 | #define SCSTR ucx_ss2c_s |
olaf@275 | 115 | #endif /* C11 feature test */ |
olaf@275 | 116 | |
olaf@275 | 117 | #endif /* C++ */ |
olaf@275 | 118 | |
olaf@275 | 119 | #ifdef __cplusplus |
olaf@275 | 120 | extern "C" { |
olaf@275 | 121 | #endif |
olaf@275 | 122 | |
olaf@275 | 123 | |
universe@116 | 124 | /** |
universe@116 | 125 | * Creates a new sstr_t based on a C string. |
universe@116 | 126 | * |
universe@116 | 127 | * The length is implicitly inferred by using a call to <code>strlen()</code>. |
olaf@20 | 128 | * |
universe@116 | 129 | * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you |
universe@116 | 130 | * do want a copy, use sstrdup() on the return value of this function. |
universe@116 | 131 | * |
universe@116 | 132 | * @param cstring the C string to wrap |
universe@116 | 133 | * @return a new sstr_t containing the C string |
universe@116 | 134 | * |
universe@116 | 135 | * @see sstrn() |
olaf@20 | 136 | */ |
universe@116 | 137 | sstr_t sstr(char *cstring); |
olaf@20 | 138 | |
universe@116 | 139 | /** |
universe@116 | 140 | * Creates a new sstr_t of the specified length based on a C string. |
olaf@20 | 141 | * |
universe@116 | 142 | * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you |
universe@116 | 143 | * do want a copy, use sstrdup() on the return value of this function. |
universe@116 | 144 | * |
universe@116 | 145 | * @param cstring the C string to wrap |
universe@116 | 146 | * @param length the length of the string |
universe@116 | 147 | * @return a new sstr_t containing the C string |
universe@116 | 148 | * |
universe@116 | 149 | * @see sstr() |
universe@116 | 150 | * @see S() |
olaf@20 | 151 | */ |
universe@116 | 152 | sstr_t sstrn(char *cstring, size_t length); |
olaf@20 | 153 | |
olaf@20 | 154 | |
olaf@275 | 155 | scstr_t scstr(const char *cstring); |
olaf@275 | 156 | scstr_t scstrn(const char *cstring, size_t length); |
olaf@275 | 157 | |
universe@116 | 158 | /** |
universe@116 | 159 | * Returns the cumulated length of all specified strings. |
olaf@20 | 160 | * |
universe@116 | 161 | * At least one string must be specified. |
universe@116 | 162 | * |
universe@116 | 163 | * <b>Attention:</b> if the count argument does not match the count of the |
universe@116 | 164 | * specified strings, the behavior is undefined. |
universe@116 | 165 | * |
universe@116 | 166 | * @param count the total number of specified strings (so at least 1) |
universe@116 | 167 | * @param string the first string |
universe@116 | 168 | * @param ... all other strings |
universe@116 | 169 | * @return the cumulated length of all strings |
olaf@20 | 170 | */ |
universe@116 | 171 | size_t sstrnlen(size_t count, sstr_t string, ...); |
olaf@20 | 172 | |
universe@119 | 173 | /** |
olaf@183 | 174 | * Concatenates two or more strings. |
olaf@183 | 175 | * |
olaf@183 | 176 | * The resulting string will be allocated by standard <code>malloc()</code>. |
olaf@183 | 177 | * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>. |
olaf@183 | 178 | * |
olaf@183 | 179 | * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- |
olaf@183 | 180 | * terminated. |
olaf@180 | 181 | * |
olaf@180 | 182 | * @param count the total number of strings to concatenate |
olaf@183 | 183 | * @param s1 first string |
olaf@183 | 184 | * @param s2 second string |
olaf@183 | 185 | * @param ... all remaining strings |
olaf@180 | 186 | * @return the concatenated string |
olaf@180 | 187 | */ |
olaf@180 | 188 | sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); |
olaf@183 | 189 | |
olaf@183 | 190 | /** |
universe@225 | 191 | * Concatenates two or more strings using a UcxAllocator. |
olaf@183 | 192 | * |
olaf@183 | 193 | * See sstrcat() for details. |
olaf@183 | 194 | * |
olaf@183 | 195 | * @param a the allocator to use |
olaf@183 | 196 | * @param count the total number of strings to concatenate |
olaf@183 | 197 | * @param s1 first string |
olaf@183 | 198 | * @param s2 second string |
olaf@183 | 199 | * @param ... all remaining strings |
olaf@183 | 200 | * @return the concatenated string |
olaf@183 | 201 | */ |
olaf@180 | 202 | sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); |
olaf@180 | 203 | |
olaf@180 | 204 | |
olaf@180 | 205 | /** |
universe@119 | 206 | * Returns a substring starting at the specified location. |
universe@119 | 207 | * |
universe@119 | 208 | * <b>Attention:</b> the new string references the same memory area as the |
universe@119 | 209 | * input string and will <b>NOT</b> be <code>NULL</code>-terminated. |
universe@119 | 210 | * Use sstrdup() to get a copy. |
universe@119 | 211 | * |
universe@119 | 212 | * @param string input string |
universe@119 | 213 | * @param start start location of the substring |
universe@119 | 214 | * @return a substring of <code>string</code> starting at <code>start</code> |
universe@119 | 215 | * |
universe@119 | 216 | * @see sstrsubsl() |
universe@119 | 217 | * @see sstrchr() |
universe@119 | 218 | */ |
universe@119 | 219 | sstr_t sstrsubs(sstr_t string, size_t start); |
universe@119 | 220 | |
universe@119 | 221 | /** |
universe@119 | 222 | * Returns a substring with a maximum length starting at the specified location. |
universe@119 | 223 | * |
universe@119 | 224 | * <b>Attention:</b> the new string references the same memory area as the |
universe@119 | 225 | * input string and will <b>NOT</b> be <code>NULL</code>-terminated. |
universe@119 | 226 | * Use sstrdup() to get a copy. |
universe@119 | 227 | * |
universe@119 | 228 | * @param string input string |
universe@119 | 229 | * @param start start location of the substring |
universe@119 | 230 | * @param length the maximum length of the substring |
universe@119 | 231 | * @return a substring of <code>string</code> starting at <code>start</code> |
universe@119 | 232 | * with a maximum length of <code>length</code> |
universe@119 | 233 | * |
universe@119 | 234 | * @see sstrsubs() |
universe@119 | 235 | * @see sstrchr() |
universe@119 | 236 | */ |
universe@119 | 237 | sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); |
universe@119 | 238 | |
universe@119 | 239 | /** |
universe@119 | 240 | * Returns a substring starting at the location of the first occurrence of the |
universe@119 | 241 | * specified character. |
universe@119 | 242 | * |
universe@119 | 243 | * If the string does not contain the character, an empty string is returned. |
universe@119 | 244 | * |
universe@119 | 245 | * @param string the string where to locate the character |
universe@119 | 246 | * @param chr the character to locate |
universe@148 | 247 | * @return a substring starting at the first location of <code>chr</code> |
universe@119 | 248 | * |
universe@119 | 249 | * @see sstrsubs() |
universe@119 | 250 | */ |
universe@119 | 251 | sstr_t sstrchr(sstr_t string, int chr); |
universe@119 | 252 | |
universe@119 | 253 | /** |
universe@148 | 254 | * Returns a substring starting at the location of the last occurrence of the |
universe@148 | 255 | * specified character. |
universe@148 | 256 | * |
universe@148 | 257 | * If the string does not contain the character, an empty string is returned. |
universe@148 | 258 | * |
universe@148 | 259 | * @param string the string where to locate the character |
universe@148 | 260 | * @param chr the character to locate |
universe@148 | 261 | * @return a substring starting at the last location of <code>chr</code> |
universe@148 | 262 | * |
universe@148 | 263 | * @see sstrsubs() |
universe@148 | 264 | */ |
universe@148 | 265 | sstr_t sstrrchr(sstr_t string, int chr); |
universe@148 | 266 | |
universe@148 | 267 | /** |
universe@214 | 268 | * Returns a substring starting at the location of the first occurrence of the |
universe@214 | 269 | * specified string. |
universe@214 | 270 | * |
universe@214 | 271 | * If the string does not contain the other string, an empty string is returned. |
universe@214 | 272 | * |
universe@214 | 273 | * If <code>match</code> is an empty string, the complete <code>string</code> is |
universe@214 | 274 | * returned. |
universe@214 | 275 | * |
universe@214 | 276 | * @param string the string to be scanned |
universe@214 | 277 | * @param match string containing the sequence of characters to match |
universe@214 | 278 | * @return a substring starting at the first occurrence of |
universe@214 | 279 | * <code>match</code>, or an empty string, if the sequence is not |
universe@214 | 280 | * present in <code>string</code> |
universe@214 | 281 | */ |
universe@214 | 282 | sstr_t sstrstr(sstr_t string, sstr_t match); |
universe@214 | 283 | |
universe@214 | 284 | /** |
universe@119 | 285 | * Splits a string into parts by using a delimiter string. |
universe@119 | 286 | * |
universe@119 | 287 | * This function will return <code>NULL</code>, if one of the following happens: |
universe@119 | 288 | * <ul> |
universe@119 | 289 | * <li>the string length is zero</li> |
universe@119 | 290 | * <li>the delimeter length is zero</li> |
universe@119 | 291 | * <li>the string equals the delimeter</li> |
universe@119 | 292 | * <li>memory allocation fails</li> |
universe@119 | 293 | * </ul> |
universe@119 | 294 | * |
universe@119 | 295 | * The integer referenced by <code>count</code> is used as input and determines |
universe@160 | 296 | * the maximum size of the resulting array, i.e. the maximum count of splits to |
universe@119 | 297 | * perform + 1. |
universe@119 | 298 | * |
universe@119 | 299 | * The integer referenced by <code>count</code> is also used as output and is |
universe@119 | 300 | * set to |
universe@119 | 301 | * <ul> |
universe@119 | 302 | * <li>-2, on memory allocation errors</li> |
universe@119 | 303 | * <li>-1, if either the string or the delimiter is an empty string</li> |
universe@119 | 304 | * <li>0, if the string equals the delimiter</li> |
universe@119 | 305 | * <li>1, if the string does not contain the delimiter</li> |
universe@160 | 306 | * <li>the count of array items, otherwise</li> |
universe@119 | 307 | * </ul> |
universe@119 | 308 | * |
universe@119 | 309 | * If the string starts with the delimiter, the first item of the resulting |
universe@160 | 310 | * array will be an empty string. |
universe@119 | 311 | * |
universe@119 | 312 | * If the string ends with the delimiter and the maximum list size is not |
universe@160 | 313 | * exceeded, the last array item will be an empty string. |
universe@233 | 314 | * In case the list size would be exceeded, the last array item will be the |
universe@233 | 315 | * remaining string after the last split, <i>including</i> the terminating |
universe@233 | 316 | * delimiter. |
universe@119 | 317 | * |
universe@160 | 318 | * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array |
universe@125 | 319 | * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with |
universe@119 | 320 | * an allocator to managed memory, to avoid this. |
olaf@20 | 321 | * |
universe@119 | 322 | * @param string the string to split |
universe@119 | 323 | * @param delim the delimiter string |
universe@160 | 324 | * @param count IN: the maximum size of the resulting array (0 = no limit), |
universe@160 | 325 | * OUT: the actual size of the array |
universe@160 | 326 | * @return a sstr_t array containing the split strings or |
universe@119 | 327 | * <code>NULL</code> on error |
universe@119 | 328 | * |
universe@125 | 329 | * @see sstrsplit_a() |
olaf@20 | 330 | */ |
universe@173 | 331 | sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); |
olaf@20 | 332 | |
universe@119 | 333 | /** |
universe@225 | 334 | * Performing sstrsplit() using a UcxAllocator. |
universe@119 | 335 | * |
universe@119 | 336 | * <i>Read the description of sstrsplit() for details.</i> |
universe@119 | 337 | * |
universe@160 | 338 | * The memory for the sstr_t.ptr pointers of the array items and the memory for |
universe@119 | 339 | * the sstr_t array itself are allocated by using the UcxAllocator.malloc() |
universe@119 | 340 | * function. |
universe@119 | 341 | * |
universe@119 | 342 | * <b>Note:</b> the allocator is not used for memory that is freed within the |
universe@119 | 343 | * same call of this function (locally scoped variables). |
universe@119 | 344 | * |
universe@125 | 345 | * @param allocator the UcxAllocator used for allocating memory |
universe@119 | 346 | * @param string the string to split |
universe@119 | 347 | * @param delim the delimiter string |
universe@160 | 348 | * @param count IN: the maximum size of the resulting array (0 = no limit), |
universe@160 | 349 | * OUT: the actual size of the array |
universe@160 | 350 | * @return a sstr_t array containing the split strings or |
universe@119 | 351 | * <code>NULL</code> on error |
universe@119 | 352 | * |
universe@119 | 353 | * @see sstrsplit() |
olaf@20 | 354 | */ |
universe@125 | 355 | sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, |
universe@173 | 356 | ssize_t *count); |
olaf@20 | 357 | |
universe@116 | 358 | /** |
universe@116 | 359 | * Compares two UCX strings with standard <code>memcmp()</code>. |
universe@116 | 360 | * |
universe@116 | 361 | * At first it compares the sstr_t.length attribute of the two strings. The |
universe@116 | 362 | * <code>memcmp()</code> function is called, if and only if the lengths match. |
universe@116 | 363 | * |
universe@116 | 364 | * @param s1 the first string |
universe@116 | 365 | * @param s2 the second string |
universe@116 | 366 | * @return -1, if the length of s1 is less than the length of s2 or 1, if the |
universe@116 | 367 | * length of s1 is greater than the length of s2 or the result of |
universe@116 | 368 | * <code>memcmp()</code> otherwise (i.e. 0 if the strings match) |
universe@116 | 369 | */ |
olaf@68 | 370 | int sstrcmp(sstr_t s1, sstr_t s2); |
olaf@20 | 371 | |
universe@116 | 372 | /** |
universe@149 | 373 | * Compares two UCX strings ignoring the case. |
universe@149 | 374 | * |
universe@149 | 375 | * At first it compares the sstr_t.length attribute of the two strings. If and |
universe@149 | 376 | * only if the lengths match, both strings are compared char by char ignoring |
universe@149 | 377 | * the case. |
universe@149 | 378 | * |
universe@149 | 379 | * @param s1 the first string |
universe@149 | 380 | * @param s2 the second string |
universe@149 | 381 | * @return -1, if the length of s1 is less than the length of s2 or 1, if the |
universe@149 | 382 | * length of s1 is greater than the length of s2 or the difference between the |
universe@149 | 383 | * first two differing characters otherwise (i.e. 0 if the strings match and |
universe@149 | 384 | * no characters differ) |
universe@149 | 385 | */ |
universe@149 | 386 | int sstrcasecmp(sstr_t s1, sstr_t s2); |
universe@149 | 387 | |
universe@149 | 388 | /** |
universe@116 | 389 | * Creates a duplicate of the specified string. |
universe@116 | 390 | * |
universe@116 | 391 | * The new sstr_t will contain a copy allocated by standard |
universe@116 | 392 | * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to |
universe@116 | 393 | * <code>free()</code>. |
universe@116 | 394 | * |
universe@118 | 395 | * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- |
universe@118 | 396 | * terminated. |
universe@118 | 397 | * |
universe@116 | 398 | * @param string the string to duplicate |
universe@118 | 399 | * @return a duplicate of the string |
universe@125 | 400 | * @see sstrdup_a() |
universe@116 | 401 | */ |
olaf@275 | 402 | sstr_t scstrdup(scstr_t string); |
olaf@275 | 403 | |
olaf@275 | 404 | #define sstrdup(s) scstrdup(SCSTR(s)) |
olaf@20 | 405 | |
universe@118 | 406 | /** |
universe@225 | 407 | * Creates a duplicate of the specified string using a UcxAllocator. |
universe@118 | 408 | * |
universe@118 | 409 | * The new sstr_t will contain a copy allocated by the allocators |
universe@118 | 410 | * ucx_allocator_malloc function. So it is implementation depended, whether the |
universe@118 | 411 | * returned sstr_t.ptr pointer must be passed to the allocators |
universe@118 | 412 | * ucx_allocator_free function manually. |
universe@118 | 413 | * |
universe@118 | 414 | * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- |
universe@118 | 415 | * terminated. |
universe@118 | 416 | * |
universe@225 | 417 | * @param allocator a valid instance of a UcxAllocator |
universe@118 | 418 | * @param string the string to duplicate |
universe@118 | 419 | * @return a duplicate of the string |
universe@119 | 420 | * @see sstrdup() |
universe@118 | 421 | */ |
olaf@275 | 422 | sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string); |
olaf@275 | 423 | |
olaf@275 | 424 | #define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s)) |
universe@118 | 425 | |
universe@118 | 426 | /** |
universe@118 | 427 | * Omits leading and trailing spaces. |
universe@118 | 428 | * |
universe@118 | 429 | * This function returns a new sstr_t containing a trimmed version of the |
universe@118 | 430 | * specified string. |
universe@118 | 431 | * |
universe@118 | 432 | * <b>Note:</b> the new sstr_t references the same memory, thus you |
universe@118 | 433 | * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to |
universe@118 | 434 | * <code>free()</code>. It is also highly recommended to avoid assignments like |
universe@118 | 435 | * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the |
universe@118 | 436 | * source string. Assignments of this type are only permitted, if the |
universe@118 | 437 | * sstr_t.ptr of the source string does not need to be freed or if another |
universe@118 | 438 | * reference to the source string exists. |
universe@118 | 439 | * |
universe@118 | 440 | * @param string the string that shall be trimmed |
universe@118 | 441 | * @return a new sstr_t containing the trimmed string |
universe@118 | 442 | */ |
olaf@96 | 443 | sstr_t sstrtrim(sstr_t string); |
olaf@96 | 444 | |
universe@146 | 445 | /** |
universe@146 | 446 | * Checks, if a string has a specific prefix. |
universe@146 | 447 | * @param string the string to check |
universe@146 | 448 | * @param prefix the prefix the string should have |
universe@146 | 449 | * @return 1, if and only if the string has the specified prefix, 0 otherwise |
universe@146 | 450 | */ |
olaf@275 | 451 | int ucx_strprefix(scstr_t string, scstr_t prefix); |
olaf@275 | 452 | |
olaf@275 | 453 | #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix)) |
universe@146 | 454 | |
universe@146 | 455 | /** |
universe@146 | 456 | * Checks, if a string has a specific suffix. |
universe@146 | 457 | * @param string the string to check |
universe@146 | 458 | * @param suffix the suffix the string should have |
universe@146 | 459 | * @return 1, if and only if the string has the specified suffix, 0 otherwise |
universe@146 | 460 | */ |
olaf@275 | 461 | int ucx_strsuffix(scstr_t string, scstr_t suffix); |
olaf@275 | 462 | |
olaf@275 | 463 | #define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix)) |
universe@146 | 464 | |
universe@210 | 465 | /** |
universe@210 | 466 | * Returns a lower case version of a string. |
universe@210 | 467 | * |
universe@210 | 468 | * This function creates a duplicate of the input string, first. See the |
universe@210 | 469 | * documentation of sstrdup() for the implications. |
universe@210 | 470 | * |
universe@210 | 471 | * @param string the input string |
universe@210 | 472 | * @return the resulting lower case string |
universe@210 | 473 | * @see sstrdup() |
universe@210 | 474 | */ |
olaf@275 | 475 | sstr_t ucx_strlower(scstr_t string); |
olaf@275 | 476 | |
olaf@275 | 477 | #define sstrlower(string) ucx_strlower(SCSTR(string)) |
universe@210 | 478 | |
universe@210 | 479 | /** |
universe@210 | 480 | * Returns a lower case version of a string. |
universe@210 | 481 | * |
universe@210 | 482 | * This function creates a duplicate of the input string, first. See the |
universe@210 | 483 | * documentation of sstrdup_a() for the implications. |
universe@210 | 484 | * |
universe@210 | 485 | * @param allocator the allocator used for duplicating the string |
universe@210 | 486 | * @param string the input string |
universe@210 | 487 | * @return the resulting lower case string |
universe@210 | 488 | * @see sstrdup_a() |
universe@210 | 489 | */ |
olaf@275 | 490 | sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string); |
olaf@275 | 491 | |
olaf@275 | 492 | #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string)) |
universe@210 | 493 | |
universe@210 | 494 | /** |
universe@210 | 495 | * Returns a upper case version of a string. |
universe@210 | 496 | * |
universe@210 | 497 | * This function creates a duplicate of the input string, first. See the |
universe@210 | 498 | * documentation of sstrdup() for the implications. |
universe@210 | 499 | * |
universe@210 | 500 | * @param string the input string |
universe@210 | 501 | * @return the resulting upper case string |
universe@210 | 502 | * @see sstrdup() |
universe@210 | 503 | */ |
olaf@275 | 504 | sstr_t ucx_strupper(scstr_t string); |
olaf@275 | 505 | |
olaf@275 | 506 | #define sstrupper(string) ucx_strupper(SCSTR(string)) |
universe@210 | 507 | |
universe@210 | 508 | /** |
universe@210 | 509 | * Returns a upper case version of a string. |
universe@210 | 510 | * |
universe@210 | 511 | * This function creates a duplicate of the input string, first. See the |
universe@210 | 512 | * documentation of sstrdup_a() for the implications. |
universe@210 | 513 | * |
universe@210 | 514 | * @param allocator the allocator used for duplicating the string |
universe@210 | 515 | * @param string the input string |
universe@210 | 516 | * @return the resulting upper case string |
universe@210 | 517 | * @see sstrdup_a() |
universe@210 | 518 | */ |
olaf@275 | 519 | sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string); |
olaf@275 | 520 | |
olaf@275 | 521 | #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string) |
universe@210 | 522 | |
olaf@20 | 523 | #ifdef __cplusplus |
olaf@20 | 524 | } |
olaf@20 | 525 | #endif |
olaf@20 | 526 | |
universe@116 | 527 | #endif /* UCX_STRING_H */ |