-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (52 loc) · 990 Bytes
/
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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"unicode/utf8"
)
var (
quiet = flag.Bool("q", false, "Don't print messages telling which files are invalid UTF-8, merely indicate it with the exit status.")
)
func check(name string, br *bufio.Reader) {
l, c, o := 1, 1, 0
for {
r, size, err := br.ReadRune()
o += size
if r == 0xfffd || !utf8.ValidRune(r) {
err = errors.New("invalid UTF-8 code")
}
if err != nil {
if err != io.EOF {
if !*quiet {
fmt.Fprintf(os.Stderr, "%s: line %d, char %d, byte offset %d: %s\n", name, l, c, o, err)
}
os.Exit(1)
}
return
}
if r == '\n' {
l++
c = 1
}
}
}
func main() {
flag.Parse()
if flag.NArg() == 0 {
check("stdin", bufio.NewReader(os.Stdin))
os.Exit(0)
}
for _, arg := range flag.Args() {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s: %s\n", os.Args[0], arg, err)
os.Exit(1)
}
check(arg, bufio.NewReader(f))
f.Close()
}
}