Skip to content

Commit 9b864e7

Browse files
René Scharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6fc4a7e commit 9b864e7

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Documentation/technical/api-strbuf.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ In order to facilitate caching and to make it possible to give
205205
parameters to the callback, `strbuf_expand()` passes a context pointer,
206206
which can be used by the programmer of the callback as she sees fit.
207207

208+
`strbuf_expand_dict_cb`::
209+
210+
Used as callback for `strbuf_expand()`, expects an array of
211+
struct strbuf_expand_dict_entry as context, i.e. pairs of
212+
placeholder and replacement string. The array needs to be
213+
terminated by an entry with placeholder set to NULL.
214+
208215
`strbuf_addf`::
209216

210217
Add a formatted string to the buffer.

strbuf.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@ void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
237237
}
238238
}
239239

240+
size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
241+
void *context)
242+
{
243+
struct strbuf_expand_dict_entry *e = context;
244+
size_t len;
245+
246+
for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
247+
if (!strncmp(placeholder, e->placeholder, len)) {
248+
if (e->value)
249+
strbuf_addstr(sb, e->value);
250+
return len;
251+
}
252+
}
253+
return 0;
254+
}
255+
240256
size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
241257
{
242258
size_t res;

strbuf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
111111

112112
typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
113113
extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
114+
struct strbuf_expand_dict_entry {
115+
const char *placeholder;
116+
const char *value;
117+
};
118+
extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
114119

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

0 commit comments

Comments
 (0)