forked from dlang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detab.d
65 lines (56 loc) · 1.35 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
62
63
64
65
/* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
/* 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];
}