From d07235a027e0c7ed2fee1aeb3bee8a858bf5ca58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sun, 7 Sep 2014 09:03:32 +0200 Subject: strbuf: export strbuf_addchars() Move strbuf_addchars() to strbuf.c, where it belongs, and make it available for other callers. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt index 3350d97..1d83362 100644 --- a/Documentation/technical/api-strbuf.txt +++ b/Documentation/technical/api-strbuf.txt @@ -142,6 +142,10 @@ then they will free() it. Add a single character to the buffer. +`strbuf_addchars`:: + + Add a character the specified number of times to the buffer. + `strbuf_insert`:: Insert data to the given position of the buffer. The remaining contents diff --git a/strbuf.c b/strbuf.c index 1170d01..5e44cf9 100644 --- a/strbuf.c +++ b/strbuf.c @@ -196,6 +196,13 @@ void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len) strbuf_setlen(sb, sb->len + len); } +void strbuf_addchars(struct strbuf *sb, int c, size_t n) +{ + strbuf_grow(sb, n); + memset(sb->buf + sb->len, c, n); + strbuf_setlen(sb, sb->len + n); +} + void strbuf_addf(struct strbuf *sb, const char *fmt, ...) { va_list ap; diff --git a/strbuf.h b/strbuf.h index 73e80ce..2c34753 100644 --- a/strbuf.h +++ b/strbuf.h @@ -121,6 +121,7 @@ static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) { strbuf_add(sb, sb2->buf, sb2->len); } extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len); +extern void strbuf_addchars(struct strbuf *sb, int c, size_t n); 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); diff --git a/utf8.c b/utf8.c index 0d20e0a..c55f3ba 100644 --- a/utf8.c +++ b/utf8.c @@ -302,13 +302,6 @@ int is_utf8(const char *text) return 1; } -static void strbuf_addchars(struct strbuf *sb, int c, size_t n) -{ - strbuf_grow(sb, n); - memset(sb->buf + sb->len, c, n); - strbuf_setlen(sb, sb->len + n); -} - static void strbuf_add_indented_text(struct strbuf *buf, const char *text, int indent, int indent2) { -- cgit v0.10.2-6-g49f6 From 415792edf51b2f87a58a942016a24a2e86a4218b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sun, 7 Sep 2014 09:06:42 +0200 Subject: strbuf: use strbuf_addchars() for adding a char multiple times Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/graph.c b/graph.c index 6404331..dfb99f6 100644 --- a/graph.c +++ b/graph.c @@ -1145,7 +1145,7 @@ int graph_next_line(struct git_graph *graph, struct strbuf *sb) static void graph_padding_line(struct git_graph *graph, struct strbuf *sb) { - int i, j; + int i; if (graph->state != GRAPH_COMMIT) { graph_next_line(graph, sb); @@ -1169,8 +1169,7 @@ static void graph_padding_line(struct git_graph *graph, struct strbuf *sb) strbuf_addch(sb, ' '); else { int num_spaces = ((graph->num_parents - 2) * 2); - for (j = 0; j < num_spaces; j++) - strbuf_addch(sb, ' '); + strbuf_addchars(sb, ' ', num_spaces); } } else { strbuf_write_column(sb, col, '|'); diff --git a/merge-recursive.c b/merge-recursive.c index dbb7104..ec0fef8 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -163,9 +163,7 @@ static void output(struct merge_options *o, int v, const char *fmt, ...) if (!show(o, v)) return; - strbuf_grow(&o->obuf, o->call_depth * 2 + 2); - memset(o->obuf.buf + o->obuf.len, ' ', o->call_depth * 2); - strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2); + strbuf_addchars(&o->obuf, ' ', o->call_depth * 2); va_start(ap, fmt); strbuf_vaddf(&o->obuf, fmt, ap); diff --git a/pretty.c b/pretty.c index 296cb56..c2198a3 100644 --- a/pretty.c +++ b/pretty.c @@ -1400,9 +1400,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */ * convert it back to chars */ padding = padding - len + local_sb.len; - strbuf_grow(sb, padding); - strbuf_setlen(sb, sb_len + padding); - memset(sb->buf + sb_len, ' ', sb->len - sb_len); + strbuf_addchars(sb, ' ', padding); memcpy(sb->buf + sb_len + offset, local_sb.buf, local_sb.len); } @@ -1679,10 +1677,8 @@ void pp_remainder(struct pretty_print_context *pp, first = 0; strbuf_grow(sb, linelen + indent + 20); - if (indent) { - memset(sb->buf + sb->len, ' ', indent); - strbuf_setlen(sb, sb->len + indent); - } + if (indent) + strbuf_addchars(sb, ' ', indent); strbuf_add(sb, line, linelen); strbuf_addch(sb, '\n'); } -- cgit v0.10.2-6-g49f6