blob: c0b21301ba4c126a49ed38b6983756b99a25aae0 [file] [log] [blame]
Lukas Sandström7499c992006-06-13 22:21:53 +02001#include "builtin.h"
Carlos Rica9690c112007-06-25 21:28:01 +02002#include "cache.h"
Linus Torvaldsa3e870f2005-05-30 12:51:00 -07003
4/*
Carlos Rica975e0da2007-07-11 20:50:34 +02005 * Returns the length of a line, without trailing spaces.
Linus Torvaldsa3e870f2005-05-30 12:51:00 -07006 *
Carlos Rica9690c112007-06-25 21:28:01 +02007 * If the line ends with newline, it will be removed too.
Linus Torvaldsa3e870f2005-05-30 12:51:00 -07008 */
Carlos Rica975e0da2007-07-11 20:50:34 +02009static size_t cleanup(char *line, size_t len)
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070010{
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040011 while (len) {
12 unsigned char c = line[len - 1];
13 if (!isspace(c))
14 break;
15 len--;
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070016 }
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040017
Carlos Rica9690c112007-06-25 21:28:01 +020018 return len;
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070019}
20
Carlos Rica9690c112007-06-25 21:28:01 +020021/*
22 * Remove empty lines from the beginning and end
23 * and also trailing spaces from every line.
24 *
Carlos Rica975e0da2007-07-11 20:50:34 +020025 * Note that the buffer will not be NUL-terminated.
26 *
Carlos Rica9690c112007-06-25 21:28:01 +020027 * Turn multiple consecutive empty lines between paragraphs
28 * into just one empty line.
29 *
30 * If the input has only empty lines and spaces,
31 * no output will be produced.
32 *
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040033 * If last line does not have a newline at the end, one is added.
Carlos Rica975e0da2007-07-11 20:50:34 +020034 *
Carlos Rica9690c112007-06-25 21:28:01 +020035 * Enable skip_comments to skip every line starting with "#".
36 */
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040037void stripspace(struct strbuf *sb, int skip_comments)
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070038{
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040039 int empties = 0;
Carlos Rica975e0da2007-07-11 20:50:34 +020040 size_t i, j, len, newlen;
41 char *eol;
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070042
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040043 /* We may have to add a newline. */
44 strbuf_grow(sb, 1);
Carlos Rica9690c112007-06-25 21:28:01 +020045
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040046 for (i = j = 0; i < sb->len; i += len, j += newlen) {
47 eol = memchr(sb->buf + i, '\n', sb->len - i);
48 len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
49
50 if (skip_comments && len && sb->buf[i] == '#') {
Carlos Rica975e0da2007-07-11 20:50:34 +020051 newlen = 0;
Carlos Rica9690c112007-06-25 21:28:01 +020052 continue;
Carlos Rica975e0da2007-07-11 20:50:34 +020053 }
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040054 newlen = cleanup(sb->buf + i, len);
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070055
56 /* Not just an empty line? */
Carlos Rica975e0da2007-07-11 20:50:34 +020057 if (newlen) {
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040058 if (empties > 0 && j > 0)
59 sb->buf[j++] = '\n';
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070060 empties = 0;
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040061 memmove(sb->buf + j, sb->buf + i, newlen);
62 sb->buf[newlen + j++] = '\n';
63 } else {
64 empties++;
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070065 }
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070066 }
Carlos Rica975e0da2007-07-11 20:50:34 +020067
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040068 strbuf_setlen(sb, j);
Lukas Sandström7499c992006-06-13 22:21:53 +020069}
70
Linus Torvaldsa633fca2006-07-28 22:44:25 -070071int cmd_stripspace(int argc, const char **argv, const char *prefix)
Lukas Sandström7499c992006-06-13 22:21:53 +020072{
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +020073 struct strbuf buf;
Johannes Schindelinf653aee2007-07-23 12:58:27 +010074 int strip_comments = 0;
75
76 if (argc > 1 && (!strcmp(argv[1], "-s") ||
77 !strcmp(argv[1], "--strip-comments")))
78 strip_comments = 1;
Carlos Rica975e0da2007-07-11 20:50:34 +020079
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +020080 strbuf_init(&buf, 0);
81 if (strbuf_read(&buf, 0, 1024) < 0)
Carlos Rica975e0da2007-07-11 20:50:34 +020082 die("could not read the input");
83
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -040084 stripspace(&buf, strip_comments);
Carlos Rica975e0da2007-07-11 20:50:34 +020085
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +020086 write_or_die(1, buf.buf, buf.len);
87 strbuf_release(&buf);
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070088 return 0;
89}