From: ZheNing Hu <adlternative@gmail.com>
To: git@vger.kernel.org
Cc: ZheNing Hu <adlternative@gmail.com>
Subject: [RFC PATCH v1 1/1] strbuf.c/h: add the constant version initialization method of strbuf
Date: Tue, 5 Jan 2021 14:45:02 +0800 [thread overview]
Message-ID: <20210105064502.725307-2-adlternative@gmail.com> (raw)
In-Reply-To: <20210105064502.725307-1-adlternative@gmail.com>
Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
strbuf.c | 33 +++++++++++++++++++++++++++++----
strbuf.h | 8 ++++++++
2 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index e3397cc4c7..6e1fd2e628 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -58,17 +58,32 @@ void strbuf_init(struct strbuf *sb, size_t hint)
strbuf_grow(sb, hint);
}
+void strbuf_const_to_no_const(struct strbuf *sb)
+{
+ if (sb->len && !sb->alloc) {
+ char *new_buf = xstrdup(sb->buf);
+ int len = sb->len;
+ strbuf_init(sb, sb->len);
+ sb->buf = new_buf;
+ sb->len = len;
+ sb->buf[sb->len] = '\0';
+ }
+}
void strbuf_release(struct strbuf *sb)
{
if (sb->alloc) {
free(sb->buf);
strbuf_init(sb, 0);
- }
+ }else if(sb->len)
+ strbuf_init(sb, 0);
}
char *strbuf_detach(struct strbuf *sb, size_t *sz)
{
char *res;
+ if (sb->len && !sb->alloc)
+ die("you should not use detach in a const_strbuf");
+
strbuf_grow(sb, 0);
res = sb->buf;
if (sz)
@@ -89,7 +104,9 @@ void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
void strbuf_grow(struct strbuf *sb, size_t extra)
{
- int new_buf = !sb->alloc;
+ int new_buf;
+ strbuf_const_to_no_const(sb);
+ new_buf = !sb->alloc;
if (unsigned_add_overflows(extra, 1) ||
unsigned_add_overflows(sb->len, extra + 1))
die("you want to use way too much memory");
@@ -108,6 +125,7 @@ void strbuf_trim(struct strbuf *sb)
void strbuf_rtrim(struct strbuf *sb)
{
+ strbuf_const_to_no_const(sb);
while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
sb->len--;
sb->buf[sb->len] = '\0';
@@ -115,6 +133,7 @@ void strbuf_rtrim(struct strbuf *sb)
void strbuf_trim_trailing_dir_sep(struct strbuf *sb)
{
+ strbuf_const_to_no_const(sb);
while (sb->len > 0 && is_dir_sep((unsigned char)sb->buf[sb->len - 1]))
sb->len--;
sb->buf[sb->len] = '\0';
@@ -122,6 +141,7 @@ void strbuf_trim_trailing_dir_sep(struct strbuf *sb)
void strbuf_trim_trailing_newline(struct strbuf *sb)
{
+ strbuf_const_to_no_const(sb);
if (sb->len > 0 && sb->buf[sb->len - 1] == '\n') {
if (--sb->len > 0 && sb->buf[sb->len - 1] == '\r')
--sb->len;
@@ -131,7 +151,9 @@ void strbuf_trim_trailing_newline(struct strbuf *sb)
void strbuf_ltrim(struct strbuf *sb)
{
- char *b = sb->buf;
+ char *b;
+ strbuf_const_to_no_const(sb);
+ b = sb->buf;
while (sb->len > 0 && isspace(*b)) {
b++;
sb->len--;
@@ -158,7 +180,9 @@ int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
void strbuf_tolower(struct strbuf *sb)
{
- char *p = sb->buf, *end = sb->buf + sb->len;
+ char *p,*end;
+ strbuf_const_to_no_const(sb);
+ p = sb->buf, end = sb->buf + sb->len;
for (; p < end; p++)
*p = tolower(*p);
}
@@ -234,6 +258,7 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
die("`pos' is too far after the end of the buffer");
if (pos + len > sb->len)
die("`pos + len' is too far after the end of the buffer");
+ strbuf_const_to_no_const(sb);
if (dlen >= len)
strbuf_grow(sb, dlen - len);
diff --git a/strbuf.h b/strbuf.h
index 223ee2094a..0bfab0177d 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -72,6 +72,13 @@ struct strbuf {
extern char strbuf_slopbuf[];
#define STRBUF_INIT { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
+#define STRBUF_INIT_CONST(str) { .alloc = 0, .len = strlen(str), .buf = str }
+
+/*
+ * Through this function, we can turn a constant buffer into a non-constant buffer
+ */
+void strbuf_const_to_no_const(struct strbuf *sb);
+
/*
* Predeclare this here, since cache.h includes this file before it defines the
* struct.
@@ -159,6 +166,7 @@ void strbuf_grow(struct strbuf *sb, size_t amount);
*/
static inline void strbuf_setlen(struct strbuf *sb, size_t len)
{
+ strbuf_const_to_no_const(sb);
if (len > (sb->alloc ? sb->alloc - 1 : 0))
die("BUG: strbuf_setlen() beyond buffer");
sb->len = len;
--
2.30.0
next prev parent reply other threads:[~2021-01-05 6:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-05 6:45 [RFC PATCH v1 0/1] strbuf.c/h: add the constant version initialization method of strbuf ZheNing Hu
2021-01-05 6:45 ` ZheNing Hu [this message]
2021-01-07 7:03 ` [RFC PATCH v1 1/1] " Eric Sunshine
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210105064502.725307-2-adlternative@gmail.com \
--to=adlternative@gmail.com \
--cc=git@vger.kernel.org \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).