docs/Writerside/topics/string.h.md

Wed, 31 Dec 2025 16:32:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 31 Dec 2025 16:32:36 +0100
changeset 1697
ca825e816a50
parent 1696
976e629ce990
permissions
-rw-r--r--

Added tag v4.0 for changeset 976e629ce990

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # String
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
3 UCX strings store character arrays together with a length and come in two variants: immutable (`cxstring`) and mutable (`cxmutstr`).
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
5 In general, UCX strings are *not* necessarily zero-terminated.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
6 If a function guarantees to return a zero-terminated string, it is explicitly mentioned in the documentation.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
7 As a rule of thumb, you _should not_ pass a character array of a UCX string structure to another API without explicitly
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 ensuring that the string is zero-terminated.
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
9
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
10 ## Basics
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
11
1553
7c46531efd52 fix the note about AnyStr and add it to the files where it was missing
Mike Becker <universe@uap-core.de>
parents: 1426
diff changeset
12 The following listing shows basic string functions.
7c46531efd52 fix the note about AnyStr and add it to the files where it was missing
Mike Becker <universe@uap-core.de>
parents: 1426
diff changeset
13
1331
02946dc73e6a add support for C-strings in cx_strcast() - resolves #549
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
14 > To simplify documentation, we introduce the pseudo-type `AnyStr` with the meaning that
1553
7c46531efd52 fix the note about AnyStr and add it to the files where it was missing
Mike Becker <universe@uap-core.de>
parents: 1426
diff changeset
15 > any UCX string and any C string are supported.
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
16 > The implementation is actually hidden behind a macro which uses `cx_strcast()` to guarantee compatibility.
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
17 > Similarly, we introduce `UcxStr` with the meaning that it is either a `cxstring` or a `cxmutstr`,
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
18 > created by `cx_strcast_m()`.
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
19 {style="note"}
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
20
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
21 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
22 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
23
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
24 struct cx_string_s {const char *ptr; size_t length;};
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
25
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26 struct cx_mutstr_s {char *ptr; size_t length;};
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
27
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
28 typedef struct cx_string_s cxstring;
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
29
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
30 typedef struct cx_mutstr_s cxmutstr;
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
31
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
32 cxstring cx_str(const char *cstring);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
33
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
34 cxstring cx_strn(const char *cstring, size_t length);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
35
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
36 cxmutstr cx_mutstr(char *cstring);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
37
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
38 cxmutstr cx_mutstrn(char *cstring, size_t length);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
39
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
40 cxmutstr cx_strdup(AnyStr string);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
41
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
42 cxmutstr cx_strdup_a(const CxAllocator *allocator, AnyStr string);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
43
1644
bbe3199e37fc changes cx_strcpy() and cx_strcpy_a() to accept any string
Mike Becker <universe@uap-core.de>
parents: 1582
diff changeset
44 int cx_strcpy(cxmutstr *dest, AnyStr source);
1304
57e062a4bb05 adds cx_strcpy() and cx_strcpy_a()
Mike Becker <universe@uap-core.de>
parents: 1297
diff changeset
45
57e062a4bb05 adds cx_strcpy() and cx_strcpy_a()
Mike Becker <universe@uap-core.de>
parents: 1297
diff changeset
46 int cx_strcpy_a(const CxAllocator *allocator,
1644
bbe3199e37fc changes cx_strcpy() and cx_strcpy_a() to accept any string
Mike Becker <universe@uap-core.de>
parents: 1582
diff changeset
47 cxmutstr *dest, AnyStr source);
1304
57e062a4bb05 adds cx_strcpy() and cx_strcpy_a()
Mike Becker <universe@uap-core.de>
parents: 1297
diff changeset
48
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
49 void cx_strfree(cxmutstr *str);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
50
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
51 void cx_strfree_a(const CxAllocator *alloc, cxmutstr *str);
1297
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
52
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
53
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
54 #define CX_NULLSTR cx_mutstr(NULL)
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
55 #define CX_SFMT(s) (int) (s).length, (s).ptr
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
56 #define CX_PRIstr ".*s"
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
57 #define cx_strcast(s) // converts any string to cxstring
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
58 #define cx_strcast_m(s) // converts any string to a UcxStr
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
59 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
60
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
61 The functions `cx_str()` and `cx_mutstr()` create a UCX string from a `const char*` or a `char*`
1334
7763892ed801 allow NULL for creating UCX strings - resolves #683
Mike Becker <universe@uap-core.de>
parents: 1331
diff changeset
62 and compute the length with a call to stdlib `strlen()` (except for `NULL` in which case the length is set to zero).
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
63 In case you already know the length, or the string is not zero-terminated, you can use `cx_strn()` or `cx_mutstrn()`.
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
64
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
65 The function `cx_strdup_a()` allocates new memory with the given `allocator` and copies the given `string`
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
66 and guarantees that the result string is zero-terminated.
1318
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1304
diff changeset
67 The function `cx_strdup()` is equivalent to `cx_strdup_a()`, except that it uses the [default allocator](allocator.h.md#default-allocator).
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
68
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
69 The functions `cx_strcpy_a()` and `cx_strcpy()` copy the contents of the `source` string to the `dest` string
1304
57e062a4bb05 adds cx_strcpy() and cx_strcpy_a()
Mike Becker <universe@uap-core.de>
parents: 1297
diff changeset
70 and also guarantee zero-termination of the resulting string.
57e062a4bb05 adds cx_strcpy() and cx_strcpy_a()
Mike Becker <universe@uap-core.de>
parents: 1297
diff changeset
71 The memory in `dest` is either freshly allocated or re-allocated to fit the size of the string plus the terminator.
57e062a4bb05 adds cx_strcpy() and cx_strcpy_a()
Mike Becker <universe@uap-core.de>
parents: 1297
diff changeset
72
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
73 Allocated strings always have the type `cxmutstr` and can be deallocated by a call to `cx_strfree()` or `cx_strfree_a()`.
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
74 The caller must make sure to use the correct allocator for deallocating a string.
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
75 It is safe to call these functions multiple times on a given string, as the pointer will be nulled and the length set to zero.
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
76 It is also safe to call the functions with a `NULL`-pointer, just like any other `free()`-like function.
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
77
1297
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
78 When you want to use a UCX string in a `printf`-like function, you can use the macro `CX_PRIstr` for the format specifier,
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
79 and the `CX_SFMT(s)` macro to expand the arguments.
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
80
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
81 The macros `cx_strcast()` and `cx_strcast_m()` take any supported string (`AnyStr`) and convert it either to a `cxstring` or a `cxmutstr`.
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
82 While `cx_strcast()` _always_ converts to a `cxstring`, `cx_strcast_m()` decides depending on the constness of the string to return either a `cxstring` or a `cxmutstr`.
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
83 You should rarely need those macros in your code, as most UCX functions are already implemented behind macros that do the conversions for you.
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
84
1582
32b82c424252 removes the CX_STR() macro and instead makes the cx_str() inlinable
Mike Becker <universe@uap-core.de>
parents: 1553
diff changeset
85 > When you want to convert a string _literal_ into a UCX string, you can also use the `cx_str()` function.
32b82c424252 removes the CX_STR() macro and instead makes the cx_str() inlinable
Mike Becker <universe@uap-core.de>
parents: 1553
diff changeset
86 > With optimizations turned on, this function gets inlined and optimizes the call to `strlen()` out, giving
32b82c424252 removes the CX_STR() macro and instead makes the cx_str() inlinable
Mike Becker <universe@uap-core.de>
parents: 1553
diff changeset
87 > you a `cxstring` structure at zero cost with the length calculated at compile-time.
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
88
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
89 ## Comparison
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
90
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
91 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
92 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
93
1426
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
94 int cx_strcmp(AnyStr s1, AnyStr s2);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
95
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
96 int cx_strcmp_p(const void *s1, const void *s2);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
97
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
98 int cx_strcasecmp_p(const void *s1, const void *s2);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
99
1426
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
100 bool cx_strprefix(AnyStr string, AnyStr prefix);
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
101
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
102 bool cx_strsuffix(AnyStr string, AnyStr suffix);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
103
1426
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
104 int cx_strcasecmp(AnyStr s1, AnyStr s2);
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
105
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
106 bool cx_strcaseprefix(AnyStr string, AnyStr prefix);
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
107
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
108 bool cx_strcasesuffix(AnyStr string, AnyStr suffix);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
109 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
110
1426
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
111 The `cx_strcmp()` function compares two strings lexicographically
1219
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
112 and returns an integer greater than, equal to, or less than 0, if `s1` is greater than, equal to, or less than `s2`, respectively.
1426
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
113
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
114 The `cx_strcmp_p()` function takes pointers to UCX strings (i.e., only to `cxstring` and `cxmutstr`), and the signature is compatible with `cx_compare_func`.
1426
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
115 Use this as a compare function for lists or other data structures.
1219
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
116
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
117 The functions `cx_strprefix()` and `cx_strsuffic()` check if `string` starts with `prefix` or ends with `suffix`, respectively.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
118
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
119 The functions `cx_strcasecmp()`, `cx_strcasecmp_p()`, `cx_strcaseprefix()`, and `cx_strcasesuffix()` are equivalent,
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
120 except that they compare the strings case-insensitive.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
121
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
122 > In the current version of UCX, case-insensitive comparisons are only guaranteed to work with ASCII characters.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
123 {style="note"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
124
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
125 ## Concatenation
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
126
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
127 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
128 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
129
1672
94360453bce4 partially revert the changes to cx_strcat() and add CX_NULLSTR macro
Mike Becker <universe@uap-core.de>
parents: 1671
diff changeset
130 cxmutstr cx_strcat(cxmutstr str, size_t count, ... );
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
131
1672
94360453bce4 partially revert the changes to cx_strcat() and add CX_NULLSTR macro
Mike Becker <universe@uap-core.de>
parents: 1671
diff changeset
132 cxmutstr cx_strcat_a(const CxAllocator *allocator,
94360453bce4 partially revert the changes to cx_strcat() and add CX_NULLSTR macro
Mike Becker <universe@uap-core.de>
parents: 1671
diff changeset
133 cxmutstr str, size_t count, ... );
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
134
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
135 size_t cx_strlen(size_t count, ...);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
136 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
137
1667
608cc0b25352 changes cx_strcat() family of function to return an int and removes two unnecessary variants
Mike Becker <universe@uap-core.de>
parents: 1652
diff changeset
138 The `cx_strcat_a()` function takes `count` UCX strings (`cxstring` or `cxmutstr` - not pointers!),
1672
94360453bce4 partially revert the changes to cx_strcat() and add CX_NULLSTR macro
Mike Becker <universe@uap-core.de>
parents: 1671
diff changeset
139 reallocates the memory in `str` for a concatenation of those strings _with a single allocation_,
1667
608cc0b25352 changes cx_strcat() family of function to return an int and removes two unnecessary variants
Mike Becker <universe@uap-core.de>
parents: 1652
diff changeset
140 and appends the contents of the strings to `str`.
1318
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1304
diff changeset
141 `cx_strcat()` is equivalent, except that it uses the [default allocator](allocator.h.md#default-allocator).
1219
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
142
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
143 When there is no `str` where the other strings shall be appended to, you can pass `CX_NULLSTR` as the first argument.
1672
94360453bce4 partially revert the changes to cx_strcat() and add CX_NULLSTR macro
Mike Becker <universe@uap-core.de>
parents: 1671
diff changeset
144 In that case, a completely new string is allocated.
94360453bce4 partially revert the changes to cx_strcat() and add CX_NULLSTR macro
Mike Becker <universe@uap-core.de>
parents: 1671
diff changeset
145
1667
608cc0b25352 changes cx_strcat() family of function to return an int and removes two unnecessary variants
Mike Becker <universe@uap-core.de>
parents: 1652
diff changeset
146 Example usage:
608cc0b25352 changes cx_strcat() family of function to return an int and removes two unnecessary variants
Mike Becker <universe@uap-core.de>
parents: 1652
diff changeset
147 ```C
1696
976e629ce990 fix online docs examples
Mike Becker <universe@uap-core.de>
parents: 1694
diff changeset
148 cxmutstr str = cx_strcat(
976e629ce990 fix online docs examples
Mike Becker <universe@uap-core.de>
parents: 1694
diff changeset
149 CX_NULLSTR, 2,
976e629ce990 fix online docs examples
Mike Becker <universe@uap-core.de>
parents: 1694
diff changeset
150 cx_str("Hello, "),
976e629ce990 fix online docs examples
Mike Becker <universe@uap-core.de>
parents: 1694
diff changeset
151 cx_str("World!")
976e629ce990 fix online docs examples
Mike Becker <universe@uap-core.de>
parents: 1694
diff changeset
152 );
1667
608cc0b25352 changes cx_strcat() family of function to return an int and removes two unnecessary variants
Mike Becker <universe@uap-core.de>
parents: 1652
diff changeset
153 ```
1219
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
154
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
155 The function `cx_strlen()` sums the length of the specified strings.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
156
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
157 > There is no reason to use `cx_strlen()` for a single UCX string.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1334
diff changeset
158 > You can access the `length` field of the structure directly.
1219
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
159
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
160 > You can mix `cxstring` and `cxmutstr` in the variadic arguments without the need of `cx_strcast()`.
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
161
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
162 ## Find Characters and Substrings
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
163
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
164 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
165 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
166
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
167 char cx_strat(AnyStr str, off_t index);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
168
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
169 UcxStr cx_strchr(AnyStr string, int chr);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
170
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
171 UcxStr cx_strrchr(AnyStr string, int chr);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
172
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
173 UcxStr cx_strstr(AnyStr string, AnyStr search);
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
174
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
175 UcxStr cx_strsubs(AnyStr string, size_t start);
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
176
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
177 UcxStr cx_strsubsl(AnyStr string, size_t start, size_t length);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
178
1673
0c338b80e7dd add cx_strcast_m()
Mike Becker <universe@uap-core.de>
parents: 1672
diff changeset
179 UcxStr cx_strtrim(AnyStr string);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
180 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
181
1668
3ffdfe1776b4 add cx_strat()
Mike Becker <universe@uap-core.de>
parents: 1667
diff changeset
182 The function `cx_strat()` returns the character at the specified `index`.
3ffdfe1776b4 add cx_strat()
Mike Becker <universe@uap-core.de>
parents: 1667
diff changeset
183 When the `index` is negative, the characters are counted from the end of the string.
3ffdfe1776b4 add cx_strat()
Mike Becker <universe@uap-core.de>
parents: 1667
diff changeset
184 When the `index` is out of bounds, the zero-character is returned.
3ffdfe1776b4 add cx_strat()
Mike Becker <universe@uap-core.de>
parents: 1667
diff changeset
185
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
186 The functions `cx_strchr()`, `cx_strrchr()`, and `cx_strstr()`, behave like their stdlib counterparts.
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
187
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
188 The function `cx_strsubs()` returns the substring starting at the specified `start` index,
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
189 and `cx_strsubsl()` returns a substring with at most `length` bytes.
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
190
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
191 The function `cx_strtrim()` returns the substring that results when removing all leading and trailing
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1334
diff changeset
192 whitespace characters.
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
193
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
194 All functions with the `_m` suffix behave exactly the same as their counterparts without `_m` suffix,
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
195 except that they operate on a `cxmustr`.
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
196 In _both_ variants the functions return a view into the given `string`
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
197 and thus the returned strings must never be passed to `cx_strfree()`.
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
198
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
199 ## Replace Substrings
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
200
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
201 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
202 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
203
1651
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
204 cxmutstr cx_strreplace(AnyStr str,
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
205 AnyStr search, AnyStr replacement);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
206
1651
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
207 cxmutstr cx_strreplace_a(const CxAllocator *allocator, AnyStr str,
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
208 AnyStr search, AnyStr replacement);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
209
1651
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
210 cxmutstr cx_strreplacen(AnyStr str,
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
211 AnyStr search, AnyStr replacement, size_t replmax);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
212
1651
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
213 cxmutstr cx_strreplacen_a(const CxAllocator *allocator, AnyStr str,
38b051dea6b1 add support for any string to all cx_strreplace() variants
Mike Becker <universe@uap-core.de>
parents: 1646
diff changeset
214 AnyStr search, AnyStr replacement, size_t replmax);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
215 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
216
1221
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
217 The function `cx_strreplace()` allocates a new string which will contain a copy of `str`
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
218 where every occurrence of `search` is replaced with `replacement`.
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
219 The new string is guaranteed to be zero-terminated even if `str` is not.
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
220
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
221 The function `cx_strreplace_a()` uses the specified `allocator` to allocate the new string.
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
222
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
223 The functions `cx_strreplacen()` and `cx_strreplacen_a()` are equivalent, except that they stop
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
224 after `replmax` number of replacements.
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
225
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
226 ## Basic Splitting
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
227
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
229 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
230
1680
1aa21afb8763 add full generic support for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1673
diff changeset
231 size_t cx_strsplit(UcxStr string, AnyStr delim,
1aa21afb8763 add full generic support for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1673
diff changeset
232 size_t limit, UcxStr *output);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
233
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
234 size_t cx_strsplit_a(const CxAllocator *allocator,
1680
1aa21afb8763 add full generic support for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1673
diff changeset
235 UcxStr string, AnyStr delim,
1aa21afb8763 add full generic support for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1673
diff changeset
236 size_t limit, UcxStr **output);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
237 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
238
1222
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
239 The `cx_strsplit()` function splits the input `string` using the specified delimiter `delim`
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
240 and writes the substrings into the pre-allocated `output` array.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
241 The maximum number of resulting strings can be specified with `limit`.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
242 That means, at most `limit-1` splits are performed.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
243 The function returns the actual number of items written to `output`.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
244
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
245 On the other hand, `cx_strsplit_a()` uses the specified `allocator` to allocate the output array
1222
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
246 and writes the pointer to the allocated memory to `output`.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
247
1680
1aa21afb8763 add full generic support for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1673
diff changeset
248 > The type of the `UcxStr` must the same for `string` and `output` (i.e., either both `cxstring` or both `cxmutstr`).
1aa21afb8763 add full generic support for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1673
diff changeset
249 > {style="note"}
1222
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
250
1680
1aa21afb8763 add full generic support for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1673
diff changeset
251 > The `allocator` in `cx_strsplit_a()` is _only_ used to allocate the output array.
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
252 > The strings will always point into the original `string`,
1222
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
253 > and you need to use `cx_strdup()` or `cx_strdup_a()` if you want copies or zero-terminated strings after performing the split.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
254 {style="note"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
255
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
256 ## Complex Tokenization
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
257
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
258 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
259 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
260
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
261 CxStrtokCtx cx_strtok(AnyStr str, AnyStr delim, size_t limit);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
262
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
263 void cx_strtok_delim(CxStrtokCtx *ctx,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
264 const cxstring *delim, size_t count);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
265
1671
cf19b7820ff0 simplify cx_strtok_next() by removing the _m() variant - relates to #792
Mike Becker <universe@uap-core.de>
parents: 1668
diff changeset
266 bool cx_strtok_next(CxStrtokCtx *ctx, UcxStr* token);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
267 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
268
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
269 You can tokenize a string by creating a _tokenization_ context with `cx_strtok()`
1671
cf19b7820ff0 simplify cx_strtok_next() by removing the _m() variant - relates to #792
Mike Becker <universe@uap-core.de>
parents: 1668
diff changeset
270 and calling `cx_strtok_next()` as long as it returns `true`.
1226
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
271
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
272 The tokenization context is initialized with the string `str` to tokenize,
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
273 one delimiter `delim`, and a `limit` for the maximum number of tokens.
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
274 When `limit` is reached, the remaining part of `str` is returned as one single token.
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
275
1694
a2757c6427cc proof-read documentation
Mike Becker <universe@uap-core.de>
parents: 1680
diff changeset
276 You can add additional delimiters to the context by calling `cx_strtok_delim()` and
1226
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
277 specifying an array of delimiters to use.
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
278
1671
cf19b7820ff0 simplify cx_strtok_next() by removing the _m() variant - relates to #792
Mike Becker <universe@uap-core.de>
parents: 1668
diff changeset
279 > Regardless of how the context was initialized, you can use `cx_strtok_next()`
cf19b7820ff0 simplify cx_strtok_next() by removing the _m() variant - relates to #792
Mike Becker <universe@uap-core.de>
parents: 1668
diff changeset
280 > with pointers to `cxstring` or `cxmutstr`. However, keep in mind that modifying
cf19b7820ff0 simplify cx_strtok_next() by removing the _m() variant - relates to #792
Mike Becker <universe@uap-core.de>
parents: 1668
diff changeset
281 > characters in a `cxmutstr` has only defined behavior, when the
cf19b7820ff0 simplify cx_strtok_next() by removing the _m() variant - relates to #792
Mike Becker <universe@uap-core.de>
parents: 1668
diff changeset
282 > underlying `str` is also a `cxmutstr` that was not initalized with constant memory.
1226
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
283
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
284 ### Example
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
285
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
286 ```C
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
287 #include <cx/string.h>
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
288
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
289 cxstring str = cx_str("an,arbitrarily;||separated;string");
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
290
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
291 // create the context
1582
32b82c424252 removes the CX_STR() macro and instead makes the cx_str() inlinable
Mike Becker <universe@uap-core.de>
parents: 1553
diff changeset
292 CxStrtokCtx ctx = cx_strtok(str, ",", 10);
1226
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
293
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
294 // add two more delimters
1582
32b82c424252 removes the CX_STR() macro and instead makes the cx_str() inlinable
Mike Becker <universe@uap-core.de>
parents: 1553
diff changeset
295 cxstring delim_more[2] = {cx_str("||"), cx_str(";")};
1226
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
296 cx_strtok_delim(&ctx, delim_more, 2);
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
297
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
298 // iterate over the tokens
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
299 cxstring tok;
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
300 while(cx_strtok_next(&ctx, &tok)) {
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
301 // to something with the tokens
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
302 // be aware that tok is NOT zero-terminated!
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
303 }
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
304 ```
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
305
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
306 ## Conversion to Numbers
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
307
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1334
diff changeset
308 For each integer type, as well as `float` and `double`, there are functions to convert a UCX string to a value of those types.
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
309
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1334
diff changeset
310 Integer conversion comes in two flavors:
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
311 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
312 int cx_strtoi(AnyStr str, int *output, int base);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
313
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
314 int cx_strtoi_lc(AnyStr str, int *output, int base,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
315 const char *groupsep);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
316 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
317
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
318 The basic variant takes a string of any UCX string type, a pointer to the `output` integer, and the `base` (one of 2, 8, 10, or 16).
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
319 Conversion is attempted with respect to the specified `base` and respects possible special notations for that base.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
320 Hexadecimal numbers may be prefixed with `0x`, `x`, or `#`, and binary numbers may be prefixed with `0b` or `b`.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
321
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
322 The `_lc` versions of the integer conversion functions are equivalent, except that they allow the specification of an
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
323 array of group separator chars, each of which is simply ignored during conversion.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
324 The default group separator for the basic version is a comma `,`.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
325
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1334
diff changeset
326 The signature for the floating-point conversions is quite similar:
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
327 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
328 int cx_strtof(AnyStr str, float *output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
329
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
330 int cx_strtof_lc(AnyStr str, float *output,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
331 char decsep, const char *groupsep);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
332 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
333
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1334
diff changeset
334 The two differences are that the floating-point versions do not support different bases,
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
335 and the `_lc` variant allows specifying not only an array of group separators,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
336 but also the character used for the decimal separator.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
337
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
338 In the basic variant, the group separator is again a comma `,`, and the decimal separator is a dot `.`.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
339
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1334
diff changeset
340 > The floating-point conversions of UCX 3.1 do not achieve the same precision as standard library implementations
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
341 > which usually use more sophisticated algorithms.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
342 > The precision might increase in future UCX releases,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
343 > but until then be aware of slight inaccuracies, in particular when working with `double`.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
344 {style="warning"}
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
345
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
346 > The UCX string to number conversions are intentionally not considering any locale settings
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
347 > and are therefore independent of any global state.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
348 {style="note"}
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
349
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
350 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
351 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
352 <a href="https://ucx.sourceforge.io/api/string_8h.html">string.h</a>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
353 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
354 </seealso>

mercurial