ucx/cx/string.h

changeset 101
7b3a3130be44
parent 49
2f71f4ee247a
equal deleted inserted replaced
100:d2bd73d28ff1 101:7b3a3130be44
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file string.h 29 * @file string.h
30 * \brief Strings that know their length. 30 * @brief Strings that know their length.
31 * \author Mike Becker 31 * @author Mike Becker
32 * \author Olaf Wintermann 32 * @author Olaf Wintermann
33 * \copyright 2-Clause BSD License 33 * @copyright 2-Clause BSD License
34 */ 34 */
35 35
36 #ifndef UCX_STRING_H 36 #ifndef UCX_STRING_H
37 #define UCX_STRING_H 37 #define UCX_STRING_H
38 38
40 #include "allocator.h" 40 #include "allocator.h"
41 41
42 /** 42 /**
43 * The maximum length of the "needle" in cx_strstr() that can use SBO. 43 * The maximum length of the "needle" in cx_strstr() that can use SBO.
44 */ 44 */
45 extern unsigned const cx_strstr_sbo_size; 45 extern const unsigned cx_strstr_sbo_size;
46 46
47 /** 47 /**
48 * The UCX string structure. 48 * The UCX string structure.
49 */ 49 */
50 struct cx_mutstr_s { 50 struct cx_mutstr_s {
51 /** 51 /**
52 * A pointer to the string. 52 * A pointer to the string.
53 * \note The string is not necessarily \c NULL terminated. 53 * @note The string is not necessarily @c NULL terminated.
54 * Always use the length. 54 * Always use the length.
55 */ 55 */
56 char *ptr; 56 char *ptr;
57 /** The length of the string */ 57 /** The length of the string */
58 size_t length; 58 size_t length;
67 * The UCX string structure for immutable (constant) strings. 67 * The UCX string structure for immutable (constant) strings.
68 */ 68 */
69 struct cx_string_s { 69 struct cx_string_s {
70 /** 70 /**
71 * A pointer to the immutable string. 71 * A pointer to the immutable string.
72 * \note The string is not necessarily \c NULL terminated. 72 * @note The string is not necessarily @c NULL terminated.
73 * Always use the length. 73 * Always use the length.
74 */ 74 */
75 const char *ptr; 75 const char *ptr;
76 /** The length of the string */ 76 /** The length of the string */
77 size_t length; 77 size_t length;
146 #else // __cplusplus 146 #else // __cplusplus
147 147
148 /** 148 /**
149 * A literal initializer for an UCX string structure. 149 * A literal initializer for an UCX string structure.
150 * 150 *
151 * The argument MUST be a string (const char*) \em literal. 151 * The argument MUST be a string (const char*) @em literal.
152 * 152 *
153 * @param literal the string literal 153 * @param literal the string literal
154 */ 154 */
155 #define CX_STR(literal) (cxstring){literal, sizeof(literal) - 1} 155 #define CX_STR(literal) (cxstring){literal, sizeof(literal) - 1}
156 156
158 158
159 159
160 /** 160 /**
161 * Wraps a mutable string that must be zero-terminated. 161 * Wraps a mutable string that must be zero-terminated.
162 * 162 *
163 * The length is implicitly inferred by using a call to \c strlen(). 163 * The length is implicitly inferred by using a call to @c strlen().
164 * 164 *
165 * \note the wrapped string will share the specified pointer to the string. 165 * @note the wrapped string will share the specified pointer to the string.
166 * If you do want a copy, use cx_strdup() on the return value of this function. 166 * If you do want a copy, use cx_strdup() on the return value of this function.
167 * 167 *
168 * If you need to wrap a constant string, use cx_str(). 168 * If you need to wrap a constant string, use cx_str().
169 * 169 *
170 * @param cstring the string to wrap, must be zero-terminated 170 * @param cstring the string to wrap, must be zero-terminated
171 * @return the wrapped string 171 * @return the wrapped string
172 * 172 *
173 * @see cx_mutstrn() 173 * @see cx_mutstrn()
174 */ 174 */
175 __attribute__((__warn_unused_result__, __nonnull__)) 175 cx_attr_nonnull
176 cx_attr_nodiscard
177 cx_attr_cstr_arg(1)
176 cxmutstr cx_mutstr(char *cstring); 178 cxmutstr cx_mutstr(char *cstring);
177 179
178 /** 180 /**
179 * Wraps a string that does not need to be zero-terminated. 181 * Wraps a string that does not need to be zero-terminated.
180 * 182 *
181 * The argument may be \c NULL if the length is zero. 183 * The argument may be @c NULL if the length is zero.
182 * 184 *
183 * \note the wrapped string will share the specified pointer to the string. 185 * @note the wrapped string will share the specified pointer to the string.
184 * If you do want a copy, use cx_strdup() on the return value of this function. 186 * If you do want a copy, use cx_strdup() on the return value of this function.
185 * 187 *
186 * If you need to wrap a constant string, use cx_strn(). 188 * If you need to wrap a constant string, use cx_strn().
187 * 189 *
188 * @param cstring the string to wrap (or \c NULL, only if the length is zero) 190 * @param cstring the string to wrap (or @c NULL, only if the length is zero)
189 * @param length the length of the string 191 * @param length the length of the string
190 * @return the wrapped string 192 * @return the wrapped string
191 * 193 *
192 * @see cx_mutstr() 194 * @see cx_mutstr()
193 */ 195 */
194 __attribute__((__warn_unused_result__)) 196 cx_attr_nodiscard
197 cx_attr_access_rw(1, 2)
195 cxmutstr cx_mutstrn( 198 cxmutstr cx_mutstrn(
196 char *cstring, 199 char *cstring,
197 size_t length 200 size_t length
198 ); 201 );
199 202
200 /** 203 /**
201 * Wraps a string that must be zero-terminated. 204 * Wraps a string that must be zero-terminated.
202 * 205 *
203 * The length is implicitly inferred by using a call to \c strlen(). 206 * The length is implicitly inferred by using a call to @c strlen().
204 * 207 *
205 * \note the wrapped string will share the specified pointer to the string. 208 * @note the wrapped string will share the specified pointer to the string.
206 * If you do want a copy, use cx_strdup() on the return value of this function. 209 * If you do want a copy, use cx_strdup() on the return value of this function.
207 * 210 *
208 * If you need to wrap a non-constant string, use cx_mutstr(). 211 * If you need to wrap a non-constant string, use cx_mutstr().
209 * 212 *
210 * @param cstring the string to wrap, must be zero-terminated 213 * @param cstring the string to wrap, must be zero-terminated
211 * @return the wrapped string 214 * @return the wrapped string
212 * 215 *
213 * @see cx_strn() 216 * @see cx_strn()
214 */ 217 */
215 __attribute__((__warn_unused_result__, __nonnull__)) 218 cx_attr_nonnull
219 cx_attr_nodiscard
220 cx_attr_cstr_arg(1)
216 cxstring cx_str(const char *cstring); 221 cxstring cx_str(const char *cstring);
217 222
218 223
219 /** 224 /**
220 * Wraps a string that does not need to be zero-terminated. 225 * Wraps a string that does not need to be zero-terminated.
221 * 226 *
222 * The argument may be \c NULL if the length is zero. 227 * The argument may be @c NULL if the length is zero.
223 * 228 *
224 * \note the wrapped string will share the specified pointer to the string. 229 * @note the wrapped string will share the specified pointer to the string.
225 * If you do want a copy, use cx_strdup() on the return value of this function. 230 * If you do want a copy, use cx_strdup() on the return value of this function.
226 * 231 *
227 * If you need to wrap a non-constant string, use cx_mutstrn(). 232 * If you need to wrap a non-constant string, use cx_mutstrn().
228 * 233 *
229 * @param cstring the string to wrap (or \c NULL, only if the length is zero) 234 * @param cstring the string to wrap (or @c NULL, only if the length is zero)
230 * @param length the length of the string 235 * @param length the length of the string
231 * @return the wrapped string 236 * @return the wrapped string
232 * 237 *
233 * @see cx_str() 238 * @see cx_str()
234 */ 239 */
235 __attribute__((__warn_unused_result__)) 240 cx_attr_nodiscard
241 cx_attr_access_r(1, 2)
236 cxstring cx_strn( 242 cxstring cx_strn(
237 const char *cstring, 243 const char *cstring,
238 size_t length 244 size_t length
239 ); 245 );
240 246
247 #ifdef __cplusplus
248 } // extern "C"
249 cx_attr_nodiscard
250 static inline cxstring cx_strcast(cxmutstr str) {
251 return cx_strn(str.ptr, str.length);
252 }
253 cx_attr_nodiscard
254 static inline cxstring cx_strcast(cxstring str) {
255 return str;
256 }
257 extern "C" {
258 #else
259 /**
260 * Internal function, do not use.
261 * @param str
262 * @return
263 * @see cx_strcast()
264 */
265 cx_attr_nodiscard
266 static inline cxstring cx_strcast_m(cxmutstr str) {
267 return (cxstring) {str.ptr, str.length};
268 }
269 /**
270 * Internal function, do not use.
271 * @param str
272 * @return
273 * @see cx_strcast()
274 */
275 cx_attr_nodiscard
276 static inline cxstring cx_strcast_c(cxstring str) {
277 return str;
278 }
279
241 /** 280 /**
242 * Casts a mutable string to an immutable string. 281 * Casts a mutable string to an immutable string.
243 * 282 *
244 * \note This is not seriously a cast. Instead you get a copy 283 * Does nothing for already immutable strings.
284 *
285 * @note This is not seriously a cast. Instead, you get a copy
245 * of the struct with the desired pointer type. Both structs still 286 * of the struct with the desired pointer type. Both structs still
246 * point to the same location, though! 287 * point to the same location, though!
247 * 288 *
248 * @param str the mutable string to cast 289 * @param str (@c cxstring or @c cxmutstr) the string to cast
249 * @return an immutable copy of the string pointer 290 * @return (@c cxstring) an immutable copy of the string pointer
250 */ 291 */
251 __attribute__((__warn_unused_result__)) 292 #define cx_strcast(str) _Generic((str), \
252 cxstring cx_strcast(cxmutstr str); 293 cxmutstr: cx_strcast_m, \
253 294 cxstring: cx_strcast_c) \
254 /** 295 (str)
255 * Passes the pointer in this string to \c free(). 296 #endif
256 * 297
257 * The pointer in the struct is set to \c NULL and the length is set to zero. 298 /**
258 * 299 * Passes the pointer in this string to @c free().
259 * \note There is no implementation for cxstring, because it is unlikely that 300 *
301 * The pointer in the struct is set to @c NULL and the length is set to zero.
302 *
303 * @note There is no implementation for cxstring, because it is unlikely that
260 * you ever have a <code>const char*</code> you are really supposed to free. 304 * you ever have a <code>const char*</code> you are really supposed to free.
261 * If you encounter such situation, you should double-check your code. 305 * If you encounter such situation, you should double-check your code.
262 * 306 *
263 * @param str the string to free 307 * @param str the string to free
264 */ 308 */
265 __attribute__((__nonnull__))
266 void cx_strfree(cxmutstr *str); 309 void cx_strfree(cxmutstr *str);
267 310
268 /** 311 /**
269 * Passes the pointer in this string to the allocators free function. 312 * Passes the pointer in this string to the allocators free function.
270 * 313 *
271 * The pointer in the struct is set to \c NULL and the length is set to zero. 314 * The pointer in the struct is set to @c NULL and the length is set to zero.
272 * 315 *
273 * \note There is no implementation for cxstring, because it is unlikely that 316 * @note There is no implementation for cxstring, because it is unlikely that
274 * you ever have a <code>const char*</code> you are really supposed to free. 317 * you ever have a <code>const char*</code> you are really supposed to free.
275 * If you encounter such situation, you should double-check your code. 318 * If you encounter such situation, you should double-check your code.
276 * 319 *
277 * @param alloc the allocator 320 * @param alloc the allocator
278 * @param str the string to free 321 * @param str the string to free
279 */ 322 */
280 __attribute__((__nonnull__)) 323 cx_attr_nonnull_arg(1)
281 void cx_strfree_a( 324 void cx_strfree_a(
282 const CxAllocator *alloc, 325 const CxAllocator *alloc,
283 cxmutstr *str 326 cxmutstr *str
284 ); 327 );
285 328
286 /** 329 /**
287 * Returns the accumulated length of all specified strings. 330 * Returns the accumulated length of all specified strings.
288 * 331 *
289 * \attention if the count argument is larger than the number of the 332 * If this sum overflows, errno is set to EOVERFLOW.
333 *
334 * @attention if the count argument is larger than the number of the
290 * specified strings, the behavior is undefined. 335 * specified strings, the behavior is undefined.
291 * 336 *
292 * @param count the total number of specified strings 337 * @param count the total number of specified strings
293 * @param ... all strings 338 * @param ... all strings
294 * @return the accumulated length of all strings 339 * @return the accumulated length of all strings
295 */ 340 */
296 __attribute__((__warn_unused_result__)) 341 cx_attr_nodiscard
297 size_t cx_strlen( 342 size_t cx_strlen(
298 size_t count, 343 size_t count,
299 ... 344 ...
300 ); 345 );
301 346
302 /** 347 /**
303 * Concatenates strings. 348 * Concatenates strings.
304 * 349 *
305 * The resulting string will be allocated by the specified allocator. 350 * The resulting string will be allocated by the specified allocator.
306 * So developers \em must pass the return value to cx_strfree_a() eventually. 351 * So developers @em must pass the return value to cx_strfree_a() eventually.
307 * 352 *
308 * If \p str already contains a string, the memory will be reallocated and 353 * If @p str already contains a string, the memory will be reallocated and
309 * the other strings are appended. Otherwise, new memory is allocated. 354 * the other strings are appended. Otherwise, new memory is allocated.
310 * 355 *
311 * \note It is guaranteed that there is only one allocation. 356 * If memory allocation fails, the pointer in the returned string will
357 * be @c NULL. Depending on the allocator, @c errno might be set.
358 *
359 * @note It is guaranteed that there is only one allocation for the
360 * resulting string.
312 * It is also guaranteed that the returned string is zero-terminated. 361 * It is also guaranteed that the returned string is zero-terminated.
313 * 362 *
314 * @param alloc the allocator to use 363 * @param alloc the allocator to use
315 * @param str the string the other strings shall be concatenated to 364 * @param str the string the other strings shall be concatenated to
316 * @param count the number of the other following strings to concatenate 365 * @param count the number of the other following strings to concatenate
317 * @param ... all other strings 366 * @param ... all other UCX strings
318 * @return the concatenated string 367 * @return the concatenated string
319 */ 368 */
320 __attribute__((__warn_unused_result__, __nonnull__)) 369 cx_attr_nodiscard
370 cx_attr_nonnull
321 cxmutstr cx_strcat_ma( 371 cxmutstr cx_strcat_ma(
322 const CxAllocator *alloc, 372 const CxAllocator *alloc,
323 cxmutstr str, 373 cxmutstr str,
324 size_t count, 374 size_t count,
325 ... 375 ...
327 377
328 /** 378 /**
329 * Concatenates strings and returns a new string. 379 * Concatenates strings and returns a new string.
330 * 380 *
331 * The resulting string will be allocated by the specified allocator. 381 * The resulting string will be allocated by the specified allocator.
332 * So developers \em must pass the return value to cx_strfree_a() eventually. 382 * So developers @em must pass the return value to cx_strfree_a() eventually.
333 * 383 *
334 * \note It is guaranteed that there is only one allocation. 384 * If memory allocation fails, the pointer in the returned string will
385 * be @c NULL. Depending on the allocator, @c errno might be set.
386 *
387 * @note It is guaranteed that there is only one allocation for the
388 * resulting string.
335 * It is also guaranteed that the returned string is zero-terminated. 389 * It is also guaranteed that the returned string is zero-terminated.
336 * 390 *
337 * @param alloc the allocator to use 391 * @param alloc (@c CxAllocator*) the allocator to use
338 * @param count the number of the other following strings to concatenate 392 * @param count (@c size_t) the number of the other following strings to concatenate
339 * @param ... all other strings 393 * @param ... all other UCX strings
340 * @return the concatenated string 394 * @return (@c cxmutstr) the concatenated string
341 */ 395 */
342 #define cx_strcat_a(alloc, count, ...) \ 396 #define cx_strcat_a(alloc, count, ...) \
343 cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__) 397 cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
344 398
345 /** 399 /**
346 * Concatenates strings and returns a new string. 400 * Concatenates strings and returns a new string.
347 * 401 *
348 * The resulting string will be allocated by standard \c malloc(). 402 * The resulting string will be allocated by standard @c malloc().
349 * So developers \em must pass the return value to cx_strfree() eventually. 403 * So developers @em must pass the return value to cx_strfree() eventually.
350 * 404 *
351 * \note It is guaranteed that there is only one allocation. 405 * If memory allocation fails, the pointer in the returned string will
406 * be @c NULL and @c errno might be set.
407 *
408 * @note It is guaranteed that there is only one allocation for the
409 * resulting string.
352 * It is also guaranteed that the returned string is zero-terminated. 410 * It is also guaranteed that the returned string is zero-terminated.
353 * 411 *
354 * @param count the number of the other following strings to concatenate 412 * @param count (@c size_t) the number of the other following strings to concatenate
355 * @param ... all other strings 413 * @param ... all other UCX strings
356 * @return the concatenated string 414 * @return (@c cxmutstr) the concatenated string
357 */ 415 */
358 #define cx_strcat(count, ...) \ 416 #define cx_strcat(count, ...) \
359 cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__) 417 cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
360 418
361 /** 419 /**
362 * Concatenates strings. 420 * Concatenates strings.
363 * 421 *
364 * The resulting string will be allocated by standard \c malloc(). 422 * The resulting string will be allocated by standard @c malloc().
365 * So developers \em must pass the return value to cx_strfree() eventually. 423 * So developers @em must pass the return value to cx_strfree() eventually.
366 * 424 *
367 * If \p str already contains a string, the memory will be reallocated and 425 * If @p str already contains a string, the memory will be reallocated and
368 * the other strings are appended. Otherwise, new memory is allocated. 426 * the other strings are appended. Otherwise, new memory is allocated.
369 * 427 *
370 * \note It is guaranteed that there is only one allocation. 428 * If memory allocation fails, the pointer in the returned string will
429 * be @c NULL and @c errno might be set.
430 *
431 * @note It is guaranteed that there is only one allocation for the
432 * resulting string.
371 * It is also guaranteed that the returned string is zero-terminated. 433 * It is also guaranteed that the returned string is zero-terminated.
372 * 434 *
373 * @param str the string the other strings shall be concatenated to 435 * @param str (@c cxmutstr) the string the other strings shall be concatenated to
374 * @param count the number of the other following strings to concatenate 436 * @param count (@c size_t) the number of the other following strings to concatenate
375 * @param ... all other strings 437 * @param ... all other strings
376 * @return the concatenated string 438 * @return (@c cxmutstr) the concatenated string
377 */ 439 */
378 #define cx_strcat_m(str, count, ...) \ 440 #define cx_strcat_m(str, count, ...) \
379 cx_strcat_ma(cxDefaultAllocator, str, count, __VA_ARGS__) 441 cx_strcat_ma(cxDefaultAllocator, str, count, __VA_ARGS__)
380 442
381 /** 443 /**
382 * Returns a substring starting at the specified location. 444 * Returns a substring starting at the specified location.
383 * 445 *
384 * \attention the new string references the same memory area as the 446 * @attention the new string references the same memory area as the
385 * input string and is usually \em not zero-terminated. 447 * input string and is usually @em not zero-terminated.
386 * Use cx_strdup() to get a copy. 448 * Use cx_strdup() to get a copy.
387 * 449 *
388 * @param string input string 450 * @param string input string
389 * @param start start location of the substring 451 * @param start start location of the substring
390 * @return a substring of \p string starting at \p start 452 * @return a substring of @p string starting at @p start
391 * 453 *
392 * @see cx_strsubsl() 454 * @see cx_strsubsl()
393 * @see cx_strsubs_m() 455 * @see cx_strsubs_m()
394 * @see cx_strsubsl_m() 456 * @see cx_strsubsl_m()
395 */ 457 */
396 __attribute__((__warn_unused_result__)) 458 cx_attr_nodiscard
397 cxstring cx_strsubs( 459 cxstring cx_strsubs(
398 cxstring string, 460 cxstring string,
399 size_t start 461 size_t start
400 ); 462 );
401 463
402 /** 464 /**
403 * Returns a substring starting at the specified location. 465 * Returns a substring starting at the specified location.
404 * 466 *
405 * The returned string will be limited to \p length bytes or the number 467 * The returned string will be limited to @p length bytes or the number
406 * of bytes available in \p string, whichever is smaller. 468 * of bytes available in @p string, whichever is smaller.
407 * 469 *
408 * \attention the new string references the same memory area as the 470 * @attention the new string references the same memory area as the
409 * input string and is usually \em not zero-terminated. 471 * input string and is usually @em not zero-terminated.
410 * Use cx_strdup() to get a copy. 472 * Use cx_strdup() to get a copy.
411 * 473 *
412 * @param string input string 474 * @param string input string
413 * @param start start location of the substring 475 * @param start start location of the substring
414 * @param length the maximum length of the returned string 476 * @param length the maximum length of the returned string
415 * @return a substring of \p string starting at \p start 477 * @return a substring of @p string starting at @p start
416 * 478 *
417 * @see cx_strsubs() 479 * @see cx_strsubs()
418 * @see cx_strsubs_m() 480 * @see cx_strsubs_m()
419 * @see cx_strsubsl_m() 481 * @see cx_strsubsl_m()
420 */ 482 */
421 __attribute__((__warn_unused_result__)) 483 cx_attr_nodiscard
422 cxstring cx_strsubsl( 484 cxstring cx_strsubsl(
423 cxstring string, 485 cxstring string,
424 size_t start, 486 size_t start,
425 size_t length 487 size_t length
426 ); 488 );
427 489
428 /** 490 /**
429 * Returns a substring starting at the specified location. 491 * Returns a substring starting at the specified location.
430 * 492 *
431 * \attention the new string references the same memory area as the 493 * @attention the new string references the same memory area as the
432 * input string and is usually \em not zero-terminated. 494 * input string and is usually @em not zero-terminated.
433 * Use cx_strdup() to get a copy. 495 * Use cx_strdup() to get a copy.
434 * 496 *
435 * @param string input string 497 * @param string input string
436 * @param start start location of the substring 498 * @param start start location of the substring
437 * @return a substring of \p string starting at \p start 499 * @return a substring of @p string starting at @p start
438 * 500 *
439 * @see cx_strsubsl_m() 501 * @see cx_strsubsl_m()
440 * @see cx_strsubs() 502 * @see cx_strsubs()
441 * @see cx_strsubsl() 503 * @see cx_strsubsl()
442 */ 504 */
443 __attribute__((__warn_unused_result__)) 505 cx_attr_nodiscard
444 cxmutstr cx_strsubs_m( 506 cxmutstr cx_strsubs_m(
445 cxmutstr string, 507 cxmutstr string,
446 size_t start 508 size_t start
447 ); 509 );
448 510
449 /** 511 /**
450 * Returns a substring starting at the specified location. 512 * Returns a substring starting at the specified location.
451 * 513 *
452 * The returned string will be limited to \p length bytes or the number 514 * The returned string will be limited to @p length bytes or the number
453 * of bytes available in \p string, whichever is smaller. 515 * of bytes available in @p string, whichever is smaller.
454 * 516 *
455 * \attention the new string references the same memory area as the 517 * @attention the new string references the same memory area as the
456 * input string and is usually \em not zero-terminated. 518 * input string and is usually @em not zero-terminated.
457 * Use cx_strdup() to get a copy. 519 * Use cx_strdup() to get a copy.
458 * 520 *
459 * @param string input string 521 * @param string input string
460 * @param start start location of the substring 522 * @param start start location of the substring
461 * @param length the maximum length of the returned string 523 * @param length the maximum length of the returned string
462 * @return a substring of \p string starting at \p start 524 * @return a substring of @p string starting at @p start
463 * 525 *
464 * @see cx_strsubs_m() 526 * @see cx_strsubs_m()
465 * @see cx_strsubs() 527 * @see cx_strsubs()
466 * @see cx_strsubsl() 528 * @see cx_strsubsl()
467 */ 529 */
468 __attribute__((__warn_unused_result__)) 530 cx_attr_nodiscard
469 cxmutstr cx_strsubsl_m( 531 cxmutstr cx_strsubsl_m(
470 cxmutstr string, 532 cxmutstr string,
471 size_t start, 533 size_t start,
472 size_t length 534 size_t length
473 ); 535 );
478 * 540 *
479 * If the string does not contain the character, an empty string is returned. 541 * If the string does not contain the character, an empty string is returned.
480 * 542 *
481 * @param string the string where to locate the character 543 * @param string the string where to locate the character
482 * @param chr the character to locate 544 * @param chr the character to locate
483 * @return a substring starting at the first location of \p chr 545 * @return a substring starting at the first location of @p chr
484 * 546 *
485 * @see cx_strchr_m() 547 * @see cx_strchr_m()
486 */ 548 */
487 __attribute__((__warn_unused_result__)) 549 cx_attr_nodiscard
488 cxstring cx_strchr( 550 cxstring cx_strchr(
489 cxstring string, 551 cxstring string,
490 int chr 552 int chr
491 ); 553 );
492 554
496 * 558 *
497 * If the string does not contain the character, an empty string is returned. 559 * If the string does not contain the character, an empty string is returned.
498 * 560 *
499 * @param string the string where to locate the character 561 * @param string the string where to locate the character
500 * @param chr the character to locate 562 * @param chr the character to locate
501 * @return a substring starting at the first location of \p chr 563 * @return a substring starting at the first location of @p chr
502 * 564 *
503 * @see cx_strchr() 565 * @see cx_strchr()
504 */ 566 */
505 __attribute__((__warn_unused_result__)) 567 cx_attr_nodiscard
506 cxmutstr cx_strchr_m( 568 cxmutstr cx_strchr_m(
507 cxmutstr string, 569 cxmutstr string,
508 int chr 570 int chr
509 ); 571 );
510 572
514 * 576 *
515 * If the string does not contain the character, an empty string is returned. 577 * If the string does not contain the character, an empty string is returned.
516 * 578 *
517 * @param string the string where to locate the character 579 * @param string the string where to locate the character
518 * @param chr the character to locate 580 * @param chr the character to locate
519 * @return a substring starting at the last location of \p chr 581 * @return a substring starting at the last location of @p chr
520 * 582 *
521 * @see cx_strrchr_m() 583 * @see cx_strrchr_m()
522 */ 584 */
523 __attribute__((__warn_unused_result__)) 585 cx_attr_nodiscard
524 cxstring cx_strrchr( 586 cxstring cx_strrchr(
525 cxstring string, 587 cxstring string,
526 int chr 588 int chr
527 ); 589 );
528 590
532 * 594 *
533 * If the string does not contain the character, an empty string is returned. 595 * If the string does not contain the character, an empty string is returned.
534 * 596 *
535 * @param string the string where to locate the character 597 * @param string the string where to locate the character
536 * @param chr the character to locate 598 * @param chr the character to locate
537 * @return a substring starting at the last location of \p chr 599 * @return a substring starting at the last location of @p chr
538 * 600 *
539 * @see cx_strrchr() 601 * @see cx_strrchr()
540 */ 602 */
541 __attribute__((__warn_unused_result__)) 603 cx_attr_nodiscard
542 cxmutstr cx_strrchr_m( 604 cxmutstr cx_strrchr_m(
543 cxmutstr string, 605 cxmutstr string,
544 int chr 606 int chr
545 ); 607 );
546 608
547 /** 609 /**
548 * Returns a substring starting at the location of the first occurrence of the 610 * Returns a substring starting at the location of the first occurrence of the
549 * specified string. 611 * specified string.
550 * 612 *
551 * If \p haystack does not contain \p needle, an empty string is returned. 613 * If @p haystack does not contain @p needle, an empty string is returned.
552 * 614 *
553 * If \p needle is an empty string, the complete \p haystack is 615 * If @p needle is an empty string, the complete @p haystack is
554 * returned. 616 * returned.
555 * 617 *
556 * @param haystack the string to be scanned 618 * @param haystack the string to be scanned
557 * @param needle string containing the sequence of characters to match 619 * @param needle string containing the sequence of characters to match
558 * @return a substring starting at the first occurrence of 620 * @return a substring starting at the first occurrence of
559 * \p needle, or an empty string, if the sequence is not 621 * @p needle, or an empty string, if the sequence is not
560 * contained 622 * contained
561 * @see cx_strstr_m() 623 * @see cx_strstr_m()
562 */ 624 */
563 __attribute__((__warn_unused_result__)) 625 cx_attr_nodiscard
564 cxstring cx_strstr( 626 cxstring cx_strstr(
565 cxstring haystack, 627 cxstring haystack,
566 cxstring needle 628 cxstring needle
567 ); 629 );
568 630
569 /** 631 /**
570 * Returns a substring starting at the location of the first occurrence of the 632 * Returns a substring starting at the location of the first occurrence of the
571 * specified string. 633 * specified string.
572 * 634 *
573 * If \p haystack does not contain \p needle, an empty string is returned. 635 * If @p haystack does not contain @p needle, an empty string is returned.
574 * 636 *
575 * If \p needle is an empty string, the complete \p haystack is 637 * If @p needle is an empty string, the complete @p haystack is
576 * returned. 638 * returned.
577 * 639 *
578 * @param haystack the string to be scanned 640 * @param haystack the string to be scanned
579 * @param needle string containing the sequence of characters to match 641 * @param needle string containing the sequence of characters to match
580 * @return a substring starting at the first occurrence of 642 * @return a substring starting at the first occurrence of
581 * \p needle, or an empty string, if the sequence is not 643 * @p needle, or an empty string, if the sequence is not
582 * contained 644 * contained
583 * @see cx_strstr() 645 * @see cx_strstr()
584 */ 646 */
585 __attribute__((__warn_unused_result__)) 647 cx_attr_nodiscard
586 cxmutstr cx_strstr_m( 648 cxmutstr cx_strstr_m(
587 cxmutstr haystack, 649 cxmutstr haystack,
588 cxstring needle 650 cxstring needle
589 ); 651 );
590 652
591 /** 653 /**
592 * Splits a given string using a delimiter string. 654 * Splits a given string using a delimiter string.
593 * 655 *
594 * \note The resulting array contains strings that point to the source 656 * @note The resulting array contains strings that point to the source
595 * \p string. Use cx_strdup() to get copies. 657 * @p string. Use cx_strdup() to get copies.
596 * 658 *
597 * @param string the string to split 659 * @param string the string to split
598 * @param delim the delimiter 660 * @param delim the delimiter
599 * @param limit the maximum number of split items 661 * @param limit the maximum number of split items
600 * @param output a pre-allocated array of at least \p limit length 662 * @param output a pre-allocated array of at least @p limit length
601 * @return the actual number of split items 663 * @return the actual number of split items
602 */ 664 */
603 __attribute__((__warn_unused_result__, __nonnull__)) 665 cx_attr_nodiscard
666 cx_attr_nonnull
667 cx_attr_access_w(4, 3)
604 size_t cx_strsplit( 668 size_t cx_strsplit(
605 cxstring string, 669 cxstring string,
606 cxstring delim, 670 cxstring delim,
607 size_t limit, 671 size_t limit,
608 cxstring *output 672 cxstring *output
609 ); 673 );
610 674
611 /** 675 /**
612 * Splits a given string using a delimiter string. 676 * Splits a given string using a delimiter string.
613 * 677 *
614 * The array pointed to by \p output will be allocated by \p allocator. 678 * The array pointed to by @p output will be allocated by @p allocator.
615 * 679 *
616 * \note The resulting array contains strings that point to the source 680 * @note The resulting array contains strings that point to the source
617 * \p string. Use cx_strdup() to get copies. 681 * @p string. Use cx_strdup() to get copies.
618 * 682 *
619 * \attention If allocation fails, the \c NULL pointer will be written to 683 * @attention If allocation fails, the @c NULL pointer will be written to
620 * \p output and the number returned will be zero. 684 * @p output and the number returned will be zero.
621 * 685 *
622 * @param allocator the allocator to use for allocating the resulting array 686 * @param allocator the allocator to use for allocating the resulting array
623 * @param string the string to split 687 * @param string the string to split
624 * @param delim the delimiter 688 * @param delim the delimiter
625 * @param limit the maximum number of split items 689 * @param limit the maximum number of split items
626 * @param output a pointer where the address of the allocated array shall be 690 * @param output a pointer where the address of the allocated array shall be
627 * written to 691 * written to
628 * @return the actual number of split items 692 * @return the actual number of split items
629 */ 693 */
630 __attribute__((__warn_unused_result__, __nonnull__)) 694 cx_attr_nodiscard
695 cx_attr_nonnull
696 cx_attr_access_w(5)
631 size_t cx_strsplit_a( 697 size_t cx_strsplit_a(
632 const CxAllocator *allocator, 698 const CxAllocator *allocator,
633 cxstring string, 699 cxstring string,
634 cxstring delim, 700 cxstring delim,
635 size_t limit, 701 size_t limit,
638 704
639 705
640 /** 706 /**
641 * Splits a given string using a delimiter string. 707 * Splits a given string using a delimiter string.
642 * 708 *
643 * \note The resulting array contains strings that point to the source 709 * @note The resulting array contains strings that point to the source
644 * \p string. Use cx_strdup() to get copies. 710 * @p string. Use cx_strdup() to get copies.
645 * 711 *
646 * @param string the string to split 712 * @param string the string to split
647 * @param delim the delimiter 713 * @param delim the delimiter
648 * @param limit the maximum number of split items 714 * @param limit the maximum number of split items
649 * @param output a pre-allocated array of at least \p limit length 715 * @param output a pre-allocated array of at least @p limit length
650 * @return the actual number of split items 716 * @return the actual number of split items
651 */ 717 */
652 __attribute__((__warn_unused_result__, __nonnull__)) 718 cx_attr_nodiscard
719 cx_attr_nonnull
720 cx_attr_access_w(4, 3)
653 size_t cx_strsplit_m( 721 size_t cx_strsplit_m(
654 cxmutstr string, 722 cxmutstr string,
655 cxstring delim, 723 cxstring delim,
656 size_t limit, 724 size_t limit,
657 cxmutstr *output 725 cxmutstr *output
658 ); 726 );
659 727
660 /** 728 /**
661 * Splits a given string using a delimiter string. 729 * Splits a given string using a delimiter string.
662 * 730 *
663 * The array pointed to by \p output will be allocated by \p allocator. 731 * The array pointed to by @p output will be allocated by @p allocator.
664 * 732 *
665 * \note The resulting array contains strings that point to the source 733 * @note The resulting array contains strings that point to the source
666 * \p string. Use cx_strdup() to get copies. 734 * @p string. Use cx_strdup() to get copies.
667 * 735 *
668 * \attention If allocation fails, the \c NULL pointer will be written to 736 * @attention If allocation fails, the @c NULL pointer will be written to
669 * \p output and the number returned will be zero. 737 * @p output and the number returned will be zero.
670 * 738 *
671 * @param allocator the allocator to use for allocating the resulting array 739 * @param allocator the allocator to use for allocating the resulting array
672 * @param string the string to split 740 * @param string the string to split
673 * @param delim the delimiter 741 * @param delim the delimiter
674 * @param limit the maximum number of split items 742 * @param limit the maximum number of split items
675 * @param output a pointer where the address of the allocated array shall be 743 * @param output a pointer where the address of the allocated array shall be
676 * written to 744 * written to
677 * @return the actual number of split items 745 * @return the actual number of split items
678 */ 746 */
679 __attribute__((__warn_unused_result__, __nonnull__)) 747 cx_attr_nodiscard
748 cx_attr_nonnull
749 cx_attr_access_w(5)
680 size_t cx_strsplit_ma( 750 size_t cx_strsplit_ma(
681 const CxAllocator *allocator, 751 const CxAllocator *allocator,
682 cxmutstr string, 752 cxmutstr string,
683 cxstring delim, 753 cxstring delim,
684 size_t limit, 754 size_t limit,
688 /** 758 /**
689 * Compares two strings. 759 * Compares two strings.
690 * 760 *
691 * @param s1 the first string 761 * @param s1 the first string
692 * @param s2 the second string 762 * @param s2 the second string
693 * @return negative if \p s1 is smaller than \p s2, positive if \p s1 is larger 763 * @return negative if @p s1 is smaller than @p s2, positive if @p s1 is larger
694 * than \p s2, zero if both strings equal 764 * than @p s2, zero if both strings equal
695 */ 765 */
696 __attribute__((__warn_unused_result__)) 766 cx_attr_nodiscard
697 int cx_strcmp( 767 int cx_strcmp(
698 cxstring s1, 768 cxstring s1,
699 cxstring s2 769 cxstring s2
700 ); 770 );
701 771
702 /** 772 /**
703 * Compares two strings ignoring case. 773 * Compares two strings ignoring case.
704 * 774 *
705 * @param s1 the first string 775 * @param s1 the first string
706 * @param s2 the second string 776 * @param s2 the second string
707 * @return negative if \p s1 is smaller than \p s2, positive if \p s1 is larger 777 * @return negative if @p s1 is smaller than @p s2, positive if @p s1 is larger
708 * than \p s2, zero if both strings equal ignoring case 778 * than @p s2, zero if both strings equal ignoring case
709 */ 779 */
710 __attribute__((__warn_unused_result__)) 780 cx_attr_nodiscard
711 int cx_strcasecmp( 781 int cx_strcasecmp(
712 cxstring s1, 782 cxstring s1,
713 cxstring s2 783 cxstring s2
714 ); 784 );
715 785
718 * 788 *
719 * This function has a compatible signature for the use as a cx_compare_func. 789 * This function has a compatible signature for the use as a cx_compare_func.
720 * 790 *
721 * @param s1 the first string 791 * @param s1 the first string
722 * @param s2 the second string 792 * @param s2 the second string
723 * @return negative if \p s1 is smaller than \p s2, positive if \p s1 is larger 793 * @return negative if @p s1 is smaller than @p s2, positive if @p s1 is larger
724 * than \p s2, zero if both strings equal 794 * than @p s2, zero if both strings equal
725 */ 795 */
726 __attribute__((__warn_unused_result__, __nonnull__)) 796 cx_attr_nodiscard
797 cx_attr_nonnull
727 int cx_strcmp_p( 798 int cx_strcmp_p(
728 const void *s1, 799 const void *s1,
729 const void *s2 800 const void *s2
730 ); 801 );
731 802
734 * 805 *
735 * This function has a compatible signature for the use as a cx_compare_func. 806 * This function has a compatible signature for the use as a cx_compare_func.
736 * 807 *
737 * @param s1 the first string 808 * @param s1 the first string
738 * @param s2 the second string 809 * @param s2 the second string
739 * @return negative if \p s1 is smaller than \p s2, positive if \p s1 is larger 810 * @return negative if @p s1 is smaller than @p s2, positive if @p s1 is larger
740 * than \p s2, zero if both strings equal ignoring case 811 * than @p s2, zero if both strings equal ignoring case
741 */ 812 */
742 __attribute__((__warn_unused_result__, __nonnull__)) 813 cx_attr_nodiscard
814 cx_attr_nonnull
743 int cx_strcasecmp_p( 815 int cx_strcasecmp_p(
744 const void *s1, 816 const void *s1,
745 const void *s2 817 const void *s2
746 ); 818 );
747 819
748 820
749 /** 821 /**
750 * Creates a duplicate of the specified string. 822 * Creates a duplicate of the specified string.
751 * 823 *
752 * The new string will contain a copy allocated by \p allocator. 824 * The new string will contain a copy allocated by @p allocator.
753 * 825 *
754 * \note The returned string is guaranteed to be zero-terminated. 826 * @note The returned string is guaranteed to be zero-terminated.
755 * 827 *
756 * @param allocator the allocator to use 828 * @param allocator the allocator to use
757 * @param string the string to duplicate 829 * @param string the string to duplicate
758 * @return a duplicate of the string 830 * @return a duplicate of the string
759 * @see cx_strdup() 831 * @see cx_strdup()
760 */ 832 */
761 __attribute__((__warn_unused_result__, __nonnull__)) 833 cx_attr_nodiscard
834 cx_attr_nonnull
762 cxmutstr cx_strdup_a( 835 cxmutstr cx_strdup_a(
763 const CxAllocator *allocator, 836 const CxAllocator *allocator,
764 cxstring string 837 cxstring string
765 ); 838 );
766 839
767 /** 840 /**
768 * Creates a duplicate of the specified string. 841 * Creates a duplicate of the specified string.
769 * 842 *
770 * The new string will contain a copy allocated by standard 843 * The new string will contain a copy allocated by standard
771 * \c malloc(). So developers \em must pass the return value to cx_strfree(). 844 * @c malloc(). So developers @em must pass the return value to cx_strfree().
772 * 845 *
773 * \note The returned string is guaranteed to be zero-terminated. 846 * @note The returned string is guaranteed to be zero-terminated.
774 * 847 *
775 * @param string the string to duplicate 848 * @param string (@c cxstring) the string to duplicate
776 * @return a duplicate of the string 849 * @return (@c cxmutstr) a duplicate of the string
777 * @see cx_strdup_a() 850 * @see cx_strdup_a()
778 */ 851 */
779 #define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string) 852 #define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string)
780 853
781 854
782 /** 855 /**
783 * Creates a duplicate of the specified string. 856 * Creates a duplicate of the specified string.
784 * 857 *
785 * The new string will contain a copy allocated by \p allocator. 858 * The new string will contain a copy allocated by @p allocator.
786 * 859 *
787 * \note The returned string is guaranteed to be zero-terminated. 860 * @note The returned string is guaranteed to be zero-terminated.
788 * 861 *
789 * @param allocator the allocator to use 862 * @param allocator (@c CxAllocator*) the allocator to use
790 * @param string the string to duplicate 863 * @param string (@c cxmutstr) the string to duplicate
791 * @return a duplicate of the string 864 * @return (@c cxmutstr) a duplicate of the string
792 * @see cx_strdup_m() 865 * @see cx_strdup_m()
793 */ 866 */
794 #define cx_strdup_ma(allocator, string) cx_strdup_a(allocator, cx_strcast(string)) 867 #define cx_strdup_ma(allocator, string) cx_strdup_a(allocator, cx_strcast(string))
795 868
796 /** 869 /**
797 * Creates a duplicate of the specified string. 870 * Creates a duplicate of the specified string.
798 * 871 *
799 * The new string will contain a copy allocated by standard 872 * The new string will contain a copy allocated by standard
800 * \c malloc(). So developers \em must pass the return value to cx_strfree(). 873 * @c malloc(). So developers @em must pass the return value to cx_strfree().
801 * 874 *
802 * \note The returned string is guaranteed to be zero-terminated. 875 * @note The returned string is guaranteed to be zero-terminated.
803 * 876 *
804 * @param string the string to duplicate 877 * @param string (@c cxmutstr) the string to duplicate
805 * @return a duplicate of the string 878 * @return (@c cxmutstr) a duplicate of the string
806 * @see cx_strdup_ma() 879 * @see cx_strdup_ma()
807 */ 880 */
808 #define cx_strdup_m(string) cx_strdup_a(cxDefaultAllocator, cx_strcast(string)) 881 #define cx_strdup_m(string) cx_strdup_a(cxDefaultAllocator, cx_strcast(string))
809 882
810 /** 883 /**
811 * Omits leading and trailing spaces. 884 * Omits leading and trailing spaces.
812 * 885 *
813 * \note the returned string references the same memory, thus you 886 * @note the returned string references the same memory, thus you
814 * must \em not free the returned memory. 887 * must @em not free the returned memory.
815 * 888 *
816 * @param string the string that shall be trimmed 889 * @param string the string that shall be trimmed
817 * @return the trimmed string 890 * @return the trimmed string
818 */ 891 */
819 __attribute__((__warn_unused_result__)) 892 cx_attr_nodiscard
820 cxstring cx_strtrim(cxstring string); 893 cxstring cx_strtrim(cxstring string);
821 894
822 /** 895 /**
823 * Omits leading and trailing spaces. 896 * Omits leading and trailing spaces.
824 * 897 *
825 * \note the returned string references the same memory, thus you 898 * @note the returned string references the same memory, thus you
826 * must \em not free the returned memory. 899 * must @em not free the returned memory.
827 * 900 *
828 * @param string the string that shall be trimmed 901 * @param string the string that shall be trimmed
829 * @return the trimmed string 902 * @return the trimmed string
830 */ 903 */
831 __attribute__((__warn_unused_result__)) 904 cx_attr_nodiscard
832 cxmutstr cx_strtrim_m(cxmutstr string); 905 cxmutstr cx_strtrim_m(cxmutstr string);
833 906
834 /** 907 /**
835 * Checks, if a string has a specific prefix. 908 * Checks, if a string has a specific prefix.
836 * 909 *
837 * @param string the string to check 910 * @param string the string to check
838 * @param prefix the prefix the string should have 911 * @param prefix the prefix the string should have
839 * @return \c true, if and only if the string has the specified prefix, 912 * @return @c true, if and only if the string has the specified prefix,
840 * \c false otherwise 913 * @c false otherwise
841 */ 914 */
842 __attribute__((__warn_unused_result__)) 915 cx_attr_nodiscard
843 bool cx_strprefix( 916 bool cx_strprefix(
844 cxstring string, 917 cxstring string,
845 cxstring prefix 918 cxstring prefix
846 ); 919 );
847 920
848 /** 921 /**
849 * Checks, if a string has a specific suffix. 922 * Checks, if a string has a specific suffix.
850 * 923 *
851 * @param string the string to check 924 * @param string the string to check
852 * @param suffix the suffix the string should have 925 * @param suffix the suffix the string should have
853 * @return \c true, if and only if the string has the specified suffix, 926 * @return @c true, if and only if the string has the specified suffix,
854 * \c false otherwise 927 * @c false otherwise
855 */ 928 */
856 __attribute__((__warn_unused_result__)) 929 cx_attr_nodiscard
857 bool cx_strsuffix( 930 bool cx_strsuffix(
858 cxstring string, 931 cxstring string,
859 cxstring suffix 932 cxstring suffix
860 ); 933 );
861 934
862 /** 935 /**
863 * Checks, if a string has a specific prefix, ignoring the case. 936 * Checks, if a string has a specific prefix, ignoring the case.
864 * 937 *
865 * @param string the string to check 938 * @param string the string to check
866 * @param prefix the prefix the string should have 939 * @param prefix the prefix the string should have
867 * @return \c true, if and only if the string has the specified prefix, 940 * @return @c true, if and only if the string has the specified prefix,
868 * \c false otherwise 941 * @c false otherwise
869 */ 942 */
870 __attribute__((__warn_unused_result__)) 943 cx_attr_nodiscard
871 bool cx_strcaseprefix( 944 bool cx_strcaseprefix(
872 cxstring string, 945 cxstring string,
873 cxstring prefix 946 cxstring prefix
874 ); 947 );
875 948
876 /** 949 /**
877 * Checks, if a string has a specific suffix, ignoring the case. 950 * Checks, if a string has a specific suffix, ignoring the case.
878 * 951 *
879 * @param string the string to check 952 * @param string the string to check
880 * @param suffix the suffix the string should have 953 * @param suffix the suffix the string should have
881 * @return \c true, if and only if the string has the specified suffix, 954 * @return @c true, if and only if the string has the specified suffix,
882 * \c false otherwise 955 * @c false otherwise
883 */ 956 */
884 __attribute__((__warn_unused_result__)) 957 cx_attr_nodiscard
885 bool cx_strcasesuffix( 958 bool cx_strcasesuffix(
886 cxstring string, 959 cxstring string,
887 cxstring suffix 960 cxstring suffix
888 ); 961 );
889 962
909 982
910 /** 983 /**
911 * Replaces a pattern in a string with another string. 984 * Replaces a pattern in a string with another string.
912 * 985 *
913 * The pattern is taken literally and is no regular expression. 986 * The pattern is taken literally and is no regular expression.
914 * Replaces at most \p replmax occurrences. 987 * Replaces at most @p replmax occurrences.
915 * 988 *
916 * The returned string will be allocated by \p allocator and is guaranteed 989 * The returned string will be allocated by @p allocator and is guaranteed
917 * to be zero-terminated. 990 * to be zero-terminated.
918 * 991 *
919 * If allocation fails, or the input string is empty, 992 * If allocation fails, or the input string is empty,
920 * the returned string will be empty. 993 * the returned string will be empty.
921 * 994 *
924 * @param pattern the pattern to search for 997 * @param pattern the pattern to search for
925 * @param replacement the replacement string 998 * @param replacement the replacement string
926 * @param replmax maximum number of replacements 999 * @param replmax maximum number of replacements
927 * @return the resulting string after applying the replacements 1000 * @return the resulting string after applying the replacements
928 */ 1001 */
929 __attribute__((__warn_unused_result__, __nonnull__)) 1002 cx_attr_nodiscard
1003 cx_attr_nonnull
930 cxmutstr cx_strreplacen_a( 1004 cxmutstr cx_strreplacen_a(
931 const CxAllocator *allocator, 1005 const CxAllocator *allocator,
932 cxstring str, 1006 cxstring str,
933 cxstring pattern, 1007 cxstring pattern,
934 cxstring replacement, 1008 cxstring replacement,
937 1011
938 /** 1012 /**
939 * Replaces a pattern in a string with another string. 1013 * Replaces a pattern in a string with another string.
940 * 1014 *
941 * The pattern is taken literally and is no regular expression. 1015 * The pattern is taken literally and is no regular expression.
942 * Replaces at most \p replmax occurrences. 1016 * Replaces at most @p replmax occurrences.
943 * 1017 *
944 * The returned string will be allocated by \c malloc() and is guaranteed 1018 * The returned string will be allocated by @c malloc() and is guaranteed
945 * to be zero-terminated. 1019 * to be zero-terminated.
946 * 1020 *
947 * If allocation fails, or the input string is empty, 1021 * If allocation fails, or the input string is empty,
948 * the returned string will be empty. 1022 * the returned string will be empty.
949 * 1023 *
950 * @param str the string where replacements should be applied 1024 * @param str (@c cxstring) the string where replacements should be applied
951 * @param pattern the pattern to search for 1025 * @param pattern (@c cxstring) the pattern to search for
952 * @param replacement the replacement string 1026 * @param replacement (@c cxstring) the replacement string
953 * @param replmax maximum number of replacements 1027 * @param replmax (@c size_t) maximum number of replacements
954 * @return the resulting string after applying the replacements 1028 * @return (@c cxmutstr) the resulting string after applying the replacements
955 */ 1029 */
956 #define cx_strreplacen(str, pattern, replacement, replmax) \ 1030 #define cx_strreplacen(str, pattern, replacement, replmax) \
957 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, replmax) 1031 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, replmax)
958 1032
959 /** 1033 /**
960 * Replaces a pattern in a string with another string. 1034 * Replaces a pattern in a string with another string.
961 * 1035 *
962 * The pattern is taken literally and is no regular expression. 1036 * The pattern is taken literally and is no regular expression.
963 * 1037 *
964 * The returned string will be allocated by \p allocator and is guaranteed 1038 * The returned string will be allocated by @p allocator and is guaranteed
965 * to be zero-terminated. 1039 * to be zero-terminated.
966 * 1040 *
967 * If allocation fails, or the input string is empty, 1041 * If allocation fails, or the input string is empty,
968 * the returned string will be empty. 1042 * the returned string will be empty.
969 * 1043 *
970 * @param allocator the allocator to use 1044 * @param allocator (@c CxAllocator*) the allocator to use
971 * @param str the string where replacements should be applied 1045 * @param str (@c cxstring) the string where replacements should be applied
972 * @param pattern the pattern to search for 1046 * @param pattern (@c cxstring) the pattern to search for
973 * @param replacement the replacement string 1047 * @param replacement (@c cxstring) the replacement string
974 * @return the resulting string after applying the replacements 1048 * @return (@c cxmutstr) the resulting string after applying the replacements
975 */ 1049 */
976 #define cx_strreplace_a(allocator, str, pattern, replacement) \ 1050 #define cx_strreplace_a(allocator, str, pattern, replacement) \
977 cx_strreplacen_a(allocator, str, pattern, replacement, SIZE_MAX) 1051 cx_strreplacen_a(allocator, str, pattern, replacement, SIZE_MAX)
978 1052
979 /** 1053 /**
980 * Replaces a pattern in a string with another string. 1054 * Replaces a pattern in a string with another string.
981 * 1055 *
982 * The pattern is taken literally and is no regular expression. 1056 * The pattern is taken literally and is no regular expression.
983 * Replaces at most \p replmax occurrences. 1057 * Replaces at most @p replmax occurrences.
984 * 1058 *
985 * The returned string will be allocated by \c malloc() and is guaranteed 1059 * The returned string will be allocated by @c malloc() and is guaranteed
986 * to be zero-terminated. 1060 * to be zero-terminated.
987 * 1061 *
988 * If allocation fails, or the input string is empty, 1062 * If allocation fails, or the input string is empty,
989 * the returned string will be empty. 1063 * the returned string will be empty.
990 * 1064 *
991 * @param str the string where replacements should be applied 1065 * @param str (@c cxstring) the string where replacements should be applied
992 * @param pattern the pattern to search for 1066 * @param pattern (@c cxstring) the pattern to search for
993 * @param replacement the replacement string 1067 * @param replacement (@c cxstring) the replacement string
994 * @return the resulting string after applying the replacements 1068 * @return (@c cxmutstr) the resulting string after applying the replacements
995 */ 1069 */
996 #define cx_strreplace(str, pattern, replacement) \ 1070 #define cx_strreplace(str, pattern, replacement) \
997 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, SIZE_MAX) 1071 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, SIZE_MAX)
998 1072
999 /** 1073 /**
1002 * @param str the string to tokenize 1076 * @param str the string to tokenize
1003 * @param delim the delimiter (must not be empty) 1077 * @param delim the delimiter (must not be empty)
1004 * @param limit the maximum number of tokens that shall be returned 1078 * @param limit the maximum number of tokens that shall be returned
1005 * @return a new string tokenization context 1079 * @return a new string tokenization context
1006 */ 1080 */
1007 __attribute__((__warn_unused_result__)) 1081 cx_attr_nodiscard
1008 CxStrtokCtx cx_strtok( 1082 CxStrtokCtx cx_strtok(
1009 cxstring str, 1083 cxstring str,
1010 cxstring delim, 1084 cxstring delim,
1011 size_t limit 1085 size_t limit
1012 ); 1086 );
1017 * @param str the string to tokenize 1091 * @param str the string to tokenize
1018 * @param delim the delimiter (must not be empty) 1092 * @param delim the delimiter (must not be empty)
1019 * @param limit the maximum number of tokens that shall be returned 1093 * @param limit the maximum number of tokens that shall be returned
1020 * @return a new string tokenization context 1094 * @return a new string tokenization context
1021 */ 1095 */
1022 __attribute__((__warn_unused_result__)) 1096 cx_attr_nodiscard
1023 CxStrtokCtx cx_strtok_m( 1097 CxStrtokCtx cx_strtok_m(
1024 cxmutstr str, 1098 cxmutstr str,
1025 cxstring delim, 1099 cxstring delim,
1026 size_t limit 1100 size_t limit
1027 ); 1101 );
1034 * @param ctx the tokenization context 1108 * @param ctx the tokenization context
1035 * @param token a pointer to memory where the next token shall be stored 1109 * @param token a pointer to memory where the next token shall be stored
1036 * @return true if successful, false if the limit or the end of the string 1110 * @return true if successful, false if the limit or the end of the string
1037 * has been reached 1111 * has been reached
1038 */ 1112 */
1039 __attribute__((__warn_unused_result__, __nonnull__)) 1113 cx_attr_nonnull
1114 cx_attr_nodiscard
1115 cx_attr_access_w(2)
1040 bool cx_strtok_next( 1116 bool cx_strtok_next(
1041 CxStrtokCtx *ctx, 1117 CxStrtokCtx *ctx,
1042 cxstring *token 1118 cxstring *token
1043 ); 1119 );
1044 1120
1052 * @param ctx the tokenization context 1128 * @param ctx the tokenization context
1053 * @param token a pointer to memory where the next token shall be stored 1129 * @param token a pointer to memory where the next token shall be stored
1054 * @return true if successful, false if the limit or the end of the string 1130 * @return true if successful, false if the limit or the end of the string
1055 * has been reached 1131 * has been reached
1056 */ 1132 */
1057 __attribute__((__warn_unused_result__, __nonnull__)) 1133 cx_attr_nonnull
1134 cx_attr_nodiscard
1135 cx_attr_access_w(2)
1058 bool cx_strtok_next_m( 1136 bool cx_strtok_next_m(
1059 CxStrtokCtx *ctx, 1137 CxStrtokCtx *ctx,
1060 cxmutstr *token 1138 cxmutstr *token
1061 ); 1139 );
1062 1140
1065 * 1143 *
1066 * @param ctx the tokenization context 1144 * @param ctx the tokenization context
1067 * @param delim array of more delimiters 1145 * @param delim array of more delimiters
1068 * @param count number of elements in the array 1146 * @param count number of elements in the array
1069 */ 1147 */
1070 __attribute__((__nonnull__)) 1148 cx_attr_nonnull
1149 cx_attr_access_r(2, 3)
1071 void cx_strtok_delim( 1150 void cx_strtok_delim(
1072 CxStrtokCtx *ctx, 1151 CxStrtokCtx *ctx,
1073 const cxstring *delim, 1152 const cxstring *delim,
1074 size_t count 1153 size_t count
1075 ); 1154 );
1076 1155
1156 /* ------------------------------------------------------------------------- *
1157 * string to number conversion functions *
1158 * ------------------------------------------------------------------------- */
1159
1160 /**
1161 * @copydoc cx_strtouz_lc()
1162 */
1163 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1164 int cx_strtos_lc(cxstring str, short *output, int base, const char *groupsep);
1165 /**
1166 * @copydoc cx_strtouz_lc()
1167 */
1168 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1169 int cx_strtoi_lc(cxstring str, int *output, int base, const char *groupsep);
1170 /**
1171 * @copydoc cx_strtouz_lc()
1172 */
1173 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1174 int cx_strtol_lc(cxstring str, long *output, int base, const char *groupsep);
1175 /**
1176 * @copydoc cx_strtouz_lc()
1177 */
1178 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1179 int cx_strtoll_lc(cxstring str, long long *output, int base, const char *groupsep);
1180 /**
1181 * @copydoc cx_strtouz_lc()
1182 */
1183 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1184 int cx_strtoi8_lc(cxstring str, int8_t *output, int base, const char *groupsep);
1185 /**
1186 * @copydoc cx_strtouz_lc()
1187 */
1188 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1189 int cx_strtoi16_lc(cxstring str, int16_t *output, int base, const char *groupsep);
1190 /**
1191 * @copydoc cx_strtouz_lc()
1192 */
1193 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1194 int cx_strtoi32_lc(cxstring str, int32_t *output, int base, const char *groupsep);
1195 /**
1196 * @copydoc cx_strtouz_lc()
1197 */
1198 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1199 int cx_strtoi64_lc(cxstring str, int64_t *output, int base, const char *groupsep);
1200 /**
1201 * @copydoc cx_strtouz_lc()
1202 */
1203 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1204 int cx_strtoz_lc(cxstring str, ssize_t *output, int base, const char *groupsep);
1205 /**
1206 * @copydoc cx_strtouz_lc()
1207 */
1208 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1209 int cx_strtous_lc(cxstring str, unsigned short *output, int base, const char *groupsep);
1210 /**
1211 * @copydoc cx_strtouz_lc()
1212 */
1213 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1214 int cx_strtou_lc(cxstring str, unsigned int *output, int base, const char *groupsep);
1215 /**
1216 * @copydoc cx_strtouz_lc()
1217 */
1218 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1219 int cx_strtoul_lc(cxstring str, unsigned long *output, int base, const char *groupsep);
1220 /**
1221 * @copydoc cx_strtouz_lc()
1222 */
1223 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1224 int cx_strtoull_lc(cxstring str, unsigned long long *output, int base, const char *groupsep);
1225 /**
1226 * @copydoc cx_strtouz_lc()
1227 */
1228 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1229 int cx_strtou8_lc(cxstring str, uint8_t *output, int base, const char *groupsep);
1230 /**
1231 * @copydoc cx_strtouz_lc()
1232 */
1233 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1234 int cx_strtou16_lc(cxstring str, uint16_t *output, int base, const char *groupsep);
1235 /**
1236 * @copydoc cx_strtouz_lc()
1237 */
1238 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1239 int cx_strtou32_lc(cxstring str, uint32_t *output, int base, const char *groupsep);
1240 /**
1241 * @copydoc cx_strtouz_lc()
1242 */
1243 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1244 int cx_strtou64_lc(cxstring str, uint64_t *output, int base, const char *groupsep);
1245
1246 /**
1247 * Converts a string to a number.
1248 *
1249 * The function returns non-zero when conversion is not possible.
1250 * In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base.
1251 * It sets errno to ERANGE when the target datatype is too small.
1252 *
1253 * @param str the string to convert
1254 * @param output a pointer to the integer variable where the result shall be stored
1255 * @param base 2, 8, 10, or 16
1256 * @param groupsep each character in this string is treated as group separator and ignored during conversion
1257 * @retval zero success
1258 * @retval non-zero conversion was not possible
1259 */
1260 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1261 int cx_strtouz_lc(cxstring str, size_t *output, int base, const char *groupsep);
1262
1263 /**
1264 * Converts a string to a single precision floating point number.
1265 *
1266 * The function returns non-zero when conversion is not possible.
1267 * In that case the function sets errno to EINVAL when the reason is an invalid character.
1268 * It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.
1269 *
1270 * The decimal separator is assumed to be a dot character.
1271 * The comma character is treated as group separator and ignored during parsing.
1272 * If you want to choose a different format, use cx_strtof_lc().
1273 *
1274 * @param str the string to convert
1275 * @param output a pointer to the float variable where the result shall be stored
1276 * @param decsep the decimal separator
1277 * @param groupsep each character in this string is treated as group separator and ignored during conversion
1278 * @retval zero success
1279 * @retval non-zero conversion was not possible
1280 */
1281 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1282 int cx_strtof_lc(cxstring str, float *output, char decsep, const char *groupsep);
1283
1284 /**
1285 * Converts a string to a double precision floating point number.
1286 *
1287 * The function returns non-zero when conversion is not possible.
1288 * In that case the function sets errno to EINVAL when the reason is an invalid character.
1289 * It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.
1290 *
1291 * The decimal separator is assumed to be a dot character.
1292 * The comma character is treated as group separator and ignored during parsing.
1293 * If you want to choose a different format, use cx_strtof_lc().
1294 *
1295 * @param str the string to convert
1296 * @param output a pointer to the float variable where the result shall be stored
1297 * @param decsep the decimal separator
1298 * @param groupsep each character in this string is treated as group separator and ignored during conversion
1299 * @retval zero success
1300 * @retval non-zero conversion was not possible
1301 */
1302 cx_attr_access_w(2) cx_attr_nonnull_arg(2)
1303 int cx_strtod_lc(cxstring str, double *output, char decsep, const char *groupsep);
1304
1305 #ifndef CX_STR_IMPLEMENTATION
1306 /**
1307 * @copydoc cx_strtouz_lc()
1308 */
1309 #define cx_strtos_lc(str, output, base, groupsep) cx_strtos_lc(cx_strcast(str), output, base, groupsep)
1310 /**
1311 * @copydoc cx_strtouz_lc()
1312 */
1313 #define cx_strtoi_lc(str, output, base, groupsep) cx_strtoi_lc(cx_strcast(str), output, base, groupsep)
1314 /**
1315 * @copydoc cx_strtouz_lc()
1316 */
1317 #define cx_strtol_lc(str, output, base, groupsep) cx_strtol_lc(cx_strcast(str), output, base, groupsep)
1318 /**
1319 * @copydoc cx_strtouz_lc()
1320 */
1321 #define cx_strtoll_lc(str, output, base, groupsep) cx_strtoll_lc(cx_strcast(str), output, base, groupsep)
1322 /**
1323 * @copydoc cx_strtouz_lc()
1324 */
1325 #define cx_strtoi8_lc(str, output, base, groupsep) cx_strtoi8_lc(cx_strcast(str), output, base, groupsep)
1326 /**
1327 * @copydoc cx_strtouz_lc()
1328 */
1329 #define cx_strtoi16_lc(str, output, base, groupsep) cx_strtoi16_lc(cx_strcast(str), output, base, groupsep)
1330 /**
1331 * @copydoc cx_strtouz_lc()
1332 */
1333 #define cx_strtoi32_lc(str, output, base, groupsep) cx_strtoi32_lc(cx_strcast(str), output, base, groupsep)
1334 /**
1335 * @copydoc cx_strtouz_lc()
1336 */
1337 #define cx_strtoi64_lc(str, output, base, groupsep) cx_strtoi64_lc(cx_strcast(str), output, base, groupsep)
1338 /**
1339 * @copydoc cx_strtouz_lc()
1340 */
1341 #define cx_strtoz_lc(str, output, base, groupsep) cx_strtoz_lc(cx_strcast(str), output, base, groupsep)
1342 /**
1343 * @copydoc cx_strtouz_lc()
1344 */
1345 #define cx_strtous_lc(str, output, base, groupsep) cx_strtous_lc(cx_strcast(str), output, base, groupsep)
1346 /**
1347 * @copydoc cx_strtouz_lc()
1348 */
1349 #define cx_strtou_lc(str, output, base, groupsep) cx_strtou_lc(cx_strcast(str), output, base, groupsep)
1350 /**
1351 * @copydoc cx_strtouz_lc()
1352 */
1353 #define cx_strtoul_lc(str, output, base, groupsep) cx_strtoul_lc(cx_strcast(str), output, base, groupsep)
1354 /**
1355 * @copydoc cx_strtouz_lc()
1356 */
1357 #define cx_strtoull_lc(str, output, base, groupsep) cx_strtoull_lc(cx_strcast(str), output, base, groupsep)
1358 /**
1359 * @copydoc cx_strtouz_lc()
1360 */
1361 #define cx_strtou8_lc(str, output, base, groupsep) cx_strtou8_lc(cx_strcast(str), output, base, groupsep)
1362 /**
1363 * @copydoc cx_strtouz_lc()
1364 */
1365 #define cx_strtou16_lc(str, output, base, groupsep) cx_strtou16_lc(cx_strcast(str), output, base, groupsep)
1366 /**
1367 * @copydoc cx_strtouz_lc()
1368 */
1369 #define cx_strtou32_lc(str, output, base, groupsep) cx_strtou32_lc(cx_strcast(str), output, base, groupsep)
1370 /**
1371 * @copydoc cx_strtouz_lc()
1372 */
1373 #define cx_strtou64_lc(str, output, base, groupsep) cx_strtou64_lc(cx_strcast(str), output, base, groupsep)
1374 /**
1375 * Converts a string to a number.
1376 *
1377 * The function returns non-zero when conversion is not possible.
1378 * In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base.
1379 * It sets errno to ERANGE when the target datatype is too small.
1380 *
1381 * @param str the string to convert
1382 * @param output a pointer to the integer variable where the result shall be stored
1383 * @param base 2, 8, 10, or 16
1384 * @param groupsep each character in this string is treated as group separator and ignored during conversion
1385 * @retval zero success
1386 * @retval non-zero conversion was not possible
1387 */
1388 #define cx_strtouz_lc(str, output, base, groupsep) cx_strtouz_lc(cx_strcast(str), output, base, groupsep)
1389
1390 /**
1391 * @copydoc cx_strtouz()
1392 */
1393 #define cx_strtos(str, output, base) cx_strtos_lc(str, output, base, ",")
1394 /**
1395 * @copydoc cx_strtouz()
1396 */
1397 #define cx_strtoi(str, output, base) cx_strtoi_lc(str, output, base, ",")
1398 /**
1399 * @copydoc cx_strtouz()
1400 */
1401 #define cx_strtol(str, output, base) cx_strtol_lc(str, output, base, ",")
1402 /**
1403 * @copydoc cx_strtouz()
1404 */
1405 #define cx_strtoll(str, output, base) cx_strtoll_lc(str, output, base, ",")
1406 /**
1407 * @copydoc cx_strtouz()
1408 */
1409 #define cx_strtoi8(str, output, base) cx_strtoi8_lc(str, output, base, ",")
1410 /**
1411 * @copydoc cx_strtouz()
1412 */
1413 #define cx_strtoi16(str, output, base) cx_strtoi16_lc(str, output, base, ",")
1414 /**
1415 * @copydoc cx_strtouz()
1416 */
1417 #define cx_strtoi32(str, output, base) cx_strtoi32_lc(str, output, base, ",")
1418 /**
1419 * @copydoc cx_strtouz()
1420 */
1421 #define cx_strtoi64(str, output, base) cx_strtoi64_lc(str, output, base, ",")
1422 /**
1423 * @copydoc cx_strtouz()
1424 */
1425 #define cx_strtoz(str, output, base) cx_strtoz_lc(str, output, base, ",")
1426 /**
1427 * @copydoc cx_strtouz()
1428 */
1429 #define cx_strtous(str, output, base) cx_strtous_lc(str, output, base, ",")
1430 /**
1431 * @copydoc cx_strtouz()
1432 */
1433 #define cx_strtou(str, output, base) cx_strtou_lc(str, output, base, ",")
1434 /**
1435 * @copydoc cx_strtouz()
1436 */
1437 #define cx_strtoul(str, output, base) cx_strtoul_lc(str, output, base, ",")
1438 /**
1439 * @copydoc cx_strtouz()
1440 */
1441 #define cx_strtoull(str, output, base) cx_strtoull_lc(str, output, base, ",")
1442 /**
1443 * @copydoc cx_strtouz()
1444 */
1445 #define cx_strtou8(str, output, base) cx_strtou8_lc(str, output, base, ",")
1446 /**
1447 * @copydoc cx_strtouz()
1448 */
1449 #define cx_strtou16(str, output, base) cx_strtou16_lc(str, output, base, ",")
1450 /**
1451 * @copydoc cx_strtouz()
1452 */
1453 #define cx_strtou32(str, output, base) cx_strtou32_lc(str, output, base, ",")
1454 /**
1455 * @copydoc cx_strtouz()
1456 */
1457 #define cx_strtou64(str, output, base) cx_strtou64_lc(str, output, base, ",")
1458 /**
1459 * Converts a string to a number.
1460 *
1461 * The function returns non-zero when conversion is not possible.
1462 * In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base.
1463 * It sets errno to ERANGE when the target datatype is too small.
1464 *
1465 * The comma character is treated as group separator and ignored during parsing.
1466 * If you want to choose the set of group separators, use the @c _lc variant of this function (e.g. cx_strtouz_lc()).
1467 *
1468 * @param str the string to convert
1469 * @param output a pointer to the integer variable where the result shall be stored
1470 * @param base 2, 8, 10, or 16
1471 * @retval zero success
1472 * @retval non-zero conversion was not possible
1473 */
1474 #define cx_strtouz(str, output, base) cx_strtouz_lc(str, output, base, ",")
1475
1476 /**
1477 * Converts a string to a single precision floating point number.
1478 *
1479 * The function returns non-zero when conversion is not possible.
1480 * In that case the function sets errno to EINVAL when the reason is an invalid character.
1481 * It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.
1482 *
1483 * The decimal separator is assumed to be a dot character.
1484 * The comma character is treated as group separator and ignored during parsing.
1485 * If you want to choose a different format, use cx_strtof_lc().
1486 *
1487 * @param str the string to convert
1488 * @param output a pointer to the float variable where the result shall be stored
1489 * @param decsep the decimal separator
1490 * @param groupsep each character in this string is treated as group separator and ignored during conversion
1491 * @retval zero success
1492 * @retval non-zero conversion was not possible
1493 */
1494 #define cx_strtof_lc(str, output, decsep, groupsep) cx_strtof_lc(cx_strcast(str), output, decsep, groupsep)
1495 /**
1496 * Converts a string to a double precision floating point number.
1497 *
1498 * The function returns non-zero when conversion is not possible.
1499 * In that case the function sets errno to EINVAL when the reason is an invalid character.
1500 *
1501 * The decimal separator is assumed to be a dot character.
1502 * The comma character is treated as group separator and ignored during parsing.
1503 * If you want to choose a different format, use cx_strtof_lc().
1504 *
1505 * @param str the string to convert
1506 * @param output a pointer to the double variable where the result shall be stored
1507 * @param decsep the decimal separator
1508 * @param groupsep each character in this string is treated as group separator and ignored during conversion
1509 * @retval zero success
1510 * @retval non-zero conversion was not possible
1511 */
1512 #define cx_strtod_lc(str, output, decsep, groupsep) cx_strtod_lc(cx_strcast(str), output, decsep, groupsep)
1513
1514 /**
1515 * Converts a string to a single precision floating point number.
1516 *
1517 * The function returns non-zero when conversion is not possible.
1518 * In that case the function sets errno to EINVAL when the reason is an invalid character.
1519 * It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.
1520 *
1521 * The decimal separator is assumed to be a dot character.
1522 * The comma character is treated as group separator and ignored during parsing.
1523 * If you want to choose a different format, use cx_strtof_lc().
1524 *
1525 * @param str the string to convert
1526 * @param output a pointer to the float variable where the result shall be stored
1527 * @retval zero success
1528 * @retval non-zero conversion was not possible
1529 */
1530 #define cx_strtof(str, output) cx_strtof_lc(str, output, '.', ",")
1531 /**
1532 * Converts a string to a double precision floating point number.
1533 *
1534 * The function returns non-zero when conversion is not possible.
1535 * In that case the function sets errno to EINVAL when the reason is an invalid character.
1536 *
1537 * The decimal separator is assumed to be a dot character.
1538 * The comma character is treated as group separator and ignored during parsing.
1539 * If you want to choose a different format, use cx_strtof_lc().
1540 *
1541 * @param str the string to convert
1542 * @param output a pointer to the double variable where the result shall be stored
1543 * @retval zero success
1544 * @retval non-zero conversion was not possible
1545 */
1546 #define cx_strtod(str, output) cx_strtod_lc(str, output, '.', ",")
1547
1548 #endif
1077 1549
1078 #ifdef __cplusplus 1550 #ifdef __cplusplus
1079 } // extern "C" 1551 } // extern "C"
1080 #endif 1552 #endif
1081 1553

mercurial