Skip to content

Commit

Permalink
add strbuf_expand_dict_cb(), a helper for simple cases
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Nov 24, 2008
1 parent 6fc4a7e commit 9b864e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Documentation/technical/api-strbuf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ In order to facilitate caching and to make it possible to give
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.
Expand Down
16 changes: 16 additions & 0 deletions strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);

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, ...);
Expand Down

0 comments on commit 9b864e7

Please sign in to comment.