Blender  V2.93
BLI_string.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 #include <inttypes.h>
27 #include <stdarg.h>
28 
29 #include "BLI_compiler_attrs.h"
30 #include "BLI_utildefines.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 char *BLI_strdupn(const char *str, const size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
37  ATTR_NONNULL();
38 
40 
41 char *BLI_strdupcat(const char *__restrict str1,
42  const char *__restrict str2) ATTR_WARN_UNUSED_RESULT
44 
45 char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
46  ATTR_NONNULL();
47 
48 char *BLI_strncpy_ensure_pad(char *__restrict dst,
49  const char *__restrict src,
50  const char pad,
51  size_t maxncpy) ATTR_NONNULL();
52 
53 size_t BLI_strncpy_rlen(char *__restrict dst,
54  const char *__restrict src,
55  const size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
56 
57 size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) ATTR_WARN_UNUSED_RESULT
58  ATTR_NONNULL();
59 
60 char *BLI_str_quoted_substrN(const char *__restrict str,
61  const char *__restrict prefix) ATTR_WARN_UNUSED_RESULT
63 
64 char *BLI_str_replaceN(const char *__restrict str,
65  const char *__restrict substr_old,
66  const char *__restrict substr_new) ATTR_WARN_UNUSED_RESULT
68 
69 void BLI_str_replace_char(char *string, char src, char dst) ATTR_NONNULL();
70 
71 bool BLI_str_replace_table_exact(char *string,
72  const size_t string_len,
73  const char *replace_table[][2],
74  int replace_table_len);
75 
76 size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
78 size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
79  ATTR_NONNULL(1, 3) ATTR_PRINTF_FORMAT(3, 4);
80 
81 size_t BLI_vsnprintf(char *__restrict buffer,
82  size_t maxncpy,
83  const char *__restrict format,
84  va_list arg) ATTR_PRINTF_FORMAT(3, 0);
85 size_t BLI_vsnprintf_rlen(char *__restrict buffer,
86  size_t maxncpy,
87  const char *__restrict format,
88  va_list arg) ATTR_PRINTF_FORMAT(3, 0);
89 
90 char *BLI_sprintfN(const char *__restrict format, ...) ATTR_WARN_UNUSED_RESULT
92 
93 size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const size_t dst_maxncpy)
94  ATTR_NONNULL();
95 size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, const size_t src_maxncpy)
96  ATTR_NONNULL();
97 const char *BLI_str_escape_find_quote(const char *str) ATTR_NONNULL();
98 
99 size_t BLI_str_format_int_grouped(char dst[16], int num) ATTR_NONNULL();
100 size_t BLI_str_format_uint64_grouped(char dst[16], uint64_t num) ATTR_NONNULL();
101 void BLI_str_format_byte_unit(char dst[15], long long int bytes, const bool base_10)
102  ATTR_NONNULL();
103 
104 int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
105 char *BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
106 char *BLI_strncasestr(const char *s, const char *find, size_t len) ATTR_WARN_UNUSED_RESULT
107  ATTR_NONNULL();
108 int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
109 int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT
110  ATTR_NONNULL();
111 int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
112 int BLI_strcmp_ignore_pad(const char *str1,
113  const char *str2,
114  const char pad) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
115 
116 size_t BLI_strnlen(const char *str, const size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
117 
118 void BLI_str_tolower_ascii(char *str, const size_t len) ATTR_NONNULL();
119 void BLI_str_toupper_ascii(char *str, const size_t len) ATTR_NONNULL();
120 void BLI_str_rstrip(char *str) ATTR_NONNULL();
121 int BLI_str_rstrip_float_zero(char *str, const char pad) ATTR_NONNULL();
122 
123 int BLI_str_index_in_array_n(const char *__restrict str,
124  const char **__restrict str_array,
125  const int str_array_len) ATTR_NONNULL();
126 int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array)
127  ATTR_NONNULL();
128 
129 bool BLI_str_startswith(const char *__restrict str, const char *__restrict start) ATTR_NONNULL();
130 bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL();
131 bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, size_t length)
132  ATTR_NONNULL();
133 
134 size_t BLI_str_partition(const char *str, const char delim[], const char **sep, const char **suf)
135  ATTR_NONNULL();
136 size_t BLI_str_rpartition(const char *str, const char delim[], const char **sep, const char **suf)
137  ATTR_NONNULL();
138 size_t BLI_str_partition_ex(const char *str,
139  const char *end,
140  const char delim[],
141  const char **sep,
142  const char **suf,
143  const bool from_right) ATTR_NONNULL(1, 3, 4, 5);
144 
145 int BLI_string_max_possible_word_count(const int str_len);
146 bool BLI_string_has_word_prefix(const char *haystack, const char *needle, size_t needle_len);
147 bool BLI_string_all_words_matched(const char *name,
148  const char *str,
149  int (*words)[2],
150  const int words_len);
151 
152 int BLI_string_find_split_words(const char *str,
153  const size_t len,
154  const char delim,
155  int r_words[][2],
156  int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
157 
158 /* -------------------------------------------------------------------- */
163 #define STRNCPY(dst, src) BLI_strncpy(dst, src, ARRAY_SIZE(dst))
164 #define STRNCPY_RLEN(dst, src) BLI_strncpy_rlen(dst, src, ARRAY_SIZE(dst))
165 #define SNPRINTF(dst, format, ...) BLI_snprintf(dst, ARRAY_SIZE(dst), format, __VA_ARGS__)
166 #define SNPRINTF_RLEN(dst, format, ...) \
167  BLI_snprintf_rlen(dst, ARRAY_SIZE(dst), format, __VA_ARGS__)
168 #define STR_CONCAT(dst, len, suffix) \
169  len += BLI_strncpy_rlen(dst + len, suffix, ARRAY_SIZE(dst) - len)
170 #define STR_CONCATF(dst, len, format, ...) \
171  len += BLI_snprintf_rlen(dst + len, ARRAY_SIZE(dst) - len, format, __VA_ARGS__)
174 /* -------------------------------------------------------------------- */
180 /* Manual line breaks for readability. */
181 /* clang-format off */
182 /* STR_ELEM#(v, ...): is the first arg equal any others? */
183 /* Internal helpers. */
184 #define _VA_STR_ELEM2(v, a) (strcmp(v, a) == 0)
185 #define _VA_STR_ELEM3(v, a, b) \
186  (_VA_STR_ELEM2(v, a) || (_VA_STR_ELEM2(v, b)))
187 #define _VA_STR_ELEM4(v, a, b, c) \
188  (_VA_STR_ELEM3(v, a, b) || (_VA_STR_ELEM2(v, c)))
189 #define _VA_STR_ELEM5(v, a, b, c, d) \
190  (_VA_STR_ELEM4(v, a, b, c) || (_VA_STR_ELEM2(v, d)))
191 #define _VA_STR_ELEM6(v, a, b, c, d, e) \
192  (_VA_STR_ELEM5(v, a, b, c, d) || (_VA_STR_ELEM2(v, e)))
193 #define _VA_STR_ELEM7(v, a, b, c, d, e, f) \
194  (_VA_STR_ELEM6(v, a, b, c, d, e) || (_VA_STR_ELEM2(v, f)))
195 #define _VA_STR_ELEM8(v, a, b, c, d, e, f, g) \
196  (_VA_STR_ELEM7(v, a, b, c, d, e, f) || (_VA_STR_ELEM2(v, g)))
197 #define _VA_STR_ELEM9(v, a, b, c, d, e, f, g, h) \
198  (_VA_STR_ELEM8(v, a, b, c, d, e, f, g) || (_VA_STR_ELEM2(v, h)))
199 #define _VA_STR_ELEM10(v, a, b, c, d, e, f, g, h, i) \
200  (_VA_STR_ELEM9(v, a, b, c, d, e, f, g, h) || (_VA_STR_ELEM2(v, i)))
201 #define _VA_STR_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
202  (_VA_STR_ELEM10(v, a, b, c, d, e, f, g, h, i) || (_VA_STR_ELEM2(v, j)))
203 #define _VA_STR_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
204  (_VA_STR_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || (_VA_STR_ELEM2(v, k)))
205 #define _VA_STR_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
206  (_VA_STR_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || (_VA_STR_ELEM2(v, l)))
207 #define _VA_STR_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
208  (_VA_STR_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || (_VA_STR_ELEM2(v, m)))
209 #define _VA_STR_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
210  (_VA_STR_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || (_VA_STR_ELEM2(v, n)))
211 #define _VA_STR_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
212  (_VA_STR_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || (_VA_STR_ELEM2(v, o)))
213 #define _VA_STR_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
214  (_VA_STR_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || (_VA_STR_ELEM2(v, p)))
215 /* clang-format on */
216 
217 /* reusable STR_ELEM macro */
218 #define STR_ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_STR_ELEM, __VA_ARGS__)
219 
222 #ifdef __cplusplus
223 }
224 #endif
#define ATTR_MALLOC
#define ATTR_NONNULL(...)
void BLI_kdtree_nd_() int BLI_kdtree_nd_() int BLI_kdtree_nd_() int BLI_kdtree_nd_() ATTR_WARN_UNUSED_RESULT
int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array) ATTR_NONNULL()
Definition: string.c:986
bool BLI_str_startswith(const char *__restrict str, const char *__restrict start) ATTR_NONNULL()
Definition: string.c:1006
size_t size_t BLI_vsnprintf_rlen(char *__restrict buffer, size_t maxncpy, const char *__restrict format, va_list arg) ATTR_PRINTF_FORMAT(3
size_t BLI_str_partition_ex(const char *str, const char *end, const char delim[], const char **sep, const char **suf, const bool from_right) ATTR_NONNULL(1
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
void BLI_str_format_byte_unit(char dst[15], long long int bytes, const bool base_10) ATTR_NONNULL()
Definition: string.c:1206
bool BLI_str_replace_table_exact(char *string, const size_t string_len, const char *replace_table[][2], int replace_table_len)
Definition: string.c:549
size_t ATTR_PRINTF_FORMAT(3, 4)
char * BLI_strncasestr(const char *s, const char *find, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:635
void BLI_str_rstrip(char *str) ATTR_NONNULL()
Definition: string.c:915
bool BLI_string_has_word_prefix(const char *haystack, const char *needle, size_t needle_len)
Definition: string.c:604
bool BLI_string_all_words_matched(const char *name, const char *str, int(*words)[2], const int words_len)
Definition: string.c:616
int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:689
void BLI_str_tolower_ascii(char *str, const size_t len) ATTR_NONNULL()
Definition: string.c:890
char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:578
char * BLI_strdupn(const char *str, const size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:54
size_t int BLI_string_max_possible_word_count(const int str_len)
Definition: string.c:599
int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:666
bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, size_t length) ATTR_NONNULL()
Definition: string.c:1017
size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:201
size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:187
char * BLI_str_replaceN(const char *__restrict str, const char *__restrict substr_old, const char *__restrict substr_new) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:470
int BLI_string_find_split_words(const char *str, const size_t len, const char delim, int r_words[][2], int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:1242
int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:766
size_t size_t char size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const size_t dst_maxncpy) ATTR_NONNULL()
Definition: string.c:333
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
int BLI_strcmp_ignore_pad(const char *str1, const char *str2, const char pad) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:837
char * BLI_strncpy_ensure_pad(char *__restrict dst, const char *__restrict src, const char pad, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:132
int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:570
int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len) ATTR_NONNULL()
Definition: string.c:964
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
size_t BLI_strnlen(const char *str, const size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:878
int BLI_str_rstrip_float_zero(char *str, const char pad) ATTR_NONNULL()
Definition: string.c:936
size_t BLI_str_partition(const char *str, const char delim[], const char **sep, const char **suf) ATTR_NONNULL()
Definition: string.c:1056
size_t BLI_vsnprintf(char *__restrict buffer, size_t maxncpy, const char *__restrict format, va_list arg) ATTR_PRINTF_FORMAT(3
char * BLI_strdupcat(const char *__restrict str1, const char *__restrict str2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:81
size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, const size_t src_maxncpy) ATTR_NONNULL()
Definition: string.c:375
bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL()
Definition: string.c:1040
void BLI_str_replace_char(char *string, char src, char dst) ATTR_NONNULL()
Definition: string.c:532
size_t BLI_str_format_uint64_grouped(char dst[16], uint64_t num) ATTR_NONNULL()
Definition: string.c:1186
char * BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:432
size_t BLI_str_format_int_grouped(char dst[16], int num) ATTR_NONNULL()
Definition: string.c:1170
size_t BLI_str_rpartition(const char *str, const char delim[], const char **sep, const char **suf) ATTR_NONNULL()
Definition: string.c:1071
void BLI_str_toupper_ascii(char *str, const size_t len) ATTR_NONNULL()
Definition: string.c:901
const char * BLI_str_escape_find_quote(const char *str) ATTR_NONNULL()
Definition: string.c:410
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
#define str(s)
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
format
Definition: logImageCore.h:47
static unsigned a[3]
Definition: RandGen.cpp:92
unsigned __int64 uint64_t
Definition: stdint.h:93
uint len