Cleanup: avoid redundant strlen calls

This commit is contained in:
Campbell Barton
2023-09-20 12:11:33 +10:00
parent e6ef1c36f0
commit fb81c37077
7 changed files with 18 additions and 11 deletions

View File

@@ -127,9 +127,9 @@ static void text_copy_data(Main * /*bmain*/, ID *id_dst, const ID *id_src, const
LISTBASE_FOREACH (TextLine *, line_src, &text_src->lines) {
TextLine *line_dst = static_cast<TextLine *>(MEM_mallocN(sizeof(*line_dst), __func__));
line_dst->line = BLI_strdup(line_src->line);
line_dst->format = nullptr;
line_dst->line = BLI_strdupn(line_src->line, line_src->len);
line_dst->len = line_src->len;
line_dst->format = nullptr;
BLI_addtail(&text_dst->lines, line_dst);
}

View File

@@ -446,13 +446,13 @@ bool BLI_path_make_safe_filename_ex(char *filename, bool allow_tokens)
#ifdef WIN32
{
const size_t len = strlen(filename);
const char *invalid_names[] = {
"con", "prn", "aux", "null", "com1", "com2", "com3", "com4",
"com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
"lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", NULL,
};
char *filename_lower = BLI_strdup(filename);
const size_t len = strlen(filename);
char *filename_lower = BLI_strdupn(filename, len);
const char **iname;
/* Forbid trailing dot (trailing space has already been replaced above). */

View File

@@ -243,14 +243,15 @@ static ConsoleLine *console_lb_add_str__internal(ListBase *lb, char *str, bool o
{
ConsoleLine *ci = static_cast<ConsoleLine *>(
MEM_callocN(sizeof(ConsoleLine), "ConsoleLine Add"));
const int str_len = strlen(str);
if (own) {
ci->line = str;
}
else {
ci->line = BLI_strdup(str);
ci->line = BLI_strdupn(str, str_len);
}
ci->len = ci->len_alloc = strlen(str);
ci->len = ci->len_alloc = str_len;
BLI_addtail(lb, ci);
return ci;

View File

@@ -1530,8 +1530,8 @@ static int text_convert_whitespace_exec(bContext *C, wmOperator *op)
}
/* Put new_line in the `tmp->line` spot. */
tmp->line = BLI_strdup(tmp_line);
tmp->len = strlen(tmp_line);
tmp->line = BLI_strdupn(tmp_line, tmp->len);
tmp->format = nullptr;
}
}

View File

@@ -242,7 +242,9 @@ static void _build_translations_cache(PyObject *py_messages, const char *locale)
/* Do not overwrite existing keys! */
if (BPY_app_translations_py_pgettext(msgctxt, msgid) == msgid) {
GHashKey *key = _ghashutil_keyalloc(msgctxt, msgid);
BLI_ghash_insert(_translations_cache, key, BLI_strdup(PyUnicode_AsUTF8(trans)));
Py_ssize_t trans_str_len;
const char *trans_str = PyUnicode_AsUTF8AndSize(trans, &trans_str_len);
BLI_ghash_insert(_translations_cache, key, BLI_strdupn(trans_str, trans_str_len));
}
}
}

View File

@@ -33,7 +33,9 @@ static char *pyop_poll_message_get_fn(bContext * /*C*/, void *user_data)
PyObject *py_func_or_msg = PyTuple_GET_ITEM(py_args, 0);
if (PyUnicode_Check(py_func_or_msg)) {
return BLI_strdup(PyUnicode_AsUTF8(py_func_or_msg));
Py_ssize_t msg_len;
const char *msg = PyUnicode_AsUTF8AndSize(py_func_or_msg, &msg_len);
return BLI_strdupn(msg, msg_len);
}
PyObject *py_args_after_first = PyTuple_GetSlice(py_args, 1, PY_SSIZE_T_MAX);
@@ -52,7 +54,9 @@ static char *pyop_poll_message_get_fn(bContext * /*C*/, void *user_data)
/* pass */
}
else if (PyUnicode_Check(py_msg)) {
msg = BLI_strdup(PyUnicode_AsUTF8(py_msg));
Py_ssize_t msg_src_len;
const char *msg_src = PyUnicode_AsUTF8AndSize(py_msg, &msg_src_len);
msg = BLI_strdupn(msg_src, msg_src_len);
}
else {
PyErr_Format(PyExc_TypeError,

View File

@@ -607,7 +607,7 @@ std::string WM_operatortype_description_or_name(bContext *C,
if (text.empty()) {
const std::string text_orig = WM_operatortype_name(ot, properties);
if (!text_orig.empty()) {
text = BLI_strdup(text_orig.c_str());
text = BLI_strdupn(text_orig.c_str(), text_orig.size());
}
}
return text;