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.
This commit is contained in:
parent
231eac160e
commit
8cc5483331
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
/* 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 (!text->curl) {
|
||||
return NULL;
|
||||
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';
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
if (r_buf_strlen) {
|
||||
*r_buf_strlen = length;
|
||||
}
|
||||
|
||||
*buf_step = '\0';
|
||||
*r_buf_strlen = buf_len;
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue