strbuf: add and use strbuf_insertstr()

Add a function for inserting a C string into a strbuf.  Use it
throughout the source to get rid of magic string length constants and
explicit strlen() calls.

Like strbuf_addstr(), implement it as an inline function to avoid the
implicit strlen() calls to cause runtime overhead.

Helped-by: Taylor Blau <me@ttaylorr.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe
2020-02-09 14:44:23 +01:00
committed by Junio C Hamano
parent d0654dc308
commit a91cc7fad0
11 changed files with 27 additions and 15 deletions

View File

@ -244,6 +244,18 @@ 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 a NUL-terminated string to the given position of the buffer.
* The remaining contents will be shifted, not overwritten. It's an
* inline function to allow the compiler to resolve strlen() calls on
* constants at compile time.
*/
static inline void strbuf_insertstr(struct strbuf *sb, size_t pos,
const char *s)
{
strbuf_insert(sb, pos, s, strlen(s));
}
/**
* Insert data to the given position of the buffer giving a printf format
* string. The contents will be shifted, not overwritten.