@matheusbsbs / matheus-bagliano / commits / 954ecd4353
Fork 0

whitespace: minor cleanup

The variable leading_space is initially used to represent the index of the last space seen before a non-space. Then later it represents the index of the first non-indent character. It will prove simpler to replace it by a variable representing a number of bytes. Eventually it will represent the number of bytes written so far (in the stream != NULL case). Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

J. Bruce Fields committed Dec 16, 2007 at 11:31 UTC 954ecd435389916643efeb5b1ade82250170c071
1 file changed +9 -12
ws.c
+9 -12
@@ -121,7 +121,7 @@ unsigned check_and_emit_line(const char *line, int len, unsigned ws_rule,
121 const char *reset, const char *ws)
122 {
123 unsigned result = 0;
124 - int leading_space = -1;
124 + int written = 0;
125 int trailing_whitespace = -1;
126 int trailing_newline = 0;
127 int i;
@@ -147,18 +147,18 @@ unsigned check_and_emit_line(const char *line, int len, unsigned ws_rule,
147 /* Check for space before tab in initial indent. */
148 for (i = 0; i < len; i++) {
149 if (line[i] == ' ') {
150 - leading_space = i;
150 + written = i + 1;
151 continue;
152 }
153 if (line[i] != '\t')
154 break;
155 - if ((ws_rule & WS_SPACE_BEFORE_TAB) && (leading_space != -1))
155 + if ((ws_rule & WS_SPACE_BEFORE_TAB) && (written != 0))
156 result |= WS_SPACE_BEFORE_TAB;
157 break;
158 }
159
160 /* Check for indent using non-tab. */
161 - if ((ws_rule & WS_INDENT_WITH_NON_TAB) && leading_space >= 7)
161 + if ((ws_rule & WS_INDENT_WITH_NON_TAB) && written >= 8)
162 result |= WS_INDENT_WITH_NON_TAB;
163
164 if (stream) {
@@ -166,23 +166,20 @@ unsigned check_and_emit_line(const char *line, int len, unsigned ws_rule,
166 if ((result & WS_SPACE_BEFORE_TAB) ||
167 (result & WS_INDENT_WITH_NON_TAB)) {
168 fputs(ws, stream);
169 - fwrite(line, leading_space + 1, 1, stream);
169 + fwrite(line, written, 1, stream);
170 fputs(reset, stream);
171 - leading_space++;
171 }
173 - else
174 - leading_space = 0;
172
176 - /* Now the rest of the line starts at leading_space.
173 + /* Now the rest of the line starts at written.
174 * The non-highlighted part ends at trailing_whitespace. */
175 if (trailing_whitespace == -1)
176 trailing_whitespace = len;
177
178 /* Emit non-highlighted (middle) segment. */
182 - if (trailing_whitespace - leading_space > 0) {
179 + if (trailing_whitespace - written > 0) {
180 fputs(set, stream);
184 - fwrite(line + leading_space,
185 - trailing_whitespace - leading_space, 1, stream);
181 + fwrite(line + written,
182 + trailing_whitespace - written, 1, stream);
183 fputs(reset, stream);
184 }
185