Skip to content

Commit 63af4a8

Browse files
tklausergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Tobias Klauser <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 22f698c commit 63af4a8

File tree

9 files changed

+88
-78
lines changed

9 files changed

+88
-78
lines changed

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ static int parse_mail(struct am_state *state, const char *mail)
13431343
strbuf_addstr(&msg, "\n\n");
13441344
if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
13451345
die_errno(_("could not read '%s'"), am_path(state, "msg"));
1346-
stripspace(&msg, 0);
1346+
strbuf_stripspace(&msg, 0);
13471347

13481348
if (state->signoff)
13491349
am_signoff(&msg);

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ static int edit_branch_description(const char *branch_name)
786786
strbuf_release(&buf);
787787
return -1;
788788
}
789-
stripspace(&buf, 1);
789+
strbuf_stripspace(&buf, 1);
790790

791791
strbuf_addf(&name, "branch.%s.description", branch_name);
792792
status = git_config_set(name.buf, buf.len ? buf.buf : NULL);

builtin/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
775775
s->hints = 0;
776776

777777
if (clean_message_contents)
778-
stripspace(&sb, 0);
778+
strbuf_stripspace(&sb, 0);
779779

780780
if (signoff)
781781
append_signoff(&sb, ignore_non_trailer(&sb), 0);
@@ -1014,7 +1014,7 @@ static int template_untouched(struct strbuf *sb)
10141014
if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
10151015
return 0;
10161016

1017-
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
1017+
strbuf_stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
10181018
if (!skip_prefix(sb->buf, tmpl.buf, &start))
10191019
start = sb->buf;
10201020
strbuf_release(&tmpl);
@@ -1726,7 +1726,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17261726
wt_status_truncate_message_at_cut_line(&sb);
17271727

17281728
if (cleanup_mode != CLEANUP_NONE)
1729-
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1729+
strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);
17301730
if (template_untouched(&sb) && !allow_empty_message) {
17311731
rollback_index_files();
17321732
fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
806806
abort_commit(remoteheads, NULL);
807807
}
808808
read_merge_msg(&msg);
809-
stripspace(&msg, 0 < option_edit);
809+
strbuf_stripspace(&msg, 0 < option_edit);
810810
if (!msg.len)
811811
abort_commit(remoteheads, _("Empty commit message."));
812812
strbuf_release(&merge_msg);

builtin/notes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static void prepare_note_data(const unsigned char *object, struct note_data *d,
192192
if (launch_editor(d->edit_path, &d->buf, NULL)) {
193193
die(_("Please supply the note contents using either -m or -F option"));
194194
}
195-
stripspace(&d->buf, 1);
195+
strbuf_stripspace(&d->buf, 1);
196196
}
197197
}
198198

@@ -215,7 +215,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
215215
if (d->buf.len)
216216
strbuf_addch(&d->buf, '\n');
217217
strbuf_addstr(&d->buf, arg);
218-
stripspace(&d->buf, 0);
218+
strbuf_stripspace(&d->buf, 0);
219219

220220
d->given = 1;
221221
return 0;
@@ -232,7 +232,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
232232
die_errno(_("cannot read '%s'"), arg);
233233
} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
234234
die_errno(_("could not open or read '%s'"), arg);
235-
stripspace(&d->buf, 0);
235+
strbuf_stripspace(&d->buf, 0);
236236

237237
d->given = 1;
238238
return 0;

builtin/stripspace.c

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,6 @@
11
#include "builtin.h"
22
#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"
694

705
static void comment_lines(struct strbuf *buf)
716
{
@@ -111,7 +46,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
11146
die_errno("could not read the input");
11247

11348
if (mode == STRIP_SPACE)
114-
stripspace(&buf, strip_comments);
49+
strbuf_stripspace(&buf, strip_comments);
11550
else
11651
comment_lines(&buf);
11752

builtin/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static void create_tag(const unsigned char *object, const char *tag,
498498
}
499499

500500
if (opt->cleanup_mode != CLEANUP_NONE)
501-
stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
501+
strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
502502

503503
if (!opt->message_given && !buf->len)
504504
die(_("no tag message?"));

strbuf.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,3 +743,69 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
743743
}
744744
strbuf_setlen(sb, sb->len + len);
745745
}
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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,16 @@ extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
418418
* Strip whitespace from a buffer. The second parameter controls if
419419
* comments are considered contents to be removed or not.
420420
*/
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+
}
422431

423432
static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
424433
{

0 commit comments

Comments
 (0)