Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e93f725

Browse files
committed
Add C functions to centralize entab processing
1 parent db90bcf commit e93f725

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/tools/entab/entab.c

+23-23
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ extern char *optarg;
2828
extern int optind;
2929

3030

31+
static void
32+
output_accumulated_spaces(int *prv_spaces, char **dst)
33+
{
34+
for (; *prv_spaces > 0; *prv_spaces--)
35+
*((*dst)++) = ' ';
36+
}
37+
38+
39+
static void
40+
trim_trailing_whitespace(int *prv_spaces, char **dst, char *out_line)
41+
{
42+
while (*dst > out_line &&
43+
(*((*dst) - 1) == ' ' || *((*dst) - 1) == '\t'))
44+
(*dst)--;
45+
*prv_spaces = 0;
46+
}
47+
48+
3149
int
3250
main(int argc, char **argv)
3351
{
@@ -168,19 +186,14 @@ main(int argc, char **argv)
168186
prv_spaces = 0;
169187
}
170188
else
171-
/* output accumulated spaces */
172-
{
173-
for (; prv_spaces > 0; prv_spaces--)
174-
*(dst++) = ' ';
175-
}
189+
output_accumulated_spaces(&prv_spaces, &dst);
176190
}
177191
}
178192
/* Not a potential space/tab replacement */
179193
else
180194
{
181195
/* output accumulated spaces */
182-
for (; prv_spaces > 0; prv_spaces--)
183-
*(dst++) = ' ';
196+
output_accumulated_spaces(&prv_spaces, &dst);
184197
/* This can only happen in a quote. */
185198
if (*src == '\t')
186199
col_in_tab = 0;
@@ -211,29 +224,16 @@ main(int argc, char **argv)
211224
clip_lines == TRUE &&
212225
quote_char == ' ' &&
213226
escaped == FALSE)
214-
{
215-
/* trim spaces starting from the end */
216-
while (dst > out_line &&
217-
(*(dst - 1) == ' ' || *(dst - 1) == '\t'))
218-
dst--;
219-
prv_spaces = 0;
220-
}
227+
trim_trailing_whitespace(&prv_spaces, &dst, out_line);
221228
*(dst++) = *src;
222229
}
223230
col_in_tab %= tab_size;
224231
++src;
225232
}
226233
/* for cases where the last line of file has no newline */
227234
if (clip_lines == TRUE && escaped == FALSE)
228-
{
229-
while (dst > out_line &&
230-
(*(dst - 1) == ' ' || *(dst - 1) == '\t'))
231-
dst--;
232-
prv_spaces = 0;
233-
}
234-
/* output accumulated spaces */
235-
for (; prv_spaces > 0; prv_spaces--)
236-
*(dst++) = ' ';
235+
trim_trailing_whitespace(&prv_spaces, &dst, out_line);
236+
output_accumulated_spaces(&prv_spaces, &dst);
237237
*dst = NUL;
238238

239239
if (fputs(out_line, stdout) == EOF)

0 commit comments

Comments
 (0)