Blender V5.3
BLI_string_utf8.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <stdarg.h>
12
13#include "BLI_compiler_attrs.hh"
14#include "BLI_sys_types.hh"
15
16namespace blender {
17
18char *BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
19 ATTR_NONNULL(1, 2);
20size_t BLI_strncpy_utf8_rlen(char *__restrict dst,
21 const char *__restrict src,
22 size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 2);
27size_t BLI_strncpy_utf8_rlen_unterminated(char *__restrict dst,
28 const char *__restrict src,
29 size_t dst_maxncpy);
30
36ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1);
44int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1);
53int BLI_str_utf8_invalid_substitute(char *str, size_t str_len, const char substitute)
54 ATTR_NONNULL(1);
55
64[[nodiscard]] const char *BLI_str_utf8_invalid_substitute_if_needed(
65 const char *str, size_t str_len, const char substitute, char *buf, const size_t buf_maxncpy)
66 ATTR_NONNULL(1, 4);
67
89 ATTR_NONNULL(1);
100unsigned int BLI_str_utf8_as_unicode_step_safe(const char *__restrict p,
101 size_t p_len,
102 size_t *__restrict index) ATTR_WARN_UNUSED_RESULT
103 ATTR_NONNULL(1, 3);
118 const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT
119 ATTR_NONNULL(1, 3);
120
134size_t BLI_str_utf8_from_unicode(unsigned int c, char *dst, size_t dst_maxncpy) ATTR_NONNULL(2);
135size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w,
136 const char *__restrict src_c,
137 size_t dst_w_maxncpy) ATTR_NONNULL(1, 2);
138size_t BLI_str_utf32_as_utf8(char *__restrict dst,
139 const char32_t *__restrict src,
140 size_t dst_maxncpy) ATTR_NONNULL(1, 2);
144size_t BLI_str_utf32_as_utf8_len_ex(const char32_t *src, size_t src_maxlen) ATTR_WARN_UNUSED_RESULT
145 ATTR_NONNULL(1);
150
166const char *BLI_str_find_prev_char_utf8(const char *p, const char *str_start)
180const char *BLI_str_find_next_char_utf8(const char *p, const char *str_end)
182
186size_t BLI_wstrlen_utf8(const wchar_t *src) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
187size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes)
189size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
190size_t BLI_strnlen_utf8_ex(const char *strc, size_t strc_maxlen, size_t *r_len_bytes)
191 ATTR_NONNULL(1, 3);
197size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen)
199size_t BLI_strncpy_wchar_as_utf8(char *__restrict dst,
200 const wchar_t *__restrict src,
201 size_t dst_maxncpy) ATTR_NONNULL(1, 2);
202size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w,
203 const char *__restrict src_c,
204 size_t dst_w_maxncpy) ATTR_NONNULL(1, 2);
205
209size_t BLI_snprintf_utf8(char *__restrict dst,
210 size_t dst_maxncpy,
211 const char *__restrict format,
212 ...) ATTR_NONNULL(1, 3) ATTR_PRINTF_FORMAT(3, 4);
218size_t BLI_snprintf_utf8_rlen(char *__restrict dst,
219 size_t dst_maxncpy,
220 const char *__restrict format,
221 ...) ATTR_NONNULL(1, 3) ATTR_PRINTF_FORMAT(3, 4);
222
226size_t BLI_vsnprintf_utf8(char *__restrict dst,
227 size_t dst_maxncpy,
228 const char *__restrict format,
229 va_list arg) ATTR_PRINTF_FORMAT(3, 0);
234size_t BLI_vsnprintf_utf8_rlen(char *__restrict dst,
235 size_t dst_maxncpy,
236 const char *__restrict format,
237 va_list arg) ATTR_PRINTF_FORMAT(3, 0);
238
244int BLI_wcswidth_or_error(const char32_t *pwcs, size_t n) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
245
251char32_t BLI_str_utf32_char_to_upper(char32_t wc);
257char32_t BLI_str_utf32_char_to_lower(char32_t wc);
258
259bool BLI_str_utf32_char_is_breaking_space(char32_t codepoint);
260bool BLI_str_utf32_char_is_optional_break_after(char32_t codepoint, char32_t codepoint_prev);
261bool BLI_str_utf32_char_is_optional_break_before(char32_t codepoint, char32_t codepoint_prev);
262bool BLI_str_utf32_char_is_terminal_punctuation(char32_t codepoint);
263
269
270size_t BLI_str_partition_utf8(const char *str,
271 const unsigned int delim[],
272 const char **r_sep,
273 const char **r_suf) ATTR_NONNULL(1, 2, 3, 4);
274size_t BLI_str_rpartition_utf8(const char *str,
275 const unsigned int delim[],
276 const char **r_sep,
277 const char **r_suf) ATTR_NONNULL(1, 2, 3, 4);
278size_t BLI_str_partition_ex_utf8(const char *str,
279 const char *end,
280 const unsigned int delim[],
281 const char **r_sep,
282 const char **r_suf,
283 bool from_right) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 3, 4, 5);
284
291bool BLI_str_utf8_truncate_at_size(char *str, const size_t str_size);
292
293int BLI_str_utf8_offset_to_index(const char *str,
294 size_t str_len,
295 int offset_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
302int BLI_str_utf8_offset_from_index(const char *str,
303 size_t str_len,
304 int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
305int BLI_str_utf8_offset_to_column(const char *str,
306 size_t str_len,
307 int offset_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
309 size_t str_len,
310 int column_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
312 size_t str_len,
313 int offset_target,
314 int tab_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
316 size_t str_len,
317 int column_target,
318 int tab_width) ATTR_WARN_UNUSED_RESULT
319 ATTR_NONNULL(1);
320
321int BLI_str_utf8_column_count(const char *str, size_t str_len) ATTR_WARN_UNUSED_RESULT
322 ATTR_NONNULL(1);
323
325#define BLI_UTF8_MAX 6
326#define BLI_UTF8_WIDTH_MAX 2 /* columns */
327#define BLI_UTF8_ERR ((unsigned int)-1)
328
329/* -------------------------------------------------------------------- */
334
335#define STRNCPY_UTF8(dst, src) BLI_strncpy_utf8(dst, src, ARRAY_SIZE(dst))
336#define STRNCPY_UTF8_RLEN(dst, src) BLI_strncpy_utf8_rlen(dst, src, ARRAY_SIZE(dst))
337
338#define STRNLEN_UTF8(str) BLI_strnlen_utf8(str, ARRAY_SIZE(str))
339
340#define SNPRINTF_UTF8(dst, format, ...) \
341 BLI_snprintf_utf8(dst, ARRAY_SIZE(dst), format, __VA_ARGS__)
342#define SNPRINTF_UTF8_RLEN(dst, format, ...) \
343 BLI_snprintf_utf8_rlen(dst, ARRAY_SIZE(dst), format, __VA_ARGS__)
344#define VSNPRINTF_UTF8(dst, format, args) BLI_vsnprintf_utf8(dst, ARRAY_SIZE(dst), format, args)
345#define VSNPRINTF_UTF8_RLEN(dst, format, args) \
346 BLI_vsnprintf_utf8_rlen(dst, ARRAY_SIZE(dst), format, args)
347
349
350} // namespace blender
#define str(s)
BMesh Iterators.
Object ATTR_RETURNS_NONNULL
const char * BLI_str_utf8_invalid_substitute_if_needed(const char *str, size_t str_len, const char substitute, char *buf, const size_t buf_maxncpy) ATTR_NONNULL(1
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
size_t size_t size_t BLI_str_utf32_as_utf8_len_ex(const char32_t *src, size_t src_maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool BLI_str_utf32_char_is_breaking_space(char32_t codepoint)
size_t BLI_str_utf8_from_unicode(unsigned int c, char *dst, size_t dst_maxncpy) ATTR_NONNULL(2)
size_t size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w, const char *__restrict src_c, size_t dst_w_maxncpy) ATTR_NONNULL(1
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1)
int BLI_wcswidth_or_error(const char32_t *pwcs, size_t n) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int BLI_str_utf8_as_unicode_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool BLI_str_utf32_char_is_terminal_punctuation(char32_t codepoint)
int BLI_str_utf8_char_width_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
char size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w, const char *__restrict src_c, size_t dst_w_maxncpy) ATTR_NONNULL(1
size_t size_t size_t bool BLI_str_utf8_truncate_at_size(char *str, const size_t str_size)
unsigned int BLI_str_utf8_as_unicode_step_safe(const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t size_t BLI_vsnprintf_utf8_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format, va_list arg) ATTR_PRINTF_FORMAT(3
size_t size_t BLI_str_utf32_as_utf8(char *__restrict dst, const char32_t *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
int BLI_str_utf8_offset_to_column(const char *str, size_t str_len, int offset_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t size_t BLI_str_rpartition_utf8(const char *str, const unsigned int delim[], const char **r_sep, const char **r_suf) ATTR_NONNULL(1
size_t BLI_str_partition_utf8(const char *str, const unsigned int delim[], const char **r_sep, const char **r_suf) ATTR_NONNULL(1
unsigned int unsigned int BLI_str_utf8_as_unicode_step_or_error(const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
int BLI_str_utf8_invalid_substitute(char *str, size_t str_len, const char substitute) ATTR_NONNULL(1)
bool BLI_str_utf32_char_is_optional_break_after(char32_t codepoint, char32_t codepoint_prev)
bool BLI_str_utf32_char_is_optional_break_before(char32_t codepoint, char32_t codepoint_prev)
size_t BLI_strncpy_wchar_as_utf8(char *__restrict dst, const wchar_t *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
int BLI_str_utf8_char_width_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_str_utf8_column_count(const char *str, size_t str_len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
const char const char size_t BLI_wstrlen_utf8(const wchar_t *src) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
const char const char * BLI_str_find_next_char_utf8(const char *p, const char *str_end) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
int BLI_str_utf8_offset_to_column_with_tabs(const char *str, size_t str_len, int offset_target, int tab_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
const char int BLI_str_utf8_size_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t BLI_vsnprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format, va_list arg) ATTR_PRINTF_FORMAT(3
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t ATTR_PRINTF_FORMAT(3, 4)
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
int BLI_str_utf8_offset_from_column(const char *str, size_t str_len, int column_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char32_t BLI_str_utf32_char_to_lower(char32_t wc)
size_t size_t size_t BLI_str_partition_ex_utf8(const char *str, const char *end, const unsigned int delim[], const char **r_sep, const char **r_suf, bool from_right) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
char size_t size_t BLI_strncpy_utf8_rlen_unterminated(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
int BLI_wcwidth_safe(char32_t ucs) ATTR_WARN_UNUSED_RESULT
unsigned int BLI_str_utf8_as_unicode_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int unsigned int size_t BLI_str_utf8_from_unicode_len(unsigned int c) ATTR_WARN_UNUSED_RESULT
int BLI_str_utf8_offset_to_index(const char *str, size_t str_len, int offset_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes) ATTR_NONNULL(1
bool ATTR_WARN_UNUSED_RESULT
void ATTR_NONNULL(1, 2)
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
size_t BLI_str_utf32_as_utf8_len(const char32_t *src) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t BLI_snprintf_utf8_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
char32_t BLI_str_utf32_char_to_upper(char32_t wc)
int BLI_str_utf8_offset_from_column_with_tabs(const char *str, size_t str_len, int column_target, int tab_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t BLI_strnlen_utf8_ex(const char *strc, size_t strc_maxlen, size_t *r_len_bytes) ATTR_NONNULL(1
size_t size_t int BLI_wcwidth_or_error(char32_t ucs) ATTR_WARN_UNUSED_RESULT