src/ucx/string.h

Tue, 08 May 2018 12:49:56 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 08 May 2018 12:49:56 +0200
branch
constsstr
changeset 288
6af5798342e8
parent 276
f1b2146d4805
child 300
d1f814633049
permissions
-rw-r--r--

makes sstrcat and sstrnlen scstr_t compatible

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

mercurial