src/ucx/string.h

Tue, 29 May 2018 11:05:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 29 May 2018 11:05:12 +0200
changeset 325
a3e63cb21e20
parent 322
fd21d1840dff
child 328
2bf1da3c411e
permissions
-rw-r--r--

changes sstr shortcut macros s.t. they distinguish sstr_t and scstr_t + add macros which can completely disable the shortcuts

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

mercurial