summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 6fc4a7e)
raw | patch | inline | side by side (parent: 6fc4a7e)
author | René Scharfe <rene.scharfe@lsrfire.ath.cx> | |
Sat, 22 Nov 2008 23:09:30 +0000 (00:09 +0100) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Mon, 24 Nov 2008 03:55:47 +0000 (19:55 -0800) |
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>
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>
Documentation/technical/api-strbuf.txt | patch | blob | history | |
strbuf.c | patch | blob | history | |
strbuf.h | patch | blob | history |
index a9668e5f2d2b1a7ffac45e4111ca6d8a4818af2b..a8ee2fe6a1504b943ff9c3c51807bf0f839182b1 100644 (file)
parameters to the callback, `strbuf_expand()` passes a context pointer,
which can be used by the programmer of the callback as she sees fit.
+`strbuf_expand_dict_cb`::
+
+ Used as callback for `strbuf_expand()`, expects an array of
+ struct strbuf_expand_dict_entry as context, i.e. pairs of
+ placeholder and replacement string. The array needs to be
+ terminated by an entry with placeholder set to NULL.
+
`strbuf_addf`::
Add a formatted string to the buffer.
diff --git a/strbuf.c b/strbuf.c
index 720737d856b694bc5239f0c18af372959c99e744..13be67e4d3e38472adbff019a34e3f5ce9621dd7 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
}
}
+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;
diff --git a/strbuf.h b/strbuf.h
index eba7ba423a2d3a383ef93f663c95695438269edf..b1670d99456e96b5de01918e157e10df519cae13 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
+struct strbuf_expand_dict_entry {
+ const char *placeholder;
+ const char *value;
+};
+extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
__attribute__((format(printf,2,3)))
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);