docs/Writerside/topics/string.h.md

Wed, 16 Apr 2025 20:35:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 Apr 2025 20:35:34 +0200
changeset 1297
0811fb9a8dba
parent 1226
129ef9bb1477
permissions
-rw-r--r--

bring back CX_PRIstr and CX_SFMT macros - resolves #612

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
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
12 > To make documentation simpler, we introduce the pseudo-type `AnyStr` with the meaning that
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
13 > both `cxstring` and `cxmutstr` are accepted for that argument.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
14 > The implementation is actually hidden behind a macro which uses `cx_strcast()` to guarantee compatibility.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
15 {style="note"}
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
16
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
17 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
18 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
19
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
20 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
21
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
22 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
23
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
24 typedef struct cx_string_s cxstring;
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 typedef struct cx_mutstr_s cxmutstr;
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 cxstring cx_str(const char *cstring);
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 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
31
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
32 cxmutstr cx_mutstr(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 cxmutstr cx_mutstrn(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 cxstring cx_strcast(AnyStr str);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
37
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
38 cxmutstr cx_strdup(AnyStr string);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
39
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
40 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
41
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
42 void cx_strfree(cxmutstr *str);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
43
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
44 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
45
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
46
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
47 #define CX_SFMT(s) (int) (s).length, (s).ptr
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
48 #define CX_PRIstr ".*s"
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
49 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
50
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
51 The functions `cx_str()` and `cx_mutstr()` create a UCX string from a `const char*` or a `char*`
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
52 and compute the length with a call to stdlib `strlen()`.
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
53 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
54
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
55 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
56 and guarantees that the result string is zero-terminated.
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
57 The function `cx_strdup()` is equivalent to `cx_strdup_a()`, except that it uses the default stdlib allocator.
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
58
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
59 Allocated strings are always of type `cxmutstr` and can be deallocated by a call to `cx_strfree()` or `cx_strfree_a()`.
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
60 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
61 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
62 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
63
1297
0811fb9a8dba bring back CX_PRIstr and CX_SFMT macros - resolves #612
Mike Becker <universe@uap-core.de>
parents: 1226
diff changeset
64 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
65 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
66
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
67 > When you want to convert a string _literal_ into a UCX string, you can also use the `CX_STR(lit)` macro.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
68 > This macro uses the fact that `sizeof(lit)` for a string literal `lit` is always the string length plus one,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
69 > effectively saving an invocation of `strlen()`.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
70 > However, this only works for literals - in all other cases you must use `cx_str()` or `cx_strn`.
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
71
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
72 ## Comparison
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
73
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
74 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
75 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
76
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
77 int cx_strcmp(cxstring s1, cxstring s2);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
78
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
79 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
80
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
81 bool cx_strprefix(cxstring string, cxstring prefix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
82
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
83 bool cx_strsuffix(cxstring string, cxstring suffix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
84
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
85 int cx_strcasecmp(cxstring s1, cxstring s2);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
86
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
87 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
88
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
89 bool cx_strcaseprefix(cxstring string, cxstring prefix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
90
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
91 bool cx_strcasesuffix(cxstring string, cxstring suffix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
92 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
93
1219
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
94 The `cx_strcmp()` function compares two UCX strings lexicographically
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
95 and returns an integer greater than, equal to, or less than 0, if `s1` is greater than, equal to, or less than `s2`, respectively.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
96 The `cx_strcmp_p()` function is equivalent, except that it takes pointers to the UCX strings and the signature is compatible with `cx_compare_func`.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
97
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
98 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
99
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
100 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
101 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
102
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
103 > 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
104 {style="note"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
105
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
106 ## Concatenation
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
107
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
108 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
109 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
110
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
111 cxmutstr cx_strcat(size_t count, ... );
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
112
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
113 cxmutstr cx_strcat_a(const CxAllocator *alloc, size_t count, ... );
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
114
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
115 cxmutstr cx_strcat_m(cxmutstr str, size_t count, ... );
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
116
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
117 cxmutstr cx_strcat_ma(const CxAllocator *alloc,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
118 cxmutstr str, size_t count, ... );
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
119
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
120 size_t cx_strlen(size_t count, ...);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
121 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
122
1219
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
123 The `cx_strcat_a()` function takes `count` UCX strings,
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
124 allocates memory for a concatenation of those strings _with a single allocation_,
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
125 and copies the contents of the strings to the new memory.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
126 `cx_strcat()` is equivalent, except that is uses the default stdlib allocator.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
127
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
128 The `cx_strcat_ma()` and `cx_strcat_m()` append the `count` strings to the specified string `str` and,
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
129 instead of allocating new memory, reallocate the existing memory in `str`.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
130 If the pointer in `str` is `NULL`, there is no difference to `cx_strcat_a()`.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
131 Note, that `count` always denotes the number of variadic arguments in _both_ variants.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
132
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
133 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
134
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
135 > There is no reason to use `cx_strlen()` for a single UCX string.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
136 > Just access the `length` field of the structure directly.
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
137
9c1c33ac077a write section in string docu about comparisons and concatenation
Mike Becker <universe@uap-core.de>
parents: 1218
diff changeset
138 > 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
139
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
140 ## Find Characters and Substrings
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
141
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
142 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
143 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
144
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
145 cxstring cx_strchr(cxstring string, int chr);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
146
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
147 cxstring cx_strrchr(cxstring string, int chr);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
148
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
149 cxstring cx_strstr(cxstring string, cxstring search);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
150
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
151 cxstring cx_strsubs(cxstring string, size_t start);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
152
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
153 cxstring cx_strsubsl(cxstring string, size_t start, size_t length);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
154
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
155 cxstring cx_strtrim(cxstring string);
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
156
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
157 cxmutstr cx_strchr_m(cxmutstr string, int chr);
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
158
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
159 cxmutstr cx_strrchr_m(cxmutstr string, int chr);
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
160
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
161 cxmutstr cx_strstr_m(cxmutstr string, cxstring search);
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
162
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
163 cxmutstr cx_strsubs_m(cxmutstr string, size_t start);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
164
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
165 cxmutstr cx_strsubsl_m(cxmutstr string, size_t start, size_t length);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
166
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
167 cxmutstr cx_strtrim_m(cxmutstr string);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
168 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
169
1220
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
170 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
171
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
172 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
173 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
174
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
175 The function `cx_strtrim()` returns the substring that results when removing all leading and trailing
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
176 whitespace characters (a space character is one of the following string: `" \t\r\n\v\f"`).
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
177
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
178 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
179 except that they operate on a `cxmustr`.
e9dfb1e92481 document substring family of functions
Mike Becker <universe@uap-core.de>
parents: 1219
diff changeset
180 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
181 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
182
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
183 ## Replace Substrings
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
184
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
185 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
186 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
187
1221
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
188 cxmutstr cx_strreplace(cxstring str,
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
189 cxstring search, cxstring replacement);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
190
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
191 cxmutstr cx_strreplace_a(const CxAllocator *allocator, cxstring str,
1221
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
192 cxstring search, cxstring replacement);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
193
1221
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
194 cxmutstr cx_strreplacen(cxstring str,
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
195 cxstring search, cxstring replacement, size_t replmax);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
196
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
197 cxmutstr cx_strreplacen_a(const CxAllocator *allocator, cxstring str,
1221
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
198 cxstring search, cxstring replacement, size_t replmax);
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
199 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
200
1221
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
201 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
202 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
203 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
204
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
205 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
206
304f4f7b37d1 document cx_strreplace() family of functions and improve docstrings
Mike Becker <universe@uap-core.de>
parents: 1220
diff changeset
207 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
208 after `replmax` number of replacements.
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
209
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
210 ## Basic Splitting
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
211
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
212 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
213 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
214
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
215 size_t cx_strsplit(cxstring string, cxstring delim,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
216 size_t limit, cxstring *output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
218 size_t cx_strsplit_a(const CxAllocator *allocator,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
219 cxstring string, cxstring delim,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220 size_t limit, cxstring **output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
221
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
222 size_t cx_strsplit_m(cxmutstr string, cxstring delim,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
223 size_t limit, cxmutstr *output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
224
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
225 size_t cx_strsplit_ma(const CxAllocator *allocator,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226 cxmutstr string, cxstring delim,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
227 size_t limit, cxmutstr **output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
229
1222
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
230 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
231 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
232 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
233 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
234 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
235
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
236 On the other hand, `cx_strsplit_a()` uses the specified `allocator` to allocate the output array,
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
237 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
238
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
239 The functions `cx_strsplit_m()` and `cx_strsplit_ma()` are equivalent to `cx_strsplit()` and `cx_strsplit_a()`,
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
240 except that they work on `cxmustr` instead of `cxstring`.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
241
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
242 > The `allocator` in `cx_strsplit_a()` and `cx_strsplit_ma()` is _only_ used to allocate the output array.
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
243 > The strings will always point into the original `string`
952e712df557 add documentation for cx_strsplit()
Mike Becker <universe@uap-core.de>
parents: 1221
diff changeset
244 > 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
245 {style="note"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
246
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
247 ## Complex Tokenization
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
248
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
249 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
250 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
251
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
252 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
253
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
254 void cx_strtok_delim(CxStrtokCtx *ctx,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
255 const cxstring *delim, size_t count);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
256
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
257 bool cx_strtok_next(CxStrtokCtx *ctx, cxstring *token);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
258
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
259 bool cx_strtok_next_m(CxStrtokCtx *ctx, cxmutstr *token);
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
1226
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
262 You can tokenize a string by creating a _tokenization_ context with `cx_strtok()`,
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
263 and calling `cx_strtok_next()` or `cx_strtok_next_m()` as long as they return `true`.
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
264
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
265 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
266 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
267 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
268
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
269 You can add additional delimiters to the context by calling `cx_strtok_delim()`, and
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
270 specifying an array of delimiters to use.
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 > Regardless of how the context was initialized, you can use either `cx_strtok_next()`
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
273 > or `cx_strtok_next_m()` to retrieve the tokens. However, keep in mind that modifying
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
274 > characters in a token returned by `cx_strtok_next_m()` has only defined behavior, when the
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
275 > underlying `str` is a `cxmutstr`.
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
276
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
277 ### Example
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
278
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
279 ```C
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
280 #include <cx/string.h>
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
281
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
282 cxstring str = cx_str("an,arbitrarily;||separated;string");
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 // create the context
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
285 CxStrtokCtx ctx = cx_strtok(str, CX_STR(","), 10);
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
286
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
287 // add two more delimters
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
288 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
289 cx_strtok_delim(&ctx, delim_more, 2);
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 // iterate over the tokens
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
292 cxstring tok;
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
293 while(cx_strtok_next(&ctx, &tok)) {
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
294 // to something with the tokens
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
295 // be aware that tok is NOT zero-terminated!
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
296 }
129ef9bb1477 complete documentation for string.h
Mike Becker <universe@uap-core.de>
parents: 1222
diff changeset
297 ```
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
298
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
299 ## Conversion to Numbers
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
300
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
301 For each integer type, as well as `float` and `double`, there are functions to convert a UCX string to a number of that type.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
302
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
303 Integer conversion comes in two flavours:
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
304 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
305 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
306
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
307 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
308 const char *groupsep);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
309 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
310
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
311 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
312 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
313 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
314
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
315 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
316 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
317 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
318
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
319 The signature for the floating point conversions is quite similar:
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
320 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
321 int cx_strtof(AnyStr str, float *output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
322
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
323 int cx_strtof_lc(AnyStr str, float *output,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
324 char decsep, const char *groupsep);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
325 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
326
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
327 The two differences are that the floating point versions do not support different bases,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
328 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
329 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
330
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
331 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
332
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
333 > The floating point conversions of UCX 3.1 do not achieve the same precision as standard library implementations
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
334 > which usually use more sophisticated algorithms.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
335 > The precision might increase in future UCX releases,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
336 > 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
337 {style="warning"}
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
338
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
339 > 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
340 > and are therefore independent of any global state.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
341 {style="note"}
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
342
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
343 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
344 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
345 <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
346 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
347 </seealso>

mercurial