src/cx/string.h

Mon, 29 Aug 2022 20:54:42 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 29 Aug 2022 20:54:42 +0200
changeset 576
ba0c4ff6698e
child 577
26447d59a5ab
permissions
-rw-r--r--

first proposal for the string header

universe@576 1 /*
universe@576 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@576 3 *
universe@576 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@576 5 *
universe@576 6 * Redistribution and use in source and binary forms, with or without
universe@576 7 * modification, are permitted provided that the following conditions are met:
universe@576 8 *
universe@576 9 * 1. Redistributions of source code must retain the above copyright
universe@576 10 * notice, this list of conditions and the following disclaimer.
universe@576 11 *
universe@576 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@576 13 * notice, this list of conditions and the following disclaimer in the
universe@576 14 * documentation and/or other materials provided with the distribution.
universe@576 15 *
universe@576 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@576 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@576 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@576 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@576 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@576 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@576 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@576 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@576 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@576 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@576 26 * POSSIBILITY OF SUCH DAMAGE.
universe@576 27 */
universe@576 28 /**
universe@576 29 * \file string.h
universe@576 30 * \brief Strings that know their length.
universe@576 31 * \author Mike Becker
universe@576 32 * \author Olaf Wintermann
universe@576 33 * \version 3.0
universe@576 34 * \copyright 2-Clause BSD License
universe@576 35 */
universe@576 36
universe@576 37 #ifndef UCX_STRING_H
universe@576 38 #define UCX_STRING_H
universe@576 39
universe@576 40 #include "common.h"
universe@576 41 #include "allocator.h"
universe@576 42
universe@576 43 /**
universe@576 44 * The UCX string structure.
universe@576 45 */
universe@576 46 struct {
universe@576 47 /**
universe@576 48 * A pointer to the string.
universe@576 49 * \note The string is not necessarily \c NULL terminated.
universe@576 50 * Always use the length.
universe@576 51 */
universe@576 52 char *ptr;
universe@576 53 /** The length of the string */
universe@576 54 size_t length;
universe@576 55 } cx_mutstr_s;
universe@576 56
universe@576 57 /**
universe@576 58 * A mutable string.
universe@576 59 */
universe@576 60 typedef struct cx_mutstr_s cxmutstr;
universe@576 61
universe@576 62 /**
universe@576 63 * The UCX string structure for immutable (constant) strings.
universe@576 64 */
universe@576 65 struct {
universe@576 66 /**
universe@576 67 * A pointer to the immutable string.
universe@576 68 * \note The string is not necessarily \c NULL terminated.
universe@576 69 * Always use the length.
universe@576 70 */
universe@576 71 char const *ptr;
universe@576 72 /** The length of the string */
universe@576 73 size_t length;
universe@576 74 } cx_string_s;
universe@576 75
universe@576 76 /**
universe@576 77 * An immutable string.
universe@576 78 */
universe@576 79 typedef struct cx_string_s cxstring;
universe@576 80
universe@576 81 #ifdef __cplusplus
universe@576 82 extern "C" {
universe@576 83 #endif
universe@576 84
universe@576 85
universe@576 86 /**
universe@576 87 * Wraps a mutable string that must be zero-terminated.
universe@576 88 *
universe@576 89 * The length is implicitly inferred by using a call to \c strlen().
universe@576 90 * As a special case, a \c NULL argument is treated like an empty string.
universe@576 91 *
universe@576 92 * \note the wrapped string will share the specified pointer to the string.
universe@576 93 * If you do want a copy, use cx_strdup() on the return value of this function.
universe@576 94 *
universe@576 95 * If you need to wrap a constant string, use cx_str().
universe@576 96 *
universe@576 97 * @param cstring the string to wrap, must be zero-terminated (or \c NULL)
universe@576 98 * @return the wrapped string
universe@576 99 *
universe@576 100 * @see cx_mutstrn()
universe@576 101 */
universe@576 102 __attribute__((__warn_unused_result__))
universe@576 103 cxmutstr cx_mutstr(char *cstring);
universe@576 104
universe@576 105 /**
universe@576 106 * Wraps a string that does not need to be zero-terminated.
universe@576 107 *
universe@576 108 * The argument may be \c NULL if the length is zero.
universe@576 109 *
universe@576 110 * \note the wrapped string will share the specified pointer to the string.
universe@576 111 * If you do want a copy, use cx_strdup() on the return value of this function.
universe@576 112 *
universe@576 113 * If you need to wrap a constant string, use cx_strn().
universe@576 114 *
universe@576 115 * @param cstring the string to wrap (or \c NULL, if the length is zero)
universe@576 116 * @param length the length of the string
universe@576 117 * @return the wrapped string
universe@576 118 *
universe@576 119 * @see cx_mutstr()
universe@576 120 */
universe@576 121 __attribute__((__warn_unused_result__))
universe@576 122 cxmutstr cx_mutstrn(
universe@576 123 char *cstring,
universe@576 124 size_t length
universe@576 125 );
universe@576 126
universe@576 127 /**
universe@576 128 * Wraps a string that must be zero-terminated.
universe@576 129 *
universe@576 130 * The length is implicitly inferred by using a call to \c strlen().
universe@576 131 * As a special case, a \c NULL argument is treated like an empty string.
universe@576 132 *
universe@576 133 * \note the wrapped string will share the specified pointer to the string.
universe@576 134 * If you do want a copy, use cx_strdup() on the return value of this function.
universe@576 135 *
universe@576 136 * If you need to wrap a non-constant string, use cx_mutstr().
universe@576 137 *
universe@576 138 * @param cstring the string to wrap, must be zero-terminated (or \c NULL)
universe@576 139 * @return the wrapped string
universe@576 140 *
universe@576 141 * @see cx_strn()
universe@576 142 */
universe@576 143 __attribute__((__warn_unused_result__))
universe@576 144 cxstring cx_str(char const *cstring);
universe@576 145
universe@576 146
universe@576 147 /**
universe@576 148 * Wraps a string that does not need to be zero-terminated.
universe@576 149 *
universe@576 150 * The argument may be \c NULL if the length is zero.
universe@576 151 *
universe@576 152 * \note the wrapped string will share the specified pointer to the string.
universe@576 153 * If you do want a copy, use cx_strdup() on the return value of this function.
universe@576 154 *
universe@576 155 * If you need to wrap a non-constant string, use cx_mutstrn().
universe@576 156 *
universe@576 157 * @param cstring the string to wrap (or \c NULL, if the length is zero)
universe@576 158 * @param length the length of the string
universe@576 159 * @return the wrapped string
universe@576 160 *
universe@576 161 * @see cx_str()
universe@576 162 */
universe@576 163 __attribute__((__warn_unused_result__))
universe@576 164 cxstring cx_strn(
universe@576 165 char const *cstring,
universe@576 166 size_t length
universe@576 167 );
universe@576 168
universe@576 169 /**
universe@576 170 * Casts a mutable string to an immutable string.
universe@576 171 *
universe@576 172 * \note This is not seriously a cast. Instead you get a copy
universe@576 173 * of the struct with the desired pointer type. Both structs still
universe@576 174 * point to the same location, though!
universe@576 175 *
universe@576 176 * @param str the mutable string to cast
universe@576 177 * @return an immutable copy of the string pointer
universe@576 178 */
universe@576 179 __attribute__((__warn_unused_result__))
universe@576 180 cxstring cx_strcast(cxmutstr str);
universe@576 181
universe@576 182 /**
universe@576 183 * Passes the pointer in this string to \c free().
universe@576 184 *
universe@576 185 * The pointer in the struct is set to \c NULL and the length is set to zero.
universe@576 186 *
universe@576 187 * \note There is no implementation for cxstring, because it is unlikely that
universe@576 188 * you ever have a \c char \c const* you are really supposed to free. If you
universe@576 189 * encounter such situation, you should double-check your code.
universe@576 190 *
universe@576 191 * @param str the string to free
universe@576 192 */
universe@576 193 void cx_strfree(cxmutstr *str);
universe@576 194
universe@576 195 /**
universe@576 196 * Returns the accumulated length of all specified strings.
universe@576 197 *
universe@576 198 * \attention if the count argument is larger than the number of the
universe@576 199 * specified strings, the behavior is undefined.
universe@576 200 *
universe@576 201 * @param count the total number of specified strings
universe@576 202 * @param ... all strings
universe@576 203 * @return the accumulated length of all strings
universe@576 204 */
universe@576 205 __attribute__((__warn_unused_result__))
universe@576 206 size_t cx_strlen(
universe@576 207 size_t count,
universe@576 208 ...
universe@576 209 );
universe@576 210
universe@576 211 /**
universe@576 212 * Concatenates two or more strings.
universe@576 213 *
universe@576 214 * The resulting string will be allocated by the specified allocator.
universe@576 215 * So developers \em must pass the return value to cx_strfree() eventually.
universe@576 216 *
universe@576 217 * \note It is guaranteed that there is only one allocation.
universe@576 218 *
universe@576 219 * @param alloc the allocator to use
universe@576 220 * @param count the total number of strings to concatenate
universe@576 221 * @param ... all strings
universe@576 222 * @return the concatenated string
universe@576 223 */
universe@576 224 __attribute__((__warn_unused_result__, __nonnull__))
universe@576 225 cxmutstr cx_strcat_a(
universe@576 226 CxAllocator *alloc,
universe@576 227 size_t count,
universe@576 228 ...
universe@576 229 );
universe@576 230
universe@576 231 /**
universe@576 232 * Concatenates two or more strings.
universe@576 233 *
universe@576 234 * The resulting string will be allocated by standard \c malloc().
universe@576 235 * So developers \em must pass the return value to cx_strfree() eventually.
universe@576 236 *
universe@576 237 * @param count the total number of strings to concatenate
universe@576 238 * @param ... all strings
universe@576 239 * @return the concatenated string
universe@576 240 */
universe@576 241 #define cx_strcat(count, ...) \
universe@576 242 cx_strcat_a(cxDefaultAllocator, count, __VA_ARGS__)
universe@576 243
universe@576 244 /**
universe@576 245 * Returns a substring starting at the specified location.
universe@576 246 *
universe@576 247 * \attention the new string references the same memory area as the
universe@576 248 * input string and is usually \em not zero-terminated.
universe@576 249 * Use cx_strdup() to get a copy.
universe@576 250 *
universe@576 251 * @param string input string
universe@576 252 * @param start start location of the substring
universe@576 253 * @return a substring of \p string starting at \p start
universe@576 254 *
universe@576 255 * @see cx_strsubsl()
universe@576 256 * @see cx_strsubs_m()
universe@576 257 * @see cx_strsubsl_m()
universe@576 258 */
universe@576 259 __attribute__((__warn_unused_result__))
universe@576 260 cxstring cx_strsubs(
universe@576 261 cxstring string,
universe@576 262 size_t start
universe@576 263 );
universe@576 264
universe@576 265 /**
universe@576 266 * Returns a substring starting at the specified location.
universe@576 267 *
universe@576 268 * The returned string will be limited to \p length bytes or the number
universe@576 269 * of bytes available in \p string, whichever is smaller.
universe@576 270 *
universe@576 271 * \attention the new string references the same memory area as the
universe@576 272 * input string and is usually \em not zero-terminated.
universe@576 273 * Use cx_strdup() to get a copy.
universe@576 274 *
universe@576 275 * @param string input string
universe@576 276 * @param start start location of the substring
universe@576 277 * @param length the maximum length of the returned string
universe@576 278 * @return a substring of \p string starting at \p start
universe@576 279 *
universe@576 280 * @see cx_strsubs()
universe@576 281 * @see cx_strsubs_m()
universe@576 282 * @see cx_strsubsl_m()
universe@576 283 */
universe@576 284 __attribute__((__warn_unused_result__))
universe@576 285 cxstring cx_strsubsl(
universe@576 286 cxstring string,
universe@576 287 size_t start,
universe@576 288 size_t length
universe@576 289 );
universe@576 290
universe@576 291 /**
universe@576 292 * Returns a substring starting at the specified location.
universe@576 293 *
universe@576 294 * \attention the new string references the same memory area as the
universe@576 295 * input string and is usually \em not zero-terminated.
universe@576 296 * Use cx_strdup() to get a copy.
universe@576 297 *
universe@576 298 * @param string input string
universe@576 299 * @param start start location of the substring
universe@576 300 * @return a substring of \p string starting at \p start
universe@576 301 *
universe@576 302 * @see cx_strsubsl_m()
universe@576 303 * @see cx_strsubs()
universe@576 304 * @see cx_strsubsl()
universe@576 305 */
universe@576 306 __attribute__((__warn_unused_result__))
universe@576 307 cxmutstr cx_strsubs_m(
universe@576 308 cxmutstr string,
universe@576 309 size_t start
universe@576 310 );
universe@576 311
universe@576 312 /**
universe@576 313 * Returns a substring starting at the specified location.
universe@576 314 *
universe@576 315 * The returned string will be limited to \p length bytes or the number
universe@576 316 * of bytes available in \p string, whichever is smaller.
universe@576 317 *
universe@576 318 * \attention the new string references the same memory area as the
universe@576 319 * input string and is usually \em not zero-terminated.
universe@576 320 * Use cx_strdup() to get a copy.
universe@576 321 *
universe@576 322 * @param string input string
universe@576 323 * @param start start location of the substring
universe@576 324 * @param length the maximum length of the returned string
universe@576 325 * @return a substring of \p string starting at \p start
universe@576 326 *
universe@576 327 * @see cx_strsubs_m()
universe@576 328 * @see cx_strsubs()
universe@576 329 * @see cx_strsubsl()
universe@576 330 */
universe@576 331 __attribute__((__warn_unused_result__))
universe@576 332 cxmutstr cx_strsubsl_m(
universe@576 333 cxmutstr string,
universe@576 334 size_t start,
universe@576 335 size_t length
universe@576 336 );
universe@576 337
universe@576 338 /**
universe@576 339 * Returns a substring starting at the location of the first occurrence of the
universe@576 340 * specified character.
universe@576 341 *
universe@576 342 * If the string does not contain the character, an empty string is returned.
universe@576 343 *
universe@576 344 * @param string the string where to locate the character
universe@576 345 * @param chr the character to locate
universe@576 346 * @return a substring starting at the first location of \p chr
universe@576 347 *
universe@576 348 * @see cx_strchr_m()
universe@576 349 */
universe@576 350 __attribute__((__warn_unused_result__))
universe@576 351 cxstring cx_strchr(
universe@576 352 cxstring string,
universe@576 353 int chr
universe@576 354 );
universe@576 355
universe@576 356 /**
universe@576 357 * Returns a substring starting at the location of the first occurrence of the
universe@576 358 * specified character.
universe@576 359 *
universe@576 360 * If the string does not contain the character, an empty string is returned.
universe@576 361 *
universe@576 362 * @param string the string where to locate the character
universe@576 363 * @param chr the character to locate
universe@576 364 * @return a substring starting at the first location of \p chr
universe@576 365 *
universe@576 366 * @see cx_strchr()
universe@576 367 */
universe@576 368 __attribute__((__warn_unused_result__))
universe@576 369 cxmutstr cx_strchr_m(
universe@576 370 cxmutstr string,
universe@576 371 int chr
universe@576 372 );
universe@576 373
universe@576 374 /**
universe@576 375 * Returns a substring starting at the location of the last occurrence of the
universe@576 376 * specified character.
universe@576 377 *
universe@576 378 * If the string does not contain the character, an empty string is returned.
universe@576 379 *
universe@576 380 * @param string the string where to locate the character
universe@576 381 * @param chr the character to locate
universe@576 382 * @return a substring starting at the last location of \p chr
universe@576 383 *
universe@576 384 * @see cx_strrchr_m()
universe@576 385 */
universe@576 386 __attribute__((__warn_unused_result__))
universe@576 387 cxstring cx_strrchr(
universe@576 388 cxstring string,
universe@576 389 int chr
universe@576 390 );
universe@576 391
universe@576 392 /**
universe@576 393 * Returns a substring starting at the location of the last occurrence of the
universe@576 394 * specified character.
universe@576 395 *
universe@576 396 * If the string does not contain the character, an empty string is returned.
universe@576 397 *
universe@576 398 * @param string the string where to locate the character
universe@576 399 * @param chr the character to locate
universe@576 400 * @return a substring starting at the last location of \p chr
universe@576 401 *
universe@576 402 * @see cx_strrchr()
universe@576 403 */
universe@576 404 __attribute__((__warn_unused_result__))
universe@576 405 cxmutstr cx_strrchr_m(
universe@576 406 cxmutstr string,
universe@576 407 int chr
universe@576 408 );
universe@576 409
universe@576 410 /**
universe@576 411 * Returns a substring starting at the location of the first occurrence of the
universe@576 412 * specified string.
universe@576 413 *
universe@576 414 * If \p haystack does not contain \p needle, an empty string is returned.
universe@576 415 *
universe@576 416 * If \p needle is an empty string, the complete \p haystack is
universe@576 417 * returned.
universe@576 418 *
universe@576 419 * @param haystack the string to be scanned
universe@576 420 * @param needle string containing the sequence of characters to match
universe@576 421 * @return a substring starting at the first occurrence of
universe@576 422 * \p needle, or an empty string, if the sequence is not
universe@576 423 * contained
universe@576 424 * @see cx_strstr_m()
universe@576 425 */
universe@576 426 __attribute__((__warn_unused_result__))
universe@576 427 cxstring cx_strstr(
universe@576 428 cxstring haystack,
universe@576 429 cxstring needle
universe@576 430 );
universe@576 431
universe@576 432 /**
universe@576 433 * Returns a substring starting at the location of the first occurrence of the
universe@576 434 * specified string.
universe@576 435 *
universe@576 436 * If \p haystack does not contain \p needle, an empty string is returned.
universe@576 437 *
universe@576 438 * If \p needle is an empty string, the complete \p haystack is
universe@576 439 * returned.
universe@576 440 *
universe@576 441 * @param haystack the string to be scanned
universe@576 442 * @param needle string containing the sequence of characters to match
universe@576 443 * @return a substring starting at the first occurrence of
universe@576 444 * \p needle, or an empty string, if the sequence is not
universe@576 445 * contained
universe@576 446 * @see cx_strstr()
universe@576 447 */
universe@576 448 __attribute__((__warn_unused_result__))
universe@576 449 cxmutstr cx_strstr_m(
universe@576 450 cxmutstr haystack,
universe@576 451 cxstring needle
universe@576 452 );
universe@576 453
universe@576 454 /**
universe@576 455 * Splits a given string using a delimiter string.
universe@576 456 *
universe@576 457 * \note The resulting array contains strings that point to the source
universe@576 458 * \p string. Use cx_strdup() to get copies.
universe@576 459 *
universe@576 460 * @param string the string to split
universe@576 461 * @param delim the delimiter
universe@576 462 * @param limit the maximum number of split items
universe@576 463 * @param output a pre-allocated array of at least \p limit length
universe@576 464 * @return the actual number of split items
universe@576 465 */
universe@576 466 __attribute__((__warn_unused_result__, __nonnull__))
universe@576 467 size_t cx_strsplit(
universe@576 468 cxstring string,
universe@576 469 cxstring delim,
universe@576 470 size_t limit,
universe@576 471 cxstring *output
universe@576 472 );
universe@576 473
universe@576 474 /**
universe@576 475 * Splits a given string using a delimiter string.
universe@576 476 *
universe@576 477 * The array pointed to by \p output will be allocated by \p allocator.
universe@576 478 *
universe@576 479 * \note The resulting array contains strings that point to the source
universe@576 480 * \p string. Use cx_strdup() to get copies.
universe@576 481 *
universe@576 482 * \attention If allocation fails, the \c NULL pointer will be written to
universe@576 483 * \p output and the number returned will be zero.
universe@576 484 *
universe@576 485 * @param allocator the allocator to use for allocating the resulting array
universe@576 486 * @param string the string to split
universe@576 487 * @param delim the delimiter
universe@576 488 * @param limit the maximum number of split items
universe@576 489 * @param output a pointer where the address of the allocated array shall be
universe@576 490 * written to
universe@576 491 * @return the actual number of split items
universe@576 492 */
universe@576 493 __attribute__((__warn_unused_result__, __nonnull__))
universe@576 494 size_t cx_strsplit_a(
universe@576 495 CxAllocator *allocator,
universe@576 496 cxstring string,
universe@576 497 cxstring delim,
universe@576 498 size_t limit,
universe@576 499 cxstring **output
universe@576 500 );
universe@576 501
universe@576 502
universe@576 503 /**
universe@576 504 * Splits a given string using a delimiter string.
universe@576 505 *
universe@576 506 * \note The resulting array contains strings that point to the source
universe@576 507 * \p string. Use cx_strdup() to get copies.
universe@576 508 *
universe@576 509 * @param string the string to split
universe@576 510 * @param delim the delimiter
universe@576 511 * @param limit the maximum number of split items
universe@576 512 * @param output a pre-allocated array of at least \p limit length
universe@576 513 * @return the actual number of split items
universe@576 514 */
universe@576 515 __attribute__((__warn_unused_result__, __nonnull__))
universe@576 516 size_t cx_strsplit_m(
universe@576 517 cxmutstr string,
universe@576 518 cxstring delim,
universe@576 519 size_t limit,
universe@576 520 cxmutstr *output
universe@576 521 );
universe@576 522
universe@576 523 /**
universe@576 524 * Splits a given string using a delimiter string.
universe@576 525 *
universe@576 526 * The array pointed to by \p output will be allocated by \p allocator.
universe@576 527 *
universe@576 528 * \note The resulting array contains strings that point to the source
universe@576 529 * \p string. Use cx_strdup() to get copies.
universe@576 530 *
universe@576 531 * \attention If allocation fails, the \c NULL pointer will be written to
universe@576 532 * \p output and the number returned will be zero.
universe@576 533 *
universe@576 534 * @param allocator the allocator to use for allocating the resulting array
universe@576 535 * @param string the string to split
universe@576 536 * @param delim the delimiter
universe@576 537 * @param limit the maximum number of split items
universe@576 538 * @param output a pointer where the address of the allocated array shall be
universe@576 539 * written to
universe@576 540 * @return the actual number of split items
universe@576 541 */
universe@576 542 __attribute__((__warn_unused_result__, __nonnull__))
universe@576 543 size_t cx_strsplit_ma(
universe@576 544 CxAllocator *allocator,
universe@576 545 cxmutstr string,
universe@576 546 cxstring delim,
universe@576 547 size_t limit,
universe@576 548 cxmutstr **output
universe@576 549 );
universe@576 550
universe@576 551 /**
universe@576 552 * Compares two strings.
universe@576 553 *
universe@576 554 * @param s1 the first string
universe@576 555 * @param s2 the second string
universe@576 556 * @return negative if \p s1 is smaller than \p s2, positive if \p s1 is larger
universe@576 557 * than \p s2, zero if both strings equal
universe@576 558 */
universe@576 559 __attribute__((__warn_unused_result__))
universe@576 560 int cx_strcmp(
universe@576 561 cxstring s1,
universe@576 562 cxstring s2
universe@576 563 );
universe@576 564
universe@576 565 /**
universe@576 566 * Compares two strings ignoring case.
universe@576 567 *
universe@576 568 * @param s1 the first string
universe@576 569 * @param s2 the second string
universe@576 570 * @return negative if \p s1 is smaller than \p s2, positive if \p s1 is larger
universe@576 571 * than \p s2, zero if both strings equal ignoring case
universe@576 572 */
universe@576 573 __attribute__((__warn_unused_result__))
universe@576 574 int cx_strcasecmp(
universe@576 575 cxstring s1,
universe@576 576 cxstring s2
universe@576 577 );
universe@576 578
universe@576 579 /**
universe@576 580 * Creates a duplicate of the specified string.
universe@576 581 *
universe@576 582 * The new string will contain a copy allocated by standard
universe@576 583 * \c malloc(). So developers \em must pass the return value to cx_strfree().
universe@576 584 *
universe@576 585 * \note The returned string is guaranteed to be zero-terminated and can safely
universe@576 586 * be passed to other APIs.
universe@576 587 *
universe@576 588 * @param string the string to duplicate
universe@576 589 * @return a duplicate of the string
universe@576 590 * @see cx_strdup_a()
universe@576 591 */
universe@576 592 __attribute__((__warn_unused_result__))
universe@576 593 cxmutstr cx_strdup(cxstring string);
universe@576 594
universe@576 595 /**
universe@576 596 * Creates a duplicate of the specified string.
universe@576 597 *
universe@576 598 * The new string will contain a copy allocated by \p allocator.
universe@576 599 *
universe@576 600 * \note The returned string is guaranteed to be zero-terminated and can safely
universe@576 601 * be passed to other APIs.
universe@576 602 *
universe@576 603 * @param allocator the allocator to use
universe@576 604 * @param string the string to duplicate
universe@576 605 * @return a duplicate of the string
universe@576 606 * @see cx_strdup()
universe@576 607 */
universe@576 608 __attribute__((__warn_unused_result__, __nonnull__))
universe@576 609 cxmutstr cx_strdup_a(
universe@576 610 CxAllocator *allocator,
universe@576 611 cxstring string
universe@576 612 );
universe@576 613
universe@576 614 /**
universe@576 615 * Omits leading and trailing spaces.
universe@576 616 *
universe@576 617 * \note the returned string references the same memory, thus you
universe@576 618 * must \em not free the returned memory.
universe@576 619 *
universe@576 620 * @param string the string that shall be trimmed
universe@576 621 * @return the trimmed string
universe@576 622 */
universe@576 623 __attribute__((__warn_unused_result__))
universe@576 624 cxstring cx_strtrim(cxstring string);
universe@576 625
universe@576 626 /**
universe@576 627 * Omits leading and trailing spaces.
universe@576 628 *
universe@576 629 * \note the returned string references the same memory, thus you
universe@576 630 * must \em not free the returned memory.
universe@576 631 *
universe@576 632 * @param string the string that shall be trimmed
universe@576 633 * @return the trimmed string
universe@576 634 */
universe@576 635 __attribute__((__warn_unused_result__))
universe@576 636 cxmutstr cx_strtrim_m(cxmutstr string);
universe@576 637
universe@576 638 /**
universe@576 639 * Checks, if a string has a specific prefix.
universe@576 640 *
universe@576 641 * @param string the string to check
universe@576 642 * @param prefix the prefix the string should have
universe@576 643 * @return \c true, if and only if the string has the specified prefix,
universe@576 644 * \c false otherwise
universe@576 645 */
universe@576 646 __attribute__((__warn_unused_result__))
universe@576 647 bool cx_strprefix(
universe@576 648 cxstring string,
universe@576 649 cxstring prefix
universe@576 650 );
universe@576 651
universe@576 652 /**
universe@576 653 * Checks, if a string has a specific suffix.
universe@576 654 *
universe@576 655 * @param string the string to check
universe@576 656 * @param suffix the suffix the string should have
universe@576 657 * @return \c true, if and only if the string has the specified suffix,
universe@576 658 * \c false otherwise
universe@576 659 */
universe@576 660 __attribute__((__warn_unused_result__))
universe@576 661 int cx_strsuffix(
universe@576 662 cxstring string,
universe@576 663 cxstring suffix
universe@576 664 );
universe@576 665
universe@576 666 /**
universe@576 667 * Checks, if a string has a specific prefix, ignoring the case.
universe@576 668 *
universe@576 669 * @param string the string to check
universe@576 670 * @param prefix the prefix the string should have
universe@576 671 * @return \c true, if and only if the string has the specified prefix,
universe@576 672 * \c false otherwise
universe@576 673 */
universe@576 674 __attribute__((__warn_unused_result__))
universe@576 675 int cx_strcaseprefix(
universe@576 676 cxstring string,
universe@576 677 cxstring prefix
universe@576 678 );
universe@576 679
universe@576 680 /**
universe@576 681 * Checks, if a string has a specific suffix, ignoring the case.
universe@576 682 *
universe@576 683 * @param string the string to check
universe@576 684 * @param suffix the suffix the string should have
universe@576 685 * @return \c true, if and only if the string has the specified suffix,
universe@576 686 * \c false otherwise
universe@576 687 */
universe@576 688 __attribute__((__warn_unused_result__))
universe@576 689 int cx_strcasesuffix(
universe@576 690 cxstring string,
universe@576 691 cxstring suffix
universe@576 692 );
universe@576 693
universe@576 694 /**
universe@576 695 * Converts the string to lower case.
universe@576 696 *
universe@576 697 * The change is made in-place. If you want a copy, use cx_strdup(), first.
universe@576 698 *
universe@576 699 * @param string the string to modify
universe@576 700 * @see cx_strdup()
universe@576 701 */
universe@576 702 void cx_strlower(cxmutstr string);
universe@576 703
universe@576 704 /**
universe@576 705 * Converts the string to upper case.
universe@576 706 *
universe@576 707 * The change is made in-place. If you want a copy, use cx_strdup(), first.
universe@576 708 *
universe@576 709 * @param string the string to modify
universe@576 710 * @see cx_strdup()
universe@576 711 */
universe@576 712 void cx_strupper(cxmutstr string);
universe@576 713
universe@576 714 /**
universe@576 715 * Replaces a pattern in a string with another string.
universe@576 716 *
universe@576 717 * The pattern is taken literally and is no regular expression.
universe@576 718 * Replaces at most \p replmax occurrences.
universe@576 719 *
universe@576 720 * The returned string will be allocated by \c malloc() and \em must be passed
universe@576 721 * to cx_strfree() eventually.
universe@576 722 *
universe@576 723 * If allocation fails, or the input string is empty,
universe@576 724 * the returned string will point to \c NULL.
universe@576 725 *
universe@576 726 * @param str the string where replacements should be applied
universe@576 727 * @param pattern the pattern to search for
universe@576 728 * @param replacement the replacement string
universe@576 729 * @param replmax maximum number of replacements
universe@576 730 * @return the resulting string after applying the replacements
universe@576 731 */
universe@576 732 __attribute__((__warn_unused_result__))
universe@576 733 cxmutstr cx_strreplace(
universe@576 734 cxstring str,
universe@576 735 cxstring pattern,
universe@576 736 cxstring replacement,
universe@576 737 size_t replmax
universe@576 738 );
universe@576 739
universe@576 740 /**
universe@576 741 * Replaces a pattern in a string with another string.
universe@576 742 *
universe@576 743 * The pattern is taken literally and is no regular expression.
universe@576 744 * Replaces at most \p replmax occurrences.
universe@576 745 *
universe@576 746 * The returned string will be allocated by \p allocator.
universe@576 747 *
universe@576 748 * If allocation fails, or the input string is empty,
universe@576 749 * the returned string will point to \c NULL.
universe@576 750 *
universe@576 751 * @param allocator the allocator to use
universe@576 752 * @param str the string where replacements should be applied
universe@576 753 * @param pattern the pattern to search for
universe@576 754 * @param replacement the replacement string
universe@576 755 * @param replmax maximum number of replacements
universe@576 756 * @return the resulting string after applying the replacements
universe@576 757 */
universe@576 758 __attribute__((__warn_unused_result__, __nonnull__))
universe@576 759 cxmutstr cx_strreplace_a(
universe@576 760 CxAllocator *allocator,
universe@576 761 cxstring str,
universe@576 762 cxstring pattern,
universe@576 763 cxstring replacement,
universe@576 764 size_t replmax
universe@576 765 );
universe@576 766
universe@576 767 #ifdef __cplusplus
universe@576 768 } // extern "C"
universe@576 769 #endif
universe@576 770
universe@576 771 #endif //UCX_STRING_H

mercurial