docs/Writerside/topics/string.h.md

Thu, 20 Feb 2025 20:49:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 20 Feb 2025 20:49:04 +0100
changeset 1218
cbb48edaf433
parent 1217
23f25e91d367
child 1219
9c1c33ac077a
permissions
-rw-r--r--

write Section about basic string functions

relates to #451

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);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
45 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
46
1218
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
47 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
48 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
49 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
50
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
51 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
52 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
53 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
54
cbb48edaf433 write Section about basic string functions
Mike Becker <universe@uap-core.de>
parents: 1217
diff changeset
55 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
56 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
57 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
58 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
59
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
60 > 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
61 > 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
62 > effectively saving an invocation of `strlen()`.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
63 > 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
64
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
65 ## Comparison
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
66
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
67 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
68 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
69
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
70 int cx_strcmp(cxstring s1, cxstring s2);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
71
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
72 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
73
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
74 bool cx_strprefix(cxstring string, cxstring prefix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
75
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
76 bool cx_strsuffix(cxstring string, cxstring suffix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
77
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
78 int cx_strcasecmp(cxstring s1, cxstring s2);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
79
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
80 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
81
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
82 bool cx_strcaseprefix(cxstring string, cxstring prefix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
83
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
84 bool cx_strcasesuffix(cxstring string, cxstring suffix);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
85 ```
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 > Documentation work in progress.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
88 >{style="warning"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
89
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
90 ## Concatenation
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
91
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
92 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
93 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
94
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
95 cxmutstr cx_strcat(size_t count, ... );
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
96
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
97 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
98
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
99 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
100
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
101 cxmutstr cx_strcat_ma(const CxAllocator *alloc,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
102 cxmutstr str, size_t count, ... );
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
103
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
104 size_t cx_strlen(size_t count, ...);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
105 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
106
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
107 > Documentation work in progress.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
108 >{style="warning"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
109
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
110 ## Find Characters and Substrings
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
111
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
112 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
113 #include <cx/string.h>
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 cxstring cx_strchr(cxstring string, int chr);
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_strchr_m(cxmutstr string, int chr);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
118
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
119 cxstring cx_strrchr(cxstring string,int chr);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
120
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
121 cxmutstr cx_strrchr_m(cxmutstr string, int chr);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
122
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
123 cxstring cx_strstr(cxstring haystack, cxstring needle);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
124
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
125 cxmutstr cx_strstr_m(cxmutstr haystack, cxstring needle);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
126
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
127 cxstring cx_strsubs(cxstring string, size_t start);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
128
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
129 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
130
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
131 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
132
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
133 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
134
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
135 cxstring cx_strtrim(cxstring string);
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 cxmutstr cx_strtrim_m(cxmutstr string);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
138 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
139
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
140 > Documentation work in progress.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
141 >{style="warning"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
142
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
143 ## Replace Substrings
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
144
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
145 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
146 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
147
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
148 cxmutstr cx_strreplace(cxstring str, cxstring pattern, cxstring repl);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
149
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
150 cxmutstr cx_strreplace_a(const CxAllocator *allocator, cxstring str,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
151 cxstring pattern, cxstring repl);
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 cxmutstr cx_strreplacen(cxstring str, cxstring pattern, cxstring repl,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
154 size_t replmax);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
155
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
156 cxmutstr cx_strreplacen_a(const CxAllocator *allocator, cxstring str,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
157 cxstring pattern, cxstring repl, size_t replmax);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
158 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
159
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
160 > Documentation work in progress.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
161 >{style="warning"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
162
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
163 ## Basic Splitting
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
164
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
165 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
166 #include <cx/string.h>
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
167
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
168 size_t cx_strsplit(cxstring string, cxstring delim,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
169 size_t limit, cxstring *output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
170
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
171 size_t cx_strsplit_a(const CxAllocator *allocator,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
172 cxstring string, cxstring delim,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
173 size_t limit, cxstring **output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
174
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
175 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
176 size_t limit, cxmutstr *output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
177
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
178 size_t cx_strsplit_ma(const CxAllocator *allocator,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
179 cxmutstr string, cxstring delim,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
180 size_t limit, cxmutstr **output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
181 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
182
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
183 > Documentation work in progress.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
184 >{style="warning"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
185
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
186 ## Complex Tokenization
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
187
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
188 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
189 #include <cx/string.h>
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 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
192
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
193 void cx_strtok_delim(CxStrtokCtx *ctx,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
194 const cxstring *delim, size_t count);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
195
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
196 bool cx_strtok_next(CxStrtokCtx *ctx, cxstring *token);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
197
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
198 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
199 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
200
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
201 > Documentation work in progress.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
202 >{style="warning"}
1165
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
203
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
204 ## Conversion to Numbers
e4e2c43d12c2 basic structur for the strings chapter
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
205
1217
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
206 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
207
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
208 Integer conversion comes in two flavours:
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
209 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
210 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
211
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
212 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
213 const char *groupsep);
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
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
216 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
217 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
218 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
219
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220 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
221 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
222 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
223
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
224 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
225 ```C
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226 int cx_strtof(AnyStr str, float *output);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
227
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228 int cx_strtof_lc(AnyStr str, float *output,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
229 char decsep, const char *groupsep);
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
230 ```
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
231
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
232 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
233 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
234 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
235
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
236 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
237
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
238 > 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
239 > which usually use more sophisticated algorithms.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
240 > The precision might increase in future UCX releases,
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
241 > 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
242 {style="warning"}
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
243
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
244 > 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
245 > and are therefore independent of any global state.
23f25e91d367 start documenting the string functions
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
246 {style="note"}
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
247
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
248 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
249 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
250 <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
251 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1165
diff changeset
252 </seealso>

mercurial