src/ucx/string.h

Sun, 01 Apr 2018 09:51:01 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Apr 2018 09:51:01 +0200
branch
constsstr
changeset 276
f1b2146d4805
parent 275
96f643d30ff1
child 288
6af5798342e8
permissions
-rw-r--r--

adapts sstrtrim, sstrsplit, sstrcmp and sstrstr to new const string API

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
olaf@276 267
olaf@276 268 const char* ucx_strstr(
olaf@276 269 const char *str,
olaf@276 270 size_t length,
olaf@276 271 const char *match,
olaf@276 272 size_t matchlen,
olaf@276 273 size_t *newlen);
olaf@276 274
universe@148 275 /**
universe@214 276 * Returns a substring starting at the location of the first occurrence of the
universe@214 277 * specified string.
universe@214 278 *
universe@214 279 * If the string does not contain the other string, an empty string is returned.
universe@214 280 *
universe@214 281 * If <code>match</code> is an empty string, the complete <code>string</code> is
universe@214 282 * returned.
universe@214 283 *
universe@214 284 * @param string the string to be scanned
universe@214 285 * @param match string containing the sequence of characters to match
universe@214 286 * @return a substring starting at the first occurrence of
universe@214 287 * <code>match</code>, or an empty string, if the sequence is not
universe@214 288 * present in <code>string</code>
universe@214 289 */
olaf@276 290 sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
olaf@276 291 #define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
olaf@276 292
olaf@276 293 scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
olaf@276 294 #define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
universe@214 295
universe@214 296 /**
universe@119 297 * Splits a string into parts by using a delimiter string.
universe@119 298 *
universe@119 299 * This function will return <code>NULL</code>, if one of the following happens:
universe@119 300 * <ul>
universe@119 301 * <li>the string length is zero</li>
universe@119 302 * <li>the delimeter length is zero</li>
universe@119 303 * <li>the string equals the delimeter</li>
universe@119 304 * <li>memory allocation fails</li>
universe@119 305 * </ul>
universe@119 306 *
universe@119 307 * The integer referenced by <code>count</code> is used as input and determines
universe@160 308 * the maximum size of the resulting array, i.e. the maximum count of splits to
universe@119 309 * perform + 1.
universe@119 310 *
universe@119 311 * The integer referenced by <code>count</code> is also used as output and is
universe@119 312 * set to
universe@119 313 * <ul>
universe@119 314 * <li>-2, on memory allocation errors</li>
universe@119 315 * <li>-1, if either the string or the delimiter is an empty string</li>
universe@119 316 * <li>0, if the string equals the delimiter</li>
universe@119 317 * <li>1, if the string does not contain the delimiter</li>
universe@160 318 * <li>the count of array items, otherwise</li>
universe@119 319 * </ul>
universe@119 320 *
universe@119 321 * If the string starts with the delimiter, the first item of the resulting
universe@160 322 * array will be an empty string.
universe@119 323 *
universe@119 324 * If the string ends with the delimiter and the maximum list size is not
universe@160 325 * exceeded, the last array item will be an empty string.
universe@233 326 * In case the list size would be exceeded, the last array item will be the
universe@233 327 * remaining string after the last split, <i>including</i> the terminating
universe@233 328 * delimiter.
universe@119 329 *
universe@160 330 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
universe@125 331 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
universe@119 332 * an allocator to managed memory, to avoid this.
olaf@20 333 *
universe@119 334 * @param string the string to split
universe@119 335 * @param delim the delimiter string
universe@160 336 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 337 * OUT: the actual size of the array
universe@160 338 * @return a sstr_t array containing the split strings or
universe@119 339 * <code>NULL</code> on error
universe@119 340 *
universe@125 341 * @see sstrsplit_a()
olaf@20 342 */
olaf@276 343 sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
olaf@276 344
olaf@276 345 #define sstrsplit(s, delim, count) ucx_strsplit(SCSTR(s), SCSTR(delim), count)
olaf@20 346
universe@119 347 /**
universe@225 348 * Performing sstrsplit() using a UcxAllocator.
universe@119 349 *
universe@119 350 * <i>Read the description of sstrsplit() for details.</i>
universe@119 351 *
universe@160 352 * The memory for the sstr_t.ptr pointers of the array items and the memory for
universe@119 353 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
universe@119 354 * function.
universe@119 355 *
universe@119 356 * <b>Note:</b> the allocator is not used for memory that is freed within the
universe@119 357 * same call of this function (locally scoped variables).
universe@119 358 *
universe@125 359 * @param allocator the UcxAllocator used for allocating memory
universe@119 360 * @param string the string to split
universe@119 361 * @param delim the delimiter string
universe@160 362 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 363 * OUT: the actual size of the array
universe@160 364 * @return a sstr_t array containing the split strings or
universe@119 365 * <code>NULL</code> on error
universe@119 366 *
universe@119 367 * @see sstrsplit()
olaf@20 368 */
olaf@276 369 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
universe@173 370 ssize_t *count);
olaf@20 371
olaf@276 372 #define sstrsplit_a(a, s, d, c) ucx_strsplit_a(a, SCSTR(s), SCSTR(d, c))
olaf@276 373
universe@116 374 /**
universe@116 375 * Compares two UCX strings with standard <code>memcmp()</code>.
universe@116 376 *
universe@116 377 * At first it compares the sstr_t.length attribute of the two strings. The
universe@116 378 * <code>memcmp()</code> function is called, if and only if the lengths match.
universe@116 379 *
universe@116 380 * @param s1 the first string
universe@116 381 * @param s2 the second string
universe@116 382 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@116 383 * length of s1 is greater than the length of s2 or the result of
universe@116 384 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
universe@116 385 */
olaf@276 386 int ucx_str_cmp(scstr_t s1, scstr_t s2);
olaf@276 387
olaf@276 388 #define sstrcmp(s1, s2) ucx_str_cmp(SCSTR(s1), SCSTR(s2))
olaf@20 389
universe@116 390 /**
universe@149 391 * Compares two UCX strings ignoring the case.
universe@149 392 *
universe@149 393 * At first it compares the sstr_t.length attribute of the two strings. If and
universe@149 394 * only if the lengths match, both strings are compared char by char ignoring
universe@149 395 * the case.
universe@149 396 *
universe@149 397 * @param s1 the first string
universe@149 398 * @param s2 the second string
universe@149 399 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@149 400 * length of s1 is greater than the length of s2 or the difference between the
universe@149 401 * first two differing characters otherwise (i.e. 0 if the strings match and
universe@149 402 * no characters differ)
universe@149 403 */
olaf@276 404 int ucx_str_casecmp(scstr_t s1, scstr_t s2);
olaf@276 405
olaf@276 406 #define sstrcasecmp(s1, s2) ucx_str_casecmp(SCSTR(s1), SCSTR(s2))
universe@149 407
universe@149 408 /**
universe@116 409 * Creates a duplicate of the specified string.
universe@116 410 *
universe@116 411 * The new sstr_t will contain a copy allocated by standard
universe@116 412 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
universe@116 413 * <code>free()</code>.
universe@116 414 *
universe@118 415 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@118 416 * terminated.
universe@118 417 *
universe@116 418 * @param string the string to duplicate
universe@118 419 * @return a duplicate of the string
universe@125 420 * @see sstrdup_a()
universe@116 421 */
olaf@275 422 sstr_t scstrdup(scstr_t string);
olaf@275 423
olaf@275 424 #define sstrdup(s) scstrdup(SCSTR(s))
olaf@20 425
universe@118 426 /**
universe@225 427 * Creates a duplicate of the specified string using a UcxAllocator.
universe@118 428 *
universe@118 429 * The new sstr_t will contain a copy allocated by the allocators
universe@118 430 * ucx_allocator_malloc function. So it is implementation depended, whether the
universe@118 431 * returned sstr_t.ptr pointer must be passed to the allocators
universe@118 432 * ucx_allocator_free function manually.
universe@118 433 *
universe@118 434 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@118 435 * terminated.
universe@118 436 *
universe@225 437 * @param allocator a valid instance of a UcxAllocator
universe@118 438 * @param string the string to duplicate
universe@118 439 * @return a duplicate of the string
universe@119 440 * @see sstrdup()
universe@118 441 */
olaf@275 442 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
olaf@275 443
olaf@275 444 #define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
universe@118 445
olaf@276 446
olaf@276 447 size_t ucx_strtrim(const char *str, size_t length, size_t *newlen);
olaf@276 448
universe@118 449 /**
universe@118 450 * Omits leading and trailing spaces.
universe@118 451 *
universe@118 452 * This function returns a new sstr_t containing a trimmed version of the
universe@118 453 * specified string.
universe@118 454 *
universe@118 455 * <b>Note:</b> the new sstr_t references the same memory, thus you
universe@118 456 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
universe@118 457 * <code>free()</code>. It is also highly recommended to avoid assignments like
universe@118 458 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
universe@118 459 * source string. Assignments of this type are only permitted, if the
universe@118 460 * sstr_t.ptr of the source string does not need to be freed or if another
universe@118 461 * reference to the source string exists.
universe@118 462 *
universe@118 463 * @param string the string that shall be trimmed
universe@118 464 * @return a new sstr_t containing the trimmed string
universe@118 465 */
olaf@96 466 sstr_t sstrtrim(sstr_t string);
olaf@96 467
olaf@276 468 scstr_t scstrtrim(scstr_t string);
olaf@276 469
universe@146 470 /**
universe@146 471 * Checks, if a string has a specific prefix.
universe@146 472 * @param string the string to check
universe@146 473 * @param prefix the prefix the string should have
universe@146 474 * @return 1, if and only if the string has the specified prefix, 0 otherwise
universe@146 475 */
olaf@275 476 int ucx_strprefix(scstr_t string, scstr_t prefix);
olaf@275 477
olaf@275 478 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
universe@146 479
universe@146 480 /**
universe@146 481 * Checks, if a string has a specific suffix.
universe@146 482 * @param string the string to check
universe@146 483 * @param suffix the suffix the string should have
universe@146 484 * @return 1, if and only if the string has the specified suffix, 0 otherwise
universe@146 485 */
olaf@275 486 int ucx_strsuffix(scstr_t string, scstr_t suffix);
olaf@275 487
olaf@275 488 #define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
universe@146 489
universe@210 490 /**
universe@210 491 * Returns a lower case version of a string.
universe@210 492 *
universe@210 493 * This function creates a duplicate of the input string, first. See the
universe@210 494 * documentation of sstrdup() for the implications.
universe@210 495 *
universe@210 496 * @param string the input string
universe@210 497 * @return the resulting lower case string
universe@210 498 * @see sstrdup()
universe@210 499 */
olaf@275 500 sstr_t ucx_strlower(scstr_t string);
olaf@275 501
olaf@275 502 #define sstrlower(string) ucx_strlower(SCSTR(string))
universe@210 503
universe@210 504 /**
universe@210 505 * Returns a lower case version of a string.
universe@210 506 *
universe@210 507 * This function creates a duplicate of the input string, first. See the
universe@210 508 * documentation of sstrdup_a() for the implications.
universe@210 509 *
universe@210 510 * @param allocator the allocator used for duplicating the string
universe@210 511 * @param string the input string
universe@210 512 * @return the resulting lower case string
universe@210 513 * @see sstrdup_a()
universe@210 514 */
olaf@275 515 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
olaf@275 516
olaf@275 517 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
universe@210 518
universe@210 519 /**
universe@210 520 * Returns a upper case version of a string.
universe@210 521 *
universe@210 522 * This function creates a duplicate of the input string, first. See the
universe@210 523 * documentation of sstrdup() for the implications.
universe@210 524 *
universe@210 525 * @param string the input string
universe@210 526 * @return the resulting upper case string
universe@210 527 * @see sstrdup()
universe@210 528 */
olaf@275 529 sstr_t ucx_strupper(scstr_t string);
olaf@275 530
olaf@275 531 #define sstrupper(string) ucx_strupper(SCSTR(string))
universe@210 532
universe@210 533 /**
universe@210 534 * Returns a upper case version of a string.
universe@210 535 *
universe@210 536 * This function creates a duplicate of the input string, first. See the
universe@210 537 * documentation of sstrdup_a() for the implications.
universe@210 538 *
universe@210 539 * @param allocator the allocator used for duplicating the string
universe@210 540 * @param string the input string
universe@210 541 * @return the resulting upper case string
universe@210 542 * @see sstrdup_a()
universe@210 543 */
olaf@275 544 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
olaf@275 545
olaf@275 546 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
universe@210 547
olaf@20 548 #ifdef __cplusplus
olaf@20 549 }
olaf@20 550 #endif
olaf@20 551
universe@116 552 #endif /* UCX_STRING_H */

mercurial