Re: [PATCH 08/11] util: Add util to pad string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 20/06/13 01:11, John Ferlan wrote:
On 06/07/2013 01:03 PM, Osier Yang wrote:
The string can be padded either on the left (@from_right=false) or right
(@from_right=true).
---
  src/libvirt_private.syms |  1 +
  src/util/virstring.c     | 38 ++++++++++++++++++++++++++++++++++++++
  src/util/virstring.h     |  6 ++++++
  tests/utiltest.c         | 28 ++++++++++++++++++++++++++++
  4 files changed, 73 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ce39cc6..27fb0b5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1822,6 +1822,7 @@ virStringArrayHasString;
  virStringFreeList;
  virStringJoin;
  virStringListLength;
+virStringPad;
  virStringSplit;
  virStrncpy;
  virStrndup;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 1937f82..498daab 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -608,3 +608,41 @@ size_t virStringListLength(char **strings)
return i;
  }
+
This needs a description, params, returns, etc. section.  To me padding
usually denotes adding something to the end of a string

I would like to understand the value of "length" (which I believe is a
size_t rather than a unsigned int, right?



I think most importantly - caller is expected to free the returned
buffer, right?


+char *
+virStringPad(const char *src,
+             char padchar,
+             unsigned int length,
+             bool from_right)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    int len;
+    int i;

It seems you're trying to create a virBufferPrependChar() to go along
with the existing virBufferAddChar() or virBufferAddLit().

Essentially you're creating a string buffer of "length - len" filled
with 'padchar' and then prepending or appending it onto the existing
string.

I guess I'm somewhat surprised this hasn't already been done.

In any case, I would have expected something as follows (error
conditions aside):


if APPPEND  (eg, change 1234 into 12340000)
     virBufferAdd(&buf, src);
     for (i = len; i < length; i++)
         virBufferAddChar(&buf, padchar);

Oh, sure, I didn't notify the  virBufferAddChar

Osier

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]