strbuf: make stripspace() part of strbuf
This function is also used in other builtins than stripspace, so it makes sense to have it in a more generic place. Since it operates on an strbuf and the function is declared in strbuf.h, move it to strbuf.c and add the corresponding prefix to its name, just like other API functions in the strbuf_* family. Also switch all current users of stripspace() to the new function name and keep a temporary wrapper inline function for any topic branches still using stripspace(). Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Tobias Klauser <tklauser@distanz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tobias Klauser committed
Oct 16, 2015 at 17:16 UTC
63af4a8446a624ad0d83d9b766c0eb5fbf83be12
9 files changed
+88
-78
builtin/am.c
+1
-1
@@ -1343,7 +1343,7 @@ static int parse_mail(struct am_state *state, const char *mail)
1343
strbuf_addstr(&msg, "\n\n");
1344
if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
1345
die_errno(_("could not read '%s'"), am_path(state, "msg"));
1346
- stripspace(&msg, 0);
1346
+ strbuf_stripspace(&msg, 0);
1347
1348
if (state->signoff)
1349
am_signoff(&msg);
builtin/branch.c
+1
-1
@@ -786,7 +786,7 @@ static int edit_branch_description(const char *branch_name)
786
strbuf_release(&buf);
787
return -1;
788
}
789
- stripspace(&buf, 1);
789
+ strbuf_stripspace(&buf, 1);
790
791
strbuf_addf(&name, "branch.%s.description", branch_name);
792
status = git_config_set(name.buf, buf.len ? buf.buf : NULL);
builtin/commit.c
+3
-3
@@ -775,7 +775,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
775
s->hints = 0;
776
777
if (clean_message_contents)
778
- stripspace(&sb, 0);
778
+ strbuf_stripspace(&sb, 0);
779
780
if (signoff)
781
append_signoff(&sb, ignore_non_trailer(&sb), 0);
@@ -1014,7 +1014,7 @@ static int template_untouched(struct strbuf *sb)
1014
if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
1015
return 0;
1016
1017
- stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
1017
+ strbuf_stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
1018
if (!skip_prefix(sb->buf, tmpl.buf, &start))
1019
start = sb->buf;
1020
strbuf_release(&tmpl);
@@ -1726,7 +1726,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1726
wt_status_truncate_message_at_cut_line(&sb);
1727
1728
if (cleanup_mode != CLEANUP_NONE)
1729
- stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1729
+ strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1730
if (template_untouched(&sb) && !allow_empty_message) {
1731
rollback_index_files();
1732
fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
builtin/merge.c
+1
-1
@@ -806,7 +806,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
806
abort_commit(remoteheads, NULL);
807
}
808
read_merge_msg(&msg);
809
- stripspace(&msg, 0 < option_edit);
809
+ strbuf_stripspace(&msg, 0 < option_edit);
810
if (!msg.len)
811
abort_commit(remoteheads, _("Empty commit message."));
812
strbuf_release(&merge_msg);
builtin/notes.c
+3
-3
@@ -192,7 +192,7 @@ static void prepare_note_data(const unsigned char *object, struct note_data *d,
192
if (launch_editor(d->edit_path, &d->buf, NULL)) {
193
die(_("Please supply the note contents using either -m or -F option"));
194
}
195
- stripspace(&d->buf, 1);
195
+ strbuf_stripspace(&d->buf, 1);
196
}
197
}
198
@@ -215,7 +215,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
215
if (d->buf.len)
216
strbuf_addch(&d->buf, '\n');
217
strbuf_addstr(&d->buf, arg);
218
- stripspace(&d->buf, 0);
218
+ strbuf_stripspace(&d->buf, 0);
219
220
d->given = 1;
221
return 0;
@@ -232,7 +232,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
232
die_errno(_("cannot read '%s'"), arg);
233
} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
234
die_errno(_("could not open or read '%s'"), arg);
235
- stripspace(&d->buf, 0);
235
+ strbuf_stripspace(&d->buf, 0);
236
237
d->given = 1;
238
return 0;
builtin/stripspace.c
+2
-67
@@ -1,71 +1,6 @@
1
#include "builtin.h"
2
#include "cache.h"
3
-
4
-/*
5
- * Returns the length of a line, without trailing spaces.
6
- *
7
- * If the line ends with newline, it will be removed too.
8
- */
9
-static size_t cleanup(char *line, size_t len)
10
-{
11
- while (len) {
12
- unsigned char c = line[len - 1];
13
- if (!isspace(c))
14
- break;
15
- len--;
16
- }
17
-
18
- return len;
19
-}
20
-
21
-/*
22
- * Remove empty lines from the beginning and end
23
- * and also trailing spaces from every line.
24
- *
25
- * Turn multiple consecutive empty lines between paragraphs
26
- * into just one empty line.
27
- *
28
- * If the input has only empty lines and spaces,
29
- * no output will be produced.
30
- *
31
- * If last line does not have a newline at the end, one is added.
32
- *
33
- * Enable skip_comments to skip every line starting with comment
34
- * character.
35
- */
36
-void stripspace(struct strbuf *sb, int skip_comments)
37
-{
38
- int empties = 0;
39
- size_t i, j, len, newlen;
40
- char *eol;
41
-
42
- /* We may have to add a newline. */
43
- strbuf_grow(sb, 1);
44
-
45
- for (i = j = 0; i < sb->len; i += len, j += newlen) {
46
- eol = memchr(sb->buf + i, '\n', sb->len - i);
47
- len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
48
-
49
- if (skip_comments && len && sb->buf[i] == comment_line_char) {
50
- newlen = 0;
51
- continue;
52
- }
53
- newlen = cleanup(sb->buf + i, len);
54
-
55
- /* Not just an empty line? */
56
- if (newlen) {
57
- if (empties > 0 && j > 0)
58
- sb->buf[j++] = '\n';
59
- empties = 0;
60
- memmove(sb->buf + j, sb->buf + i, newlen);
61
- sb->buf[newlen + j++] = '\n';
62
- } else {
63
- empties++;
64
- }
65
- }
66
-
67
- strbuf_setlen(sb, j);
68
-}
3
+#include "strbuf.h"
4
5
static void comment_lines(struct strbuf *buf)
6
{
@@ -111,7 +46,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
46
die_errno("could not read the input");
47
48
if (mode == STRIP_SPACE)
114
- stripspace(&buf, strip_comments);
49
+ strbuf_stripspace(&buf, strip_comments);
50
else
51
comment_lines(&buf);
52
builtin/tag.c
+1
-1
@@ -498,7 +498,7 @@ static void create_tag(const unsigned char *object, const char *tag,
498
}
499
500
if (opt->cleanup_mode != CLEANUP_NONE)
501
- stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
501
+ strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
502
503
if (!opt->message_given && !buf->len)
504
die(_("no tag message?"));
strbuf.c
+66
@@ -743,3 +743,69 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
743
}
744
strbuf_setlen(sb, sb->len + len);
745
}
746
+
747
+/*
748
+ * Returns the length of a line, without trailing spaces.
749
+ *
750
+ * If the line ends with newline, it will be removed too.
751
+ */
752
+static size_t cleanup(char *line, size_t len)
753
+{
754
+ while (len) {
755
+ unsigned char c = line[len - 1];
756
+ if (!isspace(c))
757
+ break;
758
+ len--;
759
+ }
760
+
761
+ return len;
762
+}
763
+
764
+/*
765
+ * Remove empty lines from the beginning and end
766
+ * and also trailing spaces from every line.
767
+ *
768
+ * Turn multiple consecutive empty lines between paragraphs
769
+ * into just one empty line.
770
+ *
771
+ * If the input has only empty lines and spaces,
772
+ * no output will be produced.
773
+ *
774
+ * If last line does not have a newline at the end, one is added.
775
+ *
776
+ * Enable skip_comments to skip every line starting with comment
777
+ * character.
778
+ */
779
+void strbuf_stripspace(struct strbuf *sb, int skip_comments)
780
+{
781
+ int empties = 0;
782
+ size_t i, j, len, newlen;
783
+ char *eol;
784
+
785
+ /* We may have to add a newline. */
786
+ strbuf_grow(sb, 1);
787
+
788
+ for (i = j = 0; i < sb->len; i += len, j += newlen) {
789
+ eol = memchr(sb->buf + i, '\n', sb->len - i);
790
+ len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
791
+
792
+ if (skip_comments && len && sb->buf[i] == comment_line_char) {
793
+ newlen = 0;
794
+ continue;
795
+ }
796
+ newlen = cleanup(sb->buf + i, len);
797
+
798
+ /* Not just an empty line? */
799
+ if (newlen) {
800
+ if (empties > 0 && j > 0)
801
+ sb->buf[j++] = '\n';
802
+ empties = 0;
803
+ memmove(sb->buf + j, sb->buf + i, newlen);
804
+ sb->buf[newlen + j++] = '\n';
805
+ } else {
806
+ empties++;
807
+ }
808
+ }
809
+
810
+ strbuf_setlen(sb, j);
811
+}
strbuf.h
+10
-1
@@ -418,7 +418,16 @@ extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
418
* Strip whitespace from a buffer. The second parameter controls if
419
* comments are considered contents to be removed or not.
420
*/
421
-extern void stripspace(struct strbuf *buf, int skip_comments);
421
+extern void strbuf_stripspace(struct strbuf *buf, int skip_comments);
422
+
423
+/**
424
+ * Temporary alias until all topic branches have switched to use
425
+ * strbuf_stripspace directly.
426
+ */
427
+static inline void stripspace(struct strbuf *buf, int skip_comments)
428
+{
429
+ strbuf_stripspace(buf, skip_comments);
430
+}
431
432
static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
433
{