src/ucx/string.h

Wed, 16 May 2018 19:27:45 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 May 2018 19:27:45 +0200
changeset 321
9af21a50b516
parent 320
0ffb71f15426
child 322
fd21d1840dff
permissions
-rw-r--r--

adds scstr_t to modules.md + fixes parenthesis bug in sstrsplit_a macro

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@316 61 /** Expands a sstr_t or scstr_t to printf arguments. */
universe@283 62 #define SFMT(s) (int) (s).length, (s).ptr
universe@283 63
universe@316 64 /** Format specifier for a sstr_t or scstr_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@316 74 /** A pointer to the string
universe@316 75 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
universe@316 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
universe@316 81 /**
universe@316 82 * The UCX string structure for immutable (constant) strings.
universe@316 83 */
olaf@275 84 typedef struct {
universe@316 85 /** A constant pointer to the immutable string
universe@316 86 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
olaf@275 87 const char *ptr;
universe@316 88 /** The length of the string */
universe@316 89 size_t length;
olaf@275 90 } scstr_t;
olaf@288 91
olaf@275 92 #ifdef __cplusplus
olaf@275 93 }
olaf@275 94 #endif
olaf@275 95
olaf@275 96
olaf@275 97 #ifdef __cplusplus
universe@321 98 /**
universe@321 99 * One of two type adjustment functions that return a scstr_t.
universe@321 100 *
universe@321 101 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
universe@321 102 *
universe@321 103 * <b>Do not use this function manually.</b>
universe@321 104 *
universe@321 105 * @param str some sstr_t
universe@321 106 * @return an immutable (scstr_t) version of the provided string.
universe@321 107 */
olaf@275 108 inline scstr_t s2scstr(sstr_t s) {
olaf@275 109 scstr_t c;
olaf@275 110 c.ptr = s.ptr;
olaf@275 111 c.length = s.ptr;
olaf@275 112 return c;
olaf@275 113 }
universe@321 114
universe@321 115 /**
universe@321 116 * One of two type adjustment functions that return a scstr_t.
universe@321 117 *
universe@321 118 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
universe@321 119 * This variant is used, when the string is already immutable and no operation
universe@321 120 * needs to be performed.
universe@321 121 *
universe@321 122 * <b>Do not use this function manually.</b>
universe@321 123 *
universe@321 124 * @param str some scstr_t
universe@321 125 * @return the argument itself
universe@321 126 */
universe@321 127 inline scstr_t s2scstr(scstr_t str) {
universe@321 128 return str;
olaf@275 129 }
universe@321 130
universe@321 131 /**
universe@321 132 * Converts a UCX string to an immutable UCX string (scstr_t).
universe@321 133 * @param str some UCX string
universe@321 134 * @return the an immutable version of the provided string
universe@321 135 */
universe@321 136 #define SCSTR(s) s2scstr(s)
olaf@275 137 #else
olaf@275 138
universe@316 139 /**
universe@316 140 * One of two type adjustment functions that return a scstr_t.
universe@316 141 *
universe@321 142 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
universe@316 143 * This variant is used, when the string is already immutable and no operation
universe@316 144 * needs to be performed.
universe@316 145 *
universe@321 146 * <b>Do not use this function manually.</b>
universe@321 147 *
universe@316 148 * @param str some scstr_t
universe@316 149 * @return the argument itself
universe@316 150 */
universe@316 151 scstr_t ucx_sc2sc(scstr_t str);
universe@316 152
universe@316 153 /**
universe@316 154 * One of two type adjustment functions that return a scstr_t.
universe@316 155 *
universe@321 156 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
universe@321 157 *
universe@321 158 * <b>Do not use this function manually.</b>
universe@316 159 *
universe@316 160 * @param str some sstr_t
universe@316 161 * @return an immutable (scstr_t) version of the provided string.
universe@316 162 */
olaf@275 163 scstr_t ucx_ss2sc(sstr_t str);
universe@316 164
olaf@275 165 #if __STDC_VERSION__ >= 201112L
universe@316 166 /**
universe@321 167 * Converts a UCX string to an immutable UCX string (scstr_t).
universe@316 168 * @param str some UCX string
universe@316 169 * @return the an immutable version of the provided string
universe@316 170 */
olaf@275 171 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
universe@316 172
olaf@275 173 #elif defined(__GNUC__) || defined(__clang__)
universe@316 174
universe@316 175 /**
universe@321 176 * Converts a UCX string to an immutable UCX string (scstr_t).
universe@316 177 * @param str some UCX string
universe@316 178 * @return the an immutable version of the provided string
universe@316 179 */
olaf@275 180 #define SCSTR(str) __builtin_choose_expr( \
olaf@275 181 __builtin_types_compatible_p(typeof(str), sstr_t), \
olaf@275 182 ucx_ss2sc, \
olaf@275 183 ucx_sc2sc)(str)
universe@316 184
olaf@275 185 #elif defined(__sun)
universe@316 186
universe@316 187 /**
universe@321 188 * Converts a UCX string to an immutable UCX string (scstr_t).
universe@316 189 * @param str some UCX string
universe@316 190 * @return the an immutable version of the provided string
universe@316 191 */
olaf@275 192 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
olaf@275 193 scstr_t ucx_tmp_var_c; \
olaf@275 194 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
olaf@275 195 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
olaf@275 196 ucx_tmp_var_c; })
universe@316 197 #else /* no generics and no builtins */
universe@316 198
universe@316 199 /**
universe@321 200 * Converts a UCX string to an immutable UCX string (scstr_t).
universe@316 201 *
universe@316 202 * This internal function (ab)uses the C standard an expects one single
universe@320 203 * argument which is then implicitly converted to scstr_t without a warning.
universe@316 204 *
universe@316 205 * @return the an immutable version of the provided string
universe@316 206 */
olaf@275 207 scstr_t ucx_ss2c_s();
universe@316 208
universe@316 209 /**
universe@321 210 * Converts a UCX string to an immutable UCX string (scstr_t).
universe@316 211 * @param str some UCX string
universe@316 212 * @return the an immutable version of the provided string
universe@316 213 */
universe@316 214 #define SCSTR(str) ucx_ss2c_s(str)
olaf@275 215 #endif /* C11 feature test */
olaf@275 216
olaf@275 217 #endif /* C++ */
olaf@275 218
olaf@275 219 #ifdef __cplusplus
olaf@275 220 extern "C" {
olaf@275 221 #endif
olaf@275 222
olaf@275 223
universe@116 224 /**
universe@116 225 * Creates a new sstr_t based on a C string.
universe@116 226 *
universe@116 227 * The length is implicitly inferred by using a call to <code>strlen()</code>.
olaf@20 228 *
universe@116 229 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
universe@116 230 * do want a copy, use sstrdup() on the return value of this function.
universe@116 231 *
universe@316 232 * If you need to wrap a constant string, use scstr().
universe@316 233 *
universe@116 234 * @param cstring the C string to wrap
universe@116 235 * @return a new sstr_t containing the C string
universe@116 236 *
universe@116 237 * @see sstrn()
olaf@20 238 */
universe@116 239 sstr_t sstr(char *cstring);
olaf@20 240
universe@116 241 /**
universe@116 242 * Creates a new sstr_t of the specified length based on a C string.
olaf@20 243 *
universe@116 244 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
universe@116 245 * do want a copy, use sstrdup() on the return value of this function.
universe@116 246 *
universe@316 247 * If you need to wrap a constant string, use scstrn().
universe@316 248 *
universe@116 249 * @param cstring the C string to wrap
universe@116 250 * @param length the length of the string
universe@116 251 * @return a new sstr_t containing the C string
universe@116 252 *
universe@116 253 * @see sstr()
universe@116 254 * @see S()
olaf@20 255 */
universe@116 256 sstr_t sstrn(char *cstring, size_t length);
olaf@20 257
universe@316 258 /**
universe@316 259 * Creates a new scstr_t based on a constant C string.
universe@316 260 *
universe@316 261 * The length is implicitly inferred by using a call to <code>strlen()</code>.
universe@316 262 *
universe@316 263 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
universe@316 264 * do want a copy, use scstrdup() on the return value of this function.
universe@316 265 *
universe@316 266 * @param cstring the C string to wrap
universe@316 267 * @return a new scstr_t containing the C string
universe@316 268 *
universe@316 269 * @see scstrn()
universe@316 270 */
universe@316 271 scstr_t scstr(const char *cstring);
olaf@20 272
universe@316 273
universe@316 274 /**
universe@316 275 * Creates a new scstr_t of the specified length based on a constant C string.
universe@316 276 *
universe@316 277 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
universe@316 278 * do want a copy, use scstrdup() on the return value of this function.
universe@316 279 *
universe@316 280 *
universe@316 281 * @param cstring the C string to wrap
universe@316 282 * @param length the length of the string
universe@316 283 * @return a new scstr_t containing the C string
universe@316 284 *
universe@316 285 * @see scstr()
universe@316 286 */
olaf@275 287 scstr_t scstrn(const char *cstring, size_t length);
olaf@275 288
universe@116 289 /**
universe@116 290 * Returns the cumulated length of all specified strings.
universe@318 291 *
universe@116 292 * <b>Attention:</b> if the count argument does not match the count of the
universe@116 293 * specified strings, the behavior is undefined.
universe@116 294 *
universe@116 295 * @param count the total number of specified strings (so at least 1)
universe@318 296 * @param ... all strings
universe@116 297 * @return the cumulated length of all strings
olaf@20 298 */
universe@319 299 size_t scstrnlen(size_t count, ...);
olaf@288 300
universe@318 301 /**
universe@320 302 * Alias for scstrnlen() which automatically converts the arguments.
universe@318 303 *
universe@318 304 * @param count the total number of specified strings (so at least 1)
universe@318 305 * @param ... all strings
universe@318 306 * @return the cumulated length of all strings
universe@318 307 */
universe@319 308 #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__)
olaf@20 309
universe@119 310 /**
olaf@183 311 * Concatenates two or more strings.
olaf@183 312 *
olaf@183 313 * The resulting string will be allocated by standard <code>malloc()</code>.
olaf@183 314 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
olaf@183 315 *
olaf@183 316 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
olaf@183 317 * terminated.
olaf@180 318 *
olaf@180 319 * @param count the total number of strings to concatenate
olaf@183 320 * @param s1 first string
olaf@183 321 * @param ... all remaining strings
olaf@180 322 * @return the concatenated string
olaf@180 323 */
universe@319 324 sstr_t scstrcat(size_t count, scstr_t s1, ...);
olaf@288 325
universe@318 326 /**
universe@320 327 * Alias for scstrcat() which automatically converts the arguments.
universe@318 328 *
universe@318 329 * @param count the total number of strings to concatenate
universe@318 330 * @param s1 first string
universe@318 331 * @param ... all remaining strings
universe@318 332 * @return the concatenated string
universe@318 333 */
universe@319 334 #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__)
olaf@183 335
olaf@183 336 /**
universe@225 337 * Concatenates two or more strings using a UcxAllocator.
olaf@183 338 *
universe@319 339 * See scstrcat() for details.
olaf@183 340 *
olaf@183 341 * @param a the allocator to use
olaf@183 342 * @param count the total number of strings to concatenate
olaf@183 343 * @param s1 first string
olaf@183 344 * @param ... all remaining strings
olaf@183 345 * @return the concatenated string
olaf@183 346 */
universe@319 347 sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
olaf@180 348
universe@318 349 /**
universe@320 350 * Alias for scstrcat_a() which automatically converts the arguments.
universe@318 351 *
universe@318 352 * See sstrcat() for details.
universe@318 353 *
universe@318 354 * @param a the allocator to use
universe@318 355 * @param count the total number of strings to concatenate
universe@318 356 * @param s1 first string
universe@318 357 * @param ... all remaining strings
universe@318 358 * @return the concatenated string
universe@318 359 */
universe@318 360 #define sstrcat_a(a, count, s1, ...) \
universe@319 361 scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__)
olaf@180 362
olaf@180 363 /**
universe@119 364 * Returns a substring starting at the specified location.
universe@119 365 *
universe@119 366 * <b>Attention:</b> the new string references the same memory area as the
universe@119 367 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@119 368 * Use sstrdup() to get a copy.
universe@119 369 *
universe@119 370 * @param string input string
universe@119 371 * @param start start location of the substring
universe@119 372 * @return a substring of <code>string</code> starting at <code>start</code>
universe@119 373 *
universe@119 374 * @see sstrsubsl()
universe@119 375 * @see sstrchr()
universe@119 376 */
universe@119 377 sstr_t sstrsubs(sstr_t string, size_t start);
universe@119 378
universe@119 379 /**
universe@119 380 * Returns a substring with a maximum length starting at the specified location.
universe@119 381 *
universe@119 382 * <b>Attention:</b> the new string references the same memory area as the
universe@119 383 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@119 384 * Use sstrdup() to get a copy.
universe@119 385 *
universe@119 386 * @param string input string
universe@119 387 * @param start start location of the substring
universe@119 388 * @param length the maximum length of the substring
universe@119 389 * @return a substring of <code>string</code> starting at <code>start</code>
universe@119 390 * with a maximum length of <code>length</code>
universe@119 391 *
universe@119 392 * @see sstrsubs()
universe@119 393 * @see sstrchr()
universe@119 394 */
universe@119 395 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
universe@119 396
universe@318 397 /**
universe@318 398 * Returns a substring of an immutable string starting at the specified
universe@318 399 * location.
universe@318 400 *
universe@318 401 * <b>Attention:</b> the new string references the same memory area as the
universe@318 402 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@318 403 * Use scstrdup() to get a copy.
universe@318 404 *
universe@318 405 * @param string input string
universe@318 406 * @param start start location of the substring
universe@318 407 * @return a substring of <code>string</code> starting at <code>start</code>
universe@318 408 *
universe@318 409 * @see scstrsubsl()
universe@318 410 * @see scstrchr()
universe@318 411 */
universe@318 412 scstr_t scstrsubs(scstr_t string, size_t start);
universe@318 413
universe@318 414 /**
universe@318 415 * Returns a substring of an immutable string with a maximum length starting
universe@318 416 * at the specified location.
universe@318 417 *
universe@318 418 * <b>Attention:</b> the new string references the same memory area as the
universe@318 419 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@318 420 * Use scstrdup() to get a copy.
universe@318 421 *
universe@318 422 * @param string input string
universe@318 423 * @param start start location of the substring
universe@318 424 * @param length the maximum length of the substring
universe@318 425 * @return a substring of <code>string</code> starting at <code>start</code>
universe@318 426 * with a maximum length of <code>length</code>
universe@318 427 *
universe@318 428 * @see scstrsubs()
universe@318 429 * @see scstrchr()
universe@318 430 */
olaf@300 431 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
olaf@300 432
universe@119 433 /**
universe@119 434 * Returns a substring starting at the location of the first occurrence of the
universe@119 435 * specified character.
universe@119 436 *
universe@119 437 * If the string does not contain the character, an empty string is returned.
universe@119 438 *
universe@119 439 * @param string the string where to locate the character
universe@119 440 * @param chr the character to locate
universe@148 441 * @return a substring starting at the first location of <code>chr</code>
universe@119 442 *
universe@119 443 * @see sstrsubs()
universe@119 444 */
universe@119 445 sstr_t sstrchr(sstr_t string, int chr);
universe@119 446
universe@119 447 /**
universe@148 448 * Returns a substring starting at the location of the last occurrence of the
universe@148 449 * specified character.
universe@148 450 *
universe@148 451 * If the string does not contain the character, an empty string is returned.
universe@148 452 *
universe@148 453 * @param string the string where to locate the character
universe@148 454 * @param chr the character to locate
universe@148 455 * @return a substring starting at the last location of <code>chr</code>
universe@148 456 *
universe@148 457 * @see sstrsubs()
universe@148 458 */
universe@148 459 sstr_t sstrrchr(sstr_t string, int chr);
universe@148 460
universe@318 461 /**
universe@318 462 * Returns an immutable substring starting at the location of the first
universe@318 463 * occurrence of the specified character.
universe@318 464 *
universe@318 465 * If the string does not contain the character, an empty string is returned.
universe@318 466 *
universe@318 467 * @param string the string where to locate the character
universe@318 468 * @param chr the character to locate
universe@318 469 * @return a substring starting at the first location of <code>chr</code>
universe@318 470 *
universe@318 471 * @see scstrsubs()
universe@318 472 */
universe@318 473 scstr_t scstrchr(scstr_t string, int chr);
olaf@276 474
universe@318 475 /**
universe@318 476 * Returns an immutable substring starting at the location of the last
universe@318 477 * occurrence of the specified character.
universe@318 478 *
universe@318 479 * If the string does not contain the character, an empty string is returned.
universe@318 480 *
universe@318 481 * @param string the string where to locate the character
universe@318 482 * @param chr the character to locate
universe@318 483 * @return a substring starting at the last location of <code>chr</code>
universe@318 484 *
universe@318 485 * @see scstrsubs()
universe@318 486 */
olaf@300 487 scstr_t scstrrchr(scstr_t string, int chr);
olaf@300 488
universe@148 489 /**
universe@214 490 * Returns a substring starting at the location of the first occurrence of the
universe@214 491 * specified string.
universe@214 492 *
universe@214 493 * If the string does not contain the other string, an empty string is returned.
universe@214 494 *
universe@214 495 * If <code>match</code> is an empty string, the complete <code>string</code> is
universe@214 496 * returned.
universe@214 497 *
universe@214 498 * @param string the string to be scanned
universe@214 499 * @param match string containing the sequence of characters to match
universe@214 500 * @return a substring starting at the first occurrence of
universe@214 501 * <code>match</code>, or an empty string, if the sequence is not
universe@214 502 * present in <code>string</code>
universe@214 503 */
universe@319 504 sstr_t scstrsstr(sstr_t string, scstr_t match);
universe@318 505
universe@318 506 /**
universe@320 507 * Alias for scstrsstr() which automatically converts the match string.
universe@318 508 *
universe@318 509 * @param string the string to be scanned
universe@318 510 * @param match string containing the sequence of characters to match
universe@318 511 * @return a substring starting at the first occurrence of
universe@318 512 * <code>match</code>, or an empty string, if the sequence is not
universe@318 513 * present in <code>string</code>
universe@318 514 */
universe@319 515 #define sstrstr(string, match) scstrsstr(string, SCSTR(match))
olaf@276 516
universe@318 517 /**
universe@318 518 * Returns an immutable substring starting at the location of the
universe@318 519 * first occurrence of the specified immutable string.
universe@318 520 *
universe@318 521 * If the string does not contain the other string, an empty string is returned.
universe@318 522 *
universe@318 523 * If <code>match</code> is an empty string, the complete <code>string</code> is
universe@318 524 * returned.
universe@318 525 *
universe@318 526 * @param string the string to be scanned
universe@318 527 * @param match string containing the sequence of characters to match
universe@318 528 * @return a substring starting at the first occurrence of
universe@318 529 * <code>match</code>, or an empty string, if the sequence is not
universe@318 530 * present in <code>string</code>
universe@318 531 */
universe@319 532 scstr_t scstrscstr(scstr_t string, scstr_t match);
universe@318 533
universe@318 534 /**
universe@320 535 * Alias for scstrscstr() which automatically converts the match string.
universe@318 536 *
universe@318 537 * @param string the string to be scanned
universe@318 538 * @param match string containing the sequence of characters to match
universe@318 539 * @return a substring starting at the first occurrence of
universe@318 540 * <code>match</code>, or an empty string, if the sequence is not
universe@318 541 * present in <code>string</code>
universe@318 542 */
universe@319 543 #define sstrscstr(string, match) scstrscstr(string, SCSTR(match))
universe@214 544
universe@214 545 /**
universe@119 546 * Splits a string into parts by using a delimiter string.
universe@119 547 *
universe@119 548 * This function will return <code>NULL</code>, if one of the following happens:
universe@119 549 * <ul>
universe@119 550 * <li>the string length is zero</li>
universe@119 551 * <li>the delimeter length is zero</li>
universe@119 552 * <li>the string equals the delimeter</li>
universe@119 553 * <li>memory allocation fails</li>
universe@119 554 * </ul>
universe@119 555 *
universe@119 556 * The integer referenced by <code>count</code> is used as input and determines
universe@160 557 * the maximum size of the resulting array, i.e. the maximum count of splits to
universe@119 558 * perform + 1.
universe@119 559 *
universe@119 560 * The integer referenced by <code>count</code> is also used as output and is
universe@119 561 * set to
universe@119 562 * <ul>
universe@119 563 * <li>-2, on memory allocation errors</li>
universe@119 564 * <li>-1, if either the string or the delimiter is an empty string</li>
universe@119 565 * <li>0, if the string equals the delimiter</li>
universe@119 566 * <li>1, if the string does not contain the delimiter</li>
universe@160 567 * <li>the count of array items, otherwise</li>
universe@119 568 * </ul>
universe@119 569 *
universe@119 570 * If the string starts with the delimiter, the first item of the resulting
universe@160 571 * array will be an empty string.
universe@119 572 *
universe@119 573 * If the string ends with the delimiter and the maximum list size is not
universe@160 574 * exceeded, the last array item will be an empty string.
universe@233 575 * In case the list size would be exceeded, the last array item will be the
universe@233 576 * remaining string after the last split, <i>including</i> the terminating
universe@233 577 * delimiter.
universe@119 578 *
universe@160 579 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
universe@125 580 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
universe@119 581 * an allocator to managed memory, to avoid this.
olaf@20 582 *
universe@119 583 * @param string the string to split
universe@119 584 * @param delim the delimiter string
universe@160 585 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 586 * OUT: the actual size of the array
universe@160 587 * @return a sstr_t array containing the split strings or
universe@318 588 * <code>NULL</code> on error
universe@318 589 *
universe@319 590 * @see scstrsplit_a()
universe@318 591 */
universe@319 592 sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
universe@318 593
universe@318 594 /**
universe@320 595 * Alias for scstrsplit() which automatically converts the arguments.
universe@318 596 *
universe@318 597 * @param string the string to split
universe@318 598 * @param delim the delimiter string
universe@318 599 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@318 600 * OUT: the actual size of the array
universe@318 601 * @return a sstr_t array containing the split strings or
universe@318 602 * <code>NULL</code> on error
universe@119 603 *
universe@125 604 * @see sstrsplit_a()
olaf@20 605 */
universe@318 606 #define sstrsplit(string, delim, count) \
universe@319 607 scstrsplit(SCSTR(string), SCSTR(delim), count)
olaf@20 608
universe@119 609 /**
universe@319 610 * Performing scstrsplit() using a UcxAllocator.
universe@119 611 *
universe@319 612 * <i>Read the description of scstrsplit() for details.</i>
universe@119 613 *
universe@160 614 * The memory for the sstr_t.ptr pointers of the array items and the memory for
universe@119 615 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
universe@119 616 * function.
universe@119 617 *
universe@119 618 * <b>Note:</b> the allocator is not used for memory that is freed within the
universe@119 619 * same call of this function (locally scoped variables).
universe@119 620 *
universe@125 621 * @param allocator the UcxAllocator used for allocating memory
universe@119 622 * @param string the string to split
universe@119 623 * @param delim the delimiter string
universe@160 624 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 625 * OUT: the actual size of the array
universe@160 626 * @return a sstr_t array containing the split strings or
universe@318 627 * <code>NULL</code> on error
universe@119 628 *
universe@319 629 * @see scstrsplit()
olaf@20 630 */
universe@319 631 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
universe@173 632 ssize_t *count);
olaf@20 633
universe@318 634 /**
universe@320 635 * Alias for scstrsplit_a() which automatically converts the arguments.
universe@318 636 *
universe@318 637 * @param allocator the UcxAllocator used for allocating memory
universe@318 638 * @param string the string to split
universe@318 639 * @param delim the delimiter string
universe@318 640 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@318 641 * OUT: the actual size of the array
universe@318 642 * @return a sstr_t array containing the split strings or
universe@318 643 * <code>NULL</code> on error
universe@318 644 *
universe@318 645 * @see sstrsplit()
universe@318 646 */
universe@318 647 #define sstrsplit_a(allocator, string, delim, count) \
universe@321 648 scstrsplit_a(allocator, SCSTR(string), SCSTR(delim), count)
olaf@276 649
universe@116 650 /**
universe@116 651 * Compares two UCX strings with standard <code>memcmp()</code>.
universe@116 652 *
universe@318 653 * At first it compares the scstr_t.length attribute of the two strings. The
universe@116 654 * <code>memcmp()</code> function is called, if and only if the lengths match.
universe@116 655 *
universe@116 656 * @param s1 the first string
universe@116 657 * @param s2 the second string
universe@116 658 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@116 659 * length of s1 is greater than the length of s2 or the result of
universe@116 660 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
universe@116 661 */
universe@319 662 int scstrcmp(scstr_t s1, scstr_t s2);
olaf@276 663
universe@318 664 /**
universe@320 665 * Alias for scstrcmp() which automatically converts its arguments.
universe@318 666 *
universe@318 667 * @param s1 the first string
universe@318 668 * @param s2 the second string
universe@318 669 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@318 670 * length of s1 is greater than the length of s2 or the result of
universe@318 671 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
universe@318 672 */
universe@319 673 #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2))
olaf@20 674
universe@116 675 /**
universe@149 676 * Compares two UCX strings ignoring the case.
universe@149 677 *
universe@319 678 * At first it compares the scstr_t.length attribute of the two strings. If and
universe@149 679 * only if the lengths match, both strings are compared char by char ignoring
universe@149 680 * the case.
universe@149 681 *
universe@149 682 * @param s1 the first string
universe@149 683 * @param s2 the second string
universe@149 684 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@318 685 * length of s1 is greater than the length of s2 or the result of the platform
universe@318 686 * specific string comparison function ignoring the case.
universe@149 687 */
universe@319 688 int scstrcasecmp(scstr_t s1, scstr_t s2);
olaf@276 689
universe@318 690 /**
universe@320 691 * Alias for scstrcasecmp() which automatically converts the arguments.
universe@318 692 *
universe@318 693 * @param s1 the first string
universe@318 694 * @param s2 the second string
universe@318 695 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@318 696 * length of s1 is greater than the length of s2 or the result of the platform
universe@318 697 * specific string comparison function ignoring the case.
universe@318 698 */
universe@319 699 #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2))
universe@149 700
universe@149 701 /**
universe@116 702 * Creates a duplicate of the specified string.
universe@116 703 *
universe@116 704 * The new sstr_t will contain a copy allocated by standard
universe@116 705 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
universe@116 706 * <code>free()</code>.
universe@116 707 *
universe@118 708 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@318 709 * terminated and mutable, regardless of the argument.
universe@318 710 *
universe@318 711 * @param string the string to duplicate
universe@318 712 * @return a duplicate of the string
universe@319 713 * @see scstrdup_a()
universe@318 714 */
universe@319 715 sstr_t scstrdup(scstr_t string);
universe@318 716
universe@318 717 /**
universe@320 718 * Alias for scstrdup() which automatically converts the argument.
universe@118 719 *
universe@116 720 * @param string the string to duplicate
universe@118 721 * @return a duplicate of the string
universe@125 722 * @see sstrdup_a()
universe@116 723 */
universe@319 724 #define sstrdup(string) scstrdup(SCSTR(string))
olaf@20 725
universe@118 726 /**
universe@225 727 * Creates a duplicate of the specified string using a UcxAllocator.
universe@118 728 *
universe@118 729 * The new sstr_t will contain a copy allocated by the allocators
universe@319 730 * UcxAllocator.malloc() function. So it is implementation depended, whether the
universe@118 731 * returned sstr_t.ptr pointer must be passed to the allocators
universe@319 732 * UcxAllocator.free() function manually.
universe@118 733 *
universe@118 734 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@318 735 * terminated and mutable, regardless of the argument.
universe@118 736 *
universe@225 737 * @param allocator a valid instance of a UcxAllocator
universe@118 738 * @param string the string to duplicate
universe@118 739 * @return a duplicate of the string
universe@319 740 * @see scstrdup()
universe@118 741 */
universe@319 742 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
olaf@275 743
universe@318 744 /**
universe@320 745 * Alias for scstrdup_a() which automatically converts the argument.
universe@318 746 *
universe@318 747 * @param allocator a valid instance of a UcxAllocator
universe@318 748 * @param string the string to duplicate
universe@318 749 * @return a duplicate of the string
universe@319 750 * @see scstrdup()
universe@318 751 */
universe@319 752 #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string))
universe@118 753
olaf@276 754
universe@118 755 /**
universe@118 756 * Omits leading and trailing spaces.
universe@118 757 *
universe@118 758 * This function returns a new sstr_t containing a trimmed version of the
universe@118 759 * specified string.
universe@118 760 *
universe@118 761 * <b>Note:</b> the new sstr_t references the same memory, thus you
universe@118 762 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
universe@118 763 * <code>free()</code>. It is also highly recommended to avoid assignments like
universe@118 764 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
universe@118 765 * source string. Assignments of this type are only permitted, if the
universe@118 766 * sstr_t.ptr of the source string does not need to be freed or if another
universe@118 767 * reference to the source string exists.
universe@118 768 *
universe@118 769 * @param string the string that shall be trimmed
universe@118 770 * @return a new sstr_t containing the trimmed string
universe@118 771 */
olaf@96 772 sstr_t sstrtrim(sstr_t string);
olaf@96 773
universe@318 774 /**
universe@318 775 * Omits leading and trailing spaces.
universe@318 776 *
universe@318 777 * This function returns a new scstr_t containing a trimmed version of the
universe@318 778 * specified string.
universe@318 779 *
universe@318 780 * <b>Note:</b> the new scstr_t references the same memory, thus you
universe@318 781 * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
universe@318 782 * <code>free()</code>. It is also highly recommended to avoid assignments like
universe@318 783 * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
universe@318 784 * source string. Assignments of this type are only permitted, if the
universe@318 785 * scstr_t.ptr of the source string does not need to be freed or if another
universe@318 786 * reference to the source string exists.
universe@318 787 *
universe@318 788 * @param string the string that shall be trimmed
universe@318 789 * @return a new scstr_t containing the trimmed string
universe@318 790 */
olaf@276 791 scstr_t scstrtrim(scstr_t string);
olaf@276 792
universe@146 793 /**
universe@146 794 * Checks, if a string has a specific prefix.
universe@146 795 * @param string the string to check
universe@146 796 * @param prefix the prefix the string should have
universe@146 797 * @return 1, if and only if the string has the specified prefix, 0 otherwise
universe@146 798 */
universe@319 799 int scstrprefix(scstr_t string, scstr_t prefix);
olaf@275 800
universe@318 801 /**
universe@320 802 * Alias for scstrprefix() which automatically converts the arguments.
universe@318 803 *
universe@318 804 * @param string the string to check
universe@318 805 * @param prefix the prefix the string should have
universe@318 806 * @return 1, if and only if the string has the specified prefix, 0 otherwise
universe@318 807 */
universe@319 808 #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix))
universe@146 809
universe@146 810 /**
universe@146 811 * Checks, if a string has a specific suffix.
universe@146 812 * @param string the string to check
universe@146 813 * @param suffix the suffix the string should have
universe@146 814 * @return 1, if and only if the string has the specified suffix, 0 otherwise
universe@146 815 */
universe@319 816 int scstrsuffix(scstr_t string, scstr_t suffix);
olaf@275 817
universe@318 818 /**
universe@320 819 * Alias for scstrsuffix() which automatically converts the arguments.
universe@318 820 *
universe@318 821 * @param string the string to check
universe@318 822 * @param suffix the suffix the string should have
universe@318 823 * @return 1, if and only if the string has the specified suffix, 0 otherwise
universe@318 824 */
universe@319 825 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
universe@146 826
universe@210 827 /**
universe@210 828 * Returns a lower case version of a string.
universe@210 829 *
universe@210 830 * This function creates a duplicate of the input string, first. See the
universe@318 831 * documentation of scstrdup() for the implications.
universe@210 832 *
universe@210 833 * @param string the input string
universe@210 834 * @return the resulting lower case string
universe@318 835 * @see scstrdup()
universe@210 836 */
universe@319 837 sstr_t scstrlower(scstr_t string);
olaf@275 838
universe@318 839 /**
universe@320 840 * Alias for scstrlower() which automatically converts the argument.
universe@318 841 *
universe@318 842 * @param string the input string
universe@318 843 * @return the resulting lower case string
universe@318 844 */
universe@319 845 #define sstrlower(string) scstrlower(SCSTR(string))
universe@210 846
universe@210 847 /**
universe@210 848 * Returns a lower case version of a string.
universe@210 849 *
universe@210 850 * This function creates a duplicate of the input string, first. See the
universe@318 851 * documentation of scstrdup_a() for the implications.
universe@210 852 *
universe@210 853 * @param allocator the allocator used for duplicating the string
universe@210 854 * @param string the input string
universe@210 855 * @return the resulting lower case string
universe@318 856 * @see scstrdup_a()
universe@210 857 */
universe@319 858 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string);
olaf@275 859
universe@318 860
universe@318 861 /**
universe@320 862 * Alias for scstrlower_a() which automatically converts the argument.
universe@318 863 *
universe@318 864 * @param allocator the allocator used for duplicating the string
universe@318 865 * @param string the input string
universe@318 866 * @return the resulting lower case string
universe@318 867 */
universe@319 868 #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string))
universe@210 869
universe@210 870 /**
universe@210 871 * Returns a upper case version of a string.
universe@210 872 *
universe@210 873 * This function creates a duplicate of the input string, first. See the
universe@318 874 * documentation of scstrdup() for the implications.
universe@210 875 *
universe@210 876 * @param string the input string
universe@210 877 * @return the resulting upper case string
universe@318 878 * @see scstrdup()
universe@210 879 */
universe@319 880 sstr_t scstrupper(scstr_t string);
olaf@275 881
universe@318 882 /**
universe@320 883 * Alias for scstrupper() which automatically converts the argument.
universe@318 884 *
universe@318 885 * @param string the input string
universe@318 886 * @return the resulting upper case string
universe@318 887 */
universe@319 888 #define sstrupper(string) scstrupper(SCSTR(string))
universe@210 889
universe@210 890 /**
universe@210 891 * Returns a upper case version of a string.
universe@210 892 *
universe@210 893 * This function creates a duplicate of the input string, first. See the
universe@318 894 * documentation of scstrdup_a() for the implications.
universe@210 895 *
universe@210 896 * @param allocator the allocator used for duplicating the string
universe@210 897 * @param string the input string
universe@210 898 * @return the resulting upper case string
universe@318 899 * @see scstrdup_a()
universe@210 900 */
universe@319 901 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
olaf@275 902
universe@318 903 /**
universe@320 904 * Alias for scstrupper_a() which automatically converts the argument.
universe@318 905 *
universe@318 906 * @param allocator the allocator used for duplicating the string
universe@318 907 * @param string the input string
universe@318 908 * @return the resulting upper case string
universe@318 909 */
universe@319 910 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string)
universe@210 911
olaf@20 912 #ifdef __cplusplus
olaf@20 913 }
olaf@20 914 #endif
olaf@20 915
universe@116 916 #endif /* UCX_STRING_H */

mercurial