[libvirt] [PATCH 10/19] util: buffer: Split getting of effective indent out of virBufferGetIndent

Peter Krempa pkrempa at redhat.com
Thu Oct 24 13:56:28 UTC 2019


The function basically does two very distinct things depending on a
bool. As a first step of conversion split out the case when @dynamic is
true and implement it as a new function and convert all callers.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virbuffer.c     | 26 +++++++++++++++++++++-----
 src/util/virbuffer.h     |  1 +
 tests/virbuftest.c       | 12 ++++++------
 4 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 12cb3b5bf7..94c2e4ef6a 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1600,6 +1600,7 @@ virBufferEscapeShell;
 virBufferEscapeSQL;
 virBufferEscapeString;
 virBufferFreeAndReset;
+virBufferGetEffectiveIndent;
 virBufferGetIndent;
 virBufferSetIndent;
 virBufferStrcat;
diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
index 69fcf946f5..0d2721b118 100644
--- a/src/util/virbuffer.c
+++ b/src/util/virbuffer.c
@@ -117,6 +117,24 @@ virBufferGetIndent(const virBuffer *buf, bool dynamic)
     return buf->indent;
 }

+
+/**
+ * virBufferGetEffectiveIndent:
+ * @buf: the buffer
+ *
+ * Returns the number of spaces that need to be appended to @buf to honour
+ * auto-indentation.
+ */
+size_t
+virBufferGetEffectiveIndent(const virBuffer *buf)
+{
+    if (buf->use && buf->content[buf->use - 1] != '\n')
+        return 0;
+
+    return buf->indent;
+}
+
+
 /**
  * virBufferGrow:
  * @buf: the buffer
@@ -161,14 +179,12 @@ void
 virBufferAdd(virBufferPtr buf, const char *str, int len)
 {
     unsigned int needSize;
-    int indent;
+    size_t indent;

-    if (!str || !buf || (len == 0 && buf->indent == 0))
+    if (!str || !buf || buf->error || (len == 0 && buf->indent == 0))
         return;

-    indent = virBufferGetIndent(buf, true);
-    if (indent < 0)
-        return;
+    indent = virBufferGetEffectiveIndent(buf);

     if (len < 0)
         len = strlen(str);
diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h
index ff24ab1019..7156d9d0d8 100644
--- a/src/util/virbuffer.h
+++ b/src/util/virbuffer.h
@@ -110,6 +110,7 @@ void virBufferSetIndent(virBufferPtr, int indent);
     virBufferSetIndent(childBuf_, virBufferGetIndent(parentBuf_, false) + 2)

 int virBufferGetIndent(const virBuffer *buf, bool dynamic);
+size_t virBufferGetEffectiveIndent(const virBuffer *buf);

 void virBufferTrim(virBufferPtr buf, const char *trim, int len);
 void virBufferAddStr(virBufferPtr buf, const char *str);
diff --git a/tests/virbuftest.c b/tests/virbuftest.c
index 064b3e96b4..0c806908e1 100644
--- a/tests/virbuftest.c
+++ b/tests/virbuftest.c
@@ -19,7 +19,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
     int ret = 0;

     if (virBufferGetIndent(buf, false) != 0 ||
-        virBufferGetIndent(buf, true) != 0) {
+        virBufferGetEffectiveIndent(buf) != 0) {
         VIR_TEST_DEBUG("Wrong indentation");
         ret = -1;
     }
@@ -29,28 +29,28 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
         ret = -1;
     }
     if (virBufferGetIndent(buf, false) != 3 ||
-        virBufferGetIndent(buf, true) != 3 ||
+        virBufferGetEffectiveIndent(buf) != 3 ||
         virBufferError(buf)) {
         VIR_TEST_DEBUG("Wrong indentation");
         ret = -1;
     }
     virBufferAdjustIndent(buf, -2);
     if (virBufferGetIndent(buf, false) != 1 ||
-        virBufferGetIndent(buf, true) != 1 ||
+        virBufferGetEffectiveIndent(buf) != 1 ||
         virBufferError(buf)) {
         VIR_TEST_DEBUG("Wrong indentation");
         ret = -1;
     }
     virBufferAdjustIndent(buf, -3);
     if (virBufferGetIndent(buf, false) != 0 ||
-        virBufferGetIndent(buf, true) != 0) {
+        virBufferGetEffectiveIndent(buf) != 0) {
         VIR_TEST_DEBUG("Indentation level not truncated");
         ret = -1;
     }
     virBufferAdjustIndent(buf, 3);
     virBufferFreeAndReset(buf);
     if (virBufferGetIndent(buf, false) != 0 ||
-        virBufferGetIndent(buf, true) != 0 ||
+        virBufferGetEffectiveIndent(buf) != 0 ||
         virBufferError(buf)) {
         VIR_TEST_DEBUG("Reset didn't clear indentation");
         ret = -1;
@@ -66,7 +66,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
         ret = -1;
     }
     if (virBufferGetIndent(buf, false) != 2 ||
-        virBufferGetIndent(buf, true) != 0) {
+        virBufferGetEffectiveIndent(buf) != 0) {
         VIR_TEST_DEBUG("Wrong indentation");
         ret = -1;
     }
-- 
2.21.0




More information about the libvir-list mailing list