src/ucx/string.h

Wed, 16 May 2018 19:33:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 May 2018 19:33:31 +0200
changeset 322
fd21d1840dff
parent 321
9af21a50b516
child 325
a3e63cb21e20
permissions
-rw-r--r--

Tags finalization of the scstr_t integration.

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

mercurial