Cleanup: pass the buffer length into `txt_insert_buf`

Also remove redundant NULL check.
This commit is contained in:
Campbell Barton 2022-04-07 15:43:23 +10:00
parent f49a736ff4
commit e2f4c4db8d
Notes: blender-bot 2023-02-14 06:00:45 +01:00
Referenced by commit 79e94caa6b, Fix error pasting text containing tabs
6 changed files with 36 additions and 28 deletions

View File

@ -45,8 +45,8 @@ struct Text *BKE_text_load_ex(struct Main *bmain,
* \note Text data-blocks have no user by default, only the 'real user' flag.
*/
struct Text *BKE_text_load(struct Main *bmain, const char *file, const char *relpath);
void BKE_text_clear(struct Text *text);
void BKE_text_write(struct Text *text, const char *str);
void BKE_text_clear(struct Text *text) ATTR_NONNULL(1);
void BKE_text_write(struct Text *text, const char *str, int str_len) ATTR_NONNULL(1, 2);
/**
* \return codes:
* - 0 if file on disk is the same or Text is in memory only.
@ -93,7 +93,8 @@ void txt_sel_clear(struct Text *text);
void txt_sel_line(struct Text *text);
void txt_sel_set(struct Text *text, int startl, int startc, int endl, int endc);
char *txt_sel_to_buf(struct Text *text, size_t *r_buf_strlen);
void txt_insert_buf(struct Text *text, const char *in_buffer);
void txt_insert_buf(struct Text *text, const char *in_buffer, int in_buffer_len)
ATTR_NONNULL(1, 2);
void txt_split_curline(struct Text *text);
void txt_backspace_char(struct Text *text);
void txt_backspace_word(struct Text *text);

View File

@ -518,9 +518,9 @@ void BKE_text_clear(Text *text) /* called directly from rna */
txt_make_dirty(text);
}
void BKE_text_write(Text *text, const char *str) /* called directly from rna */
void BKE_text_write(Text *text, const char *str, int str_len) /* called directly from rna */
{
txt_insert_buf(text, str);
txt_insert_buf(text, str, str_len);
txt_move_eof(text, 0);
txt_make_dirty(text);
}
@ -1536,33 +1536,30 @@ char *txt_sel_to_buf(Text *text, size_t *r_buf_strlen)
return buf;
}
void txt_insert_buf(Text *text, const char *in_buffer)
void txt_insert_buf(Text *text, const char *in_buffer, int in_buffer_len)
{
int l = 0, len;
BLI_assert(in_buffer_len == strlen(in_buffer));
int l = 0;
size_t i = 0, j;
TextLine *add;
char *buffer;
if (!in_buffer) {
return;
}
txt_delete_sel(text);
len = strlen(in_buffer);
buffer = BLI_strdupn(in_buffer, len);
len += txt_extended_ascii_as_utf8(&buffer);
buffer = BLI_strdupn(in_buffer, in_buffer_len);
in_buffer_len += txt_extended_ascii_as_utf8(&buffer);
/* Read the first line (or as close as possible */
while (buffer[i] && buffer[i] != '\n') {
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, len, &i));
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, in_buffer_len, &i));
}
if (buffer[i] == '\n') {
txt_split_curline(text);
i++;
while (i < len) {
while (i < in_buffer_len) {
l = 0;
while (buffer[i] && buffer[i] != '\n') {
@ -1576,8 +1573,8 @@ void txt_insert_buf(Text *text, const char *in_buffer)
i++;
}
else {
for (j = i - l; j < i && j < len;) {
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, len, &j));
for (j = i - l; j < i && j < in_buffer_len;) {
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, in_buffer_len, &j));
}
break;
}
@ -1875,7 +1872,7 @@ static void txt_convert_tab_to_spaces(Text *text)
* to multiples of TXT_TABSIZE)
*/
const char *sb = &tab_to_spaces[text->curc % TXT_TABSIZE];
txt_insert_buf(text, sb);
txt_insert_buf(text, sb, strlen(sb));
}
static bool txt_add_char_intern(Text *text, unsigned int add, bool replace_tabs)

View File

@ -267,7 +267,8 @@ static void confirm_suggestion(Text *text)
// for (i = 0; i < skipleft; i++)
// txt_move_left(text, 0);
BLI_assert(memcmp(sel->name, &line[i], over) == 0);
txt_insert_buf(text, sel->name + over);
const char *buf = sel->name + over;
txt_insert_buf(text, buf, strlen(buf));
// for (i = 0; i < skipleft; i++)
// txt_move_right(text, 0);

View File

@ -921,7 +921,7 @@ static int text_paste_exec(bContext *C, wmOperator *op)
buf = new_buf;
}
txt_insert_buf(text, buf);
txt_insert_buf(text, buf, buf_len);
text_update_edited(text);
MEM_freeN(buf);
@ -3587,7 +3587,7 @@ static int text_find_and_replace(bContext *C, wmOperator *op, short mode)
if (found) {
if (mode == TEXT_REPLACE) {
ED_text_undo_push_init(C);
txt_insert_buf(text, st->replacestr);
txt_insert_buf(text, st->replacestr, strlen(st->replacestr));
if (text->curl && text->curl->format) {
MEM_freeN(text->curl->format);
text->curl->format = NULL;
@ -3671,7 +3671,7 @@ static int text_replace_all(bContext *C)
ED_text_undo_push_init(C);
do {
txt_insert_buf(text, st->replacestr);
txt_insert_buf(text, st->replacestr, strlen(st->replacestr));
if (text->curl && text->curl->format) {
MEM_freeN(text->curl->format);
text->curl->format = NULL;

View File

@ -28,14 +28,14 @@ static void rna_Text_clear(Text *text)
static void rna_Text_write(Text *text, const char *str)
{
BKE_text_write(text, str);
BKE_text_write(text, str, strlen(str));
WM_main_add_notifier(NC_TEXT | NA_EDITED, text);
}
static void rna_Text_from_string(Text *text, const char *str)
{
BKE_text_clear(text);
BKE_text_write(text, str);
BKE_text_write(text, str, strlen(str));
}
static void rna_Text_as_string(Text *text, int *r_result_len, const char **result)

View File

@ -6,6 +6,8 @@
* This file extends the text editor with C/Python API methods and attributes.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "DNA_text_types.h"
@ -103,9 +105,16 @@ static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args)
/* Parse the region range. */
const char *buf;
Py_ssize_t buf_len;
TextRegion region;
if (!PyArg_ParseTuple(
args, "s|((ii)(ii))", &buf, &region.curl, &region.curc, &region.sell, &region.selc)) {
if (!PyArg_ParseTuple(args,
"s#|((ii)(ii))",
&buf,
&buf_len,
&region.curl,
&region.curc,
&region.sell,
&region.selc)) {
return NULL;
}
@ -114,7 +123,7 @@ static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args)
}
/* Set the selected text. */
txt_insert_buf(text, buf);
txt_insert_buf(text, buf, buf_len);
/* Update the text editor. */
WM_main_add_notifier(NC_TEXT | NA_EDITED, text);