Blender  V3.3
BLI_string.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
4 #pragma once
5 
10 #include <inttypes.h>
11 #include <stdarg.h>
12 
13 #include "BLI_compiler_attrs.h"
14 #include "BLI_utildefines.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
30 
39 
46 char *BLI_strdupcat(const char *__restrict str1,
47  const char *__restrict str2) ATTR_WARN_UNUSED_RESULT
49 
60 char *BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL();
61 
72 char *BLI_strncpy_ensure_pad(char *__restrict dst,
73  const char *__restrict src,
74  char pad,
75  size_t maxncpy) ATTR_NONNULL();
76 
90 size_t BLI_strncpy_rlen(char *__restrict dst,
91  const char *__restrict src,
92  size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
93 
94 size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) ATTR_WARN_UNUSED_RESULT
95  ATTR_NONNULL();
96 
109 bool BLI_str_quoted_substr_range(const char *__restrict str,
110  const char *__restrict prefix,
111  int *__restrict r_start,
112  int *__restrict r_end) ATTR_WARN_UNUSED_RESULT
113  ATTR_NONNULL(1, 2, 3, 4);
114 #if 0 /* UNUSED */
115 char *BLI_str_quoted_substrN(const char *__restrict str,
116  const char *__restrict prefix) ATTR_WARN_UNUSED_RESULT
118 #endif
132 bool BLI_str_quoted_substr(const char *__restrict str,
133  const char *__restrict prefix,
134  char *result,
135  size_t result_maxlen);
149 char *BLI_str_replaceN(const char *__restrict str,
150  const char *__restrict substr_old,
151  const char *__restrict substr_new) ATTR_WARN_UNUSED_RESULT
153 
161 void BLI_str_replace_char(char *str, char src, char dst) ATTR_NONNULL();
162 
170 bool BLI_str_replace_table_exact(char *string,
171  size_t string_len,
172  const char *replace_table[][2],
173  int replace_table_len);
174 
178 size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
183 size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
184  ATTR_NONNULL(1, 3) ATTR_PRINTF_FORMAT(3, 4);
185 
189 size_t BLI_vsnprintf(char *__restrict buffer,
190  size_t maxncpy,
191  const char *__restrict format,
192  va_list arg) ATTR_PRINTF_FORMAT(3, 0);
196 size_t BLI_vsnprintf_rlen(char *__restrict buffer,
197  size_t maxncpy,
198  const char *__restrict format,
199  va_list arg) ATTR_PRINTF_FORMAT(3, 0);
200 
205 char *BLI_sprintfN(const char *__restrict format, ...) ATTR_WARN_UNUSED_RESULT
207 
220 size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
221  ATTR_NONNULL();
234 size_t BLI_str_unescape_ex(char *__restrict dst,
235  const char *__restrict src,
236  size_t src_maxncpy,
237  /* Additional arguments. */
238  size_t dst_maxncpy,
239  bool *r_is_complete) ATTR_NONNULL();
250 size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, size_t src_maxncpy)
251  ATTR_NONNULL();
252 
261 const char *BLI_str_escape_find_quote(const char *str) ATTR_NONNULL();
262 
271 size_t BLI_str_format_int_grouped(char dst[16], int num) ATTR_NONNULL();
280 size_t BLI_str_format_uint64_grouped(char dst[16], uint64_t num) ATTR_NONNULL();
291 void BLI_str_format_byte_unit(char dst[15], long long int bytes, bool base_10) ATTR_NONNULL();
310 void BLI_str_format_decimal_unit(char dst[7], int number_to_format) ATTR_NONNULL();
316 int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
320 char *BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
324 char *BLI_strncasestr(const char *s, const char *find, size_t len) ATTR_WARN_UNUSED_RESULT
325  ATTR_NONNULL();
326 int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
327 int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT
328  ATTR_NONNULL();
333 int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
338 int BLI_strcmp_ignore_pad(const char *str1, const char *str2, char pad) ATTR_WARN_UNUSED_RESULT
339  ATTR_NONNULL();
340 
344 size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
345 
350 void BLI_str_tolower_ascii(char *str, size_t len) ATTR_NONNULL();
351 void BLI_str_toupper_ascii(char *str, size_t len) ATTR_NONNULL();
352 
353 char BLI_tolower_ascii(const char c);
354 char BLI_toupper_ascii(const char c);
355 
359 void BLI_str_rstrip(char *str) ATTR_NONNULL();
369 int BLI_str_rstrip_float_zero(char *str, char pad) ATTR_NONNULL();
370 
379 int BLI_str_index_in_array_n(const char *__restrict str,
380  const char **__restrict str_array,
381  int str_array_len) ATTR_NONNULL();
389 int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array)
390  ATTR_NONNULL();
391 
399 bool BLI_str_startswith(const char *__restrict str, const char *__restrict start) ATTR_NONNULL();
407 bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL();
408 bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, size_t length)
409  ATTR_NONNULL();
410 
421 size_t BLI_str_partition(const char *str, const char delim[], const char **sep, const char **suf)
422  ATTR_NONNULL();
433 size_t BLI_str_rpartition(const char *str, const char delim[], const char **sep, const char **suf)
434  ATTR_NONNULL();
447 size_t BLI_str_partition_ex(const char *str,
448  const char *end,
449  const char delim[],
450  const char **sep,
451  const char **suf,
452  bool from_right) ATTR_NONNULL(1, 3, 4, 5);
453 
454 int BLI_string_max_possible_word_count(int str_len);
455 bool BLI_string_has_word_prefix(const char *haystack, const char *needle, size_t needle_len);
456 bool BLI_string_all_words_matched(const char *name,
457  const char *str,
458  int (*words)[2],
459  int words_len);
460 
471 int BLI_string_find_split_words(const char *str,
472  size_t len,
473  char delim,
474  int r_words[][2],
475  int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
476 
477 /* -------------------------------------------------------------------- */
483 #define STRNCPY(dst, src) BLI_strncpy(dst, src, ARRAY_SIZE(dst))
484 #define STRNCPY_RLEN(dst, src) BLI_strncpy_rlen(dst, src, ARRAY_SIZE(dst))
485 #define SNPRINTF(dst, format, ...) BLI_snprintf(dst, ARRAY_SIZE(dst), format, __VA_ARGS__)
486 #define SNPRINTF_RLEN(dst, format, ...) \
487  BLI_snprintf_rlen(dst, ARRAY_SIZE(dst), format, __VA_ARGS__)
488 #define STR_CONCAT(dst, len, suffix) \
489  len += BLI_strncpy_rlen(dst + len, suffix, ARRAY_SIZE(dst) - len)
490 #define STR_CONCATF(dst, len, format, ...) \
491  len += BLI_snprintf_rlen(dst + len, ARRAY_SIZE(dst) - len, format, __VA_ARGS__)
492 
495 /* -------------------------------------------------------------------- */
501 /* Manual line breaks for readability. */
502 /* clang-format off */
503 /* STR_ELEM#(v, ...): is the first arg equal any others? */
504 /* Internal helpers. */
505 #define _VA_STR_ELEM2(v, a) (strcmp(v, a) == 0)
506 #define _VA_STR_ELEM3(v, a, b) \
507  (_VA_STR_ELEM2(v, a) || (_VA_STR_ELEM2(v, b)))
508 #define _VA_STR_ELEM4(v, a, b, c) \
509  (_VA_STR_ELEM3(v, a, b) || (_VA_STR_ELEM2(v, c)))
510 #define _VA_STR_ELEM5(v, a, b, c, d) \
511  (_VA_STR_ELEM4(v, a, b, c) || (_VA_STR_ELEM2(v, d)))
512 #define _VA_STR_ELEM6(v, a, b, c, d, e) \
513  (_VA_STR_ELEM5(v, a, b, c, d) || (_VA_STR_ELEM2(v, e)))
514 #define _VA_STR_ELEM7(v, a, b, c, d, e, f) \
515  (_VA_STR_ELEM6(v, a, b, c, d, e) || (_VA_STR_ELEM2(v, f)))
516 #define _VA_STR_ELEM8(v, a, b, c, d, e, f, g) \
517  (_VA_STR_ELEM7(v, a, b, c, d, e, f) || (_VA_STR_ELEM2(v, g)))
518 #define _VA_STR_ELEM9(v, a, b, c, d, e, f, g, h) \
519  (_VA_STR_ELEM8(v, a, b, c, d, e, f, g) || (_VA_STR_ELEM2(v, h)))
520 #define _VA_STR_ELEM10(v, a, b, c, d, e, f, g, h, i) \
521  (_VA_STR_ELEM9(v, a, b, c, d, e, f, g, h) || (_VA_STR_ELEM2(v, i)))
522 #define _VA_STR_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
523  (_VA_STR_ELEM10(v, a, b, c, d, e, f, g, h, i) || (_VA_STR_ELEM2(v, j)))
524 #define _VA_STR_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
525  (_VA_STR_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || (_VA_STR_ELEM2(v, k)))
526 #define _VA_STR_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
527  (_VA_STR_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || (_VA_STR_ELEM2(v, l)))
528 #define _VA_STR_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
529  (_VA_STR_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || (_VA_STR_ELEM2(v, m)))
530 #define _VA_STR_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
531  (_VA_STR_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || (_VA_STR_ELEM2(v, n)))
532 #define _VA_STR_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
533  (_VA_STR_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || (_VA_STR_ELEM2(v, o)))
534 #define _VA_STR_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
535  (_VA_STR_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || (_VA_STR_ELEM2(v, p)))
536 /* clang-format on */
537 
538 /* reusable STR_ELEM macro */
539 #define STR_ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_STR_ELEM, __VA_ARGS__)
540 
543 #ifdef __cplusplus
544 }
545 #endif
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_MALLOC
#define ATTR_NONNULL(...)
int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array) ATTR_NONNULL()
Definition: string.c:847
void BLI_str_replace_char(char *str, char src, char dst) ATTR_NONNULL()
Definition: string.c:503
bool BLI_str_startswith(const char *__restrict str, const char *__restrict start) ATTR_NONNULL()
Definition: string.c:860
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 size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
int BLI_str_rstrip_float_zero(char *str, char pad) ATTR_NONNULL()
Definition: string.c:963
size_t ATTR_PRINTF_FORMAT(3, 4)
size_t int BLI_string_max_possible_word_count(int str_len)
Definition: string.c:559
size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, size_t src_maxncpy) ATTR_NONNULL()
Definition: string.c:327
bool BLI_string_all_words_matched(const char *name, const char *str, int(*words)[2], int words_len)
Definition: string.c:576
char * BLI_strncasestr(const char *s, const char *find, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:592
void BLI_str_rstrip(char *str) ATTR_NONNULL()
Definition: string.c:951
bool BLI_string_has_word_prefix(const char *haystack, const char *needle, size_t needle_len)
Definition: string.c:564
size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:120
int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:646
char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:538
bool BLI_str_quoted_substr_range(const char *__restrict str, const char *__restrict prefix, int *__restrict r_start, int *__restrict r_end) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:899
int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:623
bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, size_t length) ATTR_NONNULL()
Definition: string.c:871
size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:134
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:448
int BLI_strcmp_ignore_pad(const char *str1, const char *str2, char pad) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:786
bool BLI_str_replace_table_exact(char *string, size_t string_len, const char *replace_table[][2], int replace_table_len)
Definition: string.c:513
void BLI_str_tolower_ascii(char *str, size_t len) ATTR_NONNULL()
Definition: string.c:927
int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:719
int BLI_string_find_split_words(const char *str, size_t len, char delim, int r_words[][2], int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:1049
char BLI_toupper_ascii(const char c)
Definition: string.c:922
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, int str_array_len) ATTR_NONNULL()
Definition: string.c:832
bool bool BLI_str_quoted_substr(const char *__restrict str, const char *__restrict prefix, char *result, size_t result_maxlen)
Definition: string.c:424
int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:533
void BLI_str_format_decimal_unit(char dst[7], int number_to_format) ATTR_NONNULL()
Definition: string.c:1158
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:42
size_t BLI_str_unescape_ex(char *__restrict dst, const char *__restrict src, size_t src_maxncpy, size_t dst_maxncpy, bool *r_is_complete) ATTR_NONNULL()
Definition: string.c:301
size_t BLI_str_partition(const char *str, const char delim[], const char **sep, const char **suf) ATTR_NONNULL()
Definition: string.c:989
void BLI_str_toupper_ascii(char *str, size_t len) ATTR_NONNULL()
Definition: string.c:936
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:33
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:47
char BLI_tolower_ascii(const char c)
Definition: string.c:917
bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL()
Definition: string.c:887
size_t BLI_str_partition_ex(const char *str, const char *end, const char delim[], const char **sep, const char **suf, bool from_right) ATTR_NONNULL(1
size_t BLI_str_format_uint64_grouped(char dst[16], uint64_t num) ATTR_NONNULL()
Definition: string.c:1122
size_t BLI_str_format_int_grouped(char dst[16], int num) ATTR_NONNULL()
Definition: string.c:1114
size_t BLI_str_rpartition(const char *str, const char delim[], const char **sep, const char **suf) ATTR_NONNULL()
Definition: string.c:994
char * BLI_strncpy_ensure_pad(char *__restrict dst, const char *__restrict src, char pad, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:78
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
const char * BLI_str_escape_find_quote(const char *str) ATTR_NONNULL()
Definition: string.c:341
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void BLI_str_format_byte_unit(char dst[15], long long int bytes, bool base_10) ATTR_NONNULL()
Definition: string.c:1132
size_t size_t char size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL()
Definition: string.c:250
int pad[32 - sizeof(int)]
SyclQueue void void * src
int len
Definition: draw_manager.c:108
#define str(s)
ccl_global float * buffer
format
Definition: logImageCore.h:38
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
T length(const vec_base< T, Size > &a)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
unsigned __int64 uint64_t
Definition: stdint.h:90