Debian Codegraph @

utf8/cmd/valid/main.go

raw
1 package main
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "os"
7 "regexp"
8 "strconv"
9 "strings"
10 stdlib "unicode/utf8"
11
12 "github.com/segmentio/asm/ascii"
13 "github.com/segmentio/asm/utf8"
14 )
15
16 func main() {
17 var data []byte
18 if len(os.Args) > 1 {
19 s := os.Args[1]
20 s, err := strconv.Unquote(`"` + s + `"`)
21 if err != nil {
22 panic(err)
23 }
24 data = []byte(s)
25 } else {
26 var err error
27 data, err = ioutil.ReadAll(os.Stdin)
28 if err != nil {
29 panic(err)
30 }
31 }
32
33 s := string(data)
34 lines := strings.Split(s, "\n")
35 if len(lines) > 0 && strings.HasPrefix(lines[0], "go test fuzz") {
36 fmt.Println("Got fuzzer input")
37 // TODO: parse with go/parse instead of regexp?
38 r := regexp.MustCompile(`^\[\]byte\((.+)\)`)
39 results := r.FindStringSubmatch(lines[1])
40 s, err := strconv.Unquote(results[1])
41 if err != nil {
42 panic(err)
43 }
44 data = []byte(s)
45 }
46
47 fmt.Println(string(data))
48 fmt.Println(data)
49 fmt.Println(len(data), "bytes")
50
51 uref := stdlib.Valid(data)
52 aref := ascii.Valid(data)
53 fmt.Println("stdlib: utf8:", uref, "ascii:", aref)
54
55 v := utf8.Validate(data)
56 fmt.Println("valid: utf8:", v.IsUTF8(), "ascii:", v.IsASCII(), "v:", v)
57
58 if uref != v.IsUTF8() || aref != v.IsASCII() {
59 os.Exit(1)
60 }
61 }