This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch main
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=4d38360e014ea4dfe02c35d97deb47ac2f7fd35d

commit 4d38360e014ea4dfe02c35d97deb47ac2f7fd35d
Author: Guillem Jover <[email protected]>
AuthorDate: Thu Jun 22 02:42:10 2023 +0200

    libdpkg: Add varbuf setter functions
    
    Add _buf(), _varbuf(), _str() and _strn() setter functions to help
    initialize varbufs.
---
 lib/dpkg/libdpkg.map  |  2 ++
 lib/dpkg/t/t-varbuf.c | 35 ++++++++++++++++++++++++++++++++++-
 lib/dpkg/varbuf.c     | 14 ++++++++++++++
 lib/dpkg/varbuf.h     |  5 +++++
 4 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map
index d874a142d..fa4329bb1 100644
--- a/lib/dpkg/libdpkg.map
+++ b/lib/dpkg/libdpkg.map
@@ -114,6 +114,8 @@ LIBDPKG_PRIVATE {
        varbuf_reset;
        varbuf_grow;
        varbuf_trunc;
+       varbuf_set_varbuf;
+       varbuf_set_buf;
        varbuf_add_varbuf;
        varbuf_add_char;
        varbuf_dup_char;
diff --git a/lib/dpkg/t/t-varbuf.c b/lib/dpkg/t/t-varbuf.c
index 3329e8695..05b066cfc 100644
--- a/lib/dpkg/t/t-varbuf.c
+++ b/lib/dpkg/t/t-varbuf.c
@@ -154,6 +154,38 @@ test_varbuf_trunc(void)
        varbuf_destroy(&vb);
 }
 
+static void
+test_varbuf_set(void)
+{
+       struct varbuf vb, cb;
+
+       varbuf_init(&vb, 10);
+       varbuf_init(&cb, 10);
+
+       varbuf_set_buf(&vb, "1234567890", 5);
+       test_pass(vb.used == 5);
+       test_mem(vb.buf, ==, "12345", 5);
+
+       varbuf_set_buf(&vb, "abcd", 4);
+       test_pass(vb.used == 4);
+       test_mem(vb.buf, ==, "abcd", 4);
+
+       varbuf_set_varbuf(&cb, &vb);
+       test_pass(cb.used == 4);
+       test_mem(cb.buf, ==, "abcd", 4);
+
+       varbuf_set_str(&vb, "12345");
+       test_pass(vb.used == 5);
+       test_str(vb.buf, ==, "12345");
+
+       varbuf_set_strn(&vb, "1234567890", 8);
+       test_pass(vb.used == 8);
+       test_str(vb.buf, ==, "12345678");
+
+       varbuf_destroy(&cb);
+       varbuf_destroy(&vb);
+}
+
 static void
 test_varbuf_add_varbuf(void)
 {
@@ -503,13 +535,14 @@ test_varbuf_detach(void)
 
 TEST_ENTRY(test)
 {
-       test_plan(162);
+       test_plan(172);
 
        test_varbuf_init();
        test_varbuf_prealloc();
        test_varbuf_new();
        test_varbuf_grow();
        test_varbuf_trunc();
+       test_varbuf_set();
        test_varbuf_add_varbuf();
        test_varbuf_add_buf();
        test_varbuf_add_str();
diff --git a/lib/dpkg/varbuf.c b/lib/dpkg/varbuf.c
index 47636d793..f4a581db8 100644
--- a/lib/dpkg/varbuf.c
+++ b/lib/dpkg/varbuf.c
@@ -99,6 +99,20 @@ varbuf_get_str(struct varbuf *v)
        return v->buf;
 }
 
+void
+varbuf_set_buf(struct varbuf *v, const void *buf, size_t size)
+{
+       varbuf_reset(v);
+       varbuf_add_buf(v, buf, size);
+       varbuf_end_str(v);
+}
+
+void
+varbuf_set_varbuf(struct varbuf *v, struct varbuf *other)
+{
+       varbuf_set_buf(v, other->buf, other->used);
+}
+
 void
 varbuf_add_varbuf(struct varbuf *v, const struct varbuf *other)
 {
diff --git a/lib/dpkg/varbuf.h b/lib/dpkg/varbuf.h
index 3fd973e26..b7e5e37f7 100644
--- a/lib/dpkg/varbuf.h
+++ b/lib/dpkg/varbuf.h
@@ -83,6 +83,11 @@ void varbuf_reset(struct varbuf *v);
 void varbuf_destroy(struct varbuf *v);
 void varbuf_free(struct varbuf *v);
 
+void varbuf_set_varbuf(struct varbuf *v, struct varbuf *other);
+void varbuf_set_buf(struct varbuf *v, const void *buf, size_t size);
+#define varbuf_set_str(v, s) varbuf_set_buf(v, s, strlen(s))
+#define varbuf_set_strn(v, s, n) varbuf_set_buf(v, s, strnlen(s, n))
+
 void varbuf_add_varbuf(struct varbuf *v, const struct varbuf *other);
 void varbuf_add_char(struct varbuf *v, int c);
 void varbuf_dup_char(struct varbuf *v, int c, size_t n);

-- 
Dpkg.Org's dpkg

Reply via email to