[Bf-blender-cvs] [f4ff36431cc] blender-v3.2-release: Fix text.as_string() adding a trailing new-line

Campbell Barton noreply at git.blender.org
Tue May 17 09:49:52 CEST 2022


Commit: f4ff36431ccfac2f0a99fc23c18fe0d9de38b36d
Author: Campbell Barton
Date:   Tue May 17 17:06:36 2022 +1000
Branches: blender-v3.2-release
https://developer.blender.org/rBf4ff36431ccfac2f0a99fc23c18fe0d9de38b36d

Fix text.as_string() adding a trailing new-line

Moving Text.as_string() from Python to C [0] added an extra new-line
causing a round-trip from_string/to_string to add a new-line,
this also broke the undo test `test_undo.text_editor_simple` in
`../lib/tests/ui_simulate/run.py`.

[0]: 231eac160ee394d41c84e0cc36845facb7594ba5

===================================================================

M	source/blender/blenkernel/intern/text.c

===================================================================

diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index ec6387c9cf6..9acf387b930 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -1425,11 +1425,15 @@ void txt_from_buf_for_undo(Text *text, const char *buf, size_t buf_len)
 
 char *txt_to_buf(Text *text, size_t *r_buf_strlen)
 {
+  const bool has_data = !BLI_listbase_is_empty(&text->lines);
   /* Identical to #txt_to_buf_for_undo except that the string is nil terminated. */
   size_t buf_len = 0;
   LISTBASE_FOREACH (const TextLine *, l, &text->lines) {
     buf_len += l->len + 1;
   }
+  if (has_data) {
+    buf_len -= 1;
+  }
   char *buf = MEM_mallocN(buf_len + 1, __func__);
   char *buf_step = buf;
   LISTBASE_FOREACH (const TextLine *, l, &text->lines) {
@@ -1437,6 +1441,11 @@ char *txt_to_buf(Text *text, size_t *r_buf_strlen)
     buf_step += l->len;
     *buf_step++ = '\n';
   }
+  /* Remove the trailing new-line so a round-trip doesn't add a newline:
+   * Python for e.g. `text.from_string(text.as_string())`. */
+  if (has_data) {
+    buf_step--;
+  }
   *buf_step = '\0';
   *r_buf_strlen = buf_len;
   return buf;



More information about the Bf-blender-cvs mailing list