diff options
author | Campbell Barton <campbell@blender.org> | 2022-04-07 08:43:23 +0300 |
---|---|---|
committer | Campbell Barton <campbell@blender.org> | 2022-04-07 08:45:20 +0300 |
commit | e2f4c4db8d6cbe4694c24d599e16ee3889871bdd (patch) | |
tree | a3be9aecfcac72e0ddaf45471b56a29900101ebf /source/blender/blenkernel | |
parent | f49a736ff4023231483c7e535ca2a7f2869d641d (diff) |
Cleanup: pass the buffer length into `txt_insert_buf`
Also remove redundant NULL check.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_text.h | 7 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 29 |
2 files changed, 17 insertions, 19 deletions
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index a5b71d42cdc..b05abb5a73c 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -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); diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 8759d7a0e5f..9d8a97c90b8 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -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) |