git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/4] add strbuf_expand_dict_cb(), a helper for simple cases
@ 2008-11-22 23:09 René Scharfe
  2008-11-22 23:13 ` [PATCH 2/4] merge-recursive: use strbuf_expand() instead of interpolate() René Scharfe
  2008-12-04 20:30 ` [PATCH 1/4] add strbuf_expand_dict_cb(), a helper for simple cases Jon Loeliger
  0 siblings, 2 replies; 7+ messages in thread
From: René Scharfe @ 2008-11-22 23:09 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano, Jon Loeliger

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>
---
 Documentation/technical/api-strbuf.txt |    7 +++++++
 strbuf.c                               |   16 ++++++++++++++++
 strbuf.h                               |    5 +++++
 3 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt
index a9668e5..519c63b 100644
--- a/Documentation/technical/api-strbuf.txt
+++ b/Documentation/technical/api-strbuf.txt
@@ -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.
diff --git a/strbuf.c b/strbuf.c
index 720737d..860748f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -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;
diff --git a/strbuf.h b/strbuf.h
index eba7ba4..b1670d9 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -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, ...);
-- 
1.6.0.4.755.g6e139

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-12-04 20:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-22 23:09 [PATCH 1/4] add strbuf_expand_dict_cb(), a helper for simple cases René Scharfe
2008-11-22 23:13 ` [PATCH 2/4] merge-recursive: use strbuf_expand() instead of interpolate() René Scharfe
2008-11-22 23:15   ` [PATCH 3/4] daemon: " René Scharfe
2008-11-22 23:16     ` [PATCH 4/4] remove the unused files interpolate.c and interpolate.h René Scharfe
2008-11-22 23:19     ` [PATCH 3.1/4] daemon: inline fill_in_extra_table_entries() René Scharfe
2008-11-22 23:21       ` [PATCH 3.2/4] daemon: deglobalize variable 'directory' René Scharfe
2008-12-04 20:30 ` [PATCH 1/4] add strbuf_expand_dict_cb(), a helper for simple cases Jon Loeliger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).