[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