Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : #include "cvector.h"
28 :
29 : // A heap string that grows by itself. The buffer always holds a NUL-terminated
30 : // C string and its capacity is a power of two, 16 bytes at least. It offers the
31 : // usual operations (append, insert, find, replace, split, ...) and takes a
32 : // custom allocator.
33 : //
34 : // The maps don't know cstr as a key type; use cstring_c_str() as a char * key.
35 : // The map copies the content, so changing or destroying the cstr later doesn't
36 : // touch the entry.
37 : //
38 : // Everything from here to the matching pop is the public ABI and the shared
39 : // library exports it. The library builds with -fvisibility=hidden, so anything
40 : // outside these blocks stays internal.
41 : #pragma GCC visibility push(default)
42 :
43 : // The string type. It's opaque; code holds a cstr.
44 : typedef struct cstring cstring;
45 :
46 : // The handle of a growable, NUL-terminated string, a pointer to an opaque
47 : // cstring. Make one with cstr_construct() or cstring_create() and free it with
48 : // cstr_destroy() or cstring_destroy().
49 : typedef cstring *cstr;
50 :
51 : // Creates a string holding a copy of initial (NULL for an empty one), with
52 : // your own allocator. m_procs is the allocator table, or NULL for malloc and
53 : // free; all four of its functions must be set, and the string keeps its own
54 : // copy of the table. The capacity is rounded up to a power of two, 16 at
55 : // least. Gives the handle, or NULL on failure with a library-owned error
56 : // string in *err when err isn't NULL. Destroy it with cstring_destroy().
57 : cstr cstring_create_full(const char *initial, ccol_memmgmt_procs *m_procs,
58 : char **err);
59 :
60 : // Creates a string holding a copy of initial (NULL for an empty one), using
61 : // malloc and free. The capacity is rounded up to a power of two, 16 at least.
62 : // Gives the handle, or NULL on failure with a library-owned error string in
63 : // *err when err isn't NULL. Destroy it with cstring_destroy().
64 : static inline __attribute__((always_inline)) cstr
65 20568 : cstring_create(const char *initial, char **err) {
66 20565 : return cstring_create_full(initial, NULL, err);
67 : }
68 :
69 : // Creates a string holding a copy of initial (NULL for an empty one), using
70 : // malloc and free, for callers that only check for NULL: It gives the handle,
71 : // or NULL when an allocation fails, with no error string. Destroy it with
72 : // cstring_destroy().
73 : static inline __attribute__((always_inline)) cstr
74 10 : cstring_new(const char *initial) {
75 6 : return cstring_create_full(initial, NULL, NULL);
76 : }
77 :
78 : // Gives the allocator of s, or NULL for the default one, so a caller can
79 : // allocate related memory the same way. Asserts on a NULL s.
80 : ccol_memmgmt_procs *cstring_get_mprocs(cstr s);
81 :
82 : // Internal: Frees the string. Use cstring_destroy() instead.
83 : void cstring__destroy(cstr s);
84 :
85 : // Frees the string and sets s to NULL, so a second call is harmless. s must be
86 : // a modifiable lvalue and is evaluated once; a NULL s does nothing. Pointers
87 : // from cstring_c_str() are invalid afterwards. It's a statement.
88 : #define cstring_destroy(s) \
89 : ccol__cstring_destroy_impl( \
90 : s, ccol__uniq(ccol__cstring_destroy_slot, __COUNTER__))
91 :
92 : // Internal: The body of cstring_destroy(). slot comes from ccol__uniq(), so
93 : // destroy macros can nest without -Wshadow warnings.
94 : #define ccol__cstring_destroy_impl(s, slot) \
95 : do { \
96 : __typeof__(s) *slot = &(s); \
97 : if (*slot) { \
98 : cstring__destroy(*slot); \
99 : *slot = NULL; \
100 : } \
101 : } while (0)
102 :
103 : // Internal: Destroys *sp and sets it to NULL. It's the cleanup function the
104 : // _scoped forms attach with ccol__destructor.
105 2 : static inline void cstring__destroy_cleanup(cstr *sp) {
106 2 : if (sp && *sp) {
107 2 : cstring__destroy(*sp);
108 2 : *sp = NULL;
109 : }
110 : }
111 :
112 : // Gives the length of s in bytes, without the '\0'. Asserts on a NULL s.
113 : size_t cstring_length(cstr s);
114 :
115 : // Gives the NUL-terminated content of s, for any API that takes a char *. The
116 : // pointer is valid until the next change to s. Asserts on a NULL s.
117 : const char *cstring_c_str(cstr s);
118 :
119 : // Gives the byte at idx, or '\0' when idx is out of range. Asserts on a NULL s.
120 : char cstring_at(cstr s, size_t idx);
121 :
122 : // Says whether s has length 0. Asserts on a NULL s.
123 : bool cstring_is_empty(cstr s);
124 :
125 : // Appends str to s, growing the buffer when needed; str may point into s. It
126 : // gives ccol_success, ccol_invalid_args for a NULL str, ccol_container_full
127 : // when the length would overflow size_t, or ccol_not_enough_memory, with s
128 : // unchanged on failure. Asserts on a NULL s.
129 : ccol_retval cstring_append(cstr s, const char *str);
130 :
131 : // Puts str in front of the content of s, growing the buffer when needed. It
132 : // gives ccol_success, ccol_invalid_args for a NULL str, ccol_container_full
133 : // when the length would overflow size_t, or ccol_not_enough_memory, with s
134 : // unchanged on failure. Asserts on a NULL s.
135 : ccol_retval cstring_prepend(cstr s, const char *str);
136 :
137 : // Inserts str at byte position pos (0 to the length), moving the rest to the
138 : // right; str may point into s. It gives ccol_success, ccol_invalid_args for a
139 : // NULL str or a pos past the end, ccol_container_full when the length would
140 : // overflow size_t, or ccol_not_enough_memory, with s unchanged on failure.
141 : // Asserts on a NULL s.
142 : ccol_retval cstring_insert(cstr s, size_t pos, const char *str);
143 :
144 : // Replaces the content of s with a copy of str; str may point into s. It
145 : // gives ccol_success, ccol_invalid_args for a NULL str, or
146 : // ccol_not_enough_memory, with s unchanged on failure (ccol_container_full
147 : // would need a str that fills the address space). Asserts on a NULL s.
148 : ccol_retval cstring_set(cstr s, const char *str);
149 :
150 : // Empties s and tries to shrink its buffer back to 16 bytes; when that realloc
151 : // fails the capacity stays, but the length is 0 anyway. Asserts on a NULL s.
152 : void cstring_reset(cstr s);
153 :
154 : // Makes room for at least min_capacity bytes, the '\0' included, rounded up to
155 : // a power of two (16 at least), so later appends don't reallocate. It never
156 : // shrinks. Gives false when the size is too large or the realloc fails. Asserts
157 : // on a NULL s.
158 : bool cstring_reserve(cstr s, size_t min_capacity);
159 :
160 : // Turns the ASCII lower-case letters of s into upper case in place. Every other
161 : // byte stays, whatever the locale. Asserts on a NULL s.
162 : void cstring_to_upper(cstr s);
163 :
164 : // Turns the ASCII upper-case letters of s into lower case in place. Every other
165 : // byte stays, whatever the locale. Asserts on a NULL s.
166 : void cstring_to_lower(cstr s);
167 :
168 : // Removes white space at both ends of s in place. White space is the six ASCII
169 : // bytes space, tab, LF, VT, FF and CR, whatever the locale, so a byte above
170 : // 0x7F (a piece of UTF-8) is never cut. Asserts on a NULL s.
171 : void cstring_trim(cstr s);
172 :
173 : // Replaces every non-overlapping needle in s with replacement. The result is
174 : // built in a new buffer, so a replacement that contains needle is fine. It
175 : // gives ccol_success (also when needle isn't found), ccol_invalid_args for a
176 : // NULL or empty needle or a NULL replacement, ccol_container_full on a length
177 : // overflow, or ccol_not_enough_memory. Asserts on a NULL s.
178 : ccol_retval cstring_replace(cstr s, const char *needle,
179 : const char *replacement);
180 :
181 : // Compares s with str as strcmp() does; a NULL str gives 1. Asserts on a NULL
182 : // s.
183 : int cstring_compare(cstr s, const char *str);
184 :
185 : // Says whether the content of s equals str; a NULL str gives false. Asserts on
186 : // a NULL s.
187 : bool cstring_equals(cstr s, const char *str);
188 :
189 : // Says whether s starts with prefix. An empty prefix matches, a NULL one
190 : // doesn't. Asserts on a NULL s.
191 : bool cstring_starts_with(cstr s, const char *prefix);
192 :
193 : // Says whether s ends with suffix. An empty suffix matches, a NULL one doesn't.
194 : // Asserts on a NULL s.
195 : bool cstring_ends_with(cstr s, const char *suffix);
196 :
197 : // Gives the index of the first needle in s, 0 for an empty needle, or
198 : // ccol_invalid_size when it isn't there or needle is NULL. Asserts on a NULL s.
199 : size_t cstring_find(cstr s, const char *needle);
200 :
201 : // Gives the index of the last needle in s (overlaps count: The last "aba" in
202 : // "ababa" is at 2), the length of s for an empty needle, or ccol_invalid_size
203 : // when it isn't there or needle is NULL. When s has 256 bytes or more, the
204 : // search runs backwards in time linear in both lengths. A needle longer than
205 : // 128 bytes may take a temporary table of one size_t per byte from the
206 : // allocator of s; if that's refused the answer is the same but the time can be
207 : // the product of the lengths. Asserts on a NULL s.
208 : size_t cstring_rfind(cstr s, const char *needle);
209 :
210 : // Creates a string holding the bytes [start, start + length) of s, clamped to
211 : // its end; a start at or past the end gives an empty string. The new string
212 : // uses the allocator of s. Gives NULL, with a library-owned error string in
213 : // *err when err isn't NULL, when the allocation fails. You destroy the result.
214 : // Asserts on a NULL s.
215 : cstr cstring_substring(cstr s, size_t start, size_t length, char **err);
216 :
217 : // Creates an independent copy of s that uses the allocator of s, or gives NULL
218 : // on failure with a library-owned error string in *err when err isn't NULL.
219 : // You destroy the result. Asserts on a NULL s.
220 : cstr cstring_copy(cstr s, char **err);
221 :
222 : // Splits s at every delimiter (not NULL or empty) into a cvec of new cstr
223 : // tokens, with empty tokens kept; "a,,b" split at "," gives "a", "" and "b".
224 : // Gives NULL on failure, with a library-owned error string in *err when err
225 : // isn't NULL. You destroy every token and then the vector. Asserts on a NULL
226 : // s.
227 : cvec cstring_split(cstr s, const char *delimiter, char **err);
228 :
229 : // Declares a string handle named s without creating the string. s is
230 : // uninitialized; create the string with cstr_init() or cstr_init_mp() and free
231 : // it with cstr_destroy(). It's a declaration.
232 : #define cstr_declare(s) cstr s
233 :
234 : // Declares a string handle named s, set to NULL. Create the string with
235 : // cstr_init() or cstr_init_mp(). Whatever string s holds is destroyed when s
236 : // goes out of scope, on every way out, so don't destroy it yourself and don't
237 : // return it. It's a declaration.
238 : #define cstr_declare_scoped(s) \
239 : cstr s ccol__destructor(cstring__destroy_cleanup) = NULL
240 :
241 : // Creates a string holding a copy of initial (NULL for an empty one), using
242 : // malloc and free, and assigns it to s, which cstr_declare() or
243 : // cstr_declare_scoped() declared. s is assigned once, after the creation. A
244 : // failed creation prints the reason and ends the program. It's a statement.
245 : #define cstr_init(s, initial) \
246 : do { \
247 : char *cstr__init_err = NULL; \
248 : __typeof__(s) cstr__init_s = cstring_create((initial), &cstr__init_err); \
249 : if (!cstr__init_s) { \
250 : ccol__fatal_err("cstr_init('%s'): %s", #s, \
251 : cstr__init_err ? cstr__init_err : "unknown error"); \
252 : } \
253 : (s) = cstr__init_s; \
254 : } while (0)
255 :
256 : // Creates a string holding a copy of initial (NULL for an empty one) and
257 : // assigns it to s, which cstr_declare() or cstr_declare_scoped() declared.
258 : // mprocs is the allocator table, or NULL for malloc and free; the string keeps
259 : // its own copy. A failed creation (an allocator with a NULL function included)
260 : // prints the reason and ends the program. It's a statement.
261 : #define cstr_init_mp(s, initial, mprocs) \
262 : do { \
263 : char *cstr__init_mp_err = NULL; \
264 : __typeof__(s) cstr__init_mp_s = \
265 : cstring_create_full((initial), (mprocs), &cstr__init_mp_err); \
266 : if (!cstr__init_mp_s) { \
267 : ccol__fatal_err( \
268 : "cstr_init_mp('%s'): %s", #s, \
269 : cstr__init_mp_err ? cstr__init_mp_err : "unknown error"); \
270 : } \
271 : (s) = cstr__init_mp_s; \
272 : } while (0)
273 :
274 : // Declares s and creates in it a string holding a copy of initial (NULL for an
275 : // empty one), using malloc and free. A failed creation prints the reason and
276 : // ends the program. Free the string with cstr_destroy().
277 : #define cstr_construct(s, initial) \
278 : cstr_declare(s); \
279 : cstr_init(s, initial)
280 :
281 : // Declares s and creates in it a string holding a copy of initial (NULL for an
282 : // empty one), using malloc and free. The string is destroyed when s goes out
283 : // of scope, on every way out, so don't destroy it yourself and don't return
284 : // it. A failed creation prints the reason and ends the program.
285 : #define cstr_construct_scoped(s, initial) \
286 : cstr_declare_scoped(s); \
287 : cstr_init(s, initial)
288 :
289 : // Declares s and creates in it a string holding a copy of initial (NULL for an
290 : // empty one). mprocs is the allocator table, or NULL for malloc and free; the
291 : // string keeps its own copy. A failed creation prints the reason and ends the
292 : // program. Free the string with cstr_destroy().
293 : #define cstr_construct_mp(s, initial, mprocs) \
294 : cstr_declare(s); \
295 : cstr_init_mp(s, initial, mprocs)
296 :
297 : // Declares s and creates in it a string holding a copy of initial (NULL for an
298 : // empty one). mprocs is the allocator table, or NULL for malloc and free; the
299 : // string keeps its own copy. The string is destroyed when s goes out of scope,
300 : // on every way out, so don't destroy it yourself and don't return it. A failed
301 : // creation prints the reason and ends the program.
302 : #define cstr_construct_mp_scoped(s, initial, mprocs) \
303 : cstr_declare_scoped(s); \
304 : cstr_init_mp(s, initial, mprocs)
305 :
306 : // Frees the string and sets s to NULL, so a second call is harmless. s must be
307 : // a modifiable lvalue and is evaluated once; a NULL s does nothing. It's a
308 : // statement.
309 : #define cstr_destroy(s) cstring_destroy((s))
310 :
311 : // Appends str to s, growing the buffer when needed; str may point into s. A
312 : // NULL str, a length that would overflow size_t or a failed allocation prints
313 : // the error code and ends the program. Asserts on a NULL s. It's a statement.
314 : #define cstr_append(s, str) \
315 : do { \
316 : ccol_retval cstr__append_r = cstring_append((s), (str)); \
317 : if (cstr__append_r != ccol_success) { \
318 : ccol__fatal_err("cstr_append('%s'): r: %d (%s)", #s, cstr__append_r, \
319 : ccol_retval_to_str(cstr__append_r)); \
320 : } \
321 : } while (0)
322 :
323 : // Puts str in front of the content of s, growing the buffer when needed. A
324 : // NULL str, a length that would overflow size_t or a failed allocation prints
325 : // the error code and ends the program. Asserts on a NULL s. It's a statement.
326 : #define cstr_prepend(s, str) \
327 : do { \
328 : ccol_retval cstr__prepend_r = cstring_prepend((s), (str)); \
329 : if (cstr__prepend_r != ccol_success) { \
330 : ccol__fatal_err("cstr_prepend('%s'): r: %d (%s)", #s, cstr__prepend_r, \
331 : ccol_retval_to_str(cstr__prepend_r)); \
332 : } \
333 : } while (0)
334 :
335 : // Inserts str into s at byte position pos (0 to the length), moving the rest
336 : // to the right; str may point into s. A NULL str, a pos past the end, a length
337 : // that would overflow size_t or a failed allocation prints the error code and
338 : // ends the program. Asserts on a NULL s. It's a statement.
339 : #define cstr_insert(s, pos, str) \
340 : do { \
341 : ccol_retval cstr__insert_r = cstring_insert((s), (pos), (str)); \
342 : if (cstr__insert_r != ccol_success) { \
343 : ccol__fatal_err("cstr_insert('%s'): r: %d (%s)", #s, cstr__insert_r, \
344 : ccol_retval_to_str(cstr__insert_r)); \
345 : } \
346 : } while (0)
347 :
348 : // Replaces the whole content of s with a copy of str; str may point into s. A
349 : // NULL str or a failed allocation prints the error code and ends the program.
350 : // Asserts on a NULL s. It's a statement.
351 : #define cstr_set(s, str) \
352 : do { \
353 : ccol_retval cstr__set_r = cstring_set((s), (str)); \
354 : if (cstr__set_r != ccol_success) { \
355 : ccol__fatal_err("cstr_set('%s'): r: %d (%s)", #s, cstr__set_r, \
356 : ccol_retval_to_str(cstr__set_r)); \
357 : } \
358 : } while (0)
359 :
360 : // Replaces every non-overlapping needle in s with replacement; a replacement
361 : // that contains needle is fine, and a needle that isn't there changes nothing.
362 : // A NULL or empty needle, a NULL replacement, a length overflow or a failed
363 : // allocation prints the error code and ends the program. Asserts on a NULL s.
364 : // It's a statement.
365 : #define cstr_replace(s, needle, replacement) \
366 : do { \
367 : ccol_retval cstr__replace_r = \
368 : cstring_replace((s), (needle), (replacement)); \
369 : if (cstr__replace_r != ccol_success) { \
370 : ccol__fatal_err("cstr_replace('%s'): r: %d (%s)", #s, cstr__replace_r, \
371 : ccol_retval_to_str(cstr__replace_r)); \
372 : } \
373 : } while (0)
374 :
375 : // Makes room in s for at least cap bytes, the '\0' included, rounded up to a
376 : // power of two (16 at least), so later appends don't reallocate. It never
377 : // shrinks. cap is evaluated once. A size that is too large or a failed
378 : // allocation prints an error and ends the program. It's a statement.
379 : #define cstr_reserve(s, cap) \
380 : ccol__cstr_reserve_impl(s, (cap), \
381 : ccol__uniq(ccol__cstr_reserve_cap, __COUNTER__))
382 :
383 : // Internal: The body of cstr_reserve(), with a ccol__uniq() temporary so it can
384 : // nest in any public macro.
385 : #define ccol__cstr_reserve_impl(s, cap, cstr__reserve_cap) \
386 : do { \
387 : size_t cstr__reserve_cap = (cap); \
388 : if (!cstring_reserve((s), cstr__reserve_cap)) { \
389 : ccol__fatal_err( \
390 : "cstr_reserve('%s'): failed to reserve %zu bytes (out of memory?)", \
391 : #s, cstr__reserve_cap); \
392 : } \
393 : } while (0)
394 :
395 : // Evaluates to a new cstr holding the bytes [start, start + len) of s,
396 : // clamped to its end; a start at or past the end gives an empty string. The
397 : // new string uses the allocator of s, and you destroy it. Each argument is
398 : // evaluated once. A failed allocation prints the reason and ends the program.
399 : #define cstr_substring(s, start, len) \
400 : ccol__cstr_substring_impl( \
401 : s, (start), (len), ccol__uniq(ccol__cstr_substring_start, __COUNTER__), \
402 : ccol__uniq(ccol__cstr_substring_len, __COUNTER__), \
403 : ccol__uniq(ccol__cstr_substring_err, __COUNTER__), \
404 : ccol__uniq(ccol__cstr_substring_result, __COUNTER__))
405 :
406 : // Internal: The body of cstr_substring(), with ccol__uniq() temporaries so it
407 : // can nest in any public macro.
408 : #define ccol__cstr_substring_impl(s, start, len, cstr__substring_start, \
409 : cstr__substring_len, cstr__substring_err, \
410 : cstr__substring_result) \
411 : ({ \
412 : size_t cstr__substring_start = (start); \
413 : size_t cstr__substring_len = (len); \
414 : char *cstr__substring_err = NULL; \
415 : __auto_type cstr__substring_result = \
416 : cstring_substring((s), cstr__substring_start, cstr__substring_len, \
417 : &cstr__substring_err); \
418 : if (!cstr__substring_result) { \
419 : ccol__fatal_err( \
420 : "cstr_substring('%s', start=%zu, len=%zu): %s", #s, \
421 : cstr__substring_start, cstr__substring_len, \
422 : cstr__substring_err ? cstr__substring_err : "unknown error"); \
423 : } \
424 : cstr__substring_result; \
425 : })
426 :
427 : // Evaluates to the length of s in bytes, without the '\0', as a size_t.
428 : // Asserts on a NULL s.
429 : #define cstr_length(s) cstring_length((s))
430 :
431 : // Evaluates to the NUL-terminated content of s as a const char *, for any API
432 : // that takes a C string. The pointer is valid until the next change to s.
433 : // Asserts on a NULL s.
434 : #define cstr_c_str(s) cstring_c_str((s))
435 :
436 : // Evaluates to the byte at idx of s as a char, or '\0' when idx is out of
437 : // range. Asserts on a NULL s.
438 : #define cstr_at(s, idx) cstring_at((s), (idx))
439 :
440 : // Evaluates to true when s holds no bytes. Asserts on a NULL s.
441 : #define cstr_is_empty(s) cstring_is_empty((s))
442 :
443 : // Empties s and tries to shrink its buffer back to 16 bytes, so you can fill
444 : // it again; when the shrink fails the capacity stays, but the length is 0
445 : // anyway. Asserts on a NULL s.
446 : #define cstr_reset(s) cstring_reset((s))
447 :
448 : // Turns the ASCII lower-case letters of s into upper case in place. Every other
449 : // byte stays, whatever the locale. Asserts on a NULL s.
450 : #define cstr_to_upper(s) cstring_to_upper((s))
451 :
452 : // Turns the ASCII upper-case letters of s into lower case in place. Every other
453 : // byte stays, whatever the locale. Asserts on a NULL s.
454 : #define cstr_to_lower(s) cstring_to_lower((s))
455 :
456 : // Removes white space at both ends of s in place: The six ASCII bytes space,
457 : // tab, LF, VT, FF and CR, whatever the locale, so a byte above 0x7F (a piece
458 : // of UTF-8) is never cut. Asserts on a NULL s.
459 : #define cstr_trim(s) cstring_trim((s))
460 :
461 : // Compares s with str as strcmp() does and evaluates to a negative, zero or
462 : // positive int; a NULL str gives 1. Asserts on a NULL s.
463 : #define cstr_compare(s, str) cstring_compare((s), (str))
464 :
465 : // Evaluates to true when the content of s equals str; a NULL str gives false.
466 : // Asserts on a NULL s.
467 : #define cstr_equals(s, str) cstring_equals((s), (str))
468 :
469 : // Evaluates to true when s starts with pfx. An empty pfx matches, a NULL one
470 : // doesn't. Asserts on a NULL s.
471 : #define cstr_starts_with(s, pfx) cstring_starts_with((s), (pfx))
472 :
473 : // Evaluates to true when s ends with sfx. An empty sfx matches, a NULL one
474 : // doesn't. Asserts on a NULL s.
475 : #define cstr_ends_with(s, sfx) cstring_ends_with((s), (sfx))
476 :
477 : // Evaluates to the index of the first needle in s, 0 for an empty needle, or
478 : // ccol_invalid_size when it isn't there or needle is NULL. Asserts on a NULL
479 : // s.
480 : #define cstr_find(s, needle) cstring_find((s), (needle))
481 :
482 : // Evaluates to the index of the last needle in s (overlaps count: The last
483 : // "aba" in "ababa" is at 2), the length of s for an empty needle, or
484 : // ccol_invalid_size when it isn't there or needle is NULL. Asserts on a NULL
485 : // s.
486 : #define cstr_rfind(s, needle) cstring_rfind((s), (needle))
487 :
488 : // Evaluates to a new, independent copy of s that uses the allocator of s, or
489 : // to NULL on failure with a library-owned error string in *err when err isn't
490 : // NULL. You destroy the copy. Asserts on a NULL s.
491 : #define cstr_copy(s, err) cstring_copy((s), (err))
492 :
493 : // Splits s at every occurrence of delim (not NULL or empty) and evaluates to a
494 : // cvec of new cstr tokens, with empty tokens kept; "a,,b" split at "," gives
495 : // "a", "" and "b". On failure it gives NULL, with a library-owned error string
496 : // in *err when err isn't NULL. You destroy every token and then the vector.
497 : // Asserts on a NULL s.
498 : #define cstr_split(s, delim, err) cstring_split((s), (delim), (err))
499 :
500 : #pragma GCC visibility pop
|