-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (41 loc) · 719 Bytes
/
main.go
File metadata and controls
47 lines (41 loc) · 719 Bytes
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
// Read a file and print its lines in reverse order (byte-wise).
package main
import (
"solod.dev/so/bufio"
"solod.dev/so/flag"
"solod.dev/so/fmt"
"solod.dev/so/mem"
"solod.dev/so/os"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("usage: reverse <file>")
os.Exit(1)
}
f, err := os.Open(args[0])
if err != nil {
fmt.Println("error: could not open file")
os.Exit(1)
}
defer f.Close()
scanner := bufio.NewScanner(mem.System, &f)
defer scanner.Free()
for scanner.Scan() {
b := scanner.Bytes()
reverse(b)
fmt.Println(string(b))
}
}
func reverse(b []byte) {
i := 0
j := len(b) - 1
for i < j {
tmp := b[i]
b[i] = b[j]
b[j] = tmp
i++
j--
}
}