diff options
-rw-r--r-- | source/blender/blenkernel/BKE_text.h | 10 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 82 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface_run.c | 3 |
3 files changed, 21 insertions, 74 deletions
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index e833b4a14bb..bc6df18ce25 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -14,6 +14,8 @@ struct Main; struct Text; struct TextLine; +#include "BLI_compiler_attrs.h" + /** * \note caller must handle `compiled` member. */ @@ -55,7 +57,8 @@ void BKE_text_write(struct Text *text, const char *str); int BKE_text_file_modified_check(struct Text *text); void BKE_text_file_modified_ignore(struct Text *text); -char *txt_to_buf(struct Text *text, int *r_buf_strlen); +char *txt_to_buf(struct Text *text, int *r_buf_strlen) + ATTR_NONNULL(1, 2) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL; void txt_clean_text(struct Text *text); void txt_order_cursors(struct Text *text, bool reverse); int txt_find_string(struct Text *text, const char *findstr, int wrap, int match_case); @@ -135,11 +138,12 @@ enum { /** * Create a buffer, the only requirement is #txt_from_buf_for_undo can decode it. */ -char *txt_to_buf_for_undo(struct Text *text, int *r_buf_len); +char *txt_to_buf_for_undo(struct Text *text, int *r_buf_len) + ATTR_NONNULL(1, 2) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL; /** * Decode a buffer from #txt_to_buf_for_undo. */ -void txt_from_buf_for_undo(struct Text *text, const char *buf, int buf_len); +void txt_from_buf_for_undo(struct Text *text, const char *buf, int buf_len) ATTR_NONNULL(1, 2); #ifdef __cplusplus } diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 5d0e515040d..486449c3f86 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -1436,78 +1436,20 @@ void txt_from_buf_for_undo(Text *text, const char *buf, int buf_len) char *txt_to_buf(Text *text, int *r_buf_strlen) { - int length; - TextLine *tmp, *linef, *linel; - int charf, charl; - char *buf; - - if (r_buf_strlen) { - *r_buf_strlen = 0; - } - - if (!text->curl) { - return NULL; - } - if (!text->sell) { - return NULL; - } - if (!text->lines.first) { - return NULL; - } - - linef = text->lines.first; - charf = 0; - - linel = text->lines.last; - charl = linel->len; - - if (linef == text->lines.last) { - length = charl - charf; - - buf = MEM_mallocN(length + 2, "text buffer"); - - BLI_strncpy(buf, linef->line + charf, length + 1); - buf[length] = 0; - } - else { - length = linef->len - charf; - length += charl; - length += 2; /* For the 2 '\n' */ - - tmp = linef->next; - while (tmp && tmp != linel) { - length += tmp->len + 1; - tmp = tmp->next; - } - - buf = MEM_mallocN(length + 1, "cut buffer"); - - strncpy(buf, linef->line + charf, linef->len - charf); - length = linef->len - charf; - - buf[length++] = '\n'; - - tmp = linef->next; - while (tmp && tmp != linel) { - strncpy(buf + length, tmp->line, tmp->len); - length += tmp->len; - - buf[length++] = '\n'; - - tmp = tmp->next; - } - strncpy(buf + length, linel->line, charl); - length += charl; - - /* python compiler wants an empty end line */ - buf[length++] = '\n'; - buf[length] = 0; + /* Identical to #txt_to_buf_for_undo except that the string is nil terminated. */ + int buf_len = 0; + LISTBASE_FOREACH (const TextLine *, l, &text->lines) { + buf_len += l->len + 1; } - - if (r_buf_strlen) { - *r_buf_strlen = length; + char *buf = MEM_mallocN(buf_len + 1, __func__); + char *buf_step = buf; + LISTBASE_FOREACH (const TextLine *, l, &text->lines) { + memcpy(buf_step, l->line, l->len); + buf_step += l->len; + *buf_step++ = '\n'; } - + *buf_step = '\0'; + *r_buf_strlen = buf_len; return buf; } diff --git a/source/blender/python/intern/bpy_interface_run.c b/source/blender/python/intern/bpy_interface_run.c index 01db0efecfd..500221c3c50 100644 --- a/source/blender/python/intern/bpy_interface_run.c +++ b/source/blender/python/intern/bpy_interface_run.c @@ -102,7 +102,8 @@ static bool python_script_exec( fn_dummy_py = PyC_UnicodeFromByte(fn_dummy); - buf = txt_to_buf(text, NULL); + int buf_len_dummy; + buf = txt_to_buf(text, &buf_len_dummy); text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1); MEM_freeN(buf); |