summaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2007-11-14 14:03:50 -0800
committerJunio C Hamano <gitster@pobox.com>2007-11-14 14:03:50 -0800
commit37ec2b4c26901d5f1ca19948189dc2b6f21523d5 (patch)
tree42fb61c490d77c882699f9685ab466d21a51e1d7 /strbuf.c
parent4356736571c6845cb446e4a7c9d645868e558ca0 (diff)
parent726c8ef5a5a129d8157d0043f60fe7195d2cdb77 (diff)
downloadgit-37ec2b4c26901d5f1ca19948189dc2b6f21523d5.tar.gz
Merge branch 'rs/pretty'
* rs/pretty: Fix preprocessor logic that determines the availablity of strchrnul(). Simplify strchrnul() compat code --format=pretty: avoid calculating expensive expansions twice add strbuf_adddup() --pretty=format: parse commit message only once --pretty=format: on-demand format expansion Add strchrnul()
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index cbada946c4..b9b194b320 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -106,6 +106,13 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
strbuf_setlen(sb, sb->len + len);
}
+void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
+{
+ strbuf_grow(sb, len);
+ memcpy(sb->buf + sb->len, sb->buf + pos, len);
+ strbuf_setlen(sb, sb->len + len);
+}
+
void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
{
int len;
@@ -130,6 +137,30 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
strbuf_setlen(sb, sb->len + len);
}
+void strbuf_expand(struct strbuf *sb, const char *format,
+ const char **placeholders, expand_fn_t fn, void *context)
+{
+ for (;;) {
+ const char *percent, **p;
+
+ percent = strchrnul(format, '%');
+ strbuf_add(sb, format, percent - format);
+ if (!*percent)
+ break;
+ format = percent + 1;
+
+ for (p = placeholders; *p; p++) {
+ if (!prefixcmp(format, *p))
+ break;
+ }
+ if (*p) {
+ fn(sb, *p, context);
+ format += strlen(*p);
+ } else
+ strbuf_addch(sb, '%');
+ }
+}
+
size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
{
size_t res;