Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Whitespace rules |
| 3 | * |
| 4 | * Copyright (c) 2007 Junio C Hamano |
| 5 | */ |
| 6 | |
| 7 | #include "cache.h" |
| 8 | #include "attr.h" |
| 9 | |
| 10 | static struct whitespace_rule { |
| 11 | const char *rule_name; |
| 12 | unsigned rule_bits; |
Junio C Hamano | 727c371 | 2010-04-03 00:37:00 +0100 | [diff] [blame] | 13 | unsigned loosens_error:1, |
| 14 | exclude_default:1; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 15 | } whitespace_rule_names[] = { |
Junio C Hamano | a437900 | 2009-06-21 02:35:18 -0700 | [diff] [blame] | 16 | { "trailing-space", WS_TRAILING_SPACE, 0 }, |
| 17 | { "space-before-tab", WS_SPACE_BEFORE_TAB, 0 }, |
| 18 | { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB, 0 }, |
| 19 | { "cr-at-eol", WS_CR_AT_EOL, 1 }, |
Junio C Hamano | afd9db4 | 2009-09-15 03:28:08 -0700 | [diff] [blame] | 20 | { "blank-at-eol", WS_BLANK_AT_EOL, 0 }, |
| 21 | { "blank-at-eof", WS_BLANK_AT_EOF, 0 }, |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 22 | { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 }, |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | unsigned parse_whitespace_rule(const char *string) |
| 26 | { |
| 27 | unsigned rule = WS_DEFAULT_RULE; |
| 28 | |
| 29 | while (string) { |
| 30 | int i; |
| 31 | size_t len; |
| 32 | const char *ep; |
| 33 | int negated = 0; |
| 34 | |
| 35 | string = string + strspn(string, ", \t\n\r"); |
Rohit Mani | 2c5495f | 2014-03-07 22:48:31 -0800 | [diff] [blame] | 36 | ep = strchrnul(string, ','); |
| 37 | len = ep - string; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 38 | |
| 39 | if (*string == '-') { |
| 40 | negated = 1; |
| 41 | string++; |
| 42 | len--; |
| 43 | } |
| 44 | if (!len) |
| 45 | break; |
| 46 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) { |
| 47 | if (strncmp(whitespace_rule_names[i].rule_name, |
| 48 | string, len)) |
| 49 | continue; |
| 50 | if (negated) |
| 51 | rule &= ~whitespace_rule_names[i].rule_bits; |
| 52 | else |
| 53 | rule |= whitespace_rule_names[i].rule_bits; |
| 54 | break; |
| 55 | } |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 56 | if (strncmp(string, "tabwidth=", 9) == 0) { |
| 57 | unsigned tabwidth = atoi(string + 9); |
| 58 | if (0 < tabwidth && tabwidth < 0100) { |
| 59 | rule &= ~WS_TAB_WIDTH_MASK; |
| 60 | rule |= tabwidth; |
| 61 | } |
| 62 | else |
| 63 | warning("tabwidth %.*s out of range", |
| 64 | (int)(len - 9), string + 9); |
| 65 | } |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 66 | string = ep; |
| 67 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 68 | |
| 69 | if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB) |
| 70 | die("cannot enforce both tab-in-indent and indent-with-non-tab"); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 71 | return rule; |
| 72 | } |
| 73 | |
| 74 | static void setup_whitespace_attr_check(struct git_attr_check *check) |
| 75 | { |
| 76 | static struct git_attr *attr_whitespace; |
| 77 | |
| 78 | if (!attr_whitespace) |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 79 | attr_whitespace = git_attr("whitespace"); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 80 | check[0].attr = attr_whitespace; |
| 81 | } |
| 82 | |
| 83 | unsigned whitespace_rule(const char *pathname) |
| 84 | { |
| 85 | struct git_attr_check attr_whitespace_rule; |
| 86 | |
| 87 | setup_whitespace_attr_check(&attr_whitespace_rule); |
Michael Haggerty | d932f4e | 2011-08-04 06:36:33 +0200 | [diff] [blame] | 88 | if (!git_check_attr(pathname, 1, &attr_whitespace_rule)) { |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 89 | const char *value; |
| 90 | |
| 91 | value = attr_whitespace_rule.value; |
| 92 | if (ATTR_TRUE(value)) { |
| 93 | /* true (whitespace) */ |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 94 | unsigned all_rule = ws_tab_width(whitespace_rule_cfg); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 95 | int i; |
| 96 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) |
Junio C Hamano | 727c371 | 2010-04-03 00:37:00 +0100 | [diff] [blame] | 97 | if (!whitespace_rule_names[i].loosens_error && |
| 98 | !whitespace_rule_names[i].exclude_default) |
Junio C Hamano | a437900 | 2009-06-21 02:35:18 -0700 | [diff] [blame] | 99 | all_rule |= whitespace_rule_names[i].rule_bits; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 100 | return all_rule; |
| 101 | } else if (ATTR_FALSE(value)) { |
| 102 | /* false (-whitespace) */ |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 103 | return ws_tab_width(whitespace_rule_cfg); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 104 | } else if (ATTR_UNSET(value)) { |
| 105 | /* reset to default (!whitespace) */ |
| 106 | return whitespace_rule_cfg; |
| 107 | } else { |
| 108 | /* string */ |
| 109 | return parse_whitespace_rule(value); |
| 110 | } |
| 111 | } else { |
| 112 | return whitespace_rule_cfg; |
| 113 | } |
| 114 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 115 | |
| 116 | /* The returned string should be freed by the caller. */ |
| 117 | char *whitespace_error_string(unsigned ws) |
| 118 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 119 | struct strbuf err = STRBUF_INIT; |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 120 | if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE) |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 121 | strbuf_addstr(&err, "trailing whitespace"); |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 122 | else { |
| 123 | if (ws & WS_BLANK_AT_EOL) |
| 124 | strbuf_addstr(&err, "trailing whitespace"); |
| 125 | if (ws & WS_BLANK_AT_EOF) { |
| 126 | if (err.len) |
| 127 | strbuf_addstr(&err, ", "); |
| 128 | strbuf_addstr(&err, "new blank line at EOF"); |
| 129 | } |
| 130 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 131 | if (ws & WS_SPACE_BEFORE_TAB) { |
| 132 | if (err.len) |
| 133 | strbuf_addstr(&err, ", "); |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 134 | strbuf_addstr(&err, "space before tab in indent"); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 135 | } |
| 136 | if (ws & WS_INDENT_WITH_NON_TAB) { |
| 137 | if (err.len) |
| 138 | strbuf_addstr(&err, ", "); |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 139 | strbuf_addstr(&err, "indent with spaces"); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 140 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 141 | if (ws & WS_TAB_IN_INDENT) { |
| 142 | if (err.len) |
| 143 | strbuf_addstr(&err, ", "); |
| 144 | strbuf_addstr(&err, "tab in indent"); |
| 145 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 146 | return strbuf_detach(&err, NULL); |
| 147 | } |
| 148 | |
| 149 | /* If stream is non-NULL, emits the line after checking. */ |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 150 | static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule, |
| 151 | FILE *stream, const char *set, |
| 152 | const char *reset, const char *ws) |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 153 | { |
| 154 | unsigned result = 0; |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 155 | int written = 0; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 156 | int trailing_whitespace = -1; |
| 157 | int trailing_newline = 0; |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 158 | int trailing_carriage_return = 0; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 159 | int i; |
| 160 | |
| 161 | /* Logic is simpler if we temporarily ignore the trailing newline. */ |
| 162 | if (len > 0 && line[len - 1] == '\n') { |
| 163 | trailing_newline = 1; |
| 164 | len--; |
| 165 | } |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 166 | if ((ws_rule & WS_CR_AT_EOL) && |
| 167 | len > 0 && line[len - 1] == '\r') { |
| 168 | trailing_carriage_return = 1; |
| 169 | len--; |
| 170 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 171 | |
| 172 | /* Check for trailing whitespace. */ |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 173 | if (ws_rule & WS_BLANK_AT_EOL) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 174 | for (i = len - 1; i >= 0; i--) { |
| 175 | if (isspace(line[i])) { |
| 176 | trailing_whitespace = i; |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 177 | result |= WS_BLANK_AT_EOL; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 178 | } |
| 179 | else |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Kevin Ballard | cfd1a98 | 2010-10-20 15:17:26 -0700 | [diff] [blame] | 184 | if (trailing_whitespace == -1) |
| 185 | trailing_whitespace = len; |
| 186 | |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 187 | /* Check indentation */ |
Kevin Ballard | cfd1a98 | 2010-10-20 15:17:26 -0700 | [diff] [blame] | 188 | for (i = 0; i < trailing_whitespace; i++) { |
J. Bruce Fields | 9afa2d4 | 2007-12-16 11:31:40 -0500 | [diff] [blame] | 189 | if (line[i] == ' ') |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 190 | continue; |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 191 | if (line[i] != '\t') |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 192 | break; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 193 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) { |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 194 | result |= WS_SPACE_BEFORE_TAB; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 195 | if (stream) { |
| 196 | fputs(ws, stream); |
| 197 | fwrite(line + written, i - written, 1, stream); |
| 198 | fputs(reset, stream); |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 199 | fwrite(line + i, 1, 1, stream); |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 200 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 201 | } else if (ws_rule & WS_TAB_IN_INDENT) { |
| 202 | result |= WS_TAB_IN_INDENT; |
| 203 | if (stream) { |
| 204 | fwrite(line + written, i - written, 1, stream); |
| 205 | fputs(ws, stream); |
| 206 | fwrite(line + i, 1, 1, stream); |
| 207 | fputs(reset, stream); |
| 208 | } |
| 209 | } else if (stream) { |
| 210 | fwrite(line + written, i - written + 1, 1, stream); |
| 211 | } |
J. Bruce Fields | 9afa2d4 | 2007-12-16 11:31:40 -0500 | [diff] [blame] | 212 | written = i + 1; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* Check for indent using non-tab. */ |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 216 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 217 | result |= WS_INDENT_WITH_NON_TAB; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 218 | if (stream) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 219 | fputs(ws, stream); |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 220 | fwrite(line + written, i - written, 1, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 221 | fputs(reset, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 222 | } |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 223 | written = i; |
| 224 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 225 | |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 226 | if (stream) { |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 227 | /* |
| 228 | * Now the rest of the line starts at "written". |
| 229 | * The non-highlighted part ends at "trailing_whitespace". |
| 230 | */ |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 231 | |
| 232 | /* Emit non-highlighted (middle) segment. */ |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 233 | if (trailing_whitespace - written > 0) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 234 | fputs(set, stream); |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 235 | fwrite(line + written, |
| 236 | trailing_whitespace - written, 1, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 237 | fputs(reset, stream); |
| 238 | } |
| 239 | |
| 240 | /* Highlight errors in trailing whitespace. */ |
| 241 | if (trailing_whitespace != len) { |
| 242 | fputs(ws, stream); |
| 243 | fwrite(line + trailing_whitespace, |
| 244 | len - trailing_whitespace, 1, stream); |
| 245 | fputs(reset, stream); |
| 246 | } |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 247 | if (trailing_carriage_return) |
| 248 | fputc('\r', stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 249 | if (trailing_newline) |
| 250 | fputc('\n', stream); |
| 251 | } |
| 252 | return result; |
| 253 | } |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 254 | |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 255 | void ws_check_emit(const char *line, int len, unsigned ws_rule, |
| 256 | FILE *stream, const char *set, |
| 257 | const char *reset, const char *ws) |
| 258 | { |
| 259 | (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws); |
| 260 | } |
| 261 | |
| 262 | unsigned ws_check(const char *line, int len, unsigned ws_rule) |
| 263 | { |
| 264 | return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL); |
| 265 | } |
| 266 | |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 267 | int ws_blank_line(const char *line, int len, unsigned ws_rule) |
| 268 | { |
| 269 | /* |
| 270 | * We _might_ want to treat CR differently from other |
| 271 | * whitespace characters when ws_rule has WS_CR_AT_EOL, but |
| 272 | * for now we just use this stupid definition. |
| 273 | */ |
| 274 | while (len-- > 0) { |
| 275 | if (!isspace(*line)) |
| 276 | return 0; |
| 277 | line++; |
| 278 | } |
| 279 | return 1; |
| 280 | } |
| 281 | |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 282 | /* Copy the line onto the end of the strbuf while fixing whitespaces */ |
| 283 | void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count) |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 284 | { |
| 285 | /* |
| 286 | * len is number of bytes to be copied from src, starting |
| 287 | * at src. Typically src[len-1] is '\n', unless this is |
| 288 | * the incomplete last line. |
| 289 | */ |
| 290 | int i; |
| 291 | int add_nl_to_tail = 0; |
| 292 | int add_cr_to_tail = 0; |
| 293 | int fixed = 0; |
| 294 | int last_tab_in_indent = -1; |
| 295 | int last_space_in_indent = -1; |
| 296 | int need_fix_leading_space = 0; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Strip trailing whitespace |
| 300 | */ |
Junio C Hamano | afd9db4 | 2009-09-15 03:28:08 -0700 | [diff] [blame] | 301 | if (ws_rule & WS_BLANK_AT_EOL) { |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 302 | if (0 < len && src[len - 1] == '\n') { |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 303 | add_nl_to_tail = 1; |
| 304 | len--; |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 305 | if (0 < len && src[len - 1] == '\r') { |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 306 | add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL); |
| 307 | len--; |
| 308 | } |
| 309 | } |
| 310 | if (0 < len && isspace(src[len - 1])) { |
| 311 | while (0 < len && isspace(src[len-1])) |
| 312 | len--; |
| 313 | fixed = 1; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Check leading whitespaces (indent) |
| 319 | */ |
| 320 | for (i = 0; i < len; i++) { |
| 321 | char ch = src[i]; |
| 322 | if (ch == '\t') { |
| 323 | last_tab_in_indent = i; |
| 324 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && |
| 325 | 0 <= last_space_in_indent) |
| 326 | need_fix_leading_space = 1; |
| 327 | } else if (ch == ' ') { |
| 328 | last_space_in_indent = i; |
| 329 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 330 | ws_tab_width(ws_rule) <= i - last_tab_in_indent) |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 331 | need_fix_leading_space = 1; |
| 332 | } else |
| 333 | break; |
| 334 | } |
| 335 | |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 336 | if (need_fix_leading_space) { |
| 337 | /* Process indent ourselves */ |
| 338 | int consecutive_spaces = 0; |
| 339 | int last = last_tab_in_indent + 1; |
| 340 | |
| 341 | if (ws_rule & WS_INDENT_WITH_NON_TAB) { |
| 342 | /* have "last" point at one past the indent */ |
| 343 | if (last_tab_in_indent < last_space_in_indent) |
| 344 | last = last_space_in_indent + 1; |
| 345 | else |
| 346 | last = last_tab_in_indent + 1; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * between src[0..last-1], strip the funny spaces, |
| 351 | * updating them to tab as needed. |
| 352 | */ |
| 353 | for (i = 0; i < last; i++) { |
| 354 | char ch = src[i]; |
| 355 | if (ch != ' ') { |
| 356 | consecutive_spaces = 0; |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 357 | strbuf_addch(dst, ch); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 358 | } else { |
| 359 | consecutive_spaces++; |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 360 | if (consecutive_spaces == ws_tab_width(ws_rule)) { |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 361 | strbuf_addch(dst, '\t'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 362 | consecutive_spaces = 0; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | while (0 < consecutive_spaces--) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 367 | strbuf_addch(dst, ' '); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 368 | len -= last; |
| 369 | src += last; |
| 370 | fixed = 1; |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 371 | } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) { |
| 372 | /* Expand tabs into spaces */ |
Johannes Sixt | d35711a | 2010-11-30 09:22:04 +0100 | [diff] [blame] | 373 | int start = dst->len; |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 374 | int last = last_tab_in_indent + 1; |
| 375 | for (i = 0; i < last; i++) { |
| 376 | if (src[i] == '\t') |
| 377 | do { |
| 378 | strbuf_addch(dst, ' '); |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 379 | } while ((dst->len - start) % ws_tab_width(ws_rule)); |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 380 | else |
| 381 | strbuf_addch(dst, src[i]); |
| 382 | } |
| 383 | len -= last; |
| 384 | src += last; |
| 385 | fixed = 1; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 388 | strbuf_add(dst, src, len); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 389 | if (add_cr_to_tail) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 390 | strbuf_addch(dst, '\r'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 391 | if (add_nl_to_tail) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 392 | strbuf_addch(dst, '\n'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 393 | if (fixed && error_count) |
| 394 | (*error_count)++; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 395 | } |