strbuf: add strbuf_addbuf_percentquote
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>
Jeff King committed
Jan 13, 2010 at 12:36 UTC
361df5df77255321b2ca409d892b4c24b7b0441d
3 files changed
+19
Documentation/technical/api-strbuf.txt
+7
@@ -218,6 +218,13 @@ which can be used by the programmer of the callback as she sees fit.
218
placeholder and replacement string. The array needs to be
219
terminated by an entry with placeholder set to NULL.
220
221
+`strbuf_addbuf_percentquote`::
222
+
223
+ Append the contents of one strbuf to another, quoting any
224
+ percent signs ("%") into double-percents ("%%") in the
225
+ destination. This is useful for literal data to be fed to either
226
+ strbuf_expand or to the *printf family of functions.
227
+
228
`strbuf_addf`::
229
230
Add a formatted string to the buffer.
strbuf.c
+11
@@ -257,6 +257,17 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
257
return 0;
258
}
259
260
+void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
261
+{
262
+ int i, len = src->len;
263
+
264
+ for (i = 0; i < len; i++) {
265
+ if (src->buf[i] == '%')
266
+ strbuf_addch(dst, '%');
267
+ strbuf_addch(dst, src->buf[i]);
268
+ }
269
+}
270
+
271
size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
272
{
273
size_t res;
strbuf.h
+1
@@ -116,6 +116,7 @@ struct strbuf_expand_dict_entry {
116
const char *value;
117
};
118
extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
119
+extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
120
121
__attribute__((format (printf,2,3)))
122
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);