Copy the line onto the end of the strbuf while fixing whitespaces */
| 286 | |
| 287 | /* Copy the line onto the end of the strbuf while fixing whitespaces */ |
| 288 | void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count) |
| 289 | { |
| 290 | /* |
| 291 | * len is number of bytes to be copied from src, starting |
| 292 | * at src. Typically src[len-1] is '\n', unless this is |
| 293 | * the incomplete last line. |
| 294 | */ |
| 295 | int i; |
| 296 | int add_nl_to_tail = 0; |
| 297 | int add_cr_to_tail = 0; |
| 298 | int fixed = 0; |
| 299 | int last_tab_in_indent = -1; |
| 300 | int last_space_in_indent = -1; |
| 301 | int need_fix_leading_space = 0; |
| 302 | |
| 303 | /* |
| 304 | * Remembering that we need to add '\n' at the end |
| 305 | * is sufficient to fix an incomplete line. |
| 306 | */ |
| 307 | if (ws_rule & WS_INCOMPLETE_LINE) { |
| 308 | if (0 < len && src[len - 1] != '\n') { |
| 309 | fixed = 1; |
| 310 | add_nl_to_tail = 1; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Strip trailing whitespace |
| 316 | */ |
| 317 | if (ws_rule & WS_BLANK_AT_EOL) { |
| 318 | if (0 < len && src[len - 1] == '\n') { |
| 319 | add_nl_to_tail = 1; |
| 320 | len--; |
| 321 | if (0 < len && src[len - 1] == '\r') { |
| 322 | add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL); |
| 323 | len--; |
| 324 | } |
| 325 | } |
| 326 | if (0 < len && isspace(src[len - 1])) { |
| 327 | while (0 < len && isspace(src[len-1])) |
| 328 | len--; |
| 329 | fixed = 1; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Check leading whitespaces (indent) |
| 335 | */ |
| 336 | for (i = 0; i < len; i++) { |
| 337 | char ch = src[i]; |
| 338 | if (ch == '\t') { |
| 339 | last_tab_in_indent = i; |
| 340 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && |
| 341 | 0 <= last_space_in_indent) |
| 342 | need_fix_leading_space = 1; |
| 343 | } else if (ch == ' ') { |
| 344 | last_space_in_indent = i; |
| 345 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && |
no test coverage detected