DDNet documentation
Loading...
Searching...
No Matches
str.h
Go to the documentation of this file.
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
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
47int str_copy(char *dst, const char *src, int dst_size);
48
60template<int N>
61void str_copy(char (&dst)[N], const char *src)
62{
63 str_copy(dst, src, N);
64}
65
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
95void str_append(char *dst, const char *src, int dst_size);
96
108template<int N>
109void str_append(char (&dst)[N], const char *src)
110{
111 str_append(dst, src, N);
112}
113
128void str_truncate(char *dst, int dst_size, const char *src, int truncation_len);
129
139int str_length(const char *str);
140
157[[gnu::format(printf, 3, 0)]] int str_format_v(char *buffer, int buffer_size, const char *format, va_list args);
158
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
222bool str_isspace(char c);
223
237const char *str_trim_words(const char *str, int words);
238
250bool str_has_cc(const char *str);
251
261void str_sanitize_cc(char *str);
262
273void str_sanitize(char *str);
274
281void str_sanitize_filename(char *str);
282
292bool str_valid_filename(const char *str);
293
308int str_comp_filenames(const char *a, const char *b);
309
319void str_clean_whitespaces(char *str);
320
334char *str_skip_to_whitespace(char *str);
335
341const char *str_skip_to_whitespace_const(const char *str);
342
356char *str_skip_whitespaces(char *str);
357
363const char *str_skip_whitespaces_const(const char *str);
364
380int str_comp_nocase(const char *a, const char *b);
381
399int str_comp_nocase_num(const char *a, const char *b, int num);
400
415int str_comp(const char *a, const char *b);
416
432int str_comp_num(const char *a, const char *b, int num);
433
447const char *str_startswith_nocase(const char *str, const char *prefix);
448
462const char *str_startswith(const char *str, const char *prefix);
463
477const char *str_endswith_nocase(const char *str, const char *suffix);
478
490const char *str_endswith(const char *str, const char *suffix);
491
507const char *str_find_nocase(const char *haystack, const char *needle);
508
522const char *str_find(const char *haystack, const char *needle);
523
538const char *str_next_token(const char *str, const char *delim, char *buffer, size_t buffer_size);
539
552bool str_in_list(const char *list, const char *delim, const char *needle);
553
568bool str_delimiters_around_offset(const char *haystack, const char *delim, int offset, int *start, int *end);
569
584const char *str_rchr(const char *haystack, char needle);
585
600int str_countchr(const char *haystack, char needle);
601
614void str_hex(char *dst, int dst_size, const void *data, int data_size);
615
632void str_hex_cstyle(char *dst, int dst_size, const void *data, int data_size, int bytes_per_line = 12);
633
649int str_hex_decode(void *dst, int dst_size, const char *src);
650
663void str_base64(char *dst, int dst_size, const void *data, int data_size);
664
680int str_base64_decode(void *dst, int dst_size, const char *data);
681
689void str_escape(char **dst, const char *src, const char *end);
690
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
722int str_utf8_encode(char *ptr, int chr);
723
737int str_utf8_decode(const char **ptr);
738
752void str_utf8_truncate(char *dst, int dst_size, const char *src, int truncation_len);
753
765int str_utf8_fix_truncation(char *str);
766
777void str_utf8_trim_right(char *param);
778
792void str_utf8_tolower(const char *input, char *output, size_t size);
793
803bool str_utf8_isspace(int code);
804
814bool str_utf8_isstart(char c);
815
829int str_utf8_rewind(const char *str, int cursor);
830
847const char *str_utf8_find_nocase(const char *haystack, const char *needle, const char **end = nullptr);
848
861int str_utf8_comp_nocase(const char *a, const char *b);
862
876int str_utf8_comp_nocase_num(const char *a, const char *b, int num);
877
888const char *str_utf8_skip_whitespaces(const char *str);
889
903int str_utf8_forward(const char *str, int cursor);
904
916bool str_utf8_check(const char *str);
917
931void str_utf8_copy_num(char *dst, const char *src, int dst_size, int num);
932
947void str_utf8_stats(const char *str, size_t max_size, size_t max_count, size_t *size, size_t *count);
948
962size_t str_utf8_offset_bytes_to_chars(const char *str, size_t byte_offset);
963
977size_t str_utf8_offset_chars_to_bytes(const char *str, size_t char_offset);
978
989int str_utf8_dist(const char *a, const char *b);
990
1007int str_utf8_dist_buffer(const char *a, const char *b, int *buf, int buf_len);
1008
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
1041int str_utf8_comp_confusable(const char *str1, const char *str2);
1042
1052int str_utf8_tolower_codepoint(int code);
1053
1054#endif
const char * str_rchr(const char *haystack, char needle)
Definition str.cpp:565
void str_utf8_trim_right(char *param)
Definition str.cpp:1072
void str_hex_cstyle(char *dst, int dst_size, const void *data, int data_size, int bytes_per_line)
Definition str.cpp:597
int str_utf8_tolower_codepoint(int code)
Definition tolower.cpp:5
int str_utf8_comp_nocase(const char *a, const char *b)
Definition str.cpp:1171
char * str_skip_to_whitespace(char *str)
Definition str.cpp:325
size_t str_utf8_offset_chars_to_bytes(const char *str, size_t char_offset)
Definition str.cpp:1307
void str_utf8_copy_num(char *dst, const char *src, int dst_size, int num)
Definition str.cpp:1254
int str_length(const char *str)
Definition str.cpp:52
int str_utf8_forward(const char *str, int cursor)
Definition str.cpp:1231
int str_utf8_decode(const char **ptr)
Definition str.cpp:971
void str_sanitize(char *str_in)
Definition str.cpp:171
const char * str_find_nocase(const char *haystack, const char *needle)
Definition str.cpp:448
const char * str_startswith(const char *str, const char *prefix)
Definition str.cpp:395
void str_base64(char *dst, int dst_size, const void *data_raw, int data_size)
Definition str.cpp:689
int str_utf8_encode(char *ptr, int chr)
Definition str.cpp:926
bool str_has_cc(const char *str)
Definition str.cpp:144
int str_utf32_dist_buffer(const int *a, int a_len, const int *b, int b_len, int *buf, int buf_len)
Definition str.cpp:1360
void str_sanitize_cc(char *str_in)
Definition str.cpp:159
const char * str_skip_whitespaces_const(const char *str)
Definition str.cpp:346
const char * str_skip_to_whitespace_const(const char *str)
Definition str.cpp:332
const char * str_utf8_skip_whitespaces(const char *str)
Definition str.cpp:1211
int str_format_v(char *buffer, int buffer_size, const char *format, va_list args)
Definition str.cpp:57
int str_comp_num(const char *a, const char *b, int num)
Definition str.cpp:377
bool str_delimiters_around_offset(const char *haystack, const char *delim, int offset, int *start, int *end)
Definition str.cpp:533
int str_utf8_comp_confusable(const char *str1, const char *str2)
Definition confusables.cpp:82
const char * str_next_token(const char *str, const char *delim, char *buffer, size_t buffer_size)
Definition str.cpp:500
int str_base64_decode(void *dst_raw, int dst_size, const char *data)
Definition str.cpp:766
void str_utf8_truncate(char *dst, int dst_size, const char *src, int truncation_len)
Definition str.cpp:1038
bool str_utf8_isstart(char c)
Definition str.cpp:1126
void str_utf8_stats(const char *str, size_t max_size, size_t max_count, size_t *size, size_t *count)
Definition str.cpp:1272
int str_hex_decode(void *dst, int dst_size, const char *src)
Definition str.cpp:672
int str_comp_filenames(const char *a, const char *b)
Definition str.cpp:260
void str_hex(char *dst, int dst_size, const void *data, int data_size)
Definition str.cpp:582
void str_utf8_tolower(const char *input, char *output, size_t size)
Definition str.cpp:1097
int str_comp_nocase(const char *a, const char *b)
Definition str.cpp:354
bool str_utf8_check(const char *str)
Definition str.cpp:1241
void str_truncate(char *dst, int dst_size, const char *src, int truncation_len)
Definition str.cpp:42
size_t str_utf8_offset_bytes_to_chars(const char *str, size_t byte_offset)
Definition str.cpp:1292
bool str_isspace(char c)
Definition str.cpp:126
int str_countchr(const char *haystack, char needle)
Definition str.cpp:570
int str_comp(const char *a, const char *b)
Definition str.cpp:372
void str_append(char *dst, const char *src, int dst_size)
Definition str.cpp:25
const char * str_utf8_find_nocase(const char *haystack, const char *needle, const char **end)
Definition str.cpp:1144
void str_clean_whitespaces(char *str)
Definition str.cpp:294
const char * str_startswith_nocase(const char *str, const char *prefix)
Definition str.cpp:382
int str_utf8_comp_nocase_num(const char *a, const char *b, int num)
Definition str.cpp:1187
const char * str_endswith_nocase(const char *str, const char *suffix)
Definition str.cpp:408
bool str_utf8_isspace(int code)
Definition str.cpp:1115
int str_copy(char *dst, const char *src, int dst_size)
Definition str.cpp:18
char * str_skip_whitespaces(char *str)
Definition str.cpp:339
int str_utf8_rewind(const char *str, int cursor)
Definition str.cpp:1133
int str_utf8_fix_truncation(char *str)
Definition str.cpp:1055
const char * str_find(const char *haystack, const char *needle)
Definition str.cpp:467
int str_comp_nocase_num(const char *a, const char *b, int num)
Definition str.cpp:363
int str_utf8_dist_buffer(const char *a_utf8, const char *b_utf8, int *buf, int buf_len)
Definition str.cpp:1340
const char * str_trim_words(const char *str, int words)
Definition str.cpp:131
int64_t str_toint64_base(const char *str, int base)
Definition str.cpp:896
void str_unescape_newlines(char *str)
Definition str.cpp:851
bool str_isallnum_hex(const char *str)
Definition str.cpp:115
unsigned long str_toulong_base(const char *str, int base)
Definition str.cpp:891
int str_utf8_dist(const char *a, const char *b)
Definition str.cpp:1320
int str_toint_base(const char *str, int base)
Definition str.cpp:886
bool str_in_list(const char *list, const char *delim, const char *needle)
Definition str.cpp:519
void str_escape(char **dst, const char *src, const char *end)
Definition str.cpp:835
int str_format_int(char *buffer, size_t buffer_size, int value)
Definition str.cpp:70
int str_toint(const char *str)
Definition str.cpp:869
bool str_isnum(char c)
Definition str.cpp:99
float str_tofloat(const char *str)
Definition str.cpp:901
unsigned str_quickhash(const char *str)
Definition str.cpp:918
#define str_format
Definition str.cpp:89
void str_sanitize_filename(char *str_in)
Definition str.cpp:182
const char * str_endswith(const char *str, const char *suffix)
Definition str.cpp:428
char str_uppercase(char c)
Definition str.cpp:92
bool str_isallnum(const char *str)
Definition str.cpp:104
bool str_valid_filename(const char *str)
Definition str.cpp:196
int str_format_opt(char *buffer, int buffer_size, const char *format, Args... args)
Definition str.h:181
int str_utf8_to_skeleton(const char *str, int *buf, int buf_len)
Definition confusables.cpp:65