[Concept,02/18] test: Add a way to printf() into a membuf

Message ID 20251010034255.1099728-3-sjg@u-boot.org
State New
Headers
Series expo: Extend the boot menu |

Commit Message

Simon Glass Oct. 10, 2025, 3:42 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

Add a membuf_printf() function which supports writing a formatted string
into a membuf.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/membuf.h  | 13 +++++++++++++
 lib/membuf.c      | 14 ++++++++++++++
 test/lib/membuf.c | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
  

Patch

diff --git a/include/membuf.h b/include/membuf.h
index 3352faa0606..17fe721ee3c 100644
--- a/include/membuf.h
+++ b/include/membuf.h
@@ -268,4 +268,17 @@  int membuf_new(struct membuf *mb, int size);
  */
 void membuf_dispose(struct membuf *mb);
 
+/**
+ * membuf_printf() - write a formatted string to a membuff
+ *
+ * Formats a string and writes it to the membuff. Returns the number of bytes
+ * written (not including the terminating nul).
+ *
+ * @mb: membuff to write to
+ * @fmt: format string
+ * @...: arguments for format string
+ * Return: number of bytes written, or negative error
+ */
+int membuf_printf(struct membuf *mb, const char *fmt, ...);
+
 #endif
diff --git a/lib/membuf.c b/lib/membuf.c
index 47a1b06664a..207dff5625b 100644
--- a/lib/membuf.c
+++ b/lib/membuf.c
@@ -9,6 +9,7 @@ 
 #include <errno.h>
 #include <log.h>
 #include <malloc.h>
+#include <vsprintf.h>
 #include "membuf.h"
 
 static inline bool is_full(const struct membuf *mb)
@@ -435,3 +436,16 @@  void membuf_dispose(struct membuf *mb)
 	free(mb->start);
 	membuf_uninit(mb);
 }
+
+int membuf_printf(struct membuf *mb, const char *fmt, ...)
+{
+	char buf[256];
+	va_list args;
+	int len;
+
+	va_start(args, fmt);
+	len = vsnprintf(buf, sizeof(buf), fmt, args);
+	va_end(args);
+
+	return membuf_put(mb, buf, len);
+}
diff --git a/test/lib/membuf.c b/test/lib/membuf.c
index 2e7de9cdc57..7ae9c079a4c 100644
--- a/test/lib/membuf.c
+++ b/test/lib/membuf.c
@@ -261,3 +261,43 @@  static int lib_test_membuf_init(struct unit_test_state *uts)
 	return 0;
 }
 LIB_TEST(lib_test_membuf_init, 0);
+
+/* test membuf_printf() */
+static int lib_test_membuf_printf(struct unit_test_state *uts)
+{
+	struct membuf mb;
+	int ret, exp_len;
+	char buf[100];
+	char out[100];
+
+	/* Initialize membuf with a buffer */
+	membuf_init(&mb, buf, sizeof(buf));
+
+	/* Test simple string */
+	ret = membuf_printf(&mb, "Hello");
+	ut_asserteq(5, ret);
+	ut_asserteq(5, membuf_get(&mb, out, sizeof(out)));
+	out[5] = '\0';
+	ut_asserteq_str("Hello", out);
+
+	/* Test formatted string with integers */
+	exp_len = 9;
+	membuf_purge(&mb);
+	ret = membuf_printf(&mb, "Value: %d", 42);
+	ut_asserteq(exp_len, ret);
+	ut_asserteq(exp_len, membuf_get(&mb, out, sizeof(out)));
+	out[exp_len] = '\0';
+	ut_asserteq_str("Value: 42", out);
+
+	/* Test formatted string with multiple arguments */
+	membuf_purge(&mb);
+	exp_len = 10;
+	ret = membuf_printf(&mb, "x=%d y=%d", 10, 200);
+	ut_asserteq(exp_len, ret);
+	ut_asserteq(exp_len, membuf_get(&mb, out, sizeof(out)));
+	out[exp_len] = '\0';
+	ut_asserteq_str("x=10 y=200", out);
+
+	return 0;
+}
+LIB_TEST(lib_test_membuf_printf, 0);