ucx
UAP Common Extensions
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
36#ifndef UCX_STRING_H
37#define UCX_STRING_H
38
39#include "common.h"
40#include "allocator.h"
41
46extern const unsigned cx_strstr_sbo_size;
47
56 char *ptr;
58 size_t length;
59};
60
64typedef struct cx_mutstr_s cxmutstr;
65
74 const char *ptr;
76 size_t length;
77};
78
82typedef struct cx_string_s cxstring;
83
129
134
135#ifdef __cplusplus
136extern "C" {
137
143#define CX_STR(literal) cxstring{literal, sizeof(literal) - 1}
144
145#else // __cplusplus
146
154#define CX_STR(literal) ((cxstring){literal, sizeof(literal) - 1})
155
156#endif
157
158
178cxmutstr cx_mutstr(char *cstring);
179
200 char *cstring,
201 size_t length
202);
203
223cxstring cx_str(const char *cstring);
224
225
246 const char *cstring,
247 size_t length
248);
249
250#ifdef __cplusplus
251} // extern "C"
253static inline cxstring cx_strcast(cxmutstr str) {
254 return cx_strn(str.ptr, str.length);
255}
257static inline cxstring cx_strcast(cxstring str) {
258 return str;
259}
260extern "C" {
261#else
270 return (cxstring) {str.ptr, str.length};
271}
280 return str;
281}
282
295#define cx_strcast(str) _Generic((str), \
296 cxmutstr: cx_strcast_m, \
297 cxstring: cx_strcast_c) \
298 (str)
299#endif
300
315
332 const CxAllocator *alloc,
334);
335
351 size_t count,
352 ...
353);
354
381 const CxAllocator *alloc,
383 size_t count,
384 ...
385);
386
405#define cx_strcat_a(alloc, count, ...) \
406cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
407
425#define cx_strcat(count, ...) \
426cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
427
449#define cx_strcat_m(str, count, ...) \
450cx_strcat_ma(cxDefaultAllocator, str, count, __VA_ARGS__)
451
470 cxstring string,
471 size_t start
472);
473
496 cxstring string,
497 size_t start,
498 size_t length
499);
500
519 cxmutstr string,
520 size_t start
521);
522
545 cxmutstr string,
546 size_t start,
547 size_t length
548);
549
565 cxstring string,
566 int chr
567);
568
584 cxmutstr string,
585 int chr
586);
587
603 cxstring string,
604 int chr
605);
606
622 cxmutstr string,
623 int chr
624);
625
645 cxstring haystack,
646 cxstring needle
647);
648
668 cxmutstr haystack,
669 cxstring needle
670);
671
689 cxstring string,
691 size_t limit,
692 cxstring *output
693);
694
719 const CxAllocator *allocator,
720 cxstring string,
722 size_t limit,
723 cxstring **output
724);
725
726
744 cxmutstr string,
746 size_t limit,
747 cxmutstr *output
748);
749
774 const CxAllocator *allocator,
775 cxmutstr string,
777 size_t limit,
778 cxmutstr **output
779);
780
792 cxstring s1,
793 cxstring s2
794);
795
807 cxstring s1,
808 cxstring s2
809);
810
825 const void *s1,
826 const void *s2
827);
828
843 const void *s1,
844 const void *s2
845);
846
847
864 const CxAllocator *allocator,
865 cxstring string
866);
867
881#define cx_strdup_a(allocator, string) \
882 cx_strdup_a_((allocator), cx_strcast(string))
883
897#define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string)
898
911
924
936 cxstring string,
937 cxstring prefix
938);
939
951 cxstring string,
952 cxstring suffix
953);
954
966 cxstring string,
967 cxstring prefix
968);
969
981 cxstring string,
982 cxstring suffix
983);
984
1007 const CxAllocator *allocator,
1008 cxstring str,
1009 cxstring search,
1010 cxstring replacement,
1011 size_t replmax
1012);
1013
1031#define cx_strreplacen(str, search, replacement, replmax) \
1032cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, replmax)
1033
1049#define cx_strreplace_a(allocator, str, search, replacement) \
1050cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX)
1051
1066#define cx_strreplace(str, search, replacement) \
1067cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, SIZE_MAX)
1068
1080 cxstring str,
1082 size_t limit
1083);
1084
1093#define cx_strtok(str, delim, limit) \
1094 cx_strtok_(cx_strcast(str), cx_strcast(delim), (limit))
1095
1111 CxStrtokCtx *ctx,
1112 cxstring *token
1113);
1114
1134 CxStrtokCtx *ctx,
1135 cxmutstr *token
1136);
1137
1146cx_attr_access_r(2, 3)
1149 CxStrtokCtx *ctx,
1150 const cxstring *delim,
1151 size_t count
1152);
1153
1154/* ------------------------------------------------------------------------- *
1155 * string to number conversion functions *
1156 * ------------------------------------------------------------------------- */
1157
1173int cx_strtos_lc_(cxstring str, short *output, int base, const char *groupsep);
1174
1190int cx_strtoi_lc_(cxstring str, int *output, int base, const char *groupsep);
1191
1207int cx_strtol_lc_(cxstring str, long *output, int base, const char *groupsep);
1208
1224int cx_strtoll_lc_(cxstring str, long long *output, int base, const char *groupsep);
1225
1241int cx_strtoi8_lc_(cxstring str, int8_t *output, int base, const char *groupsep);
1242
1258int cx_strtoi16_lc_(cxstring str, int16_t *output, int base, const char *groupsep);
1259
1275int cx_strtoi32_lc_(cxstring str, int32_t *output, int base, const char *groupsep);
1276
1292int cx_strtoi64_lc_(cxstring str, int64_t *output, int base, const char *groupsep);
1293
1309int cx_strtous_lc_(cxstring str, unsigned short *output, int base, const char *groupsep);
1310
1326int cx_strtou_lc_(cxstring str, unsigned int *output, int base, const char *groupsep);
1327
1343int cx_strtoul_lc_(cxstring str, unsigned long *output, int base, const char *groupsep);
1344
1360int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep);
1361
1377int cx_strtou8_lc_(cxstring str, uint8_t *output, int base, const char *groupsep);
1378
1394int cx_strtou16_lc_(cxstring str, uint16_t *output, int base, const char *groupsep);
1395
1411int cx_strtou32_lc_(cxstring str, uint32_t *output, int base, const char *groupsep);
1412
1428int cx_strtou64_lc_(cxstring str, uint64_t *output, int base, const char *groupsep);
1429
1445int cx_strtoz_lc_(cxstring str, size_t *output, int base, const char *groupsep);
1446
1462int cx_strtof_lc_(cxstring str, float *output, char decsep, const char *groupsep);
1463
1479int cx_strtod_lc_(cxstring str, double *output, char decsep, const char *groupsep);
1480
1495#define cx_strtos_lc(str, output, base, groupsep) cx_strtos_lc_(cx_strcast(str), output, base, groupsep)
1496
1511#define cx_strtoi_lc(str, output, base, groupsep) cx_strtoi_lc_(cx_strcast(str), output, base, groupsep)
1512
1527#define cx_strtol_lc(str, output, base, groupsep) cx_strtol_lc_(cx_strcast(str), output, base, groupsep)
1528
1543#define cx_strtoll_lc(str, output, base, groupsep) cx_strtoll_lc_(cx_strcast(str), output, base, groupsep)
1544
1559#define cx_strtoi8_lc(str, output, base, groupsep) cx_strtoi8_lc_(cx_strcast(str), output, base, groupsep)
1560
1575#define cx_strtoi16_lc(str, output, base, groupsep) cx_strtoi16_lc_(cx_strcast(str), output, base, groupsep)
1576
1591#define cx_strtoi32_lc(str, output, base, groupsep) cx_strtoi32_lc_(cx_strcast(str), output, base, groupsep)
1592
1607#define cx_strtoi64_lc(str, output, base, groupsep) cx_strtoi64_lc_(cx_strcast(str), output, base, groupsep)
1608
1623#define cx_strtous_lc(str, output, base, groupsep) cx_strtous_lc_(cx_strcast(str), output, base, groupsep)
1624
1639#define cx_strtou_lc(str, output, base, groupsep) cx_strtou_lc_(cx_strcast(str), output, base, groupsep)
1640
1655#define cx_strtoul_lc(str, output, base, groupsep) cx_strtoul_lc_(cx_strcast(str), output, base, groupsep)
1656
1671#define cx_strtoull_lc(str, output, base, groupsep) cx_strtoull_lc_(cx_strcast(str), output, base, groupsep)
1672
1687#define cx_strtou8_lc(str, output, base, groupsep) cx_strtou8_lc_(cx_strcast(str), output, base, groupsep)
1688
1703#define cx_strtou16_lc(str, output, base, groupsep) cx_strtou16_lc_(cx_strcast(str), output, base, groupsep)
1704
1719#define cx_strtou32_lc(str, output, base, groupsep) cx_strtou32_lc_(cx_strcast(str), output, base, groupsep)
1720
1735#define cx_strtou64_lc(str, output, base, groupsep) cx_strtou64_lc_(cx_strcast(str), output, base, groupsep)
1736
1751#define cx_strtoz_lc(str, output, base, groupsep) cx_strtoz_lc_(cx_strcast(str), output, base, groupsep)
1752
1769#define cx_strtos(str, output, base) cx_strtos_lc_(cx_strcast(str), output, base, ",")
1770
1787#define cx_strtoi(str, output, base) cx_strtoi_lc_(cx_strcast(str), output, base, ",")
1788
1805#define cx_strtol(str, output, base) cx_strtol_lc_(cx_strcast(str), output, base, ",")
1806
1823#define cx_strtoll(str, output, base) cx_strtoll_lc_(cx_strcast(str), output, base, ",")
1824
1841#define cx_strtoi8(str, output, base) cx_strtoi8_lc_(cx_strcast(str), output, base, ",")
1842
1859#define cx_strtoi16(str, output, base) cx_strtoi16_lc_(cx_strcast(str), output, base, ",")
1860
1877#define cx_strtoi32(str, output, base) cx_strtoi32_lc_(cx_strcast(str), output, base, ",")
1878
1895#define cx_strtoi64(str, output, base) cx_strtoi64_lc_(cx_strcast(str), output, base, ",")
1896
1913#define cx_strtoz(str, output, base) cx_strtoz_lc_(cx_strcast(str), output, base, ",")
1914
1931#define cx_strtous(str, output, base) cx_strtous_lc_(cx_strcast(str), output, base, ",")
1932
1949#define cx_strtou(str, output, base) cx_strtou_lc_(cx_strcast(str), output, base, ",")
1950
1967#define cx_strtoul(str, output, base) cx_strtoul_lc_(cx_strcast(str), output, base, ",")
1968
1985#define cx_strtoull(str, output, base) cx_strtoull_lc_(cx_strcast(str), output, base, ",")
1986
2003#define cx_strtou8(str, output, base) cx_strtou8_lc_(cx_strcast(str), output, base, ",")
2004
2021#define cx_strtou16(str, output, base) cx_strtou16_lc_(cx_strcast(str), output, base, ",")
2022
2039#define cx_strtou32(str, output, base) cx_strtou32_lc_(cx_strcast(str), output, base, ",")
2040
2057#define cx_strtou64(str, output, base) cx_strtou64_lc_(cx_strcast(str), output, base, ",")
2058
2073#define cx_strtof_lc(str, output, decsep, groupsep) cx_strtof_lc_(cx_strcast(str), output, decsep, groupsep)
2074
2088#define cx_strtod_lc(str, output, decsep, groupsep) cx_strtod_lc_(cx_strcast(str), output, decsep, groupsep)
2089
2106#define cx_strtof(str, output) cx_strtof_lc_(cx_strcast(str), output, '.', ",")
2107
2123#define cx_strtod(str, output) cx_strtod_lc_(cx_strcast(str), output, '.', ",")
2124
2125#ifdef __cplusplus
2126} // extern "C"
2127#endif
2128
2129#endif //UCX_STRING_H
Interface for custom allocators.
Common definitions and feature checks.
#define cx_attr_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_access_w(...)
Specifies that the function will only write through the given pointer.
Definition common.h:241
#define cx_attr_access_r(...)
Specifies that the function will only read through the given pointer.
Definition common.h:225
#define cx_attr_access_rw(...)
Specifies that the function will read and write through the given pointer.
Definition common.h:233
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:141
#define cx_attr_cstr_arg(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:206
int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep)
Converts a string to a number.
const unsigned cx_strstr_sbo_size
The maximum length of the "needle" in cx_strstr() that can use SBO.
cxmutstr cx_strreplacen_a(const CxAllocator *allocator, cxstring str, cxstring search, cxstring replacement, size_t replmax)
Replaces a string with another string.
int cx_strtoz_lc_(cxstring str, size_t *output, int base, const char *groupsep)
Converts a string to a number.
int cx_strtoi16_lc_(cxstring str, int16_t *output, int base, const char *groupsep)
Converts a string to a number.
int cx_strcasecmp_p(const void *s1, const void *s2)
Compares two strings ignoring case.
int cx_strtoi64_lc_(cxstring str, int64_t *output, int base, const char *groupsep)
Converts a string to a number.
static cxstring cx_strcast_m(cxmutstr str)
Internal function, do not use.
Definition string.h:269
int cx_strtou_lc_(cxstring str, unsigned int *output, int base, const char *groupsep)
Converts a string to a number.
cxstring cx_strsubsl(cxstring string, size_t start, size_t length)
Returns a substring starting at the specified location.
int cx_strtou16_lc_(cxstring str, uint16_t *output, int base, const char *groupsep)
Converts a string to a number.
int cx_strtoi8_lc_(cxstring str, int8_t *output, int base, const char *groupsep)
Converts a string to a number.
cxstring cx_str(const char *cstring)
Wraps a string that must be zero-terminated.
void cx_strfree(cxmutstr *str)
Passes the pointer in this string to free().
int cx_strtos_lc_(cxstring str, short *output, int base, const char *groupsep)
Converts a string to a number.
cxmutstr cx_strsubsl_m(cxmutstr string, size_t start, size_t length)
Returns a substring starting at the specified location.
cxstring cx_strrchr(cxstring string, int chr)
Returns a substring starting at the location of the last occurrence of the specified character.
cxmutstr cx_strcat_ma(const CxAllocator *alloc, cxmutstr str, size_t count,...)
Concatenates strings.
int cx_strtous_lc_(cxstring str, unsigned short *output, int base, const char *groupsep)
Converts a string to a number.
size_t cx_strsplit_ma(const CxAllocator *allocator, cxmutstr string, cxstring delim, size_t limit, cxmutstr **output)
Splits a given string using a delimiter string.
cxmutstr cx_strdup_a_(const CxAllocator *allocator, cxstring string)
Creates a duplicate of the specified string.
bool cx_strtok_next(CxStrtokCtx *ctx, cxstring *token)
Returns the next token.
#define cx_strcast(str)
Casts a mutable string to an immutable string.
Definition string.h:295
int cx_strcmp_p(const void *s1, const void *s2)
Compares two strings.
int cx_strtoi32_lc_(cxstring str, int32_t *output, int base, const char *groupsep)
Converts a string to a number.
int cx_strtod_lc_(cxstring str, double *output, char decsep, const char *groupsep)
Converts a string to a double precision floating point number.
int cx_strtoi_lc_(cxstring str, int *output, int base, const char *groupsep)
Converts a string to a number.
size_t cx_strsplit(cxstring string, cxstring delim, size_t limit, cxstring *output)
Splits a given string using a delimiter string.
int cx_strtoul_lc_(cxstring str, unsigned long *output, int base, const char *groupsep)
Converts a string to a number.
int cx_strcasecmp(cxstring s1, cxstring s2)
Compares two strings ignoring case.
int cx_strtof_lc_(cxstring str, float *output, char decsep, const char *groupsep)
Converts a string to a single precision floating point number.
cxmutstr cx_strchr_m(cxmutstr string, int chr)
Returns a substring starting at the location of the first occurrence of the specified character.
CxStrtokCtx cx_strtok_(cxstring str, cxstring delim, size_t limit)
Creates a string tokenization context.
cxmutstr cx_mutstr(char *cstring)
Wraps a mutable string that must be zero-terminated.
cxstring cx_strchr(cxstring string, int chr)
Returns a substring starting at the location of the first occurrence of the specified character.
cxstring cx_strn(const char *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
size_t cx_strlen(size_t count,...)
Returns the accumulated length of all specified strings.
cxmutstr cx_strtrim_m(cxmutstr string)
Omits leading and trailing spaces.
bool cx_strtok_next_m(CxStrtokCtx *ctx, cxmutstr *token)
Returns the next token of a mutable string.
int cx_strtou64_lc_(cxstring str, uint64_t *output, int base, const char *groupsep)
Converts a string to a number.
size_t cx_strsplit_a(const CxAllocator *allocator, cxstring string, cxstring delim, size_t limit, cxstring **output)
Splits a given string using a delimiter string.
bool cx_strprefix(cxstring string, cxstring prefix)
Checks, if a string has a specific prefix.
bool cx_strcaseprefix(cxstring string, cxstring prefix)
Checks, if a string has a specific prefix, ignoring the case.
cxstring cx_strtrim(cxstring string)
Omits leading and trailing spaces.
int cx_strtou8_lc_(cxstring str, uint8_t *output, int base, const char *groupsep)
Converts a string to a number.
cxstring cx_strstr(cxstring haystack, cxstring needle)
Returns a substring starting at the location of the first occurrence of the specified string.
cxstring cx_strsubs(cxstring string, size_t start)
Returns a substring starting at the specified location.
int cx_strtoll_lc_(cxstring str, long long *output, int base, const char *groupsep)
Converts a string to a number.
cxmutstr cx_strsubs_m(cxmutstr string, size_t start)
Returns a substring starting at the specified location.
cxmutstr cx_strrchr_m(cxmutstr string, int chr)
Returns a substring starting at the location of the last occurrence of the specified character.
size_t cx_strsplit_m(cxmutstr string, cxstring delim, size_t limit, cxmutstr *output)
Splits a given string using a delimiter string.
int cx_strcmp(cxstring s1, cxstring s2)
Compares two strings.
bool cx_strsuffix(cxstring string, cxstring suffix)
Checks, if a string has a specific suffix.
void cx_strtok_delim(CxStrtokCtx *ctx, const cxstring *delim, size_t count)
Defines an array of more delimiters for the specified tokenization context.
cxmutstr cx_strstr_m(cxmutstr haystack, cxstring needle)
Returns a substring starting at the location of the first occurrence of the specified string.
bool cx_strcasesuffix(cxstring string, cxstring suffix)
Checks, if a string has a specific suffix, ignoring the case.
int cx_strtol_lc_(cxstring str, long *output, int base, const char *groupsep)
Converts a string to a number.
cxmutstr cx_mutstrn(char *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
int cx_strtou32_lc_(cxstring str, uint32_t *output, int base, const char *groupsep)
Converts a string to a number.
static cxstring cx_strcast_c(cxstring str)
Internal function, do not use.
Definition string.h:279
void cx_strfree_a(const CxAllocator *alloc, cxmutstr *str)
Passes the pointer in this string to the allocators free function.
Structure holding the data for an allocator.
Definition allocator.h:84
The UCX string structure.
Definition string.h:51
char * ptr
A pointer to the string.
Definition string.h:56
size_t length
The length of the string.
Definition string.h:58
The UCX string structure for immutable (constant) strings.
Definition string.h:69
size_t length
The length of the string.
Definition string.h:76
const char * ptr
A pointer to the immutable string.
Definition string.h:74
Context for string tokenizing.
Definition string.h:87
size_t delim_pos
Position of next delimiter in the source string.
Definition string.h:115
cxstring str
The string to tokenize.
Definition string.h:91
cxstring delim
The primary delimiter.
Definition string.h:95
size_t delim_more_count
Length of the array containing more delimiters.
Definition string.h:103
size_t pos
Position of the currently active token in the source string.
Definition string.h:107
size_t next_pos
The position of the next token in the source string.
Definition string.h:119
size_t found
The number of already found tokens.
Definition string.h:123
size_t limit
The maximum number of tokens that shall be returned.
Definition string.h:127
const cxstring * delim_more
Optional array of more delimiters.
Definition string.h:99