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

[GSoC][RFC][PATCH 1/2] STRBUF_INIT_CONST: a new way to initialize strbuf

From
Robear Selwans <rwagih.rw@gmail.com>
Date
Feb 18, 2020, 04:18 UTC
Message-ID
<20200218041805.10939-2-robear.selwans@outlook.com>
In-Reply-To
<20200218041805.10939-1-robear.selwans@outlook.com>

A new function `STRBUF_INIT_CONST(const_str)` was added to allow for a quick initialization of strbuf.

Details: Using `STRBUF_INIT_CONST(str)` creates a new struct of type `strbuf` and initializes its `buf`, `len` and `alloc` as `str`, `strlen(str)` and `0`, respectively.

Use Case: This is meant to be used to initialize strbufs with constant values and thus, only allocating memory when needed.

Usage Example: ``` strbuf env_var = STRBUF_INIT_CONST("dev"); ```

This was added according to the issue opened at [https://github.com/gitgitgadget/git/issues/398]
Signed-off-by: Robear Selwans <robear.selwans@outlook.com>
---
 strbuf.h | 1 +
 1 file changed, 1 insertion(+)
diff --git a/strbuf.h b/strbuf.h
index bfa66569a4..1a1753424c 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -71,6 +71,7 @@ struct strbuf {
 
 extern char strbuf_slopbuf[];
 #define STRBUF_INIT  { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
+#define STRBUF_INIT_CONST(const_str)  { .alloc = 0, .len = strlen(const_str), .buf = const_str }
 
 /*
  * Predeclare this here, since cache.h includes this file before it defines the
-- 
2.17.1
Previous: Robear SelwansNext: Jeff King
Message 2 of 11 in “STRBUF_INIT_CONST Cover”
  1. Robear SelwansFeb 18, 2020
  2. [GSoC][RFC][PATCH 1/2] STRBUF_INIT_CONST: a new way to initialize strbufRobear Selwans, Feb 18, 2020
  3. Jeff KingFeb 18, 2020
  4. Robear SelwansFeb 18, 2020
  5. Jeff KingFeb 18, 2020
  6. Johannes SixtFeb 19, 2020
  7. René ScharfeFeb 20, 2020
  8. Robear SelwansFeb 21, 2020
  9. Derrick StoleeFeb 27, 2020
  10. René ScharfeFeb 27, 2020
  11. [GSoC][RFC][PATCH 2/2] STRBUF_INIT_CONST: Adapting strbuf_* functionsRobear Selwans, Feb 18, 2020

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.