Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <campbell@blender.org>2022-04-26 06:45:59 +0300
committerCampbell Barton <campbell@blender.org>2022-04-26 06:45:59 +0300
commit3ea6dbfe7364cfb1b8ef63defb263b687a2d8bd1 (patch)
tree134fcb3c3c0e0659bc62a6946b6492e0c64a526e /source/blender/blenkernel/intern/text.c
parent98ad294d1719eb9cec01af08a0db16558e9964a5 (diff)
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.
Diffstat (limited to 'source/blender/blenkernel/intern/text.c')
-rw-r--r--source/blender/blenkernel/intern/text.c42
1 files changed, 15 insertions, 27 deletions
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index 9d8a97c90b8..1ab4c9614de 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -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);