# [PATCH] commit: use strbuf_add() to add a length-limited string

3 messages from 2019-12-07 to 2019-12-09. Participants: René Scharfe, Derrick Stolee, Junio C Hamano.
Thread: https://gitlist.dev/t/52400

## René Scharfe, 2019-12-07 11:16

Subject: [PATCH] commit: use strbuf_add() to add a length-limited string
Message-ID: <f4c7d65f-d7bb-c574-fe48-8d2df0c2907a@web.de>
URL: https://gitlist.dev/e/f4c7d65f-d7bb-c574-fe48-8d2df0c2907a%40web.de

```
This is shorter and simpler.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 builtin/commit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 294dc574cd..35a1fb9ad6 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -537,7 +537,7 @@ static void export_one(const char *var, const char *s, const char *e, int hack)
 	struct strbuf buf = STRBUF_INIT;
 	if (hack)
 		strbuf_addch(&buf, hack);
-	strbuf_addf(&buf, "%.*s", (int)(e - s), s);
+	strbuf_add(&buf, s, e - s);
 	setenv(var, buf.buf, 1);
 	strbuf_release(&buf);
 }
--
2.24.0

```

## Derrick Stolee, 2019-12-07 20:17

Subject: Re: [PATCH] commit: use strbuf_add() to add a length-limited string
Message-ID: <5bc19b91-7579-357f-e241-32d3a763d9c5@gmail.com>
URL: https://gitlist.dev/e/5bc19b91-7579-357f-e241-32d3a763d9c5%40gmail.com
In-Reply-To: <f4c7d65f-d7bb-c574-fe48-8d2df0c2907a@web.de>

```
On 12/7/2019 6:16 AM, René Scharfe wrote:
> This is shorter and simpler.
> 
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  builtin/commit.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/builtin/commit.c b/builtin/commit.c
> index 294dc574cd..35a1fb9ad6 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -537,7 +537,7 @@ static void export_one(const char *var, const char *s, const char *e, int hack)
>  	struct strbuf buf = STRBUF_INIT;
>  	if (hack)
>  		strbuf_addch(&buf, hack);
> -	strbuf_addf(&buf, "%.*s", (int)(e - s), s);
> +	strbuf_add(&buf, s, e - s);

I had to look up what "%.*s" does [1, 2] but this looks like
a behavior-equivalent (and possibly faster) way to do it.

-Stolee

[1] https://stackoverflow.com/a/49580164
[2] https://en.cppreference.com/w/c/io/fprintf



```

## Junio C Hamano, 2019-12-09 19:24

Subject: Re: [PATCH] commit: use strbuf_add() to add a length-limited string
Message-ID: <xmqqh829uwsu.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqh829uwsu.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <f4c7d65f-d7bb-c574-fe48-8d2df0c2907a@web.de>

```
René Scharfe <l.s.r@web.de> writes:

> This is shorter and simpler.
>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---

Ack (this was from me abck in 2012).

>  builtin/commit.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/builtin/commit.c b/builtin/commit.c
> index 294dc574cd..35a1fb9ad6 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -537,7 +537,7 @@ static void export_one(const char *var, const char *s, const char *e, int hack)
>  	struct strbuf buf = STRBUF_INIT;
>  	if (hack)
>  		strbuf_addch(&buf, hack);
> -	strbuf_addf(&buf, "%.*s", (int)(e - s), s);
> +	strbuf_add(&buf, s, e - s);
>  	setenv(var, buf.buf, 1);
>  	strbuf_release(&buf);
>  }
> --
> 2.24.0

```
