From 8cc5483331d1a3d5c6eba055ae303788ba843526 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 11 Mar 2022 15:09:55 +1100 Subject: Text: use simplified logic for txt_to_buf This function was copied from txt_sel_to_buf, including unnecessary complexity to support selection as well as checks for the cursor which don't make sense when copying the whole buffer. Use a simple loop to copy all text into the destination buffer. --- source/blender/blenkernel/intern/text.c | 82 +++++---------------------------- 1 file changed, 12 insertions(+), 70 deletions(-) (limited to 'source/blender/blenkernel/intern/text.c') 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; } -- cgit v1.2.3