1
0
mirror of https://github.com/golang/go synced 2024-11-27 00:51:26 -07:00

strconv: use go generate to create isprint.go

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/132230043
This commit is contained in:
ChaiShushan 2014-08-28 14:55:02 -07:00 committed by Rob Pike
parent 882933f09b
commit 5db510f134
3 changed files with 50 additions and 26 deletions

View File

@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.
// DO NOT EDIT. GENERATED BY
// go run makeisprint.go >x && mv x isprint.go
// go run makeisprint.go -output isprint.go
package strconv

View File

@ -4,15 +4,26 @@
// +build ignore
// makeisprint generates the tables for strconv's compact isPrint.
//
// usage:
//
// go run makeisprint.go -output isprint.go
//
package main
import (
"bytes"
"flag"
"fmt"
"os"
"go/format"
"io/ioutil"
"log"
"unicode"
)
var filename = flag.String("output", "isprint.go", "output file name")
var (
range16 []uint16
except16 []uint16
@ -110,6 +121,8 @@ func to16(x []uint32) []uint16 {
}
func main() {
flag.Parse()
rang, except := scan(0, 0xFFFF)
range16 = to16(rang)
except16 = to16(except)
@ -117,49 +130,58 @@ func main() {
for i := rune(0); i <= unicode.MaxRune; i++ {
if isPrint(i) != unicode.IsPrint(i) {
fmt.Fprintf(os.Stderr, "%U: isPrint=%v, want %v\n", i, isPrint(i), unicode.IsPrint(i))
return
log.Fatalf("%U: isPrint=%v, want %v\n", i, isPrint(i), unicode.IsPrint(i))
}
}
fmt.Printf(`// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.` + "\n\n")
fmt.Printf("// DO NOT EDIT. GENERATED BY\n")
fmt.Printf("// go run makeisprint.go >x && mv x isprint.go\n\n")
fmt.Printf("package strconv\n\n")
var buf bytes.Buffer
fmt.Printf("// (%d+%d+%d)*2 + (%d)*4 = %d bytes\n\n",
fmt.Fprintf(&buf, `// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.`+"\n\n")
fmt.Fprintf(&buf, "// DO NOT EDIT. GENERATED BY\n")
fmt.Fprintf(&buf, "// go run makeisprint.go -output isprint.go\n\n")
fmt.Fprintf(&buf, "package strconv\n\n")
fmt.Fprintf(&buf, "// (%d+%d+%d)*2 + (%d)*4 = %d bytes\n\n",
len(range16), len(except16), len(except32),
len(range32),
(len(range16)+len(except16)+len(except32))*2+
(len(range32))*4)
fmt.Printf("var isPrint16 = []uint16{\n")
fmt.Fprintf(&buf, "var isPrint16 = []uint16{\n")
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 {
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 {
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 {
if r >= 0x20000 {
fmt.Fprintf(os.Stderr, "%U too big for isNotPrint32\n", r)
return
log.Fatalf("%U too big for isNotPrint32\n", r)
}
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")
}

View File

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run makeisprint.go -output isprint.go
package strconv
import (