* Remove empty lines from the beginning and end * and also trailing spaces from every line. * * Turn multiple consecutive empty lines between paragraphs * into just one empty line. * * If the input has only empty lines and spaces, * no output will be produced. * * If last line does not have a newline at the end, one is added. * * Pass a non-NULL comment_prefix to skip every line startin
| 1093 | * with it. |
| 1094 | */ |
| 1095 | void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix) |
| 1096 | { |
| 1097 | size_t empties = 0; |
| 1098 | size_t i, j, len, newlen; |
| 1099 | char *eol; |
| 1100 | |
| 1101 | /* We may have to add a newline. */ |
| 1102 | strbuf_grow(sb, 1); |
| 1103 | |
| 1104 | for (i = j = 0; i < sb->len; i += len, j += newlen) { |
| 1105 | eol = memchr(sb->buf + i, '\n', sb->len - i); |
| 1106 | len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; |
| 1107 | |
| 1108 | if (comment_prefix && len && |
| 1109 | starts_with(sb->buf + i, comment_prefix)) { |
| 1110 | newlen = 0; |
| 1111 | continue; |
| 1112 | } |
| 1113 | newlen = cleanup(sb->buf + i, len); |
| 1114 | |
| 1115 | /* Not just an empty line? */ |
| 1116 | if (newlen) { |
| 1117 | if (empties > 0 && j > 0) |
| 1118 | sb->buf[j++] = '\n'; |
| 1119 | empties = 0; |
| 1120 | memmove(sb->buf + j, sb->buf + i, newlen); |
| 1121 | sb->buf[newlen + j++] = '\n'; |
| 1122 | } else { |
| 1123 | empties++; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | strbuf_setlen(sb, j); |
| 1128 | } |
| 1129 | |
| 1130 | void strbuf_strip_file_from_path(struct strbuf *sb) |
| 1131 | { |
no test coverage detected