forked from dlang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tolf.d
55 lines (45 loc) · 1 KB
/
tolf.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
/* 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 line endings with LF
*/
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;
for (size_t i = 0; i < input.length; i++)
{
auto c = input[i];
switch (c)
{
case '\r':
c = '\n';
break;
case '\n':
if (i && input[i - 1] == '\r')
continue;
break;
case 0:
continue;
default:
break;
}
output ~= c;
j++;
}
return output[0 .. j];
}