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:563
void str_utf8_trim_right(char *param)
Definition str.cpp:1070
void str_hex_cstyle(char *dst, int dst_size, const void *data, int data_size, int bytes_per_line)
Definition str.cpp:595
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:1169
char * str_skip_to_whitespace(char *str)
Definition str.cpp:323
size_t str_utf8_offset_chars_to_bytes(const char *str, size_t char_offset)
Definition str.cpp:1305
void str_utf8_copy_num(char *dst, const char *src, int dst_size, int num)
Definition str.cpp:1252
int str_length(const char *str)
Definition str.cpp:52
int str_utf8_forward(const char *str, int cursor)
Definition str.cpp:1229
int str_utf8_decode(const char **ptr)
Definition str.cpp:969
void str_sanitize(char *str_in)
Definition str.cpp:171
const char * str_find_nocase(const char *haystack, const char *needle)
Definition str.cpp:446
const char * str_startswith(const char *str, const char *prefix)
Definition str.cpp:393
void str_base64(char *dst, int dst_size, const void *data_raw, int data_size)
Definition str.cpp:687
int str_utf8_encode(char *ptr, int chr)
Definition str.cpp:924
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:1358
void str_sanitize_cc(char *str_in)
Definition str.cpp:159
const char * str_skip_whitespaces_const(const char *str)
Definition str.cpp:344
const char * str_skip_to_whitespace_const(const char *str)
Definition str.cpp:330
const char * str_utf8_skip_whitespaces(const char *str)
Definition str.cpp:1209
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:375
bool str_delimiters_around_offset(const char *haystack, const char *delim, int offset, int *start, int *end)
Definition str.cpp:531
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:498
int str_base64_decode(void *dst_raw, int dst_size, const char *data)
Definition str.cpp:764
void str_utf8_truncate(char *dst, int dst_size, const char *src, int truncation_len)
Definition str.cpp:1036
bool str_utf8_isstart(char c)
Definition str.cpp:1124
void str_utf8_stats(const char *str, size_t max_size, size_t max_count, size_t *size, size_t *count)
Definition str.cpp:1270
int str_hex_decode(void *dst, int dst_size, const char *src)
Definition str.cpp:670
int str_comp_filenames(const char *a, const char *b)
Definition str.cpp:258
void str_hex(char *dst, int dst_size, const void *data, int data_size)
Definition str.cpp:580
void str_utf8_tolower(const char *input, char *output, size_t size)
Definition str.cpp:1095
int str_comp_nocase(const char *a, const char *b)
Definition str.cpp:352
bool str_utf8_check(const char *str)
Definition str.cpp:1239
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:1290
bool str_isspace(char c)
Definition str.cpp:126
int str_countchr(const char *haystack, char needle)
Definition str.cpp:568
int str_comp(const char *a, const char *b)
Definition str.cpp:370
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:1142
void str_clean_whitespaces(char *str)
Definition str.cpp:292
const char * str_startswith_nocase(const char *str, const char *prefix)
Definition str.cpp:380
int str_utf8_comp_nocase_num(const char *a, const char *b, int num)
Definition str.cpp:1185
const char * str_endswith_nocase(const char *str, const char *suffix)
Definition str.cpp:406
bool str_utf8_isspace(int code)
Definition str.cpp:1113
int str_copy(char *dst, const char *src, int dst_size)
Definition str.cpp:18
char * str_skip_whitespaces(char *str)
Definition str.cpp:337
int str_utf8_rewind(const char *str, int cursor)
Definition str.cpp:1131
int str_utf8_fix_truncation(char *str)
Definition str.cpp:1053
const char * str_find(const char *haystack, const char *needle)
Definition str.cpp:465
int str_comp_nocase_num(const char *a, const char *b, int num)
Definition str.cpp:361
int str_utf8_dist_buffer(const char *a_utf8, const char *b_utf8, int *buf, int buf_len)
Definition str.cpp:1338
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:894
void str_unescape_newlines(char *str)
Definition str.cpp:849
bool str_isallnum_hex(const char *str)
Definition str.cpp:115
unsigned long str_toulong_base(const char *str, int base)
Definition str.cpp:889
int str_utf8_dist(const char *a, const char *b)
Definition str.cpp:1318
int str_toint_base(const char *str, int base)
Definition str.cpp:884
bool str_in_list(const char *list, const char *delim, const char *needle)
Definition str.cpp:517
void str_escape(char **dst, const char *src, const char *end)
Definition str.cpp:833
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:867
bool str_isnum(char c)
Definition str.cpp:99
float str_tofloat(const char *str)
Definition str.cpp:899
unsigned str_quickhash(const char *str)
Definition str.cpp:916
#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:426
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