forked from dlang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detab.d
61 lines (52 loc) · 1.18 KB
/
detab.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Replace tabs with spaces, and remove trailing whitespace from lines.
*/
import std.file;
import std.path;
int main(string[] args)
{
foreach (f; args[1 .. $])
{
auto input = cast(char[]) std.file.read(f);
auto output = filter(input);
if (output != input)
std.file.write(f, output);
}
return 0;
}
char[] filter(char[] input)
{
char[] output;
size_t j;
int column;
for (size_t i = 0; i < input.length; i++)
{
auto c = input[i];
switch (c)
{
case '\t':
while ((column & 7) != 7)
{ output ~= ' ';
j++;
column++;
}
c = ' ';
column++;
break;
case '\r':
case '\n':
while (j && output[j - 1] == ' ')
j--;
output = output[0 .. j];
column = 0;
break;
default:
column++;
break;
}
output ~= c;
j++;
}
while (j && output[j - 1] == ' ')
j--;
return output[0 .. j];
}