git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 1/2] add strbuf_set operations

From
Jeremiah Mahler <jmmahler@gmail.com>
Date
Jun 12, 2014, 07:29 UTC
Message-ID
<f4d043b7c1e00f9c967faff39244274fe40fd371.1402557437.git.jmmahler@gmail.com>
In-Reply-To
<cover.1402557437.git.jmmahler@gmail.com>

A common use case with strubfs is to set the buffer to a new value. This must be done in two steps: a reset followed by an add.

  strbuf_reset(buf);
  strbuf_add(buf, new_buf, len);

In cases where the buffer is being built up in steps, these operations make sense and correctly convey what is being performed.

  strbuf_reset(buf);
  strbuf_add(buf, data1, len1);
  strbuf_add(buf, data2, len2);
  strbuf_add(buf, data3, len3);
However, in other cases, it can be confusing and is not very concise.
  strbuf_reset(buf);
  strbuf_add(buf, default, len1);
  if (cond1) {
    strbuf_reset(buf);
    strbuf_add(buf, data2, len2);
  }
  if (cond2) {
    strbuf_reset(buf);
    strbuf_add(buf, data3, len3);
  }

Add strbuf_set operations so that it can be re-written in a clear and concise way.

  strbuf_set(buf, default len1);
  if (cond1) {
    strbuf_set(buf, data2, len2);
  }
  if (cond2) {
    strbuf_set(buf, data3, len3);
  }
Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---
 Documentation/technical/api-strbuf.txt | 18 ++++++++++++++++++
 strbuf.c                               | 21 +++++++++++++++++++++
 strbuf.h                               | 13 +++++++++++++
 3 files changed, 52 insertions(+)
diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt
index f9c06a7..ae9c9cc 100644
--- a/Documentation/technical/api-strbuf.txt
+++ b/Documentation/technical/api-strbuf.txt
@@ -149,6 +149,24 @@ Functions
 	than zero if the first buffer is found, respectively, to be less than,
 	to match, or be greater than the second buffer.
 
+* Setting the buffer
+
+`strbuf_set`::
+
+	Replace content with data of a given length.
+
+`strbuf_setstr`::
+
+	Replace content with data from a NUL-terminated string.
+
+`strbuf_setf`::
+
+	Replace content with a formatted string.
+
+`strbuf_setbuf`::
+
+	Replace content with data from another buffer.
+
 * Adding data to the buffer
 
 NOTE: All of the functions in this section will grow the buffer as necessary.
diff --git a/strbuf.c b/strbuf.c
index ac62982..9d64b00 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -189,6 +189,27 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
 	strbuf_setlen(sb, sb->len + dlen - len);
 }
 
+void strbuf_set(struct strbuf *sb, const void *data, size_t len)
+{
+	strbuf_reset(sb);
+	strbuf_add(sb, data, len);
+}
+
+void strbuf_setf(struct strbuf *sb, const char *fmt, ...)
+{
+	va_list ap;
+	strbuf_reset(sb);
+	va_start(ap, fmt);
+	strbuf_vaddf(sb, fmt, ap);
+	va_end(ap);
+}
+
+void strbuf_setbuf(struct strbuf *sb, const struct strbuf *sb2)
+{
+	strbuf_reset(sb);
+	strbuf_add(sb, sb2->buf, sb2->len);
+}
+
 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
 {
 	strbuf_splice(sb, pos, 0, data, len);
diff --git a/strbuf.h b/strbuf.h
index e9ad03e..5041c35 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -101,6 +101,19 @@ static inline struct strbuf **strbuf_split(const struct strbuf *sb,
  */
 extern void strbuf_list_free(struct strbuf **);
 
+/*----- set buffer to data -----*/
+extern void strbuf_set(struct strbuf *sb, const void *data, size_t len);
+
+static inline void strbuf_setstr(struct strbuf *sb, const char *s)
+{
+	strbuf_set(sb, s, strlen(s));
+}
+
+__attribute__((format (printf,2,3)))
+extern void strbuf_setf(struct strbuf *sb, const char *fmt, ...);
+
+extern void strbuf_setbuf(struct strbuf *sb, const struct strbuf *sb2);
+
 /*----- add data in your buffer -----*/
 static inline void strbuf_addch(struct strbuf *sb, int c)
 {
-- 
2.0.0
Previous: Jeremiah MahlerNext: Thomas Braun
Message 2 of 16 in “add strbuf_set operations”
  1. 0/2 add strbuf_set operationsJeremiah Mahler, Jun 12, 2014
  2. 1/2 add strbuf_set operationsJeremiah Mahler, Jun 12, 2014
  3. Thomas BraunJun 12, 2014
  4. Jeremiah MahlerJun 12, 2014
  5. Junio C HamanoJun 12, 2014
  6. Jeremiah MahlerJun 12, 2014
  7. Eric SunshineJun 12, 2014
  8. Jeremiah MahlerJun 12, 2014
  9. Junio C HamanoJun 12, 2014
  10. Jeremiah MahlerJun 12, 2014
  11. Eric SunshineJun 12, 2014
  12. Jeremiah MahlerJun 12, 2014
  13. Jeff KingJun 13, 2014
  14. Jeremiah MahlerJun 14, 2014
  15. 2/2 builtin/remote: improve readability via strbuf_set()Jeremiah Mahler, Jun 12, 2014
  16. Eric SunshineJun 12, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.