From: Robear Selwans <rwagih.rw@gmail.com>
To: git@vger.kernel.org
Cc: "brian m. carlson" <sandals@crustytoothpaste.net>,
Jeff King <peff@peff.net>, Junio C Hamano <gitster@pobox.com>
Subject: [GSoC][RFC][PATCH 1/2] STRBUF_INIT_CONST: a new way to initialize strbuf
Date: Tue, 18 Feb 2020 04:18:04 +0000 [thread overview]
Message-ID: <20200218041805.10939-2-robear.selwans@outlook.com> (raw)
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
next prev parent reply other threads:[~2020-02-18 4:18 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-18 4:18 [GSoC][RFC][PATCH 0/2] STRBUF_INIT_CONST Cover Robear Selwans
2020-02-18 4:18 ` Robear Selwans [this message]
2020-02-18 6:21 ` [GSoC][RFC][PATCH 1/2] STRBUF_INIT_CONST: a new way to initialize strbuf Jeff King
2020-02-18 14:19 ` Robear Selwans
2020-02-18 20:33 ` Jeff King
2020-02-19 8:13 ` Johannes Sixt
2020-02-20 18:49 ` René Scharfe
2020-02-21 5:21 ` Robear Selwans
2020-02-27 6:50 ` Derrick Stolee
2020-02-27 15:55 ` René Scharfe
2020-02-18 4:18 ` [GSoC][RFC][PATCH 2/2] STRBUF_INIT_CONST: Adapting strbuf_* functions Robear Selwans
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200218041805.10939-2-robear.selwans@outlook.com \
--to=rwagih.rw@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
--cc=sandals@crustytoothpaste.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).