From 731fc5d980d291b1cbef6b6a85a5904411cc6826 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 20 Sep 2023 12:11:30 +1000 Subject: [PATCH] Cleanup: use strict flags for string utilities & cursor Make type conversions explicit so it's clear when char/char32_t/uint values are being mixed, also use int instead of size_t for cursor functions because the cursor is an int - which caused many int/size_t comparisons. --- source/blender/blenfont/intern/blf_font.cc | 2 +- .../blender/blenlib/BLI_string_cursor_utf8.h | 16 ++-- source/blender/blenlib/intern/string.c | 54 ++++++------ .../blenlib/intern/string_cursor_utf8.c | 82 ++++++++++--------- source/blender/blenlib/intern/string_utf8.c | 8 +- source/blender/blenlib/intern/string_utils.c | 24 +++--- 6 files changed, 95 insertions(+), 91 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index b49cc439a5e..c5f31ed975e 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -985,7 +985,7 @@ size_t blf_str_offset_from_cursor_position(FontBLF *font, else if (BLI_str_utf8_char_width_or_error(&str[data.r_offset]) == 0) { /* This is a combining character, so move to previous visible valid char. */ int offset = int(data.r_offset); - BLI_str_cursor_step_prev_utf8(str, str_len, &offset); + BLI_str_cursor_step_prev_utf8(str, int(str_len), &offset); data.r_offset = size_t(offset); } diff --git a/source/blender/blenlib/BLI_string_cursor_utf8.h b/source/blender/blenlib/BLI_string_cursor_utf8.h index f2224468e19..6ecca1f2b35 100644 --- a/source/blender/blenlib/BLI_string_cursor_utf8.h +++ b/source/blender/blenlib/BLI_string_cursor_utf8.h @@ -23,21 +23,21 @@ typedef enum eStrCursorJumpDirection { STRCUR_DIR_NEXT, } eStrCursorJumpDirection; -bool BLI_str_cursor_step_next_utf8(const char *str, size_t str_maxlen, int *pos); -bool BLI_str_cursor_step_prev_utf8(const char *str, size_t str_maxlen, int *pos); +bool BLI_str_cursor_step_next_utf8(const char *str, int str_maxlen, int *pos); +bool BLI_str_cursor_step_prev_utf8(const char *str, int str_maxlen, int *pos); -bool BLI_str_cursor_step_next_utf32(const char32_t *str, size_t str_maxlen, int *pos); -bool BLI_str_cursor_step_prev_utf32(const char32_t *str, size_t str_maxlen, int *pos); +bool BLI_str_cursor_step_next_utf32(const char32_t *str, int str_maxlen, int *pos); +bool BLI_str_cursor_step_prev_utf32(const char32_t *str, int str_maxlen, int *pos); void BLI_str_cursor_step_utf8(const char *str, - size_t str_maxlen, + int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step); void BLI_str_cursor_step_utf32(const char32_t *str, - size_t str_maxlen, + int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, @@ -57,11 +57,11 @@ void BLI_str_cursor_step_utf32(const char32_t *str, * \param r_end: returned end of word/sequence boundary (0-based) */ void BLI_str_cursor_step_bounds_utf8( - const char *str, const size_t str_maxlen, int pos, int *r_start, int *r_end); + const char *str, int str_maxlen, int pos, int *r_start, int *r_end); /** A UTF32 version of #BLI_str_cursor_step_bounds_utf8 */ void BLI_str_cursor_step_bounds_utf32( - const char32_t *str, const size_t str_maxlen, int pos, int *r_start, int *r_end); + const char32_t *str, int str_maxlen, int pos, int *r_start, int *r_end); #ifdef __cplusplus } diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index cbd1800a61d..25525b5336e 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -20,9 +20,7 @@ #include "BLI_utildefines.h" -#ifdef __GNUC__ -# pragma GCC diagnostic error "-Wsign-conversion" -#endif +#include "BLI_strict_flags.h" /* -------------------------------------------------------------------- */ /** \name String Duplicate/Copy @@ -165,7 +163,7 @@ size_t BLI_vsnprintf(char *__restrict dst, n = (size_t)vsnprintf(dst, dst_maxncpy, format, arg); - if (n != -1 && n < dst_maxncpy) { + if (n != (size_t)-1 && n < dst_maxncpy) { dst[n] = '\0'; } else { @@ -190,7 +188,7 @@ size_t BLI_vsnprintf_rlen(char *__restrict dst, n = (size_t)vsnprintf(dst, dst_maxncpy, format, arg); - if (n != -1 && n < dst_maxncpy) { + if (n != (size_t)-1 && n < dst_maxncpy) { /* pass */ } else { @@ -249,7 +247,7 @@ char *BLI_sprintfN_with_buffer( return fixed_buf; } *result_len = (size_t)retval; - if (retval < fixed_buf_size) { + if ((size_t)retval < fixed_buf_size) { return fixed_buf; } @@ -259,7 +257,7 @@ char *BLI_sprintfN_with_buffer( va_start(args, format); retval = vsnprintf(result, size, format, args); va_end(args); - BLI_assert(retval + 1 == size); + BLI_assert((size_t)(retval + 1) == size); return result; } @@ -283,7 +281,7 @@ char *BLI_vsprintfN_with_buffer(char *fixed_buf, return fixed_buf; } *result_len = (size_t)retval; - if (retval < fixed_buf_size) { + if ((size_t)retval < fixed_buf_size) { return fixed_buf; } @@ -291,7 +289,7 @@ char *BLI_vsprintfN_with_buffer(char *fixed_buf, const size_t size = (size_t)retval + 1; char *result = MEM_mallocN(sizeof(char) * size, __func__); retval = vsnprintf(result, size, format, args); - BLI_assert(retval + 1 == size); + BLI_assert((size_t)(retval + 1) == size); return result; } @@ -549,14 +547,14 @@ char *BLI_strcasestr(const char *s, const char *find) size_t len; if ((c = *find++) != 0) { - c = tolower(c); + c = (char)tolower(c); len = strlen(find); do { do { if ((sc = *s++) == 0) { return NULL; } - sc = tolower(sc); + sc = (char)tolower(sc); } while (sc != c); } while (BLI_strncasecmp(s, find, len) != 0); s--; @@ -602,14 +600,14 @@ char *BLI_strncasestr(const char *s, const char *find, size_t len) char c, sc; if ((c = *find++) != 0) { - c = tolower(c); + c = (char)tolower(c); if (len > 1) { do { do { if ((sc = *s++) == 0) { return NULL; } - sc = tolower(sc); + sc = (char)tolower(sc); } while (sc != c); } while (BLI_strncasecmp(s, find, len - 1) != 0); } @@ -619,7 +617,7 @@ char *BLI_strncasestr(const char *s, const char *find, size_t len) if ((sc = *s++) == 0) { return NULL; } - sc = tolower(sc); + sc = (char)tolower(sc); } while (sc != c); } } @@ -634,8 +632,8 @@ int BLI_strcasecmp(const char *s1, const char *s2) char c1, c2; for (i = 0;; i++) { - c1 = tolower(s1[i]); - c2 = tolower(s2[i]); + c1 = (char)tolower(s1[i]); + c2 = (char)tolower(s2[i]); if (c1 < c2) { return -1; @@ -657,8 +655,8 @@ int BLI_strncasecmp(const char *s1, const char *s2, size_t len) char c1, c2; for (i = 0; i < len; i++) { - c1 = tolower(s1[i]); - c2 = tolower(s2[i]); + c1 = (char)tolower(s1[i]); + c2 = (char)tolower(s2[i]); if (c1 < c2) { return -1; @@ -758,8 +756,8 @@ int BLI_strcasecmp_natural(const char *s1, const char *s2) break; } - c1 = tolower(s1[d1]); - c2 = tolower(s2[d2]); + c1 = (char)tolower(s1[d1]); + c2 = (char)tolower(s2[d2]); if (c1 == c2) { /* Continue iteration */ @@ -1006,7 +1004,7 @@ int BLI_str_rstrip_float_zero(char *str, const char pad) int BLI_str_rstrip_digits(char *str) { int totstrip = 0; - int str_len = strlen(str); + int str_len = (int)strlen(str); while (str_len > 0 && isdigit(str[--str_len])) { str[str_len] = '\0'; totstrip++; @@ -1087,13 +1085,13 @@ int BLI_string_find_split_words( bool charsearch = true; /* Skip leading spaces */ - for (i = 0; (i < str_maxlen) && (str[i] != '\0'); i++) { + for (i = 0; (i < (int)str_maxlen) && (str[i] != '\0'); i++) { if (str[i] != delim) { break; } } - for (; (i < str_maxlen) && (str[i] != '\0') && (n < words_max); i++) { + for (; (i < (int)str_maxlen) && (str[i] != '\0') && (n < words_max); i++) { if ((str[i] != delim) && (charsearch == true)) { r_words[n][0] = i; charsearch = false; @@ -1152,7 +1150,7 @@ size_t BLI_str_format_int_grouped(char dst[BLI_STR_FORMAT_INT32_GROUPED_SIZE], i UNUSED_VARS_NDEBUG(dst_maxncpy); char src[BLI_STR_FORMAT_INT32_GROUPED_SIZE]; - const int num_len = SNPRINTF(src, "%d", num); + const int num_len = (int)SNPRINTF(src, "%d", num); return BLI_str_format_int_grouped_ex(src, dst, num_len); } @@ -1164,7 +1162,7 @@ size_t BLI_str_format_uint64_grouped(char dst[BLI_STR_FORMAT_UINT64_GROUPED_SIZE UNUSED_VARS_NDEBUG(dst_maxncpy); char src[BLI_STR_FORMAT_UINT64_GROUPED_SIZE]; - const int num_len = SNPRINTF(src, "%" PRIu64 "", num); + const int num_len = (int)SNPRINTF(src, "%" PRIu64 "", num); return BLI_str_format_int_grouped_ex(src, dst, num_len); } @@ -1176,7 +1174,7 @@ void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], const size_t dst_maxncpy = BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE; BLI_string_debug_size(dst, dst_maxncpy); - double bytes_converted = bytes; + double bytes_converted = (double)bytes; int order = 0; int decimals; const int base = base_10 ? 1000 : 1024; @@ -1204,7 +1202,7 @@ void BLI_str_format_decimal_unit(char dst[BLI_STR_FORMAT_INT32_DECIMAL_UNIT_SIZE { BLI_string_debug_size(dst, BLI_STR_FORMAT_INT32_DECIMAL_UNIT_SIZE); - float number_to_format_converted = number_to_format; + float number_to_format_converted = (float)number_to_format; int order = 0; const float base = 1000; const char *units[] = {"", "K", "M", "B"}; @@ -1229,7 +1227,7 @@ void BLI_str_format_integer_unit(char dst[BLI_STR_FORMAT_INT32_INTEGER_UNIT_SIZE const size_t dst_maxncpy = BLI_STR_FORMAT_INT32_INTEGER_UNIT_SIZE; BLI_string_debug_size(dst, dst_maxncpy); - float number_to_format_converted = number_to_format; + float number_to_format_converted = (float)number_to_format; int order = 0; const float base = 1000; const char *units[] = {"", "K", "M", "B"}; diff --git a/source/blender/blenlib/intern/string_cursor_utf8.c b/source/blender/blenlib/intern/string_cursor_utf8.c index eaee2100773..2c1abae859a 100644 --- a/source/blender/blenlib/intern/string_cursor_utf8.c +++ b/source/blender/blenlib/intern/string_cursor_utf8.c @@ -14,9 +14,7 @@ #include "BLI_string_cursor_utf8.h" /* own include */ -#ifdef __GNUC__ -# pragma GCC diagnostic error "-Wsign-conversion" -#endif +#include "BLI_strict_flags.h" /** * The category of character as returned by #cursor_delim_type_unicode. @@ -99,49 +97,50 @@ static eStrCursorDelimType cursor_delim_type_unicode(const uint uch) } static eStrCursorDelimType cursor_delim_type_utf8(const char *ch_utf8, - const size_t ch_utf8_len, + const int ch_utf8_len, const int pos) { + BLI_assert(ch_utf8_len >= 0); /* for full unicode support we really need to have large lookup tables to figure * out what's what in every possible char set - and python, glib both have these. */ size_t index = (size_t)pos; - uint uch = BLI_str_utf8_as_unicode_step_or_error(ch_utf8, ch_utf8_len, &index); + uint uch = BLI_str_utf8_as_unicode_step_or_error(ch_utf8, (size_t)ch_utf8_len, &index); return cursor_delim_type_unicode(uch); } -bool BLI_str_cursor_step_next_utf8(const char *str, size_t str_maxlen, int *pos) +bool BLI_str_cursor_step_next_utf8(const char *str, const int str_maxlen, int *pos) { /* NOTE: Keep in sync with #BLI_str_cursor_step_next_utf32. */ - if ((*pos) >= (int)str_maxlen) { + if (*pos >= str_maxlen) { return false; } const char *str_end = str + (str_maxlen + 1); - const char *str_pos = str + (*pos); + const char *str_pos = str + *pos; const char *str_next = str_pos; do { str_next = BLI_str_find_next_char_utf8(str_next, str_end); - } while (str_next < str_end && str_next[0] != 0 && - BLI_str_utf8_char_width_or_error(str_next) == 0); - (*pos) += (str_next - str_pos); - if ((*pos) > (int)str_maxlen) { - (*pos) = (int)str_maxlen; + } while ((str_next < str_end) && (str_next[0] != 0) && + (BLI_str_utf8_char_width_or_error(str_next) == 0)); + *pos += (int)(str_next - str_pos); + if (*pos > str_maxlen) { + *pos = str_maxlen; } return true; } -bool BLI_str_cursor_step_prev_utf8(const char *str, size_t str_maxlen, int *pos) +bool BLI_str_cursor_step_prev_utf8(const char *str, const int str_maxlen, int *pos) { /* NOTE: Keep in sync with #BLI_str_cursor_step_prev_utf32. */ - if ((*pos) > 0 && (*pos) <= str_maxlen) { - const char *str_pos = str + (*pos); + if ((*pos > 0) && (*pos <= str_maxlen)) { + const char *str_pos = str + *pos; const char *str_prev = str_pos; do { str_prev = BLI_str_find_prev_char_utf8(str_prev, str); - } while (str_prev > str && BLI_str_utf8_char_width_or_error(str_prev) == 0); - (*pos) -= (str_pos - str_prev); + } while ((str_prev > str) && (BLI_str_utf8_char_width_or_error(str_prev) == 0)); + *pos -= (int)(str_pos - str_prev); return true; } @@ -149,12 +148,13 @@ bool BLI_str_cursor_step_prev_utf8(const char *str, size_t str_maxlen, int *pos) } void BLI_str_cursor_step_utf8(const char *str, - size_t str_maxlen, + const int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step) { + BLI_assert(str_maxlen >= 0); const int pos_orig = *pos; if (direction == STRCUR_DIR_NEXT) { @@ -166,13 +166,13 @@ void BLI_str_cursor_step_utf8(const char *str, } if (jump != STRCUR_JUMP_NONE) { - const eStrCursorDelimType delim_type = (*pos) < str_maxlen ? + const eStrCursorDelimType delim_type = (*pos < str_maxlen) ? cursor_delim_type_utf8(str, str_maxlen, *pos) : STRCUR_DELIM_NONE; /* jump between special characters (/,\,_,-, etc.), * look at function cursor_delim_type() for complete * list of special character, ctr -> */ - while ((*pos) < str_maxlen) { + while (*pos < str_maxlen) { if (BLI_str_cursor_step_next_utf8(str, str_maxlen, pos)) { if (*pos == str_maxlen) { break; @@ -197,19 +197,19 @@ void BLI_str_cursor_step_utf8(const char *str, } if (jump != STRCUR_JUMP_NONE) { - const eStrCursorDelimType delim_type = (*pos) > 0 ? cursor_delim_type_utf8( + const eStrCursorDelimType delim_type = (*pos > 0) ? cursor_delim_type_utf8( str, str_maxlen, *pos - 1) : STRCUR_DELIM_NONE; /* jump between special characters (/,\,_,-, etc.), * look at function cursor_delim_type() for complete * list of special character, ctr -> */ - while ((*pos) > 0) { + while (*pos > 0) { const int pos_prev = *pos; if (BLI_str_cursor_step_prev_utf8(str, str_maxlen, pos)) { if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_utf8(str, str_maxlen, *pos))) { /* left only: compensate for index/change in direction */ - if ((pos_orig - (*pos)) >= 1) { + if ((pos_orig - *pos) >= 1) { *pos = pos_prev; } break; @@ -226,41 +226,45 @@ void BLI_str_cursor_step_utf8(const char *str, } } -bool BLI_str_cursor_step_next_utf32(const char32_t *str, size_t str_maxlen, int *pos) +bool BLI_str_cursor_step_next_utf32(const char32_t *str, const int str_maxlen, int *pos) { /* NOTE: Keep in sync with #BLI_str_cursor_step_next_utf8. */ + BLI_assert(str_maxlen >= 0); - if ((*pos) >= (int)str_maxlen) { + if (*pos >= str_maxlen) { return false; } do { (*pos)++; - } while (*pos < (int)str_maxlen && str[*pos] != 0 && BLI_wcwidth_or_error(str[*pos]) == 0); + } while ((*pos < str_maxlen) && (str[*pos] != 0) && (BLI_wcwidth_or_error(str[*pos]) == 0)); return true; } -bool BLI_str_cursor_step_prev_utf32(const char32_t *str, size_t UNUSED(str_maxlen), int *pos) +bool BLI_str_cursor_step_prev_utf32(const char32_t *str, const int str_maxlen, int *pos) { /* NOTE: Keep in sync with #BLI_str_cursor_step_prev_utf8. */ + BLI_assert(str_maxlen >= 0); + UNUSED_VARS_NDEBUG(str_maxlen); - if ((*pos) <= 0) { + if (*pos <= 0) { return false; } do { (*pos)--; - } while (*pos > 0 && BLI_wcwidth_or_error(str[*pos]) == 0); + } while ((*pos > 0) && (BLI_wcwidth_or_error(str[*pos]) == 0)); return true; } void BLI_str_cursor_step_utf32(const char32_t *str, - size_t str_maxlen, + const int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step) { + BLI_assert(str_maxlen >= 0); const int pos_orig = *pos; if (direction == STRCUR_DIR_NEXT) { @@ -272,13 +276,13 @@ void BLI_str_cursor_step_utf32(const char32_t *str, } if (jump != STRCUR_JUMP_NONE) { - const eStrCursorDelimType delim_type = (*pos) < str_maxlen ? + const eStrCursorDelimType delim_type = (*pos < str_maxlen) ? cursor_delim_type_unicode((uint)str[*pos]) : STRCUR_DELIM_NONE; /* jump between special characters (/,\,_,-, etc.), * look at function cursor_delim_type_unicode() for complete * list of special character, ctr -> */ - while ((*pos) < str_maxlen) { + while (*pos < str_maxlen) { if (BLI_str_cursor_step_next_utf32(str, str_maxlen, pos)) { if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_unicode((uint)str[*pos]))) { @@ -300,19 +304,19 @@ void BLI_str_cursor_step_utf32(const char32_t *str, } if (jump != STRCUR_JUMP_NONE) { - const eStrCursorDelimType delim_type = (*pos) > 0 ? + const eStrCursorDelimType delim_type = (*pos > 0) ? cursor_delim_type_unicode((uint)str[(*pos) - 1]) : STRCUR_DELIM_NONE; /* jump between special characters (/,\,_,-, etc.), * look at function cursor_delim_type() for complete * list of special character, ctr -> */ - while ((*pos) > 0) { + while (*pos > 0) { const int pos_prev = *pos; if (BLI_str_cursor_step_prev_utf32(str, str_maxlen, pos)) { if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_unicode((uint)str[*pos]))) { /* left only: compensate for index/change in direction */ - if ((pos_orig - (*pos)) >= 1) { + if ((pos_orig - *pos) >= 1) { *pos = pos_prev; } break; @@ -330,8 +334,9 @@ void BLI_str_cursor_step_utf32(const char32_t *str, } void BLI_str_cursor_step_bounds_utf8( - const char *str, const size_t str_maxlen, const int pos, int *r_start, int *r_end) + const char *str, const int str_maxlen, const int pos, int *r_start, int *r_end) { + BLI_assert(str_maxlen >= 0); /* Identify the type of characters are on either side of the current cursor position. */ const eStrCursorDelimType prev = (pos > 0) ? cursor_delim_type_utf8(str, str_maxlen, pos - 1) : STRCUR_DELIM_NONE; @@ -352,8 +357,9 @@ void BLI_str_cursor_step_bounds_utf8( } void BLI_str_cursor_step_bounds_utf32( - const char32_t *str, const size_t str_maxlen, const int pos, int *r_start, int *r_end) + const char32_t *str, const int str_maxlen, const int pos, int *r_start, int *r_end) { + BLI_assert(str_maxlen >= 0); /* Identify the type of characters are on either side of the current cursor position. */ const eStrCursorDelimType prev = (pos > 0) ? cursor_delim_type_unicode(str[pos - 1]) : STRCUR_DELIM_NONE; diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c index 1e429468b90..5bda6260739 100644 --- a/source/blender/blenlib/intern/string_utf8.c +++ b/source/blender/blenlib/intern/string_utf8.c @@ -28,6 +28,8 @@ # pragma GCC diagnostic error "-Wsign-conversion" #endif +#include "BLI_strict_flags.h" + /* -------------------------------------------------------------------- */ /** \name UTF8 Character Decoding (Skip & Mask Lookup) * @@ -178,7 +180,7 @@ ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t length) * we only add/subtract extra utf8 bytes in code below * (ab number, aka number of bytes remaining in the utf8 sequence after the initial one). */ ab = utf8_char_compute_skip(c) - 1; - if (length <= ab) { + if (length <= (size_t)ab) { goto utf8_error; } @@ -855,7 +857,7 @@ size_t BLI_str_utf8_from_unicode(uint c, char *dst, const size_t dst_maxncpy) dst[i] = (c & 0x3f) | 0x80; c >>= 6; } - dst[0] = c | first; + dst[0] = (char)(c | first); return len; } @@ -1061,7 +1063,7 @@ int BLI_str_utf8_offset_from_index(const char *str, const size_t str_len, const UNUSED_VARS(code); index++; } - return offset; + return (int)offset; } int BLI_str_utf8_offset_to_column(const char *str, const size_t str_len, const int offset_target) diff --git a/source/blender/blenlib/intern/string_utils.c b/source/blender/blenlib/intern/string_utils.c index 7c6fac58fcf..1e3ce101f36 100644 --- a/source/blender/blenlib/intern/string_utils.c +++ b/source/blender/blenlib/intern/string_utils.c @@ -21,9 +21,7 @@ #include "DNA_listBase.h" -#ifdef __GNUC__ -# pragma GCC diagnostic error "-Wsign-conversion" -#endif +#include "BLI_strict_flags.h" /* -------------------------------------------------------------------- */ /** \name String Replace @@ -52,7 +50,7 @@ char *BLI_string_replaceN(const char *__restrict str, if (str != match) { /* Add the segment of the string from `str` to match to the buffer, * then restore the value at match. */ - BLI_dynstr_nappend(ds, str, (match - str)); + BLI_dynstr_nappend(ds, str, (int)(match - str)); /* now our current position should be set on the start of the match */ str = match; @@ -113,17 +111,17 @@ bool BLI_string_replace_table_exact(char *string, size_t BLI_string_replace_range( char *string, size_t string_maxncpy, int src_beg, int src_end, const char *dst) { - int string_len = strlen(string); + int string_len = (int)strlen(string); BLI_assert(src_beg <= src_end); BLI_assert(src_end <= string_len); const int src_len = src_end - src_beg; - int dst_len = strlen(dst); + int dst_len = (int)strlen(dst); if (src_len < dst_len) { /* Grow, first handle special cases. */ /* Special case, the src_end is entirely clipped. */ - if (UNLIKELY(string_maxncpy <= src_beg + dst_len)) { + if (UNLIKELY((int)string_maxncpy <= src_beg + dst_len)) { /* There is only room for the destination. */ dst_len = ((int)string_maxncpy - src_beg) - 1; string_len = src_end; @@ -132,7 +130,7 @@ size_t BLI_string_replace_range( const int ofs = dst_len - src_len; /* Clip the string when inserting the destination string exceeds `string_maxncpy`. */ - if (string_len + ofs >= string_maxncpy) { + if (string_len + ofs >= (int)string_maxncpy) { string_len = ((int)string_maxncpy - ofs) - 1; string[string_len] = '\0'; BLI_assert(src_end <= string_len); @@ -177,7 +175,7 @@ size_t BLI_string_split_name_number(const char *name, while (a--) { if (name[a] == delim) { r_name_left[a] = '\0'; /* truncate left part here */ - *r_number = atol(name + a + 1); + *r_number = (int)atol(name + a + 1); /* casting down to an int, can overflow for large numbers */ if (*r_number < 0) { *r_number = 0; @@ -567,7 +565,7 @@ size_t BLI_string_join_array_by_sep_char( char *BLI_string_join_arrayN(const char *strings[], uint strings_num) { - const uint result_size = BLI_string_len_array(strings, strings_num) + 1; + const size_t result_size = BLI_string_len_array(strings, strings_num) + 1; char *result = MEM_mallocN(sizeof(char) * result_size, __func__); char *c = result; for (uint i = 0; i < strings_num; i++) { @@ -583,8 +581,8 @@ char *BLI_string_join_arrayN(const char *strings[], uint strings_num) char *BLI_string_join_array_by_sep_charN(char sep, const char *strings[], uint strings_num) { - const uint result_size = BLI_string_len_array(strings, strings_num) + - (strings_num ? strings_num - 1 : 0) + 1; + const size_t result_size = BLI_string_len_array(strings, strings_num) + + (strings_num ? strings_num - 1 : 0) + 1; char *result = MEM_mallocN(sizeof(char) * result_size, __func__); char *c = result; if (strings_num != 0) { @@ -607,7 +605,7 @@ char *BLI_string_join_array_by_sep_char_with_tableN(char sep, const char *strings[], uint strings_num) { - uint result_size = 0; + size_t result_size = 0; for (uint i = 0; i < strings_num; i++) { result_size += strlen(strings[i]) + 1; }