src/ucx/string.h

Sun, 13 May 2018 07:13:09 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 13 May 2018 07:13:09 +0200
branch
constsstr
changeset 300
d1f814633049
parent 288
6af5798342e8
child 306
90b6d69bb499
permissions
-rw-r--r--

completes conversion to scstr

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

mercurial