Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/string_utf8.c
| Show All 27 Lines | |||||
| #include <string.h> | #include <string.h> | ||||
| #include <wchar.h> | #include <wchar.h> | ||||
| #include <wctype.h> | #include <wctype.h> | ||||
| #include <wcwidth.h> | #include <wcwidth.h> | ||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "BLI_string_utf8.h" /* own include */ | #include "BLI_string_utf8.h" /* own include */ | ||||
| #ifdef WIN32 | |||||
| # include "utfconv.h" | |||||
| #endif | |||||
| #ifdef __GNUC__ | #ifdef __GNUC__ | ||||
| # pragma GCC diagnostic error "-Wsign-conversion" | # pragma GCC diagnostic error "-Wsign-conversion" | ||||
| #endif | #endif | ||||
| // #define DEBUG_STRSIZE | // #define DEBUG_STRSIZE | ||||
| /* array copied from glib's gutf8.c, */ | /* array copied from glib's gutf8.c, */ | ||||
| /* Note: last two values (0xfe and 0xff) are forbidden in utf-8, | /* Note: last two values (0xfe and 0xff) are forbidden in utf-8, | ||||
| ▲ Show 20 Lines • Show All 343 Lines • ▼ Show 20 Lines | size_t BLI_strnlen_utf8(const char *strc, const size_t maxlen) | ||||
| size_t len_bytes; | size_t len_bytes; | ||||
| return BLI_strnlen_utf8_ex(strc, maxlen, &len_bytes); | return BLI_strnlen_utf8_ex(strc, maxlen, &len_bytes); | ||||
| } | } | ||||
| size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w, | size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w, | ||||
| const char *__restrict src_c, | const char *__restrict src_c, | ||||
| const size_t maxncpy) | const size_t maxncpy) | ||||
| { | { | ||||
| const size_t maxlen = maxncpy - 1; | #ifdef WIN32 | ||||
| size_t len = 0; | return conv_utf_8_to_16(src_c, dst_w, maxncpy); | ||||
| #else | |||||
| BLI_assert(maxncpy != 0); | return BLI_str_utf8_as_utf32((char32_t *)dst_w, src_c, maxncpy); | ||||
rjg: This should be `ifdef WIN32` otherwise this generates a warning on Linux as `WIN32` is not… | |||||
Done Inline ActionsGCC warns that the signedness of wchar_t (signed) is not the same as char32_t (unsigned). rjg: GCC warns that the signedness of `wchar_t` (signed) is not the same as `char32_t` (unsigned). | |||||
| #ifdef DEBUG_STRSIZE | |||||
| memset(dst_w, 0xff, sizeof(*dst_w) * maxncpy); | |||||
| #endif | #endif | ||||
| while (*src_c && len != maxlen) { | |||||
| size_t step = 0; | |||||
| uint unicode = BLI_str_utf8_as_unicode_and_size(src_c, &step); | |||||
| if (unicode != BLI_UTF8_ERR) { | |||||
| /* TODO: `wchar_t` type is an implementation-defined and may represent | |||||
| * 16-bit or 32-bit depending on operating system. | |||||
| * So the ideal would be to do the corresponding encoding. | |||||
| * But for now just assert that it has no conflicting use. */ | |||||
| BLI_assert(step <= sizeof(wchar_t)); | |||||
| *dst_w = (wchar_t)unicode; | |||||
| src_c += step; | |||||
| } | |||||
| else { | |||||
| *dst_w = '?'; | |||||
| src_c = BLI_str_find_next_char_utf8(src_c, NULL); | |||||
| } | |||||
| dst_w++; | |||||
| len++; | |||||
| } | |||||
| *dst_w = 0; | |||||
| return len; | |||||
| } | } | ||||
| /* end wchar_t / utf8 functions */ | /* end wchar_t / utf8 functions */ | ||||
| /* --------------------------------------------------------------------------*/ | /* --------------------------------------------------------------------------*/ | ||||
| /* count columns that character/string occupies, based on wcwidth.c */ | /* count columns that character/string occupies, based on wcwidth.c */ | ||||
| int BLI_wcwidth(char32_t ucs) | int BLI_wcwidth(char32_t ucs) | ||||
| ▲ Show 20 Lines • Show All 548 Lines • Show Last 20 Lines | |||||
This should be ifdef WIN32 otherwise this generates a warning on Linux as WIN32 is not defined.