diff options
author | René Scharfe <rene.scharfe@lsrfire.ath.cx> | 2008-11-23 00:09:30 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2008-11-23 19:55:47 -0800 |
commit | 9b864e730bda0e028a85387fa568429724582f22 (patch) | |
tree | d7c43e556e2e2d7955a39c83bcb9e5e4d147ef0e /strbuf.c | |
parent | 6fc4a7e546d5e2b0ce545f73b5c1829887db2462 (diff) | |
download | git-9b864e730bda0e028a85387fa568429724582f22.tar.gz |
add strbuf_expand_dict_cb(), a helper for simple cases
The new callback function strbuf_expand_dict_cb() can be used together
with strbuf_expand() if there is only a small number of placeholders
for static replacement texts. It expects its dictionary as an array of
placeholder+value pairs as context parameter, terminated by an entry
with the placeholder member set to NULL.
The new helper is intended to aid converting the remaining calls of
interpolate(). strbuf_expand() is smaller, more flexible and can be
used to go faster than interpolate(), so it should replace the latter.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -237,6 +237,22 @@ void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, } } +size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, + void *context) +{ + struct strbuf_expand_dict_entry *e = context; + size_t len; + + for (; e->placeholder && (len = strlen(e->placeholder)); e++) { + if (!strncmp(placeholder, e->placeholder, len)) { + if (e->value) + strbuf_addstr(sb, e->value); + return len; + } + } + return 0; +} + size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) { size_t res; |