src/ucx/string.h

Wed, 16 May 2018 14:02:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 May 2018 14:02:59 +0200
changeset 318
348fd9cb7b14
parent 316
be0f6bd10b52
child 319
0380e438a7ce
permissions
-rw-r--r--

adds remaining documentation for the scstr 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@318 259 * You may arbitrarily mix up mutable (<code>sstr_t</code>) and immutable
universe@318 260 * (<code>scstr_t</code>) strings.
universe@116 261 *
universe@116 262 * <b>Attention:</b> if the count argument does not match the count of the
universe@116 263 * specified strings, the behavior is undefined.
universe@116 264 *
universe@116 265 * @param count the total number of specified strings (so at least 1)
universe@318 266 * @param ... all strings
universe@116 267 * @return the cumulated length of all strings
olaf@20 268 */
olaf@288 269 size_t ucx_strnlen(size_t count, ...);
olaf@288 270
universe@318 271 /**
universe@318 272 * Alias for ucx_strnlen().
universe@318 273 *
universe@318 274 * @param count the total number of specified strings (so at least 1)
universe@318 275 * @param ... all strings
universe@318 276 * @return the cumulated length of all strings
universe@318 277 */
olaf@288 278 #define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__)
olaf@20 279
universe@119 280 /**
olaf@183 281 * Concatenates two or more strings.
olaf@183 282 *
olaf@183 283 * The resulting string will be allocated by standard <code>malloc()</code>.
olaf@183 284 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
olaf@183 285 *
olaf@183 286 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
olaf@183 287 * terminated.
olaf@180 288 *
olaf@180 289 * @param count the total number of strings to concatenate
olaf@183 290 * @param s1 first string
olaf@183 291 * @param ... all remaining strings
olaf@180 292 * @return the concatenated string
olaf@180 293 */
olaf@288 294 sstr_t ucx_strcat(size_t count, scstr_t s1, ...);
olaf@288 295
universe@318 296 /**
universe@318 297 * Alias for ucx_strcat() which automatically casts the first string.
universe@318 298 *
universe@318 299 * @param count the total number of strings to concatenate
universe@318 300 * @param s1 first string
universe@318 301 * @param ... all remaining strings
universe@318 302 * @return the concatenated string
universe@318 303 */
olaf@288 304 #define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__)
olaf@183 305
olaf@183 306 /**
universe@225 307 * Concatenates two or more strings using a UcxAllocator.
olaf@183 308 *
olaf@183 309 * See sstrcat() for details.
olaf@183 310 *
olaf@183 311 * @param a the allocator to use
olaf@183 312 * @param count the total number of strings to concatenate
olaf@183 313 * @param s1 first string
olaf@183 314 * @param ... all remaining strings
olaf@183 315 * @return the concatenated string
olaf@183 316 */
olaf@288 317 sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
olaf@180 318
universe@318 319 /**
universe@318 320 * Alias for ucx_strcat_a() which automatically casts the first string.
universe@318 321 *
universe@318 322 * See sstrcat() for details.
universe@318 323 *
universe@318 324 * @param a the allocator to use
universe@318 325 * @param count the total number of strings to concatenate
universe@318 326 * @param s1 first string
universe@318 327 * @param ... all remaining strings
universe@318 328 * @return the concatenated string
universe@318 329 */
universe@318 330 #define sstrcat_a(a, count, s1, ...) \
universe@318 331 ucx_strcat_a(a, count, SCSTR(s1), __VA_ARGS__)
olaf@180 332
olaf@180 333 /**
universe@119 334 * Returns a substring starting at the specified location.
universe@119 335 *
universe@119 336 * <b>Attention:</b> the new string references the same memory area as the
universe@119 337 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@119 338 * Use sstrdup() to get a copy.
universe@119 339 *
universe@119 340 * @param string input string
universe@119 341 * @param start start location of the substring
universe@119 342 * @return a substring of <code>string</code> starting at <code>start</code>
universe@119 343 *
universe@119 344 * @see sstrsubsl()
universe@119 345 * @see sstrchr()
universe@119 346 */
universe@119 347 sstr_t sstrsubs(sstr_t string, size_t start);
universe@119 348
universe@119 349 /**
universe@119 350 * Returns a substring with a maximum length starting at the specified location.
universe@119 351 *
universe@119 352 * <b>Attention:</b> the new string references the same memory area as the
universe@119 353 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@119 354 * Use sstrdup() to get a copy.
universe@119 355 *
universe@119 356 * @param string input string
universe@119 357 * @param start start location of the substring
universe@119 358 * @param length the maximum length of the substring
universe@119 359 * @return a substring of <code>string</code> starting at <code>start</code>
universe@119 360 * with a maximum length of <code>length</code>
universe@119 361 *
universe@119 362 * @see sstrsubs()
universe@119 363 * @see sstrchr()
universe@119 364 */
universe@119 365 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
universe@119 366
universe@318 367 /**
universe@318 368 * Returns a substring of an immutable string starting at the specified
universe@318 369 * location.
universe@318 370 *
universe@318 371 * <b>Attention:</b> the new string references the same memory area as the
universe@318 372 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@318 373 * Use scstrdup() to get a copy.
universe@318 374 *
universe@318 375 * @param string input string
universe@318 376 * @param start start location of the substring
universe@318 377 * @return a substring of <code>string</code> starting at <code>start</code>
universe@318 378 *
universe@318 379 * @see scstrsubsl()
universe@318 380 * @see scstrchr()
universe@318 381 */
universe@318 382 scstr_t scstrsubs(scstr_t string, size_t start);
universe@318 383
universe@318 384 /**
universe@318 385 * Returns a substring of an immutable string with a maximum length starting
universe@318 386 * at the specified location.
universe@318 387 *
universe@318 388 * <b>Attention:</b> the new string references the same memory area as the
universe@318 389 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
universe@318 390 * Use scstrdup() to get a copy.
universe@318 391 *
universe@318 392 * @param string input string
universe@318 393 * @param start start location of the substring
universe@318 394 * @param length the maximum length of the substring
universe@318 395 * @return a substring of <code>string</code> starting at <code>start</code>
universe@318 396 * with a maximum length of <code>length</code>
universe@318 397 *
universe@318 398 * @see scstrsubs()
universe@318 399 * @see scstrchr()
universe@318 400 */
olaf@300 401 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
olaf@300 402
universe@119 403 /**
universe@119 404 * Returns a substring starting at the location of the first occurrence of the
universe@119 405 * specified character.
universe@119 406 *
universe@119 407 * If the string does not contain the character, an empty string is returned.
universe@119 408 *
universe@119 409 * @param string the string where to locate the character
universe@119 410 * @param chr the character to locate
universe@148 411 * @return a substring starting at the first location of <code>chr</code>
universe@119 412 *
universe@119 413 * @see sstrsubs()
universe@119 414 */
universe@119 415 sstr_t sstrchr(sstr_t string, int chr);
universe@119 416
universe@119 417 /**
universe@148 418 * Returns a substring starting at the location of the last occurrence of the
universe@148 419 * specified character.
universe@148 420 *
universe@148 421 * If the string does not contain the character, an empty string is returned.
universe@148 422 *
universe@148 423 * @param string the string where to locate the character
universe@148 424 * @param chr the character to locate
universe@148 425 * @return a substring starting at the last location of <code>chr</code>
universe@148 426 *
universe@148 427 * @see sstrsubs()
universe@148 428 */
universe@148 429 sstr_t sstrrchr(sstr_t string, int chr);
universe@148 430
universe@318 431 /**
universe@318 432 * Returns an immutable substring starting at the location of the first
universe@318 433 * occurrence of the specified character.
universe@318 434 *
universe@318 435 * If the string does not contain the character, an empty string is returned.
universe@318 436 *
universe@318 437 * @param string the string where to locate the character
universe@318 438 * @param chr the character to locate
universe@318 439 * @return a substring starting at the first location of <code>chr</code>
universe@318 440 *
universe@318 441 * @see scstrsubs()
universe@318 442 */
universe@318 443 scstr_t scstrchr(scstr_t string, int chr);
olaf@276 444
universe@318 445 /**
universe@318 446 * Returns an immutable substring starting at the location of the last
universe@318 447 * occurrence of the specified character.
universe@318 448 *
universe@318 449 * If the string does not contain the character, an empty string is returned.
universe@318 450 *
universe@318 451 * @param string the string where to locate the character
universe@318 452 * @param chr the character to locate
universe@318 453 * @return a substring starting at the last location of <code>chr</code>
universe@318 454 *
universe@318 455 * @see scstrsubs()
universe@318 456 */
olaf@300 457 scstr_t scstrrchr(scstr_t string, int chr);
olaf@300 458
universe@148 459 /**
universe@214 460 * Returns a substring starting at the location of the first occurrence of the
universe@214 461 * specified string.
universe@214 462 *
universe@214 463 * If the string does not contain the other string, an empty string is returned.
universe@214 464 *
universe@214 465 * If <code>match</code> is an empty string, the complete <code>string</code> is
universe@214 466 * returned.
universe@214 467 *
universe@214 468 * @param string the string to be scanned
universe@214 469 * @param match string containing the sequence of characters to match
universe@214 470 * @return a substring starting at the first occurrence of
universe@214 471 * <code>match</code>, or an empty string, if the sequence is not
universe@214 472 * present in <code>string</code>
universe@214 473 */
olaf@276 474 sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
universe@318 475
universe@318 476 /**
universe@318 477 * Alias for ucx_sstrstr() which automatically casts the match string.
universe@318 478 *
universe@318 479 * @param string the string to be scanned
universe@318 480 * @param match string containing the sequence of characters to match
universe@318 481 * @return a substring starting at the first occurrence of
universe@318 482 * <code>match</code>, or an empty string, if the sequence is not
universe@318 483 * present in <code>string</code>
universe@318 484 */
olaf@276 485 #define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
olaf@276 486
universe@318 487 /**
universe@318 488 * Returns an immutable substring starting at the location of the
universe@318 489 * first occurrence of the specified immutable string.
universe@318 490 *
universe@318 491 * If the string does not contain the other string, an empty string is returned.
universe@318 492 *
universe@318 493 * If <code>match</code> is an empty string, the complete <code>string</code> is
universe@318 494 * returned.
universe@318 495 *
universe@318 496 * @param string the string to be scanned
universe@318 497 * @param match string containing the sequence of characters to match
universe@318 498 * @return a substring starting at the first occurrence of
universe@318 499 * <code>match</code>, or an empty string, if the sequence is not
universe@318 500 * present in <code>string</code>
universe@318 501 */
olaf@276 502 scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
universe@318 503
universe@318 504 /**
universe@318 505 * Alias for ucx_scstrstr() which automatically casts the match string.
universe@318 506 *
universe@318 507 * @param string the string to be scanned
universe@318 508 * @param match string containing the sequence of characters to match
universe@318 509 * @return a substring starting at the first occurrence of
universe@318 510 * <code>match</code>, or an empty string, if the sequence is not
universe@318 511 * present in <code>string</code>
universe@318 512 */
olaf@276 513 #define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
universe@214 514
universe@214 515 /**
universe@119 516 * Splits a string into parts by using a delimiter string.
universe@119 517 *
universe@119 518 * This function will return <code>NULL</code>, if one of the following happens:
universe@119 519 * <ul>
universe@119 520 * <li>the string length is zero</li>
universe@119 521 * <li>the delimeter length is zero</li>
universe@119 522 * <li>the string equals the delimeter</li>
universe@119 523 * <li>memory allocation fails</li>
universe@119 524 * </ul>
universe@119 525 *
universe@119 526 * The integer referenced by <code>count</code> is used as input and determines
universe@160 527 * the maximum size of the resulting array, i.e. the maximum count of splits to
universe@119 528 * perform + 1.
universe@119 529 *
universe@119 530 * The integer referenced by <code>count</code> is also used as output and is
universe@119 531 * set to
universe@119 532 * <ul>
universe@119 533 * <li>-2, on memory allocation errors</li>
universe@119 534 * <li>-1, if either the string or the delimiter is an empty string</li>
universe@119 535 * <li>0, if the string equals the delimiter</li>
universe@119 536 * <li>1, if the string does not contain the delimiter</li>
universe@160 537 * <li>the count of array items, otherwise</li>
universe@119 538 * </ul>
universe@119 539 *
universe@119 540 * If the string starts with the delimiter, the first item of the resulting
universe@160 541 * array will be an empty string.
universe@119 542 *
universe@119 543 * If the string ends with the delimiter and the maximum list size is not
universe@160 544 * exceeded, the last array item will be an empty string.
universe@233 545 * In case the list size would be exceeded, the last array item will be the
universe@233 546 * remaining string after the last split, <i>including</i> the terminating
universe@233 547 * delimiter.
universe@119 548 *
universe@160 549 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
universe@125 550 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
universe@119 551 * an allocator to managed memory, to avoid this.
olaf@20 552 *
universe@119 553 * @param string the string to split
universe@119 554 * @param delim the delimiter string
universe@160 555 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 556 * OUT: the actual size of the array
universe@160 557 * @return a sstr_t array containing the split strings or
universe@318 558 * <code>NULL</code> on error
universe@318 559 *
universe@318 560 * @see ucx_strsplit_a()
universe@318 561 */
universe@318 562 sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
universe@318 563
universe@318 564 /**
universe@318 565 * Alias for ucx_strsplit() which automatically casts the arguments.
universe@318 566 *
universe@318 567 * @param string the string to split
universe@318 568 * @param delim the delimiter string
universe@318 569 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@318 570 * OUT: the actual size of the array
universe@318 571 * @return a sstr_t array containing the split strings or
universe@318 572 * <code>NULL</code> on error
universe@119 573 *
universe@125 574 * @see sstrsplit_a()
olaf@20 575 */
universe@318 576 #define sstrsplit(string, delim, count) \
universe@318 577 ucx_strsplit(SCSTR(string), SCSTR(delim), count)
olaf@20 578
universe@119 579 /**
universe@225 580 * Performing sstrsplit() using a UcxAllocator.
universe@119 581 *
universe@119 582 * <i>Read the description of sstrsplit() for details.</i>
universe@119 583 *
universe@160 584 * The memory for the sstr_t.ptr pointers of the array items and the memory for
universe@119 585 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
universe@119 586 * function.
universe@119 587 *
universe@119 588 * <b>Note:</b> the allocator is not used for memory that is freed within the
universe@119 589 * same call of this function (locally scoped variables).
universe@119 590 *
universe@125 591 * @param allocator the UcxAllocator used for allocating memory
universe@119 592 * @param string the string to split
universe@119 593 * @param delim the delimiter string
universe@160 594 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@160 595 * OUT: the actual size of the array
universe@160 596 * @return a sstr_t array containing the split strings or
universe@318 597 * <code>NULL</code> on error
universe@119 598 *
universe@318 599 * @see ucx_strsplit()
olaf@20 600 */
olaf@276 601 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
universe@173 602 ssize_t *count);
olaf@20 603
universe@318 604 /**
universe@318 605 * Alias for ucx_strsplit_a() which automatically casts the arguments.
universe@318 606 *
universe@318 607 * @param allocator the UcxAllocator used for allocating memory
universe@318 608 * @param string the string to split
universe@318 609 * @param delim the delimiter string
universe@318 610 * @param count IN: the maximum size of the resulting array (0 = no limit),
universe@318 611 * OUT: the actual size of the array
universe@318 612 * @return a sstr_t array containing the split strings or
universe@318 613 * <code>NULL</code> on error
universe@318 614 *
universe@318 615 * @see sstrsplit()
universe@318 616 */
universe@318 617 #define sstrsplit_a(allocator, string, delim, count) \
universe@318 618 ucx_strsplit_a(allocator, SCSTR(string), SCSTR(delim, count))
olaf@276 619
universe@116 620 /**
universe@116 621 * Compares two UCX strings with standard <code>memcmp()</code>.
universe@116 622 *
universe@318 623 * At first it compares the scstr_t.length attribute of the two strings. The
universe@116 624 * <code>memcmp()</code> function is called, if and only if the lengths match.
universe@116 625 *
universe@116 626 * @param s1 the first string
universe@116 627 * @param s2 the second string
universe@116 628 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@116 629 * length of s1 is greater than the length of s2 or the result of
universe@116 630 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
universe@116 631 */
universe@315 632 int ucx_strcmp(scstr_t s1, scstr_t s2);
olaf@276 633
universe@318 634 /**
universe@318 635 * Alias for ucx_strcmp() which automatically casts its arguments.
universe@318 636 *
universe@318 637 * @param s1 the first string
universe@318 638 * @param s2 the second string
universe@318 639 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@318 640 * length of s1 is greater than the length of s2 or the result of
universe@318 641 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
universe@318 642 */
universe@315 643 #define sstrcmp(s1, s2) ucx_strcmp(SCSTR(s1), SCSTR(s2))
olaf@20 644
universe@116 645 /**
universe@149 646 * Compares two UCX strings ignoring the case.
universe@149 647 *
universe@149 648 * At first it compares the sstr_t.length attribute of the two strings. If and
universe@149 649 * only if the lengths match, both strings are compared char by char ignoring
universe@149 650 * the case.
universe@149 651 *
universe@149 652 * @param s1 the first string
universe@149 653 * @param s2 the second string
universe@149 654 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@318 655 * length of s1 is greater than the length of s2 or the result of the platform
universe@318 656 * specific string comparison function ignoring the case.
universe@149 657 */
universe@315 658 int ucx_strcasecmp(scstr_t s1, scstr_t s2);
olaf@276 659
universe@318 660 /**
universe@318 661 * Alias for ucx_strcasecmp() which automatically casts the arguments.
universe@318 662 *
universe@318 663 * @param s1 the first string
universe@318 664 * @param s2 the second string
universe@318 665 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@318 666 * length of s1 is greater than the length of s2 or the result of the platform
universe@318 667 * specific string comparison function ignoring the case.
universe@318 668 */
universe@315 669 #define sstrcasecmp(s1, s2) ucx_strcasecmp(SCSTR(s1), SCSTR(s2))
universe@149 670
universe@149 671 /**
universe@116 672 * Creates a duplicate of the specified string.
universe@116 673 *
universe@116 674 * The new sstr_t will contain a copy allocated by standard
universe@116 675 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
universe@116 676 * <code>free()</code>.
universe@116 677 *
universe@118 678 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@318 679 * terminated and mutable, regardless of the argument.
universe@318 680 *
universe@318 681 * @param string the string to duplicate
universe@318 682 * @return a duplicate of the string
universe@318 683 * @see ucx_strdup_a()
universe@318 684 */
universe@318 685 sstr_t ucx_strdup(scstr_t string);
universe@318 686
universe@318 687 /**
universe@318 688 * Alias for ucx_strdup() which automatically casts the argument.
universe@118 689 *
universe@116 690 * @param string the string to duplicate
universe@118 691 * @return a duplicate of the string
universe@125 692 * @see sstrdup_a()
universe@116 693 */
universe@318 694 #define sstrdup(string) ucx_strdup(SCSTR(string))
olaf@20 695
universe@118 696 /**
universe@225 697 * Creates a duplicate of the specified string using a UcxAllocator.
universe@118 698 *
universe@118 699 * The new sstr_t will contain a copy allocated by the allocators
universe@118 700 * ucx_allocator_malloc function. So it is implementation depended, whether the
universe@118 701 * returned sstr_t.ptr pointer must be passed to the allocators
universe@118 702 * ucx_allocator_free function manually.
universe@118 703 *
universe@118 704 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
universe@318 705 * terminated and mutable, regardless of the argument.
universe@118 706 *
universe@225 707 * @param allocator a valid instance of a UcxAllocator
universe@118 708 * @param string the string to duplicate
universe@118 709 * @return a duplicate of the string
universe@318 710 * @see ucx_strdup()
universe@118 711 */
universe@318 712 sstr_t ucx_strdup_a(UcxAllocator *allocator, scstr_t string);
olaf@275 713
universe@318 714 /**
universe@318 715 * Alias for ucx_strdup_a() which automatically casts the argument.
universe@318 716 *
universe@318 717 * @param allocator a valid instance of a UcxAllocator
universe@318 718 * @param string the string to duplicate
universe@318 719 * @return a duplicate of the string
universe@318 720 * @see ucx_strdup()
universe@318 721 */
universe@318 722 #define sstrdup_a(allocator, string) ucx_strdup_a(allocator, SCSTR(string))
universe@118 723
olaf@276 724
universe@118 725 /**
universe@118 726 * Omits leading and trailing spaces.
universe@118 727 *
universe@118 728 * This function returns a new sstr_t containing a trimmed version of the
universe@118 729 * specified string.
universe@118 730 *
universe@118 731 * <b>Note:</b> the new sstr_t references the same memory, thus you
universe@118 732 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
universe@118 733 * <code>free()</code>. It is also highly recommended to avoid assignments like
universe@118 734 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
universe@118 735 * source string. Assignments of this type are only permitted, if the
universe@118 736 * sstr_t.ptr of the source string does not need to be freed or if another
universe@118 737 * reference to the source string exists.
universe@118 738 *
universe@118 739 * @param string the string that shall be trimmed
universe@118 740 * @return a new sstr_t containing the trimmed string
universe@118 741 */
olaf@96 742 sstr_t sstrtrim(sstr_t string);
olaf@96 743
universe@318 744 /**
universe@318 745 * Omits leading and trailing spaces.
universe@318 746 *
universe@318 747 * This function returns a new scstr_t containing a trimmed version of the
universe@318 748 * specified string.
universe@318 749 *
universe@318 750 * <b>Note:</b> the new scstr_t references the same memory, thus you
universe@318 751 * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
universe@318 752 * <code>free()</code>. It is also highly recommended to avoid assignments like
universe@318 753 * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
universe@318 754 * source string. Assignments of this type are only permitted, if the
universe@318 755 * scstr_t.ptr of the source string does not need to be freed or if another
universe@318 756 * reference to the source string exists.
universe@318 757 *
universe@318 758 * @param string the string that shall be trimmed
universe@318 759 * @return a new scstr_t containing the trimmed string
universe@318 760 */
olaf@276 761 scstr_t scstrtrim(scstr_t string);
olaf@276 762
universe@146 763 /**
universe@146 764 * Checks, if a string has a specific prefix.
universe@146 765 * @param string the string to check
universe@146 766 * @param prefix the prefix the string should have
universe@146 767 * @return 1, if and only if the string has the specified prefix, 0 otherwise
universe@146 768 */
olaf@275 769 int ucx_strprefix(scstr_t string, scstr_t prefix);
olaf@275 770
universe@318 771 /**
universe@318 772 * Alias for ucx_strprefix() which automatically casts the arguments.
universe@318 773 *
universe@318 774 * @param string the string to check
universe@318 775 * @param prefix the prefix the string should have
universe@318 776 * @return 1, if and only if the string has the specified prefix, 0 otherwise
universe@318 777 */
olaf@275 778 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
universe@146 779
universe@146 780 /**
universe@146 781 * Checks, if a string has a specific suffix.
universe@146 782 * @param string the string to check
universe@146 783 * @param suffix the suffix the string should have
universe@146 784 * @return 1, if and only if the string has the specified suffix, 0 otherwise
universe@146 785 */
olaf@275 786 int ucx_strsuffix(scstr_t string, scstr_t suffix);
olaf@275 787
universe@318 788 /**
universe@318 789 * Alias for ucx_strsuffix() which automatically casts the arguments.
universe@318 790 *
universe@318 791 * @param string the string to check
universe@318 792 * @param suffix the suffix the string should have
universe@318 793 * @return 1, if and only if the string has the specified suffix, 0 otherwise
universe@318 794 */
universe@318 795 #define sstrsuffix(string, suffix) ucx_strsuffix(SCSTR(string), SCSTR(suffix))
universe@146 796
universe@210 797 /**
universe@210 798 * Returns a lower case version of a string.
universe@210 799 *
universe@210 800 * This function creates a duplicate of the input string, first. See the
universe@318 801 * documentation of scstrdup() for the implications.
universe@210 802 *
universe@210 803 * @param string the input string
universe@210 804 * @return the resulting lower case string
universe@318 805 * @see scstrdup()
universe@210 806 */
olaf@275 807 sstr_t ucx_strlower(scstr_t string);
olaf@275 808
universe@318 809 /**
universe@318 810 * Alias for ucx_strlower() which automatically casts the argument.
universe@318 811 *
universe@318 812 * @param string the input string
universe@318 813 * @return the resulting lower case string
universe@318 814 */
olaf@275 815 #define sstrlower(string) ucx_strlower(SCSTR(string))
universe@210 816
universe@210 817 /**
universe@210 818 * Returns a lower case version of a string.
universe@210 819 *
universe@210 820 * This function creates a duplicate of the input string, first. See the
universe@318 821 * documentation of scstrdup_a() for the implications.
universe@210 822 *
universe@210 823 * @param allocator the allocator used for duplicating the string
universe@210 824 * @param string the input string
universe@210 825 * @return the resulting lower case string
universe@318 826 * @see scstrdup_a()
universe@210 827 */
olaf@275 828 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
olaf@275 829
universe@318 830
universe@318 831 /**
universe@318 832 * Alias for ucx_strlower_a() which automatically casts the argument.
universe@318 833 *
universe@318 834 * @param allocator the allocator used for duplicating the string
universe@318 835 * @param string the input string
universe@318 836 * @return the resulting lower case string
universe@318 837 */
olaf@275 838 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
universe@210 839
universe@210 840 /**
universe@210 841 * Returns a upper case version of a string.
universe@210 842 *
universe@210 843 * This function creates a duplicate of the input string, first. See the
universe@318 844 * documentation of scstrdup() for the implications.
universe@210 845 *
universe@210 846 * @param string the input string
universe@210 847 * @return the resulting upper case string
universe@318 848 * @see scstrdup()
universe@210 849 */
olaf@275 850 sstr_t ucx_strupper(scstr_t string);
olaf@275 851
universe@318 852 /**
universe@318 853 * Alias for ucx_strupper() which automatically casts the argument.
universe@318 854 *
universe@318 855 * @param string the input string
universe@318 856 * @return the resulting upper case string
universe@318 857 */
olaf@275 858 #define sstrupper(string) ucx_strupper(SCSTR(string))
universe@210 859
universe@210 860 /**
universe@210 861 * Returns a upper case version of a string.
universe@210 862 *
universe@210 863 * This function creates a duplicate of the input string, first. See the
universe@318 864 * documentation of scstrdup_a() for the implications.
universe@210 865 *
universe@210 866 * @param allocator the allocator used for duplicating the string
universe@210 867 * @param string the input string
universe@210 868 * @return the resulting upper case string
universe@318 869 * @see scstrdup_a()
universe@210 870 */
olaf@275 871 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
olaf@275 872
universe@318 873 /**
universe@318 874 * Alias for ucx_strupper_a() which automatically casts the argument.
universe@318 875 *
universe@318 876 * @param allocator the allocator used for duplicating the string
universe@318 877 * @param string the input string
universe@318 878 * @return the resulting upper case string
universe@318 879 */
olaf@275 880 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
universe@210 881
olaf@20 882 #ifdef __cplusplus
olaf@20 883 }
olaf@20 884 #endif
olaf@20 885
universe@116 886 #endif /* UCX_STRING_H */

mercurial