Cleanup: simplify text copying string operations
- De-duplicate txt_new_linen & txt_new_line. - Don't accept NULL as input for txt_new_linen & txt_new_line (callers can pass in an empty string instead). - Avoid duplicate strlen calls in txt_new_linen. - Use memcpy when the length of the string is known in txt_delete_sel & BKE_text_load_ex.
This commit is contained in:
parent
98ad294d17
commit
3ea6dbfe73
|
@ -484,8 +484,9 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const
|
|||
}
|
||||
|
||||
if (is_internal == false) {
|
||||
ta->filepath = MEM_mallocN(strlen(file) + 1, "text_name");
|
||||
strcpy(ta->filepath, file);
|
||||
const size_t file_len = strlen(file);
|
||||
ta->filepath = MEM_mallocN(file_len + 1, "text_name");
|
||||
memcpy(ta->filepath, file, file_len + 1);
|
||||
}
|
||||
else {
|
||||
ta->flags |= TXT_ISMEM | TXT_ISDIRTY;
|
||||
|
@ -605,40 +606,27 @@ static void make_new_line(TextLine *line, char *newline)
|
|||
line->format = NULL;
|
||||
}
|
||||
|
||||
static TextLine *txt_new_line(const char *str)
|
||||
static TextLine *txt_new_linen(const char *str, int str_len)
|
||||
{
|
||||
TextLine *tmp;
|
||||
|
||||
if (!str) {
|
||||
str = "";
|
||||
}
|
||||
|
||||
tmp = (TextLine *)MEM_mallocN(sizeof(TextLine), "textline");
|
||||
tmp->line = MEM_mallocN(strlen(str) + 1, "textline_string");
|
||||
tmp->line = MEM_mallocN(str_len + 1, "textline_string");
|
||||
tmp->format = NULL;
|
||||
|
||||
strcpy(tmp->line, str);
|
||||
|
||||
tmp->len = strlen(str);
|
||||
memcpy(tmp->line, str, str_len);
|
||||
tmp->line[str_len] = '\0';
|
||||
tmp->len = str_len;
|
||||
tmp->next = tmp->prev = NULL;
|
||||
|
||||
BLI_assert(strlen(tmp->line) == str_len);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
static TextLine *txt_new_linen(const char *str, int n)
|
||||
static TextLine *txt_new_line(const char *str)
|
||||
{
|
||||
TextLine *tmp;
|
||||
|
||||
tmp = (TextLine *)MEM_mallocN(sizeof(TextLine), "textline");
|
||||
tmp->line = MEM_mallocN(n + 1, "textline_string");
|
||||
tmp->format = NULL;
|
||||
|
||||
BLI_strncpy(tmp->line, (str) ? str : "", n + 1);
|
||||
|
||||
tmp->len = strlen(tmp->line);
|
||||
tmp->next = tmp->prev = NULL;
|
||||
|
||||
return tmp;
|
||||
return txt_new_linen(str, strlen(str));
|
||||
}
|
||||
|
||||
void txt_clean_text(Text *text)
|
||||
|
@ -650,7 +638,7 @@ void txt_clean_text(Text *text)
|
|||
text->lines.first = text->lines.last;
|
||||
}
|
||||
else {
|
||||
text->lines.first = text->lines.last = txt_new_line(NULL);
|
||||
text->lines.first = text->lines.last = txt_new_line("");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1234,8 +1222,8 @@ static void txt_delete_sel(Text *text)
|
|||
|
||||
buf = MEM_mallocN(text->curc + (text->sell->len - text->selc) + 1, "textline_string");
|
||||
|
||||
strncpy(buf, text->curl->line, text->curc);
|
||||
strcpy(buf + text->curc, text->sell->line + text->selc);
|
||||
memcpy(buf, text->curl->line, text->curc);
|
||||
memcpy(buf + text->curc, text->sell->line + text->selc, text->sell->len - text->selc);
|
||||
buf[text->curc + (text->sell->len - text->selc)] = 0;
|
||||
|
||||
make_new_line(text->curl, buf);
|
||||
|
|
Loading…
Reference in New Issue