diff options
author | Campbell Barton <campbell@blender.org> | 2022-03-14 06:25:33 +0300 |
---|---|---|
committer | Campbell Barton <campbell@blender.org> | 2022-03-14 06:25:33 +0300 |
commit | 541ba689913e8359216a61a67a60425837af88a9 (patch) | |
tree | d74b3170e86db69571593a2265c8d4503fd392a3 | |
parent | a5571fd0e8cddeb19fd7191896bc57ded44bdd7d (diff) |
Cleanup: use size_t for BLF text API functions
Also minor cleanup to txt_sel_to_buf:
- Use memcpy instead of strncpy as the strings don't contain nil bytes.
- Replace while loops with for loops.
-rw-r--r-- | source/blender/blenkernel/BKE_text.h | 8 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 42 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_undo.c | 2 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_text_api.c | 4 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface_run.c | 2 |
5 files changed, 25 insertions, 33 deletions
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index bc6df18ce25..a5b71d42cdc 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -57,7 +57,7 @@ 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, size_t *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); @@ -92,7 +92,7 @@ void txt_sel_all(struct Text *text); 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, int *r_buf_strlen); +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_split_curline(struct Text *text); void txt_backspace_char(struct Text *text); @@ -138,12 +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, size_t *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) ATTR_NONNULL(1, 2); +void txt_from_buf_for_undo(struct Text *text, const char *buf, size_t 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 486449c3f86..991fd9e3aff 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -1349,7 +1349,7 @@ void txt_sel_set(Text *text, int startl, int startc, int endl, int endc) * - Are not null terminated. * \{ */ -char *txt_to_buf_for_undo(Text *text, int *r_buf_len) +char *txt_to_buf_for_undo(Text *text, size_t *r_buf_len) { int buf_len = 0; LISTBASE_FOREACH (const TextLine *, l, &text->lines) { @@ -1366,7 +1366,7 @@ char *txt_to_buf_for_undo(Text *text, int *r_buf_len) return buf; } -void txt_from_buf_for_undo(Text *text, const char *buf, int buf_len) +void txt_from_buf_for_undo(Text *text, const char *buf, size_t buf_len) { const char *buf_end = buf + buf_len; const char *buf_step = buf; @@ -1434,10 +1434,10 @@ void txt_from_buf_for_undo(Text *text, const char *buf, int buf_len) /** \name Cut and Paste Functions * \{ */ -char *txt_to_buf(Text *text, int *r_buf_strlen) +char *txt_to_buf(Text *text, size_t *r_buf_strlen) { /* Identical to #txt_to_buf_for_undo except that the string is nil terminated. */ - int buf_len = 0; + size_t buf_len = 0; LISTBASE_FOREACH (const TextLine *, l, &text->lines) { buf_len += l->len + 1; } @@ -1453,10 +1453,10 @@ char *txt_to_buf(Text *text, int *r_buf_strlen) return buf; } -char *txt_sel_to_buf(Text *text, int *r_buf_strlen) +char *txt_sel_to_buf(Text *text, size_t *r_buf_strlen) { char *buf; - int length = 0; + size_t length = 0; TextLine *tmp, *linef, *linel; int charf, charl; @@ -1500,42 +1500,32 @@ char *txt_sel_to_buf(Text *text, int *r_buf_strlen) if (linef == linel) { length = charl - charf; - buf = MEM_mallocN(length + 1, "sel buffer"); - - BLI_strncpy(buf, linef->line + charf, length + 1); + memcpy(buf, linef->line + charf, length + 1); } else { - length += linef->len - charf; - length += charl; - length++; /* For the '\n' */ + /* Add 1 for the '\n' */ + length = (linef->len - charf) + charl + 1; - tmp = linef->next; - while (tmp && tmp != linel) { + for (tmp = linef->next; tmp && tmp != linel; tmp = tmp->next) { length += tmp->len + 1; - tmp = tmp->next; } buf = MEM_mallocN(length + 1, "sel buffer"); - strncpy(buf, linef->line + charf, linef->len - charf); + memcpy(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); + for (tmp = linef->next; tmp && tmp != linel; tmp = tmp->next) { + memcpy(buf + length, tmp->line, tmp->len); length += tmp->len; - buf[length++] = '\n'; - - tmp = tmp->next; } - strncpy(buf + length, linel->line, charl); - length += charl; - buf[length] = 0; + memcpy(buf + length, linel->line, charl); + length += charl; + buf[length] = '\0'; } if (r_buf_strlen) { diff --git a/source/blender/editors/space_text/text_undo.c b/source/blender/editors/space_text/text_undo.c index 864193bfad8..035a9f29dc3 100644 --- a/source/blender/editors/space_text/text_undo.c +++ b/source/blender/editors/space_text/text_undo.c @@ -59,7 +59,7 @@ typedef struct TextState { static void text_state_encode(TextState *state, Text *text, BArrayStore *buffer_store) { - int buf_len = 0; + size_t buf_len = 0; uchar *buf = (uchar *)txt_to_buf_for_undo(text, &buf_len); state->buf_array_state = BLI_array_store_state_add(buffer_store, buf, buf_len, NULL); MEM_freeN(buf); diff --git a/source/blender/makesrna/intern/rna_text_api.c b/source/blender/makesrna/intern/rna_text_api.c index 0c0b8a85023..a5eadd1f36c 100644 --- a/source/blender/makesrna/intern/rna_text_api.c +++ b/source/blender/makesrna/intern/rna_text_api.c @@ -40,7 +40,9 @@ static void rna_Text_from_string(Text *text, const char *str) static void rna_Text_as_string(Text *text, int *r_result_len, const char **result) { - *result = txt_to_buf(text, r_result_len); + size_t result_len; + *result = txt_to_buf(text, &result_len); + *r_result_len = result_len; } static void rna_Text_select_set(Text *text, int startl, int startc, int endl, int endc) diff --git a/source/blender/python/intern/bpy_interface_run.c b/source/blender/python/intern/bpy_interface_run.c index 500221c3c50..8db122470b8 100644 --- a/source/blender/python/intern/bpy_interface_run.c +++ b/source/blender/python/intern/bpy_interface_run.c @@ -102,7 +102,7 @@ static bool python_script_exec( fn_dummy_py = PyC_UnicodeFromByte(fn_dummy); - int buf_len_dummy; + size_t 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); |