src/ucx/string.h

Wed, 16 May 2018 18:56:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 May 2018 18:56:44 +0200
changeset 319
0380e438a7ce
parent 318
348fd9cb7b14
child 320
0ffb71f15426
permissions
-rw-r--r--

unifies naming of sstr_t related and scstr_t related functions

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

mercurial