Blender  V3.3
Macros | Functions | Variables
BLI_string_utf8.h File Reference
#include "BLI_compiler_attrs.h"
#include "BLI_sys_types.h"

Go to the source code of this file.

Macros

#define BLI_UTF8_MAX   6
 
#define BLI_UTF8_WIDTH_MAX   2 /* columns */
 
#define BLI_UTF8_ERR   ((unsigned int)-1)
 
String Copy/Format Macros

Avoid repeating destination with sizeof(..).

Note
ARRAY_SIZE allows pointers on some platforms.
#define STRNCPY_UTF8(dst, src)   BLI_strncpy_utf8(dst, src, ARRAY_SIZE(dst))
 
#define STRNCPY_UTF8_RLEN(dst, src)   BLI_strncpy_utf8_rlen(dst, src, ARRAY_SIZE(dst))
 

Functions

char * BLI_strncpy_utf8 (char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL(1
 
char size_t BLI_strncpy_utf8_rlen (char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
 
char size_t ptrdiff_t BLI_str_utf8_invalid_byte (const char *str, size_t length) ATTR_NONNULL(1)
 
int BLI_str_utf8_invalid_strip (char *str, size_t length) ATTR_NONNULL(1)
 
int BLI_str_utf8_size (const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
int BLI_str_utf8_size_safe (const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
unsigned int BLI_str_utf8_as_unicode (const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
unsigned int BLI_str_utf8_as_unicode_step (const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT 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
 
unsigned int unsigned int size_t BLI_str_utf8_from_unicode_len (unsigned int c) ATTR_WARN_UNUSED_RESULT
 
size_t BLI_str_utf8_from_unicode (unsigned int c, char *outbuf, size_t outbuf_len) ATTR_NONNULL(2)
 
size_t BLI_str_utf8_as_utf32 (char32_t *__restrict dst_w, const char *__restrict src_c, size_t maxncpy) ATTR_NONNULL(1
 
size_t size_t BLI_str_utf32_as_utf8 (char *__restrict dst, const char32_t *__restrict src, size_t maxncpy) ATTR_NONNULL(1
 
size_t size_t size_t BLI_str_utf32_as_utf8_len (const char32_t *src) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
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
 
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
 
const char const char size_t BLI_wstrlen_utf8 (const wchar_t *src) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
 
size_t BLI_strlen_utf8_ex (const char *strc, size_t *r_len_bytes) ATTR_NONNULL(1
 
size_t BLI_strlen_utf8 (const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
 
size_t BLI_strnlen_utf8_ex (const char *strc, size_t maxlen, size_t *r_len_bytes) ATTR_NONNULL(1
 
size_t size_t BLI_strnlen_utf8 (const char *strc, size_t maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
 
size_t BLI_strncpy_wchar_as_utf8 (char *__restrict dst, const wchar_t *__restrict src, size_t maxncpy) ATTR_NONNULL(1
 
size_t size_t BLI_strncpy_wchar_from_utf8 (wchar_t *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL(1
 
size_t size_t int BLI_wcwidth (char32_t ucs) ATTR_WARN_UNUSED_RESULT
 
int BLI_wcswidth (const char32_t *pwcs, size_t n) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
int BLI_str_utf8_char_width (const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
int BLI_str_utf8_char_width_safe (const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
size_t BLI_str_partition_utf8 (const char *str, const unsigned int delim[], const char **sep, const char **suf) ATTR_NONNULL(1
 
size_t size_t BLI_str_rpartition_utf8 (const char *str, const unsigned int delim[], const char **sep, const char **suf) ATTR_NONNULL(1
 
size_t size_t size_t BLI_str_partition_ex_utf8 (const char *str, const char *end, const unsigned int delim[], const char **sep, const char **suf, bool from_right) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
 
size_t size_t size_t int BLI_str_utf8_offset_to_index (const char *str, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
int BLI_str_utf8_offset_from_index (const char *str, int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
int BLI_str_utf8_offset_to_column (const char *str, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 
int BLI_str_utf8_offset_from_column (const char *str, int column) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
 

Variables

size_t ATTR_WARN_UNUSED_RESULT
 

Macro Definition Documentation

◆ BLI_UTF8_ERR

#define BLI_UTF8_ERR   ((unsigned int)-1)

Definition at line 202 of file BLI_string_utf8.h.

◆ BLI_UTF8_MAX

#define BLI_UTF8_MAX   6

Size in bytes.

Definition at line 200 of file BLI_string_utf8.h.

◆ BLI_UTF8_WIDTH_MAX

#define BLI_UTF8_WIDTH_MAX   2 /* columns */

Definition at line 201 of file BLI_string_utf8.h.

◆ STRNCPY_UTF8

#define STRNCPY_UTF8 (   dst,
  src 
)    BLI_strncpy_utf8(dst, src, ARRAY_SIZE(dst))

Definition at line 210 of file BLI_string_utf8.h.

◆ STRNCPY_UTF8_RLEN

#define STRNCPY_UTF8_RLEN (   dst,
  src 
)    BLI_strncpy_utf8_rlen(dst, src, ARRAY_SIZE(dst))

Definition at line 211 of file BLI_string_utf8.h.

Function Documentation

◆ BLI_str_find_next_char_utf8()

const char const char* BLI_str_find_next_char_utf8 ( const char *  p,
const char *  str_end 
)
Parameters
pa pointer to a position within a UTF-8 encoded string
str_enda pointer to the byte following the end of the string.

Finds the start of the next UTF-8 character in the string after p

p does not have to be at the beginning of a UTF-8 character. No check is made to see if the character found is actually valid other than it starts with an appropriate byte.

Returns
a pointer to the found character or a pointer to the null terminating character '\0'.

Referenced by BLI_str_cursor_step_next_utf8(), ui_text_clip_give_next_off(), and ui_text_position_from_hidden().

◆ BLI_str_find_prev_char_utf8()

const char* BLI_str_find_prev_char_utf8 ( const char *  p,
const char *  str_start 
)

BLI_str_find_prev_char_utf8:

Parameters
ppointer to some position within str
str_startpointer to the beginning of a UTF-8 encoded string

Given a position p with a UTF-8 encoded string str, find the start of the previous UTF-8 character starting before. p Returns str_start if no UTF-8 characters are present in str_start before p.

p does not have to be at the beginning of a UTF-8 character. No check is made to see if the character found is actually valid other than it starts with an appropriate byte.

Returns
A pointer to the found character.

Referenced by blf_font_width_to_rstrlen(), BLI_str_cursor_step_prev_utf8(), text_delete_exec(), txt_backspace_char(), txt_move_left(), txt_wrap_move_eol(), ui_text_clip_cursor(), ui_text_clip_give_prev_off(), ui_text_clip_right_label(), and unit_find_str().

◆ BLI_str_partition_ex_utf8()

size_t size_t size_t BLI_str_partition_ex_utf8 ( const char *  str,
const char *  end,
const unsigned int  delim[],
const char **  sep,
const char **  suf,
bool  from_right 
)

◆ BLI_str_partition_utf8()

size_t BLI_str_partition_utf8 ( const char *  str,
const unsigned int  delim[],
const char **  sep,
const char **  suf 
)

Referenced by TEST().

◆ BLI_str_rpartition_utf8()

size_t size_t BLI_str_rpartition_utf8 ( const char *  str,
const unsigned int  delim[],
const char **  sep,
const char **  suf 
)

Referenced by TEST().

◆ BLI_str_utf32_as_utf8()

size_t size_t BLI_str_utf32_as_utf8 ( char *__restrict  dst,
const char32_t *__restrict  src,
size_t  maxncpy 
)

◆ BLI_str_utf32_as_utf8_len()

size_t size_t size_t BLI_str_utf32_as_utf8_len ( const char32_t *  src)
Returns
The UTF-32 len in UTF-8.

Definition at line 654 of file string_utf8.c.

References BLI_str_utf8_from_unicode_len(), len, and src.

Referenced by BKE_vfont_clipboard_set(), and ED_curve_editfont_load().

◆ BLI_str_utf8_as_unicode()

unsigned int BLI_str_utf8_as_unicode ( const char *  p)
Parameters
pa pointer to Unicode character encoded as UTF-8

Converts a sequence of bytes encoded as UTF-8 to a Unicode character. If p does not point to a valid UTF-8 encoded character, results are undefined. If you are not sure that the bytes are complete valid Unicode characters, you should use g_utf8_get_char_validated() instead.

Return value: the resulting character

Definition at line 478 of file string_utf8.c.

References BLI_UTF8_ERR, Freestyle::c, len, mask(), result, UNLIKELY, UTF8_COMPUTE, and UTF8_GET.

Referenced by BLI_str_partition_ex_utf8(), BLI_str_utf8_char_width(), BLI_str_utf8_char_width_safe(), blender::string_search::extract_normalized_words(), blender::string_search::get_fuzzy_match_errors(), insert_text_invoke(), key_event_glyph_or_text(), text_autocomplete_build(), and text_insert_invoke().

◆ BLI_str_utf8_as_unicode_step()

unsigned int BLI_str_utf8_as_unicode_step ( const char *__restrict  p,
size_t  p_len,
size_t *__restrict  index 
)

UTF8 decoding that steps over the index (unless an error is encountered).

Parameters
pThe text to step over.
p_lenThe length of p.
indexIndex of p to step over.
Returns
the code-point (p + *index) if there is a decoding error.
Note
Falls back to LATIN1 for text drawing.

Referenced by blf_font_count_missing_chars(), blf_glyph_from_utf8_and_step(), blender::string_search::damerau_levenshtein_distance(), blender::string_search::extract_normalized_words(), blender::string_search::match_word_initials(), text_autocomplete_build(), text_insert_exec(), txt_backspace_char(), txt_delete_char(), txt_insert_buf(), txt_replace_char(), and utf8_as_char32().

◆ BLI_str_utf8_as_unicode_step_or_error()

unsigned int unsigned int BLI_str_utf8_as_unicode_step_or_error ( const char *__restrict  p,
size_t  p_len,
size_t *__restrict  index 
)

UTF8 decoding that steps over the index (unless an error is encountered).

Parameters
pThe text to step over.
p_lenThe length of p.
indexIndex of p to step over.
Returns
the code-point or BLI_UTF8_ERR if there is a decoding error.
Note
The behavior for clipped text (where p_len limits decoding trailing bytes) must have the same behavior is encountering a nil byte, so functions that only use the first part of a string has matching behavior to functions that null terminate the text.

Referenced by cursor_delim_type_utf8().

◆ BLI_str_utf8_as_utf32()

size_t BLI_str_utf8_as_utf32 ( char32_t *__restrict  dst_w,
const char *__restrict  src_c,
size_t  maxncpy 
)

◆ BLI_str_utf8_char_width()

int BLI_str_utf8_char_width ( const char *  p)
Warning
can return -1 on bad chars.

Definition at line 378 of file string_utf8.c.

References BLI_str_utf8_as_unicode(), BLI_UTF8_ERR, and BLI_wcwidth().

◆ BLI_str_utf8_char_width_safe()

int BLI_str_utf8_char_width_safe ( const char *  p)

◆ BLI_str_utf8_from_unicode()

size_t BLI_str_utf8_from_unicode ( unsigned int  c,
char *  outbuf,
size_t  outbuf_len 
)

BLI_str_utf8_from_unicode:

Parameters
ca Unicode character code
outbufoutput buffer, must have at least outbuf_len bytes of space. If the length required by c exceeds outbuf_len, the bytes available bytes will be zeroed and outbuf_len returned.

Converts a single character to UTF-8.

Returns
number of bytes written.

Definition at line 575 of file string_utf8.c.

References Freestyle::c, len, UNLIKELY, and UTF8_VARS_FROM_CHAR32.

Referenced by BLI_str_utf32_as_utf8(), BLI_strncpy_wchar_as_utf8(), find_family_object(), txt_add_char_intern(), txt_extended_ascii_as_utf8(), txt_replace_char(), and wm_event_add_ghostevent().

◆ BLI_str_utf8_from_unicode_len()

unsigned int unsigned int size_t BLI_str_utf8_from_unicode_len ( unsigned int  c)

Definition at line 563 of file string_utf8.c.

References Freestyle::c, len, UTF8_VARS_FROM_CHAR32, and void.

Referenced by BLI_str_utf32_as_utf8_len(), and BLI_wstrlen_utf8().

◆ BLI_str_utf8_invalid_byte()

char size_t ptrdiff_t BLI_str_utf8_invalid_byte ( const char *  str,
size_t  length 
)

Find first UTF-8 invalid byte in given str, of length bytes.

Returns
the offset of the first invalid byte.

Definition at line 46 of file string_utf8.c.

References Freestyle::c, ELEM, blender::math::length(), str, and utf8_skip_data.

Referenced by BLI_str_utf8_invalid_strip(), and txt_extended_ascii_as_utf8().

◆ BLI_str_utf8_invalid_strip()

int BLI_str_utf8_invalid_strip ( char *  str,
size_t  length 
)

Remove any invalid UTF-8 byte (taking into account multi-bytes sequence of course).

Returns
number of stripped bytes.

Definition at line 181 of file string_utf8.c.

References BLI_assert, BLI_str_utf8_invalid_byte(), blender::math::length(), and str.

Referenced by BKE_id_new_name_validate(), id_name_final_build(), objfnt_to_ftvfontdata(), outputNumInput(), SEQ_edit_sequence_name_set(), TEST(), ui_textedit_copypaste(), and ui_textedit_end().

◆ BLI_str_utf8_offset_from_column()

int BLI_str_utf8_offset_from_column ( const char *  str,
int  column 
)

◆ BLI_str_utf8_offset_from_index()

int BLI_str_utf8_offset_from_index ( const char *  str,
int  index 
)

◆ BLI_str_utf8_offset_to_column()

int BLI_str_utf8_offset_to_column ( const char *  str,
int  offset 
)

◆ BLI_str_utf8_offset_to_index()

size_t size_t size_t int BLI_str_utf8_offset_to_index ( const char *  str,
int  offset 
)

Definition at line 761 of file string_utf8.c.

References BLI_str_utf8_size(), offset, pos, and str.

◆ BLI_str_utf8_size()

int BLI_str_utf8_size ( const char *  p)

◆ BLI_str_utf8_size_safe()

int BLI_str_utf8_size_safe ( const char *  p)

◆ BLI_strlen_utf8()

size_t BLI_strlen_utf8 ( const char *  strc)

◆ BLI_strlen_utf8_ex()

size_t BLI_strlen_utf8_ex ( const char *  strc,
size_t *  r_len_bytes 
)

◆ BLI_strncpy_utf8()

char* BLI_strncpy_utf8 ( char *__restrict  dst,
const char *__restrict  src,
size_t  maxncpy 
)

◆ BLI_strncpy_utf8_rlen()

char size_t BLI_strncpy_utf8_rlen ( char *__restrict  dst,
const char *__restrict  src,
size_t  maxncpy 
)

◆ BLI_strncpy_wchar_as_utf8()

size_t BLI_strncpy_wchar_as_utf8 ( char *__restrict  dst,
const wchar_t *__restrict  src,
size_t  maxncpy 
)

◆ BLI_strncpy_wchar_from_utf8()

size_t size_t BLI_strncpy_wchar_from_utf8 ( wchar_t *__restrict  dst,
const char *__restrict  src,
size_t  maxncpy 
)

Referenced by fsmenu_read_system().

◆ BLI_strnlen_utf8()

size_t size_t BLI_strnlen_utf8 ( const char *  strc,
size_t  maxlen 
)
Parameters
strcthe string to measure the length.
maxlenthe string length (in bytes)
Returns
the unicode length (not in bytes!)

Definition at line 342 of file string_utf8.c.

References BLI_strnlen_utf8_ex().

Referenced by blender::string_search::count_utf8_code_points(), and ui_text_position_to_hidden().

◆ BLI_strnlen_utf8_ex()

size_t BLI_strnlen_utf8_ex ( const char *  strc,
size_t  maxlen,
size_t *  r_len_bytes 
)

Referenced by ui_textedit_insert_buf().

◆ BLI_wcswidth()

int BLI_wcswidth ( const char32_t *  pwcs,
size_t  n 
)

Definition at line 373 of file string_utf8.c.

◆ BLI_wcwidth()

size_t size_t int BLI_wcwidth ( char32_t  ucs)

Count columns that character/string occupies (based on wcwidth.co).

Definition at line 364 of file string_utf8.c.

Referenced by blf_font_draw_mono(), blf_glyph_render(), BLI_str_utf8_char_width(), and BLI_str_utf8_char_width_safe().

◆ BLI_wstrlen_utf8()

const char const char size_t BLI_wstrlen_utf8 ( const wchar_t *  src)
Returns
the wchar_t length in UTF-8.

Definition at line 293 of file string_utf8.c.

References BLI_str_utf8_from_unicode_len(), len, and src.

Variable Documentation

◆ ATTR_WARN_UNUSED_RESULT

size_t ATTR_WARN_UNUSED_RESULT

Definition at line 147 of file BLI_string_utf8.h.