Commit 5db510f1 authored by ChaiShushan's avatar ChaiShushan Committed by Rob Pike

strconv: use go generate to create isprint.go

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/132230043
parent 882933f0
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// DO NOT EDIT. GENERATED BY // DO NOT EDIT. GENERATED BY
// go run makeisprint.go >x && mv x isprint.go // go run makeisprint.go -output isprint.go
package strconv package strconv
......
...@@ -4,15 +4,26 @@ ...@@ -4,15 +4,26 @@
// +build ignore // +build ignore
// makeisprint generates the tables for strconv's compact isPrint. //
// usage:
//
// go run makeisprint.go -output isprint.go
//
package main package main
import ( import (
"bytes"
"flag"
"fmt" "fmt"
"os" "go/format"
"io/ioutil"
"log"
"unicode" "unicode"
) )
var filename = flag.String("output", "isprint.go", "output file name")
var ( var (
range16 []uint16 range16 []uint16
except16 []uint16 except16 []uint16
...@@ -110,6 +121,8 @@ func to16(x []uint32) []uint16 { ...@@ -110,6 +121,8 @@ func to16(x []uint32) []uint16 {
} }
func main() { func main() {
flag.Parse()
rang, except := scan(0, 0xFFFF) rang, except := scan(0, 0xFFFF)
range16 = to16(rang) range16 = to16(rang)
except16 = to16(except) except16 = to16(except)
...@@ -117,49 +130,58 @@ func main() { ...@@ -117,49 +130,58 @@ func main() {
for i := rune(0); i <= unicode.MaxRune; i++ { for i := rune(0); i <= unicode.MaxRune; i++ {
if isPrint(i) != unicode.IsPrint(i) { if isPrint(i) != unicode.IsPrint(i) {
fmt.Fprintf(os.Stderr, "%U: isPrint=%v, want %v\n", i, isPrint(i), unicode.IsPrint(i)) log.Fatalf("%U: isPrint=%v, want %v\n", i, isPrint(i), unicode.IsPrint(i))
return
} }
} }
fmt.Printf(`// Copyright 2013 The Go Authors. All rights reserved. var buf bytes.Buffer
fmt.Fprintf(&buf, `// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.` + "\n\n") // license that can be found in the LICENSE file.`+"\n\n")
fmt.Printf("// DO NOT EDIT. GENERATED BY\n") fmt.Fprintf(&buf, "// DO NOT EDIT. GENERATED BY\n")
fmt.Printf("// go run makeisprint.go >x && mv x isprint.go\n\n") fmt.Fprintf(&buf, "// go run makeisprint.go -output isprint.go\n\n")
fmt.Printf("package strconv\n\n") fmt.Fprintf(&buf, "package strconv\n\n")
fmt.Printf("// (%d+%d+%d)*2 + (%d)*4 = %d bytes\n\n", fmt.Fprintf(&buf, "// (%d+%d+%d)*2 + (%d)*4 = %d bytes\n\n",
len(range16), len(except16), len(except32), len(range16), len(except16), len(except32),
len(range32), len(range32),
(len(range16)+len(except16)+len(except32))*2+ (len(range16)+len(except16)+len(except32))*2+
(len(range32))*4) (len(range32))*4)
fmt.Printf("var isPrint16 = []uint16{\n") fmt.Fprintf(&buf, "var isPrint16 = []uint16{\n")
for i := 0; i < len(range16); i += 2 { for i := 0; i < len(range16); i += 2 {
fmt.Printf("\t%#04x, %#04x,\n", range16[i], range16[i+1]) fmt.Fprintf(&buf, "\t%#04x, %#04x,\n", range16[i], range16[i+1])
} }
fmt.Printf("}\n\n") fmt.Fprintf(&buf, "}\n\n")
fmt.Printf("var isNotPrint16 = []uint16{\n") fmt.Fprintf(&buf, "var isNotPrint16 = []uint16{\n")
for _, r := range except16 { for _, r := range except16 {
fmt.Printf("\t%#04x,\n", r) fmt.Fprintf(&buf, "\t%#04x,\n", r)
} }
fmt.Printf("}\n\n") fmt.Fprintf(&buf, "}\n\n")
fmt.Printf("var isPrint32 = []uint32{\n") fmt.Fprintf(&buf, "var isPrint32 = []uint32{\n")
for i := 0; i < len(range32); i += 2 { for i := 0; i < len(range32); i += 2 {
fmt.Printf("\t%#06x, %#06x,\n", range32[i], range32[i+1]) fmt.Fprintf(&buf, "\t%#06x, %#06x,\n", range32[i], range32[i+1])
} }
fmt.Printf("}\n\n") fmt.Fprintf(&buf, "}\n\n")
fmt.Printf("var isNotPrint32 = []uint16{ // add 0x10000 to each entry\n") fmt.Fprintf(&buf, "var isNotPrint32 = []uint16{ // add 0x10000 to each entry\n")
for _, r := range except32 { for _, r := range except32 {
if r >= 0x20000 { if r >= 0x20000 {
fmt.Fprintf(os.Stderr, "%U too big for isNotPrint32\n", r) log.Fatalf("%U too big for isNotPrint32\n", r)
return
} }
fmt.Printf("\t%#04x,\n", r-0x10000) fmt.Fprintf(&buf, "\t%#04x,\n", r-0x10000)
}
fmt.Fprintf(&buf, "}\n")
data, err := format.Source(buf.Bytes())
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(*filename, data, 0644)
if err != nil {
log.Fatal(err)
} }
fmt.Printf("}\n")
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
//go:generate go run makeisprint.go -output isprint.go
package strconv package strconv
import ( import (
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment