1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#ifndef BASE_STR_H
5#define BASE_STR_H
6
7#include <array>
8#include <cinttypes>
9#include <cstdarg>
10#include <cstddef>
11#include <cstdint>
12#include <cstring>
13
14/**
15 * String related functions.
16 *
17 * @defgroup Strings Strings
18 */
19
20#ifdef __MINGW32__
21#undef PRId64
22#undef PRIu64
23#undef PRIX64
24#define PRId64 "I64d"
25#define PRIu64 "I64u"
26#define PRIX64 "I64X"
27#define PRIzu "Iu"
28#else
29#define PRIzu "zu"
30#endif
31
32/**
33 * Copies a string to another.
34 *
35 * @ingroup Strings
36 *
37 * @param dst Pointer to a buffer that shall receive the string.
38 * @param src String to be copied.
39 * @param dst_size Size of the buffer dst.
40 *
41 * @return Length of written string, even if it has been truncated
42 *
43 * @remark The strings are treated as null-terminated strings.
44 * @remark At most dst_size bytes are read from src.
45 * @remark Guarantees that dst string will contain null-termination.
46 */
47int str_copy(char *dst, const char *src, int dst_size);
48
49/**
50 * Copies a string to a fixed-size array of chars.
51 *
52 * @ingroup Strings
53 *
54 * @param dst Array that shall receive the string.
55 * @param src String to be copied.
56 *
57 * @remark The strings are treated as null-terminated strings.
58 * @remark Guarantees that dst string will contain null-termination.
59 */
60template<int N>
61void str_copy(char (&dst)[N], const char *src)
62{
63 str_copy(dst, src, N);
64}
65
66/**
67 * Copies a string to a fixed-size std::array of chars.
68 *
69 * @ingroup Strings
70 *
71 * @param dst Array that shall receive the string.
72 * @param src String to be copied.
73 *
74 * @remark The strings are treated as null-terminated strings.
75 * @remark Guarantees that dst string will contain null-termination.
76 */
77template<size_t N>
78void str_copy(std::array<char, N> &dst, const char *src)
79{
80 str_copy(dst.data(), src, static_cast<int>(N));
81}
82
83/**
84 * Appends a string to another.
85 *
86 * @ingroup Strings
87 *
88 * @param dst Pointer to a buffer that contains a string.
89 * @param src String to append.
90 * @param dst_size Size of the buffer of the dst string.
91 *
92 * @remark The strings are treated as null-terminated strings.
93 * @remark Guarantees that dst string will contain null-termination.
94 */
95void str_append(char *dst, const char *src, int dst_size);
96
97/**
98 * Appends a string to a fixed-size array of chars.
99 *
100 * @ingroup Strings
101 *
102 * @param dst Array that shall receive the string.
103 * @param src String to append.
104 *
105 * @remark The strings are treated as null-terminated strings.
106 * @remark Guarantees that dst string will contain null-termination.
107 */
108template<int N>
109void str_append(char (&dst)[N], const char *src)
110{
111 str_append(dst, src, N);
112}
113
114/**
115 * Truncates a string to a given length.
116 *
117 * @ingroup Strings
118 *
119 * @param dst Pointer to a buffer that shall receive the string.
120 * @param dst_size Size of the buffer dst.
121 * @param src String to be truncated.
122 * @param truncation_len Maximum length of the returned string (not
123 * counting the null-termination).
124 *
125 * @remark The strings are treated as null-terminated strings.
126 * @remark Guarantees that dst string will contain null-termination.
127 */
128void str_truncate(char *dst, int dst_size, const char *src, int truncation_len);
129
130/**
131 * Returns the length of a null-terminated string.
132 *
133 * @ingroup Strings
134 *
135 * @param str Pointer to the string.
136 *
137 * @return Length of string in bytes excluding the null-termination.
138 */
139int str_length(const char *str);
140
141/**
142 * Performs printf formatting into a buffer.
143 *
144 * @ingroup Strings
145 *
146 * @param buffer Pointer to the buffer to receive the formatted string.
147 * @param buffer_size Size of the buffer.
148 * @param format printf formatting string.
149 * @param args The variable argument list.
150 *
151 * @return Length of written string, even if it has been truncated.
152 *
153 * @remark See the C manual for syntax for the printf formatting string.
154 * @remark The strings are treated as null-terminated strings.
155 * @remark Guarantees that buffer string will contain null-termination.
156 */
157[[gnu::format(printf, 3, 0)]] int str_format_v(char *buffer, int buffer_size, const char *format, va_list args);
158
159/**
160 * Performs printf formatting into a buffer.
161 *
162 * @ingroup Strings
163 *
164 * @param buffer Pointer to the buffer to receive the formatted string.
165 * @param buffer_size Size of the buffer.
166 * @param format printf formatting string.
167 * @param ... Parameters for the formatting.
168 *
169 * @return Length of written string, even if it has been truncated.
170 *
171 * @remark See the C manual for syntax for the printf formatting string.
172 * @remark The strings are treated as null-terminated strings.
173 * @remark Guarantees that buffer string will contain null-termination.
174 */
175[[gnu::format(printf, 3, 4)]] int str_format(char *buffer, int buffer_size, const char *format, ...);
176
177#if !defined(CONF_DEBUG)
178int str_format_int(char *buffer, size_t buffer_size, int value);
179
180template<typename... Args>
181int str_format_opt(char *buffer, int buffer_size, const char *format, Args... args)
182{
183 static_assert(sizeof...(args) > 0, "Use str_copy instead of str_format without format arguments");
184 return str_format(buffer, buffer_size, format, args...);
185}
186
187template<>
188inline int str_format_opt(char *buffer, int buffer_size, const char *format, int val) // NOLINT(readability-inconsistent-declaration-parameter-name)
189{
190 if(strcmp(format, "%d") == 0)
191 {
192 return str_format_int(buffer, buffer_size, val);
193 }
194 else
195 {
196 return str_format(buffer, buffer_size, format, val);
197 }
198}
199
200#define str_format str_format_opt
201#endif
202
203char str_uppercase(char c);
204
205bool str_isnum(char c);
206
207bool str_isallnum(const char *str);
208
209bool str_isallnum_hex(const char *str);
210
211/**
212 * Determines whether a character is whitespace.
213 *
214 * @ingroup Strings
215 *
216 * @param c the character to check.
217 *
218 * @return `true` if the character is whitespace, `false` otherwise.
219 *
220 * @remark The following characters are considered whitespace: ` `, `\n`, `\r`, `\t`.
221 */
222bool str_isspace(char c);
223
224/**
225 * Trims specific number of words at the start of a string.
226 *
227 * @ingroup Strings
228 *
229 * @param str String to trim the words from.
230 * @param words Count of words to trim.
231 *
232 * @return Trimmed string
233 *
234 * @remark The strings are treated as null-terminated strings.
235 * @remark Leading whitespace is always trimmed.
236 */
237const char *str_trim_words(const char *str, int words);
238
239/**
240 * Check whether string has ASCII control characters.
241 *
242 * @ingroup Strings
243 *
244 * @param str String to check.
245 *
246 * @return Whether the string has ASCII control characters.
247 *
248 * @remark The strings are treated as null-terminated strings.
249 */
250bool str_has_cc(const char *str);
251
252/**
253 * Replaces all characters below 32 with whitespace.
254 *
255 * @ingroup Strings
256 *
257 * @param str String to sanitize.
258 *
259 * @remark The strings are treated as null-terminated strings.
260 */
261void str_sanitize_cc(char *str);
262
263/**
264 * Replaces all characters below 32 with whitespace with
265 * exception to `\t`, `\n` and `\n`.
266 *
267 * @ingroup Strings
268 *
269 * @param str String to sanitize.
270 *
271 * @remark The strings are treated as null-terminated strings.
272 */
273void str_sanitize(char *str);
274
275/**
276 * Replaces all invalid filename characters with whitespace.
277 *
278 * @param str String to sanitize.
279 * @remark The strings are treated as null-terminated strings.
280 */
281void str_sanitize_filename(char *str);
282
283/**
284 * Checks if a string is a valid filename on all supported platforms.
285 *
286 * @param str Filename to check.
287 *
288 * @return `true` if the string is a valid filename, `false` otherwise.
289 *
290 * @remark The strings are treated as null-terminated strings.
291 */
292bool str_valid_filename(const char *str);
293
294/**
295 * Compares two strings case insensitive, digit chars will be compared as numbers.
296 *
297 * @ingroup Strings
298 *
299 * @param a String to compare.
300 * @param b String to compare.
301 *
302 * @return `< 0` - String a is less than string b
303 * @return `0` - String a is equal to string b
304 * @return `> 0` - String a is greater than string b
305 *
306 * @remark The strings are treated as null-terminated strings.
307 */
308int str_comp_filenames(const char *a, const char *b);
309
310/**
311 * Removes leading and trailing spaces and limits the use of multiple spaces.
312 *
313 * @ingroup Strings
314 *
315 * @param str String to clean up.
316 *
317 * @remark The strings are treated as null-terminated strings.
318 */
319void str_clean_whitespaces(char *str);
320
321/**
322 * Skips leading non-whitespace characters.
323 *
324 * @ingroup Strings
325 *
326 * @param str Pointer to the string.
327 *
328 * @return Pointer to the first whitespace character found
329 * within the string.
330 *
331 * @remark The strings are treated as null-terminated strings.
332 * @remark Whitespace is defined according to str_isspace.
333 */
334char *str_skip_to_whitespace(char *str);
335
336/**
337 * @ingroup Strings
338 *
339 * @see str_skip_to_whitespace
340 */
341const char *str_skip_to_whitespace_const(const char *str);
342
343/**
344 * Skips leading whitespace characters.
345 *
346 * @ingroup Strings
347 *
348 * @param str Pointer to the string.
349 *
350 * @return Pointer to the first non-whitespace character found
351 * within the string.
352 *
353 * @remark The strings are treated as null-terminated strings.
354 * @remark Whitespace is defined according to str_isspace.
355 */
356char *str_skip_whitespaces(char *str);
357
358/**
359 * @ingroup Strings
360 *
361 * @see str_skip_whitespaces
362 */
363const char *str_skip_whitespaces_const(const char *str);
364
365/**
366 * Compares to strings case insensitively.
367 *
368 * @ingroup Strings
369 *
370 * @param a String to compare.
371 * @param b String to compare.
372 *
373 * @return `< 0` if string a is less than string b.
374 * @return `0` if string a is equal to string b.
375 * @return `> 0` if string a is greater than string b.
376 *
377 * @remark Only guaranteed to work with a-z/A-Z.
378 * @remark The strings are treated as null-terminated strings.
379 */
380int str_comp_nocase(const char *a, const char *b);
381
382/**
383 * Compares up to `num` characters of two strings case insensitively.
384 *
385 * @ingroup Strings
386 *
387 * @param a String to compare.
388 * @param b String to compare.
389 * @param num Maximum characters to compare.
390 *
391 * @return `< 0` if string a is less than string b.
392 * @return `0` if string a is equal to string b.
393 * @return `> 0` if string a is greater than string b.
394 *
395 * @remark Only guaranteed to work with a-z/A-Z.
396 * @remark Use `str_utf8_comp_nocase_num` for unicode support.
397 * @remark The strings are treated as null-terminated strings.
398 */
399int str_comp_nocase_num(const char *a, const char *b, int num);
400
401/**
402 * Compares two strings case sensitive.
403 *
404 * @ingroup Strings
405 *
406 * @param a String to compare.
407 * @param b String to compare.
408 *
409 * @return `< 0` if string a is less than string b.
410 * @return `0` if string a is equal to string b.
411 * @return `> 0` if string a is greater than string b.
412 *
413 * @remark The strings are treated as null-terminated strings.
414 */
415int str_comp(const char *a, const char *b);
416
417/**
418 * Compares up to `num` characters of two strings case sensitive.
419 *
420 * @ingroup Strings
421 *
422 * @param a String to compare.
423 * @param b String to compare.
424 * @param num Maximum characters to compare.
425 *
426 * @return `< 0` if string a is less than string b.
427 * @return `0` if string a is equal to string b.
428 * @return `> 0` if string a is greater than string b.
429 *
430 * @remark The strings are treated as null-terminated strings.
431 */
432int str_comp_num(const char *a, const char *b, int num);
433
434/**
435 * Checks case insensitive whether the string begins with a certain prefix.
436 *
437 * @ingroup Strings
438 *
439 * @param str String to check.
440 * @param prefix Prefix to look for.
441 *
442 * @return A pointer to the string `str` after the string prefix, or `nullptr` if
443 * the string prefix isn't a prefix of the string `str`.
444 *
445 * @remark The strings are treated as null-terminated strings.
446 */
447const char *str_startswith_nocase(const char *str, const char *prefix);
448
449/**
450 * Checks case sensitive whether the string begins with a certain prefix.
451 *
452 * @ingroup Strings
453 *
454 * @param str String to check.
455 * @param prefix Prefix to look for.
456 *
457 * @return A pointer to the string `str` after the string prefix, or `nullptr` if
458 * the string prefix isn't a prefix of the string `str`.
459 *
460 * @remark The strings are treated as null-terminated strings.
461 */
462const char *str_startswith(const char *str, const char *prefix);
463
464/**
465 * Checks case insensitive whether the string ends with a certain suffix.
466 *
467 * @ingroup Strings
468 *
469 * @param str String to check.
470 * @param suffix Suffix to look.
471 *
472 * @return A pointer to the beginning of the suffix in the string `str`.
473 * @return `nullptr` if the string suffix isn't a suffix of the string `str`.
474 *
475 * @remark The strings are treated as null-terminated strings.
476 */
477const char *str_endswith_nocase(const char *str, const char *suffix);
478
479/**
480 * Checks case sensitive whether the string ends with a certain suffix.
481 *
482 * @param str String to check.
483 * @param suffix Suffix to look for.
484 *
485 * @return A pointer to the beginning of the suffix in the string `str`.
486 * @return `nullptr` if the string suffix isn't a suffix of the string `str`.
487 *
488 * @remark The strings are treated as null-terminated strings.
489 */
490const char *str_endswith(const char *str, const char *suffix);
491
492/**
493 * Finds a string inside another string case insensitively.
494 *
495 * @ingroup Strings
496 *
497 * @param haystack String to search in.
498 * @param needle String to search for.
499 *
500 * @return A pointer into `haystack` where the needle was found.
501 * @return Returns `nullptr` if `needle` could not be found.
502 *
503 * @remark Only guaranteed to work with a-z/A-Z.
504 * @remark Use str_utf8_find_nocase for unicode support.
505 * @remark The strings are treated as null-terminated strings.
506 */
507const char *str_find_nocase(const char *haystack, const char *needle);
508
509/**
510 * Finds a string inside another string case sensitive.
511 *
512 * @ingroup Strings
513 *
514 * @param haystack String to search in.
515 * @param needle String to search for.
516 *
517 * @return A pointer into `haystack` where the needle was found.
518 * @return Returns `nullptr` if `needle` could not be found.
519 *
520 * @remark The strings are treated as null-terminated strings.
521 */
522const char *str_find(const char *haystack, const char *needle);
523
524/**
525 * Writes the next token after str into buf, returns the rest of the string.
526 *
527 * @ingroup Strings
528 *
529 * @param str Pointer to string.
530 * @param delim Delimiter for tokenization.
531 * @param buffer Buffer to store token in.
532 * @param buffer_size Size of the buffer.
533 *
534 * @return Pointer to rest of the string.
535 *
536 * @remark The token is always null-terminated.
537 */
538const char *str_next_token(const char *str, const char *delim, char *buffer, size_t buffer_size);
539
540/**
541 * Checks if needle is in list delimited by delim.
542 *
543 * @param list List.
544 * @param delim List delimiter.
545 * @param needle Item that is being looked for.
546 *
547 * @return `1` - Item is in list.
548 * @return `0` - Item isn't in list.
549 *
550 * @remark The strings are treated as null-terminated strings.
551 */
552bool str_in_list(const char *list, const char *delim, const char *needle);
553
554/**
555 * @ingroup Strings
556 *
557 * @param haystack String to search in.
558 * @param delim String to search for.
559 * @param offset Number of characters into `haystack`.
560 * @param start Will be set to the first delimiter on the left side of the offset (or `haystack` start).
561 * @param end Will be set to the first delimiter on the right side of the offset (or `haystack` end).
562 *
563 * @return `true` if both delimiters were found.
564 * @return `false` if a delimiter is missing (it uses `haystack` start and end as fallback).
565 *
566 * @remark The strings are treated as null-terminated strings.
567 */
568bool str_delimiters_around_offset(const char *haystack, const char *delim, int offset, int *start, int *end);
569
570/**
571 * Finds the last occurrence of a character
572 *
573 * @ingroup Strings
574 *
575 * @param haystack String to search in.
576 * @param needle Character to search for.
577
578 * @return A pointer into haystack where the needle was found.
579 * @return Returns `nullptr` if needle could not be found.
580 *
581 * @remark The strings are treated as null-terminated strings.
582 * @remark The zero-terminator character can also be found with this function.
583 */
584const char *str_rchr(const char *haystack, char needle);
585
586/**
587 * Counts the number of occurrences of a character in a string.
588 *
589 * @ingroup Strings
590 *
591 * @param haystack String to count in.
592 * @param needle Character to count.
593
594 * @return The number of characters in the haystack string matching
595 * the needle character.
596 *
597 * @remark The strings are treated as null-terminated strings.
598 * @remark The number of zero-terminator characters cannot be counted.
599 */
600int str_countchr(const char *haystack, char needle);
601
602/**
603 * Takes a datablock and generates a hex string of it, with spaces between bytes.
604 *
605 * @ingroup Strings
606 *
607 * @param dst Buffer to fill with hex data.
608 * @param dst_size Size of the buffer (at least 3 * data_size + 1 to contain all data).
609 * @param data Data to turn into hex.
610 * @param data_size Size of the data.
611 *
612 * @remark The destination buffer will be null-terminated.
613 */
614void str_hex(char *dst, int dst_size, const void *data, int data_size);
615
616/**
617 * Takes a datablock and generates a hex string of it, in the C style array format,
618 * i.e. with bytes formatted in 0x00-0xFF notation and commas with spaces between the bytes.
619 * The output can be split over multiple lines by specifying the maximum number of bytes
620 * that should be printed per line.
621 *
622 * @ingroup Strings
623 *
624 * @param dst Buffer to fill with hex data.
625 * @param dst_size Size of the buffer (at least `6 * data_size + 1` to contain all data).
626 * @param data Data to turn into hex.
627 * @param data_size Size of the data.
628 * @param bytes_per_line After this many printed bytes a newline will be printed.
629 *
630 * @remark The destination buffer will be null-terminated.
631 */
632void str_hex_cstyle(char *dst, int dst_size, const void *data, int data_size, int bytes_per_line = 12);
633
634/**
635 * Takes a hex string *without spaces between bytes* and returns a byte array.
636 *
637 * @ingroup Strings
638 *
639 * @param dst Buffer for the byte array.
640 * @param dst_size size of the buffer.
641 * @param src String to decode.
642 *
643 * @return `2` if string doesn't exactly fit the buffer.
644 * @return `1` if invalid character in string.
645 * @return `0` if success.
646 *
647 * @remark The contents of the buffer is only valid on success.
648 */
649int str_hex_decode(void *dst, int dst_size, const char *src);
650
651/**
652 * Takes a datablock and generates the base64 encoding of it.
653 *
654 * @ingroup Strings
655 *
656 * @param dst Buffer to fill with base64 data.
657 * @param dst_size Size of the buffer.
658 * @param data Data to turn into base64.
659 * @param data_size Size of the data.
660 *
661 * @remark The destination buffer will be null-terminated
662 */
663void str_base64(char *dst, int dst_size, const void *data, int data_size);
664
665/**
666 * Takes a base64 string without any whitespace and correct
667 * padding and returns a byte array.
668 *
669 * @ingroup Strings
670 *
671 * @param dst Buffer for the byte array.
672 * @param dst_size Size of the buffer.
673 * @param data String to decode.
674 *
675 * @return `< 0` - Error.
676 * @return `<= 0` - Success, length of the resulting byte buffer.
677 *
678 * @remark The contents of the buffer is only valid on success.
679 */
680int str_base64_decode(void *dst, int dst_size, const char *data);
681
682/**
683 * Escapes \ and " characters in a string.
684 *
685 * @param dst Destination array pointer, gets increased, will point to the terminating null.
686 * @param src Source array.
687 * @param end End of destination array.
688 */
689void str_escape(char **dst, const char *src, const char *end);
690
691/**
692 * Replaces pairs of `\` and `n` characters in a string with newline character `\n`.
693 *
694 * @param str String to unescape in place.
695 *
696 * @remark The strings are treated as null-terminated strings.
697 */
698void str_unescape_newlines(char *str);
699
700int str_toint(const char *str);
701bool str_toint(const char *str, int *out);
702int str_toint_base(const char *str, int base);
703unsigned long str_toulong_base(const char *str, int base);
704int64_t str_toint64_base(const char *str, int base = 10);
705float str_tofloat(const char *str);
706bool str_tofloat(const char *str, float *out);
707
708unsigned str_quickhash(const char *str);
709
710/**
711 * Encode a UTF-8 character.
712 *
713 * @ingroup Strings
714 *
715 * @param ptr Pointer to a buffer that should receive the data. Should be able to hold at least 4 bytes.
716 * @param chr Unicode codepoint to encode.
717 *
718 * @return Number of bytes put into the buffer.
719 *
720 * @remark Does not do null-termination of the string.
721 */
722int str_utf8_encode(char *ptr, int chr);
723
724/**
725 * Decodes a UTF-8 codepoint.
726 *
727 * @ingroup Strings
728 *
729 * @param ptr Pointer to a UTF-8 string. This pointer will be moved forward.
730 *
731 * @return The Unicode codepoint. `-1` for invalid input and 0 for end of string.
732 *
733 * @remark This function will also move the pointer forward.
734 * @remark You may call this function again after an error occurred.
735 * @remark The strings are treated as null-terminated.
736 */
737int str_utf8_decode(const char **ptr);
738
739/**
740 * Truncates a UTF-8 encoded string to a given length.
741 *
742 * @ingroup Strings
743 *
744 * @param dst Pointer to a buffer that shall receive the string.
745 * @param dst_size Size of the buffer dst.
746 * @param src String to be truncated.
747 * @param truncation_len Maximum codepoints in the returned string.
748 *
749 * @remark The strings are treated as utf8-encoded null-terminated strings.
750 * @remark Guarantees that dst string will contain null-termination.
751 */
752void str_utf8_truncate(char *dst, int dst_size, const char *src, int truncation_len);
753
754/**
755 * Fixes truncation of a Unicode character at the end of a UTF-8 string.
756 *
757 * @ingroup Strings
758 *
759 * @param str UTF-8 string.
760 *
761 * @return The new string length.
762 *
763 * @remark The strings are treated as null-terminated.
764 */
765int str_utf8_fix_truncation(char *str);
766
767/**
768 * Removes trailing characters that render as spaces by modifying the string in-place.
769 *
770 * @ingroup Strings
771 *
772 * @param param Input string.
773 *
774 * @remark The string is modified in-place.
775 * @remark The strings are treated as null-terminated.
776 */
777void str_utf8_trim_right(char *param);
778
779/**
780 * Converts the given UTF-8 string to lowercase (locale insensitive).
781 *
782 * @ingroup Strings
783 *
784 * @param input String to convert to lowercase.
785 * @param output Buffer that will receive the lowercase string.
786 * @param size Size of the output buffer.
787 *
788 * @remark The strings are treated as zero-terminated strings.
789 * @remark This function does not work in-place as converting a UTF-8 string to
790 * lowercase may increase its length.
791 */
792void str_utf8_tolower(const char *input, char *output, size_t size);
793
794/**
795 * Checks whether the given Unicode codepoint renders as space.
796 *
797 * @ingroup Strings
798 *
799 * @param code Unicode codepoint to check.
800 *
801 * @return Whether the codepoint is a space.
802 */
803bool str_utf8_isspace(int code);
804
805/**
806 * Checks whether a given byte is the start of a UTF-8 character.
807 *
808 * @ingroup Strings
809 *
810 * @param c Byte to check.
811 *
812 * @return Whether the char starts a UTF-8 character.
813 */
814bool str_utf8_isstart(char c);
815
816/**
817 * Moves a cursor backwards in an UTF-8 string,
818 *
819 * @ingroup Strings
820 *
821 * @param str UTF-8 string.
822 * @param cursor Position in the string.
823 *
824 * @return New cursor position.
825 *
826 * @remark Won't move the cursor less then 0.
827 * @remark The strings are treated as null-terminated.
828 */
829int str_utf8_rewind(const char *str, int cursor);
830
831/**
832 * Finds a UTF-8 string inside another UTF-8 string case insensitively.
833 *
834 * @ingroup Strings
835 *
836 * @param haystack String to search in.
837 * @param needle String to search for.
838 * @param end A pointer that will be set to a pointer into haystack directly behind the
839 * last character where the needle was found. Will be set to `nullptr `if needle
840 * could not be found. Optional parameter.
841 *
842 * @return A pointer into haystack where the needle was found.
843 * @return Returns `nullptr` if needle could not be found.
844 *
845 * @remark The strings are treated as null-terminated strings.
846 */
847const char *str_utf8_find_nocase(const char *haystack, const char *needle, const char **end = nullptr);
848
849/**
850 * Compares two UTF-8 strings case insensitively.
851 *
852 * @ingroup Strings
853 *
854 * @param a String to compare.
855 * @param b String to compare.
856 *
857 * @return `< 0` if string a is less than string b.
858 * @return `0` if string a is equal to string b.
859 * @return `> 0` if string a is greater than string b.
860 */
861int str_utf8_comp_nocase(const char *a, const char *b);
862
863/**
864 * Compares up to `num` bytes of two UTF-8 strings case insensitively.
865 *
866 * @ingroup Strings
867 *
868 * @param a String to compare.
869 * @param b String to compare.
870 * @param num Maximum bytes to compare.
871 *
872 * @return `< 0` if string a is less than string b.
873 * @return `0` if string a is equal to string b.
874 * @return `> 0` if string a is greater than string b.
875 */
876int str_utf8_comp_nocase_num(const char *a, const char *b, int num);
877
878/**
879 * Skips leading characters that render as spaces.
880 *
881 * @ingroup Strings
882 *
883 * @param str Input string.
884 *
885 * @return Pointer to the first non-whitespace character found within the string.
886 * @remark The strings are treated as null-terminated strings.
887 */
888const char *str_utf8_skip_whitespaces(const char *str);
889
890/**
891 * Moves a cursor forwards in an UTF-8 string.
892 *
893 * @ingroup Strings
894 *
895 * @param str UTF-8 string.
896 * @param cursor Position in the string.
897 *
898 * @return New cursor position.
899 *
900 * @remark Won't move the cursor beyond the null-termination marker.
901 * @remark The strings are treated as null-terminated.
902 */
903int str_utf8_forward(const char *str, int cursor);
904
905/**
906 * Checks if a strings contains just valid UTF-8 characters.
907 *
908 * @ingroup Strings
909 *
910 * @param str Pointer to a possible UTF-8 string.
911 *
912 * @return `false` if invalid characters were found, `true` if only valid characters were found.
913 *
914 * @remark The string is treated as null-terminated UTF-8 string.
915 */
916bool str_utf8_check(const char *str);
917
918/**
919 * Copies a number of UTF-8 characters from one string to another.
920 *
921 * @ingroup Strings
922 *
923 * @param dst Pointer to a buffer that shall receive the string.
924 * @param src String to be copied.
925 * @param dst_size Size of the buffer dst.
926 * @param num Maximum number of UTF-8 characters to be copied.
927 *
928 * @remark The strings are treated as null-terminated strings.
929 * @remark Guarantees that dst string will contain null-termination.
930 */
931void str_utf8_copy_num(char *dst, const char *src, int dst_size, int num);
932
933/**
934 * Determines the byte size and UTF-8 character count of a UTF-8 string.
935 *
936 * @ingroup Strings
937 *
938 * @param str Pointer to the string.
939 * @param max_size Maximum number of bytes to count.
940 * @param max_count Maximum number of UTF-8 characters to count.
941 * @param size Pointer to store size (number of non. Zero bytes) of the string.
942 * @param count Pointer to store count of UTF-8 characters of the string.
943 *
944 * @remark The string is treated as null-terminated UTF-8 string.
945 * @remark It's the user's responsibility to make sure the bounds are aligned.
946 */
947void str_utf8_stats(const char *str, size_t max_size, size_t max_count, size_t *size, size_t *count);
948
949/**
950 * Converts a byte offset of a UTF-8 string to the UTF-8 character offset.
951 *
952 * @ingroup Strings
953 *
954 * @param str Pointer to the string.
955 * @param byte_offset Offset in bytes.
956 *
957 * @return Offset in UTF-8 characters. Clamped to the maximum length of the string in UTF-8 characters.
958 *
959 * @remark The string is treated as a null-terminated UTF-8 string.
960 * @remark It's the user's responsibility to make sure the bounds are aligned.
961 */
962size_t str_utf8_offset_bytes_to_chars(const char *str, size_t byte_offset);
963
964/**
965 * Converts a UTF-8 character offset of a UTF-8 string to the byte offset.
966 *
967 * @ingroup Strings
968 *
969 * @param str Pointer to the string.
970 * @param char_offset Offset in UTF-8 characters.
971 *
972 * @return Offset in bytes. Clamped to the maximum length of the string in bytes.
973 *
974 * @remark The string is treated as a null-terminated UTF-8 string.
975 * @remark It's the user's responsibility to make sure the bounds are aligned.
976 */
977size_t str_utf8_offset_chars_to_bytes(const char *str, size_t char_offset);
978
979/**
980 * Computes the edit distance between two strings.
981 *
982 * @param a First string for the edit distance.
983 * @param b Second string for the edit distance.
984 *
985 * @return The edit distance between the both strings.
986 *
987 * @remark The strings are treated as null-terminated strings.
988 */
989int str_utf8_dist(const char *a, const char *b);
990
991/**
992 * Computes the edit distance between two strings, allows buffers
993 * to be passed in.
994 *
995 * @ingroup Strings
996 *
997 * @param a First string for the edit distance.
998 * @param b Second string for the edit distance.
999 * @param buf Buffer for the function.
1000 * @param buf_len Length of the buffer, must be at least as long as
1001 * twice the length of both strings combined plus two.
1002 *
1003 * @return The edit distance between the both strings.
1004 *
1005 * @remark The strings are treated as null-terminated strings.
1006 */
1007int str_utf8_dist_buffer(const char *a, const char *b, int *buf, int buf_len);
1008
1009/**
1010 * Computes the edit distance between two strings, allows buffers
1011 * to be passed in.
1012 *
1013 * @ingroup Strings
1014 *
1015 * @param a First string for the edit distance.
1016 * @param a_len Length of the first string.
1017 * @param b Second string for the edit distance.
1018 * @param b_len Length of the second string.
1019 * @param buf Buffer for the function.
1020 * @param buf_len Length of the buffer, must be at least as long as
1021 * the length of both strings combined plus two.
1022 *
1023 * @return The edit distance between the both strings.
1024 *
1025 * @remark The strings are treated as null-terminated strings.
1026 */
1027int str_utf32_dist_buffer(const int *a, int a_len, const int *b, int b_len, int *buf, int buf_len);
1028
1029int str_utf8_to_skeleton(const char *str, int *buf, int buf_len);
1030
1031/**
1032 * Checks if two strings only differ by confusable characters.
1033 *
1034 * @ingroup Strings
1035 *
1036 * @param str1 String to compare.
1037 * @param str2 String to compare.
1038 *
1039 * @return `0` if the strings are confusables.
1040 */
1041int str_utf8_comp_confusable(const char *str1, const char *str2);
1042
1043/**
1044 * Converts the given Unicode codepoint to lowercase (locale insensitive).
1045 *
1046 * @ingroup Strings
1047 *
1048 * @param code Unicode codepoint to convert.
1049 *
1050 * @return Lowercase codepoint, or the original codepoint if there is no lowercase version.
1051 */
1052int str_utf8_tolower_codepoint(int code);
1053
1054#endif
1055