-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (66 loc) · 1.29 KB
/
main.go
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
66
67
68
69
70
71
72
73
package main
import (
"bufio"
"fmt"
"io"
"os"
)
// cleanFile accepts a string representing a path
// and converts \r\n into \n
func cleanFile(fileName string) {
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
wFile, err := os.Create(fileName + ".clean")
if err != nil {
panic(err)
}
defer wFile.Close()
w := bufio.NewWriter(wFile)
data := make([]byte, 128)
for {
data = data[:cap(data)]
n, err := file.Read(data)
if err != nil {
if err == io.EOF {
break
}
}
data = data[:n]
var indexesToDelete []int
for i, b := range data {
if b == '\r' {
if i+1 < len(data) {
if data[i+1] == '\n' {
indexesToDelete = append(indexesToDelete, i)
}
}
data[i] = '\n'
}
}
data = removeFromSlice(data, indexesToDelete)
w.Write(data)
}
w.Flush()
}
// removeFromSlice accepts a byte slice and an integer slice
// and deletes each index from the integer slice from the
// byte slice
func removeFromSlice(data []byte, indexes []int) []byte {
for _, i := range indexes {
data = append(data[:i], data[i+1:]...)
}
return data
}
func main() {
args := os.Args
if len(args) < 2 {
fmt.Println("Please specify one or more files as input")
os.Exit(1)
}
for _, fileName := range args[1:] {
cleanFile(fileName)
}
}