]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`
authorPaul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
Mon, 25 Feb 2019 23:16:07 +0000 (23:16 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Feb 2019 23:03:46 +0000 (08:03 +0900)
Implement `strbuf_insertf()` and `strbuf_vinsertf()` to
insert data using a printf format string.

Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
Helped-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
strbuf.c
strbuf.h

index 82e90f1dfe92d731d143f713f8ea02b79b01da9d..87ecf7f975ce4f2a26fa3829841059b81e331981 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -249,6 +249,42 @@ void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
        strbuf_splice(sb, pos, 0, data, len);
 }
 
+void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)
+{
+       int len, len2;
+       char save;
+       va_list cp;
+
+       if (pos > sb->len)
+               die("`pos' is too far after the end of the buffer");
+       va_copy(cp, ap);
+       len = vsnprintf(sb->buf + sb->len, 0, fmt, cp);
+       va_end(cp);
+       if (len < 0)
+               BUG("your vsnprintf is broken (returned %d)", len);
+       if (!len)
+               return; /* nothing to do */
+       if (unsigned_add_overflows(sb->len, len))
+               die("you want to use way too much memory");
+       strbuf_grow(sb, len);
+       memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
+       /* vsnprintf() will append a NUL, overwriting one of our characters */
+       save = sb->buf[pos + len];
+       len2 = vsnprintf(sb->buf + pos, len + 1, fmt, ap);
+       sb->buf[pos + len] = save;
+       if (len2 != len)
+               BUG("your vsnprintf is broken (returns inconsistent lengths)");
+       strbuf_setlen(sb, sb->len + len);
+}
+
+void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       strbuf_vinsertf(sb, pos, fmt, ap);
+       va_end(ap);
+}
+
 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
 {
        strbuf_splice(sb, pos, len, "", 0);
index be02150df393a7741096babb53fd390e1d3cc1fe..8f8fe01e68dc868f4154c8b733f6a86b2df9c4bd 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -244,6 +244,15 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);
  */
 void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);
 
+/**
+ * Insert data to the given position of the buffer giving a printf format
+ * string. The contents will be shifted, not overwritten.
+ */
+void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt,
+                    va_list ap);
+
+void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...);
+
 /**
  * Remove given amount of data from a given position of the buffer.
  */