src/ucx/string.h

Mon, 14 May 2018 18:27:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:27:23 +0200
changeset 315
5b97de37aada
parent 306
90b6d69bb499
child 316
be0f6bd10b52
permissions
-rw-r--r--

finally removes the underscore of ugliness from ucx_str_cmp() and ucx_str_casecmp()

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
universe@283 61 /** Expands a sstr_t to printf arguments. */
universe@283 62 #define SFMT(s) (int) (s).length, (s).ptr
universe@283 63
universe@283 64 /** Format specifier for a sstr_t. */
universe@283 65 #define PRIsstr ".*s"
universe@283 66
olaf@20 67 #ifdef __cplusplus
olaf@20 68 extern "C" {
olaf@20 69 #endif
universe@116 70 /**
universe@116 71 * The UCX string structure.
universe@116 72 */
universe@116 73 typedef struct {
universe@116 74 /** A reference to the string (<b>not necessarily <code>NULL</code>
universe@116 75 * -terminated</b>) */
olaf@20 76 char *ptr;
universe@116 77 /** The length of the string */
olaf@20 78 size_t length;
olaf@20 79 } sstr_t;
olaf@20 80
olaf@275 81 typedef struct {
olaf@275 82 const char *ptr;
olaf@275 83 size_t length;
olaf@275 84 } scstr_t;
olaf@288 85
olaf@275 86 #ifdef __cplusplus
olaf@275 87 }
olaf@275 88 #endif
olaf@275 89
olaf@275 90
olaf@275 91 #ifdef __cplusplus
olaf@275 92 inline scstr_t s2scstr(sstr_t s) {
olaf@275 93 scstr_t c;
olaf@275 94 c.ptr = s.ptr;
olaf@275 95 c.length = s.ptr;
olaf@275 96 return c;
olaf@275 97 }
olaf@275 98 inline scstr_t s2scstr(scstr_t c) {
olaf@275 99 return c;
olaf@275 100 }
olaf@275 101 #define SCSTR s2scstr
olaf@275 102 #else
olaf@275 103
olaf@275 104 scstr_t ucx_sc2sc(scstr_t c);
olaf@275 105 scstr_t ucx_ss2sc(sstr_t str);
olaf@275 106 #if __STDC_VERSION__ >= 201112L
olaf@275 107 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
olaf@275 108 #elif defined(__GNUC__) || defined(__clang__)
olaf@275 109 #define SCSTR(str) __builtin_choose_expr( \
olaf@275 110 __builtin_types_compatible_p(typeof(str), sstr_t), \
olaf@275 111 ucx_ss2sc, \
olaf@275 112 ucx_sc2sc)(str)
olaf@275 113 #elif defined(__sun)
olaf@275 114 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
olaf@275 115 scstr_t ucx_tmp_var_c; \
olaf@275 116 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
olaf@275 117 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
olaf@275 118 ucx_tmp_var_c; })
olaf@275 119 #else
olaf@275 120 scstr_t ucx_ss2c_s();
olaf@275 121 #define SCSTR ucx_ss2c_s
olaf@275 122 #endif /* C11 feature test */
olaf@275 123
olaf@275 124 #endif /* C++ */
olaf@275 125
olaf@275 126 #ifdef __cplusplus
olaf@275 127 extern "C" {
olaf@275 128 #endif
olaf@275 129
olaf@275 130
universe@116 131 /**
universe@116 132 * Creates a new sstr_t based on a C string.
universe@116 133 *
universe@116 134 * The length is implicitly inferred by using a call to <code>strlen()</code>.
olaf@20 135 *
universe@116 136 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
universe@116 137 * do want a copy, use sstrdup() on the return value of this function.
universe@116 138 *
universe@116 139 * @param cstring the C string to wrap
universe@116 140 * @return a new sstr_t containing the C string
universe@116 141 *
universe@116 142 * @see sstrn()
olaf@20 143 */
universe@116 144 sstr_t sstr(char *cstring);
olaf@20 145
universe@116 146 /**
universe@116 147 * Creates a new sstr_t of the specified length based on a C string.
olaf@20 148 *
universe@116 149 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
universe@116 150 * do want a copy, use sstrdup() on the return value of this function.
universe@116 151 *
universe@116 152 * @param cstring the C string to wrap
universe@116 153 * @param length the length of the string
universe@116 154 * @return a new sstr_t containing the C string
universe@116 155 *
universe@116 156 * @see sstr()
universe@116 157 * @see S()
olaf@20 158 */
universe@116 159 sstr_t sstrn(char *cstring, size_t length);
olaf@20 160
olaf@20 161
olaf@275 162 scstr_t scstr(const char *cstring);
olaf@275 163 scstr_t scstrn(const char *cstring, size_t length);
olaf@275 164
universe@116 165 /**
universe@116 166 * Returns the cumulated length of all specified strings.
olaf@20 167 *
universe@116 168 * At least one string must be specified.
universe@116 169 *
universe@116 170 * <b>Attention:</b> if the count argument does not match the count of the
universe@116 171 * specified strings, the behavior is undefined.
universe@116 172 *
universe@116 173 * @param count the total number of specified strings (so at least 1)
universe@116 174 * @param string the first string
universe@116 175 * @param ... all other strings
universe@116 176 * @return the cumulated length of all strings
olaf@20 177 */
olaf@288 178 size_t ucx_strnlen(size_t count, ...);
olaf@288 179
olaf@288 180 #define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__)
olaf@20 181
universe@119 182 /**
olaf@183 183 * Concatenates two or more strings.
olaf@183 184 *
olaf@183 185 * The resulting string will be allocated by standard <code>malloc()</code>.
olaf@183 186 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
olaf@183 187 *
olaf@183 188 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
olaf@183 189 * terminated.
olaf@180 190 *
olaf@180 191 * @param count the total number of strings to concatenate
olaf@183 192 * @param s1 first string
olaf@183 193 * @param ... all remaining strings
olaf@180 194 * @return the concatenated string
olaf@180 195 */
olaf@288 196 sstr_t ucx_strcat(size_t count, scstr_t s1, ...);
olaf@288 197
olaf@288 198 #define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__)
olaf@183 199
olaf@183 200 /**
universe@225 201 * Concatenates two or more strings using a UcxAllocator.
olaf@183 202 *
olaf@183 203 * See sstrcat() for details.
olaf@183 204 *
olaf@183 205 * @param a the allocator to use
olaf@183 206 * @param count the total number of strings to concatenate
olaf@183 207 * @param s1 first string
olaf@183 208 * @param ... all remaining strings
olaf@183 209 * @return the concatenated string
olaf@183 210 */
olaf@288 211 sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
olaf@180 212
olaf@288 213 #define sstrcat_a(count, s1, ...) ucx_strcat_a(count, SCSTR(s1), __VA_ARGS__)
olaf@180 214
olaf@180 215 /**
universe@119 216 * Returns a substring starting at the specified location.
universe@119 217 *
universe@119 218 * <b>Attention:</b> the new string references the same memory area as the
universe@119 219 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@119 220 * Use sstrdup() to get a copy.
universe@119 221 *
universe@119 222 * @param string input string
universe@119 223 * @param start start location of the substring
universe@119 224 * @return a substring of <code>string</code> starting at <code>start</code>
universe@119 225 *
universe@119 226 * @see sstrsubsl()
universe@119 227 * @see sstrchr()
universe@119 228 */
universe@119 229 sstr_t sstrsubs(sstr_t string, size_t start);
universe@119 230
universe@119 231 /**
universe@119 232 * Returns a substring with a maximum length starting at the specified location.
universe@119 233 *
universe@119 234 * <b>Attention:</b> the new string references the same memory area as the
universe@119 235 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@119 236 * Use sstrdup() to get a copy.
universe@119 237 *
universe@119 238 * @param string input string
universe@119 239 * @param start start location of the substring
universe@119 240 * @param length the maximum length of the substring
universe@119 241 * @return a substring of <code>string</code> starting at <code>start</code>
universe@119 242 * with a maximum length of <code>length</code>
universe@119 243 *
universe@119 244 * @see sstrsubs()
universe@119 245 * @see sstrchr()
universe@119 246 */
universe@119 247 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
universe@119 248
olaf@300 249 scstr_t scstrsubs(scstr_t s, size_t start);
olaf@300 250 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
olaf@300 251
olaf@300 252
olaf@300 253 int ucx_strchr(const char *string, size_t length, int chr, size_t *pos);
olaf@300 254 int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos);
olaf@300 255
universe@119 256 /**
universe@119 257 * Returns a substring starting at the location of the first occurrence of the
universe@119 258 * specified character.
universe@119 259 *
universe@119 260 * If the string does not contain the character, an empty string is returned.
universe@119 261 *
universe@119 262 * @param string the string where to locate the character
universe@119 263 * @param chr the character to locate
universe@148 264 * @return a substring starting at the first location of <code>chr</code>
universe@119 265 *
universe@119 266 * @see sstrsubs()
universe@119 267 */
universe@119 268 sstr_t sstrchr(sstr_t string, int chr);
universe@119 269
universe@119 270 /**
universe@148 271 * Returns a substring starting at the location of the last occurrence of the
universe@148 272 * specified character.
universe@148 273 *
universe@148 274 * If the string does not contain the character, an empty string is returned.
universe@148 275 *
universe@148 276 * @param string the string where to locate the character
universe@148 277 * @param chr the character to locate
universe@148 278 * @return a substring starting at the last location of <code>chr</code>
universe@148 279 *
universe@148 280 * @see sstrsubs()
universe@148 281 */
universe@148 282 sstr_t sstrrchr(sstr_t string, int chr);
universe@148 283
olaf@276 284
olaf@300 285 scstr_t scstrchr(scstr_t string, int chr);
olaf@300 286 scstr_t scstrrchr(scstr_t string, int chr);
olaf@300 287
olaf@276 288 const char* ucx_strstr(
olaf@276 289 const char *str,
olaf@276 290 size_t length,
olaf@276 291 const char *match,
olaf@276 292 size_t matchlen,
olaf@276 293 size_t *newlen);
olaf@276 294
universe@148 295 /**
universe@214 296 * Returns a substring starting at the location of the first occurrence of the
universe@214 297 * specified string.
universe@214 298 *
universe@214 299 * If the string does not contain the other string, an empty string is returned.
universe@214 300 *
universe@214 301 * If <code>match</code> is an empty string, the complete <code>string</code> is
universe@214 302 * returned.
universe@214 303 *
universe@214 304 * @param string the string to be scanned
universe@214 305 * @param match string containing the sequence of characters to match
universe@214 306 * @return a substring starting at the first occurrence of
universe@214 307 * <code>match</code>, or an empty string, if the sequence is not
universe@214 308 * present in <code>string</code>
universe@214 309 */
olaf@276 310 sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
olaf@276 311 #define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
olaf@276 312
olaf@276 313 scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
olaf@276 314 #define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
universe@214 315
universe@214 316 /**
universe@119 317 * Splits a string into parts by using a delimiter string.
universe@119 318 *
universe@119 319 * This function will return <code>NULL</code>, if one of the following happens:
universe@119 320 * <ul>
universe@119 321 * <li>the string length is zero</li>
universe@119 322 * <li>the delimeter length is zero</li>
universe@119 323 * <li>the string equals the delimeter</li>
universe@119 324 * <li>memory allocation fails</li>
universe@119 325 * </ul>
universe@119 326 *
universe@119 327 * The integer referenced by <code>count</code> is used as input and determines
universe@160 328 * the maximum size of the resulting array, i.e. the maximum count of splits to
universe@119 329 * perform + 1.
universe@119 330 *
universe@119 331 * The integer referenced by <code>count</code> is also used as output and is
universe@119 332 * set to
universe@119 333 * <ul>
universe@119 334 * <li>-2, on memory allocation errors</li>
universe@119 335 * <li>-1, if either the string or the delimiter is an empty string</li>
universe@119 336 * <li>0, if the string equals the delimiter</li>
universe@119 337 * <li>1, if the string does not contain the delimiter</li>
universe@160 338 * <li>the count of array items, otherwise</li>
universe@119 339 * </ul>
universe@119 340 *
universe@119 341 * If the string starts with the delimiter, the first item of the resulting
universe@160 342 * array will be an empty string.
universe@119 343 *
universe@119 344 * If the string ends with the delimiter and the maximum list size is not
universe@160 345 * exceeded, the last array item will be an empty string.
universe@233 346 * In case the list size would be exceeded, the last array item will be the
universe@233 347 * remaining string after the last split, <i>including</i> the terminating
universe@233 348 * delimiter.
universe@119 349 *
universe@160 350 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
universe@125 351 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
universe@119 352 * an allocator to managed memory, to avoid this.
olaf@20 353 *
universe@119 354 * @param string the string to split
universe@119 355 * @param delim the delimiter string
universe@160 356 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 357 * OUT: the actual size of the array
universe@160 358 * @return a sstr_t array containing the split strings or
universe@119 359 * <code>NULL</code> on error
universe@119 360 *
universe@125 361 * @see sstrsplit_a()
olaf@20 362 */
olaf@276 363 sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
olaf@276 364
olaf@276 365 #define sstrsplit(s, delim, count) ucx_strsplit(SCSTR(s), SCSTR(delim), count)
olaf@20 366
universe@119 367 /**
universe@225 368 * Performing sstrsplit() using a UcxAllocator.
universe@119 369 *
universe@119 370 * <i>Read the description of sstrsplit() for details.</i>
universe@119 371 *
universe@160 372 * The memory for the sstr_t.ptr pointers of the array items and the memory for
universe@119 373 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
universe@119 374 * function.
universe@119 375 *
universe@119 376 * <b>Note:</b> the allocator is not used for memory that is freed within the
universe@119 377 * same call of this function (locally scoped variables).
universe@119 378 *
universe@125 379 * @param allocator the UcxAllocator used for allocating memory
universe@119 380 * @param string the string to split
universe@119 381 * @param delim the delimiter string
universe@160 382 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 383 * OUT: the actual size of the array
universe@160 384 * @return a sstr_t array containing the split strings or
universe@119 385 * <code>NULL</code> on error
universe@119 386 *
universe@119 387 * @see sstrsplit()
olaf@20 388 */
olaf@276 389 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
universe@173 390 ssize_t *count);
olaf@20 391
olaf@276 392 #define sstrsplit_a(a, s, d, c) ucx_strsplit_a(a, SCSTR(s), SCSTR(d, c))
olaf@276 393
universe@116 394 /**
universe@116 395 * Compares two UCX strings with standard <code>memcmp()</code>.
universe@116 396 *
universe@116 397 * At first it compares the sstr_t.length attribute of the two strings. The
universe@116 398 * <code>memcmp()</code> function is called, if and only if the lengths match.
universe@116 399 *
universe@116 400 * @param s1 the first string
universe@116 401 * @param s2 the second string
universe@116 402 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@116 403 * length of s1 is greater than the length of s2 or the result of
universe@116 404 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
universe@116 405 */
universe@315 406 int ucx_strcmp(scstr_t s1, scstr_t s2);
olaf@276 407
universe@315 408 #define sstrcmp(s1, s2) ucx_strcmp(SCSTR(s1), SCSTR(s2))
olaf@20 409
universe@116 410 /**
universe@149 411 * Compares two UCX strings ignoring the case.
universe@149 412 *
universe@149 413 * At first it compares the sstr_t.length attribute of the two strings. If and
universe@149 414 * only if the lengths match, both strings are compared char by char ignoring
universe@149 415 * the case.
universe@149 416 *
universe@149 417 * @param s1 the first string
universe@149 418 * @param s2 the second string
universe@149 419 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@149 420 * length of s1 is greater than the length of s2 or the difference between the
universe@149 421 * first two differing characters otherwise (i.e. 0 if the strings match and
universe@149 422 * no characters differ)
universe@149 423 */
universe@315 424 int ucx_strcasecmp(scstr_t s1, scstr_t s2);
olaf@276 425
universe@315 426 #define sstrcasecmp(s1, s2) ucx_strcasecmp(SCSTR(s1), SCSTR(s2))
universe@149 427
universe@149 428 /**
universe@116 429 * Creates a duplicate of the specified string.
universe@116 430 *
universe@116 431 * The new sstr_t will contain a copy allocated by standard
universe@116 432 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
universe@116 433 * <code>free()</code>.
universe@116 434 *
universe@118 435 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@118 436 * terminated.
universe@118 437 *
universe@116 438 * @param string the string to duplicate
universe@118 439 * @return a duplicate of the string
universe@125 440 * @see sstrdup_a()
universe@116 441 */
olaf@275 442 sstr_t scstrdup(scstr_t string);
olaf@275 443
olaf@275 444 #define sstrdup(s) scstrdup(SCSTR(s))
olaf@20 445
universe@118 446 /**
universe@225 447 * Creates a duplicate of the specified string using a UcxAllocator.
universe@118 448 *
universe@118 449 * The new sstr_t will contain a copy allocated by the allocators
universe@118 450 * ucx_allocator_malloc function. So it is implementation depended, whether the
universe@118 451 * returned sstr_t.ptr pointer must be passed to the allocators
universe@118 452 * ucx_allocator_free function manually.
universe@118 453 *
universe@118 454 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@118 455 * terminated.
universe@118 456 *
universe@225 457 * @param allocator a valid instance of a UcxAllocator
universe@118 458 * @param string the string to duplicate
universe@118 459 * @return a duplicate of the string
universe@119 460 * @see sstrdup()
universe@118 461 */
olaf@275 462 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
olaf@275 463
olaf@275 464 #define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
universe@118 465
olaf@276 466
olaf@276 467 size_t ucx_strtrim(const char *str, size_t length, size_t *newlen);
olaf@276 468
universe@118 469 /**
universe@118 470 * Omits leading and trailing spaces.
universe@118 471 *
universe@118 472 * This function returns a new sstr_t containing a trimmed version of the
universe@118 473 * specified string.
universe@118 474 *
universe@118 475 * <b>Note:</b> the new sstr_t references the same memory, thus you
universe@118 476 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
universe@118 477 * <code>free()</code>. It is also highly recommended to avoid assignments like
universe@118 478 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
universe@118 479 * source string. Assignments of this type are only permitted, if the
universe@118 480 * sstr_t.ptr of the source string does not need to be freed or if another
universe@118 481 * reference to the source string exists.
universe@118 482 *
universe@118 483 * @param string the string that shall be trimmed
universe@118 484 * @return a new sstr_t containing the trimmed string
universe@118 485 */
olaf@96 486 sstr_t sstrtrim(sstr_t string);
olaf@96 487
olaf@276 488 scstr_t scstrtrim(scstr_t string);
olaf@276 489
universe@146 490 /**
universe@146 491 * Checks, if a string has a specific prefix.
universe@146 492 * @param string the string to check
universe@146 493 * @param prefix the prefix the string should have
universe@146 494 * @return 1, if and only if the string has the specified prefix, 0 otherwise
universe@146 495 */
olaf@275 496 int ucx_strprefix(scstr_t string, scstr_t prefix);
olaf@275 497
olaf@275 498 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
universe@146 499
universe@146 500 /**
universe@146 501 * Checks, if a string has a specific suffix.
universe@146 502 * @param string the string to check
universe@146 503 * @param suffix the suffix the string should have
universe@146 504 * @return 1, if and only if the string has the specified suffix, 0 otherwise
universe@146 505 */
olaf@275 506 int ucx_strsuffix(scstr_t string, scstr_t suffix);
olaf@275 507
olaf@275 508 #define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
universe@146 509
universe@210 510 /**
universe@210 511 * Returns a lower case version of a string.
universe@210 512 *
universe@210 513 * This function creates a duplicate of the input string, first. See the
universe@210 514 * documentation of sstrdup() for the implications.
universe@210 515 *
universe@210 516 * @param string the input string
universe@210 517 * @return the resulting lower case string
universe@210 518 * @see sstrdup()
universe@210 519 */
olaf@275 520 sstr_t ucx_strlower(scstr_t string);
olaf@275 521
olaf@275 522 #define sstrlower(string) ucx_strlower(SCSTR(string))
universe@210 523
universe@210 524 /**
universe@210 525 * Returns a lower case version of a string.
universe@210 526 *
universe@210 527 * This function creates a duplicate of the input string, first. See the
universe@210 528 * documentation of sstrdup_a() for the implications.
universe@210 529 *
universe@210 530 * @param allocator the allocator used for duplicating the string
universe@210 531 * @param string the input string
universe@210 532 * @return the resulting lower case string
universe@210 533 * @see sstrdup_a()
universe@210 534 */
olaf@275 535 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
olaf@275 536
olaf@275 537 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
universe@210 538
universe@210 539 /**
universe@210 540 * Returns a upper case version of a string.
universe@210 541 *
universe@210 542 * This function creates a duplicate of the input string, first. See the
universe@210 543 * documentation of sstrdup() for the implications.
universe@210 544 *
universe@210 545 * @param string the input string
universe@210 546 * @return the resulting upper case string
universe@210 547 * @see sstrdup()
universe@210 548 */
olaf@275 549 sstr_t ucx_strupper(scstr_t string);
olaf@275 550
olaf@275 551 #define sstrupper(string) ucx_strupper(SCSTR(string))
universe@210 552
universe@210 553 /**
universe@210 554 * Returns a upper case version of a string.
universe@210 555 *
universe@210 556 * This function creates a duplicate of the input string, first. See the
universe@210 557 * documentation of sstrdup_a() for the implications.
universe@210 558 *
universe@210 559 * @param allocator the allocator used for duplicating the string
universe@210 560 * @param string the input string
universe@210 561 * @return the resulting upper case string
universe@210 562 * @see sstrdup_a()
universe@210 563 */
olaf@275 564 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
olaf@275 565
olaf@275 566 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
universe@210 567
olaf@20 568 #ifdef __cplusplus
olaf@20 569 }
olaf@20 570 #endif
olaf@20 571
universe@116 572 #endif /* UCX_STRING_H */

mercurial