[PATCH 1/2] Add strbuf_initf()

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Johannes Schindelin
Date: Wednesday, March 5, 2008 - 6:14 pm

The most common use of addf() was to init a strbuf and addf() right away.
Since it is so common, it makes sense to have a function strbuf_initf()
to wrap both calls into one.

Unfortunately, C (and cpp) has no way to make this easy without code
duplication, as we need to va_init() in strbuf_addf() possibly a few
times.  So the code for addf() is copied.  Fortunately, the code is
pretty short, so not too much had to be copied as-is.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---

	On Wed, 5 Mar 2008, Johannes Schindelin wrote:

	> On Wed, 5 Mar 2008, Junio C Hamano wrote:
	> 
	> > Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
	> > 
	> > > [... I'd submit my ] a Reviewed-by:, but I think that this 
	> > > would only be a burden on our maintainer.
	> > 
	> > I think Reviewed-by: would indeed be a very good addition to 
	> > our patch flow convention, borrowing from the kernel folks.
	> 
	> You mean you have more people to blame, then? ;-)

	Well, it was meant as a joke...

	Anyway, here is a start of a patch series that should help the 
	King Penguin...

	strbuf_initf() is something I long planned to do; Kristian just 
	pushed me over the edge.

 strbuf.c |   23 +++++++++++++++++++++++
 strbuf.h |    2 ++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index 5afa8f3..067d55a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -147,6 +147,29 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_setlen(sb, sb->len + len);
 }
 
+void strbuf_initf(struct strbuf *sb, const char *fmt, ...)
+{
+	int len;
+	va_list ap;
+
+	strbuf_init(sb, strlen(fmt) + 64);
+	va_start(ap, fmt);
+	len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
+	va_end(ap);
+	if (len < 0)
+		die("your vsnprintf is broken");
+	if (len > strbuf_avail(sb)) {
+		strbuf_grow(sb, len);
+		va_start(ap, fmt);
+		len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
+		va_end(ap);
+		if (len > strbuf_avail(sb)) {
+			die("this should not happen, your snprintf is broken");
+		}
+	}
+	strbuf_setlen(sb, sb->len + len);
+}
+
 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
 		   void *context)
 {
diff --git a/strbuf.h b/strbuf.h
index faec229..eaf2409 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -108,6 +108,8 @@ extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
 
 __attribute__((format(printf,2,3)))
 extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format(printf,2,3)))
+extern void strbuf_initf(struct strbuf *sb, const char *fmt, ...);
 
 extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 /* XXX: if read fails, any partial read is undone */
-- 
1.5.4.3.571.g9aec3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00/40] MinGW port, Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 02/40] Compile some programs only conditionally., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 03/40] Add target architecture MinGW., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 09/40] Windows: Work around misbehaved rename()., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 10/40] Windows: Treat Windows style path names., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 12/40] Windows: Implement gettimeofday()., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 13/40] Windows: Fix PRIuMAX definition., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 18/40] Windows: Implement start_command()., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 20/40] Windows: A rudimentary poll() emulation., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 27/40] Windows: Implement a custom spawnve()., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 30/40] Turn builtin_exec_path into a function., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 34/40] Windows: Make the pager work., Johannes Sixt, (Wed Feb 27, 11:54 am)
[PATCH 37/40] Windows: Make 'git help -a' work., Johannes Sixt, (Wed Feb 27, 11:55 am)
Re: [PATCH 00/40] MinGW port, Marius Storm-Olsen, (Wed Feb 27, 3:01 pm)
Re: [PATCH 00/40] MinGW port, Martin Langhoff, (Wed Feb 27, 4:34 pm)
Re: [PATCH 01/40] Add compat/regex.[ch] and compat/fnmatch ..., Johannes Schindelin, (Wed Feb 27, 4:43 pm)
Re: [PATCH 00/40] MinGW port, Johannes Schindelin, (Wed Feb 27, 4:58 pm)
Re: [PATCH 00/40] MinGW port, Nguyen Thai Ngoc Duy, (Wed Feb 27, 8:38 pm)
Re: [PATCH 37/40] Windows: Make 'git help -a' work., Paolo Bonzini, (Thu Feb 28, 2:52 am)
Re: [PATCH 02/40] Compile some programs only conditionally., Johannes Schindelin, (Thu Feb 28, 4:57 am)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Thu Feb 28, 5:05 am)
Re: [PATCH 08/40] Windows: always chmod(, 0666) before unl ..., Johannes Schindelin, (Thu Feb 28, 5:09 am)
Re: [PATCH 10/40] Windows: Treat Windows style path names., Johannes Schindelin, (Thu Feb 28, 5:18 am)
Re: [PATCH 13/40] Windows: Fix PRIuMAX definition., Johannes Schindelin, (Thu Feb 28, 5:21 am)
Re: [PATCH 03/40] Add target architecture MinGW., Paolo Bonzini, (Thu Feb 28, 5:57 am)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Thu Feb 28, 7:56 am)
Re: [PATCH 19/40] Windows: Change the name of hook scripts ..., Johannes Schindelin, (Thu Feb 28, 8:20 am)
Re: [PATCH 21/40] Windows: Disambiguate DOS style paths fr ..., Johannes Schindelin, (Thu Feb 28, 8:22 am)
Re: [PATCH 22/40] Windows: Implement asynchronous function ..., Johannes Schindelin, (Thu Feb 28, 8:28 am)
Re: [PATCH 23/40] Windows: Local clone must use the drive ..., Johannes Schindelin, (Thu Feb 28, 8:31 am)
Re: [PATCH 27/40] Windows: Implement a custom spawnve()., Johannes Schindelin, (Thu Feb 28, 8:36 am)
Re: [PATCH 33/40] When installing, be prepared that templa ..., Johannes Schindelin, (Thu Feb 28, 8:45 am)
Re: [PATCH 40/40] compat/pread.c: Add foward decl to fix w ..., Johannes Schindelin, (Thu Feb 28, 8:51 am)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Sixt, (Thu Feb 28, 1:40 pm)
Re: [PATCH 13/40] Windows: Fix PRIuMAX definition., Johannes Sixt, (Thu Feb 28, 1:45 pm)
Re: [PATCH 27/40] Windows: Implement a custom spawnve()., Johannes Sixt, (Thu Feb 28, 2:04 pm)
Re: [PATCH 02/40] Compile some programs only conditionally., Johannes Schindelin, (Thu Feb 28, 5:47 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Thu Feb 28, 6:07 pm)
Re: [PATCH 04/40] Windows: Use the Windows style PATH sepa ..., Johannes Schindelin, (Thu Feb 28, 6:09 pm)
Re: [PATCH 19/40] Windows: Change the name of hook scripts ..., Johannes Schindelin, (Thu Feb 28, 6:11 pm)
Re: [PATCH 22/40] Windows: Implement asynchronous function ..., Johannes Schindelin, (Thu Feb 28, 6:17 pm)
Re: [PATCH 27/40] Windows: Implement a custom spawnve()., Johannes Schindelin, (Thu Feb 28, 6:18 pm)
Re: [PATCH 33/40] When installing, be prepared that templa ..., Johannes Schindelin, (Thu Feb 28, 6:21 pm)
Re: [PATCH 22/40] Windows: Implement asynchronous function ..., Johannes Schindelin, (Thu Feb 28, 6:27 pm)
Re: [PATCH 22/40] Windows: Implement asynchronous function ..., Johannes Schindelin, (Thu Feb 28, 6:54 pm)
Re: [PATCH 22/40] Windows: Implement asynchronous function ..., Johannes Schindelin, (Fri Feb 29, 3:26 am)
Re: [PATCH 04/40] Windows: Use the Windows style PATH sepa ..., Johannes Schindelin, (Fri Feb 29, 5:19 am)
Re: [PATCH 04/40] Windows: Use the Windows style PATH sepa ..., Johannes Schindelin, (Fri Feb 29, 5:59 am)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Sixt, (Fri Feb 29, 2:03 pm)
Re: [PATCH 02/40] Compile some programs only conditionally., Johannes Schindelin, (Fri Feb 29, 2:53 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Fri Feb 29, 2:54 pm)
Re: [PATCH 20/40] Windows: A rudimentary poll() emulation., Robin Rosenberg, (Sat Mar 1, 8:48 am)
Re: [PATCH 00/40] MinGW port, Johannes Sixt, (Sun Mar 2, 2:20 pm)
Re: [PATCH 00/40] MinGW port, Johannes Schindelin, (Sun Mar 2, 3:07 pm)
Re: [PATCH 00/40] MinGW port, Johannes Sixt, (Mon Mar 3, 11:34 am)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Sixt, (Wed Mar 5, 2:21 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Wed Mar 5, 3:18 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Junio C Hamano, (Wed Mar 5, 3:22 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Wed Mar 5, 3:28 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Junio C Hamano, (Wed Mar 5, 3:51 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Wed Mar 5, 5:11 pm)
[PATCH 1/2] Add strbuf_initf(), Johannes Schindelin, (Wed Mar 5, 6:14 pm)
[PATCH 2/2] format-patch: add --reviewed-by=&lt;ident&gt;, Johannes Schindelin, (Wed Mar 5, 6:15 pm)
Re: [PATCH 1/2] Add strbuf_initf(), Mike Hommey, (Wed Mar 5, 11:33 pm)
Re: [PATCH 1/2] Add strbuf_initf(), Reece Dunn, (Thu Mar 6, 2:03 am)
Re: [PATCH 2/2] format-patch: add --reviewed-by=&lt;ident&gt;, Johannes Schindelin, (Thu Mar 6, 3:40 am)
Re: [PATCH 1/2] Add strbuf_initf(), Johannes Schindelin, (Thu Mar 6, 3:53 am)
Re: [PATCH 1/2] Add strbuf_initf(), Johannes Schindelin, (Thu Mar 6, 3:55 am)
Re: [PATCH 1/2] Add strbuf_initf(), Reece Dunn, (Thu Mar 6, 4:53 am)
Re: [PATCH 1/2] Add strbuf_initf(), Jeff King, (Thu Mar 6, 5:09 am)
Re: [PATCH 1/2] Add strbuf_initf(), Johannes Schindelin, (Thu Mar 6, 5:52 am)
[PATCH 1/2 v2] Add strbuf_vaddf(), use it in strbuf_addf() ..., Johannes Schindelin, (Thu Mar 6, 9:29 am)
Re: [PATCH 1/2 v2] Add strbuf_vaddf(), use it in strbuf_ad ..., Johannes Schindelin, (Thu Mar 6, 9:59 am)
Re: [PATCH 1/2] Add strbuf_initf(), Kristian , (Thu Mar 6, 11:18 am)
Re: [PATCH 1/2] Add strbuf_initf(), Johannes Schindelin, (Thu Mar 6, 11:26 am)
Re: [PATCH 1/2] Add strbuf_initf(), Kristian , (Thu Mar 6, 11:35 am)
Re: [PATCH 1/2] Add strbuf_initf(), Mike Hommey, (Thu Mar 6, 12:10 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Sixt, (Thu Mar 6, 1:38 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Sixt, (Tue Mar 11, 2:30 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Tue Mar 11, 4:28 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Sixt, (Wed Mar 12, 3:59 pm)
Re: [PATCH 03/40] Add target architecture MinGW., Johannes Schindelin, (Wed Mar 12, 4:06 pm)