Skip to content

Commit

Permalink
strbuf: add strbuf_addbuf_percentquote
Browse files Browse the repository at this point in the history
This is handy for creating strings which will be fed to printf() or
strbuf_expand().

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Jan 14, 2010
1 parent 0a0416a commit 361df5d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Documentation/technical/api-strbuf.txt
Expand Up @@ -218,6 +218,13 @@ which can be used by the programmer of the callback as she sees fit.
placeholder and replacement string. The array needs to be
terminated by an entry with placeholder set to NULL.

`strbuf_addbuf_percentquote`::

Append the contents of one strbuf to another, quoting any
percent signs ("%") into double-percents ("%%") in the
destination. This is useful for literal data to be fed to either
strbuf_expand or to the *printf family of functions.

`strbuf_addf`::

Add a formatted string to the buffer.
Expand Down
11 changes: 11 additions & 0 deletions strbuf.c
Expand Up @@ -257,6 +257,17 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
return 0;
}

void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
{
int i, len = src->len;

for (i = 0; i < len; i++) {
if (src->buf[i] == '%')
strbuf_addch(dst, '%');
strbuf_addch(dst, src->buf[i]);
}
}

size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
{
size_t res;
Expand Down
1 change: 1 addition & 0 deletions strbuf.h
Expand Up @@ -116,6 +116,7 @@ struct strbuf_expand_dict_entry {
const char *value;
};
extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);

__attribute__((format (printf,2,3)))
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
Expand Down

0 comments on commit 361df5d

Please sign in to comment.