makeisprint.go
4.25 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2012 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.
// +build ignore
//
// usage:
//
// go run makeisprint.go -output isprint.go
//
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"unicode"
)
var filename = flag.String("output", "isprint.go", "output file name")
var (
range16 []uint16
except16 []uint16
range32 []uint32
except32 []uint32
)
// bsearch16 returns the smallest i such that a[i] >= x.
// If there is no such i, bsearch16 returns len(a).
func bsearch16(a []uint16, x uint16) int {
i, j := 0, len(a)
for i < j {
h := i + (j-i)/2
if a[h] < x {
i = h + 1
} else {
j = h
}
}
return i
}
// bsearch32 returns the smallest i such that a[i] >= x.
// If there is no such i, bsearch32 returns len(a).
func bsearch32(a []uint32, x uint32) int {
i, j := 0, len(a)
for i < j {
h := i + (j-i)/2
if a[h] < x {
i = h + 1
} else {
j = h
}
}
return i
}
func isPrint(r rune) bool {
// Same algorithm, either on uint16 or uint32 value.
// First, find first i such that rang[i] >= x.
// This is the index of either the start or end of a pair that might span x.
// The start is even (rang[i&^1]) and the end is odd (rang[i|1]).
// If we find x in a range, make sure x is not in exception list.
if 0 <= r && r < 1<<16 {
rr, rang, except := uint16(r), range16, except16
i := bsearch16(rang, rr)
if i >= len(rang) || rr < rang[i&^1] || rang[i|1] < rr {
return false
}
j := bsearch16(except, rr)
return j >= len(except) || except[j] != rr
}
rr, rang, except := uint32(r), range32, except32
i := bsearch32(rang, rr)
if i >= len(rang) || rr < rang[i&^1] || rang[i|1] < rr {
return false
}
j := bsearch32(except, rr)
return j >= len(except) || except[j] != rr
}
func scan(min, max rune) (rang, except []uint32) {
lo := rune(-1)
for i := min; ; i++ {
if (i > max || !unicode.IsPrint(i)) && lo >= 0 {
// End range, but avoid flip flop.
if i+1 <= max && unicode.IsPrint(i+1) {
except = append(except, uint32(i))
continue
}
rang = append(rang, uint32(lo), uint32(i-1))
lo = -1
}
if i > max {
break
}
if lo < 0 && unicode.IsPrint(i) {
lo = i
}
}
return
}
func to16(x []uint32) []uint16 {
var y []uint16
for _, v := range x {
if uint32(uint16(v)) != v {
panic("bad 32->16 conversion")
}
y = append(y, uint16(v))
}
return y
}
func main() {
flag.Parse()
rang, except := scan(0, 0xFFFF)
range16 = to16(rang)
except16 = to16(except)
range32, except32 = scan(0x10000, unicode.MaxRune)
for i := rune(0); i <= unicode.MaxRune; i++ {
if isPrint(i) != unicode.IsPrint(i) {
log.Fatalf("%U: isPrint=%v, want %v\n", i, isPrint(i), unicode.IsPrint(i))
}
}
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
// 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.Fprintf(&buf, "var isPrint16 = []uint16{\n")
for i := 0; i < len(range16); i += 2 {
fmt.Fprintf(&buf, "\t%#04x, %#04x,\n", range16[i], range16[i+1])
}
fmt.Fprintf(&buf, "}\n\n")
fmt.Fprintf(&buf, "var isNotPrint16 = []uint16{\n")
for _, r := range except16 {
fmt.Fprintf(&buf, "\t%#04x,\n", r)
}
fmt.Fprintf(&buf, "}\n\n")
fmt.Fprintf(&buf, "var isPrint32 = []uint32{\n")
for i := 0; i < len(range32); i += 2 {
fmt.Fprintf(&buf, "\t%#06x, %#06x,\n", range32[i], range32[i+1])
}
fmt.Fprintf(&buf, "}\n\n")
fmt.Fprintf(&buf, "var isNotPrint32 = []uint16{ // add 0x10000 to each entry\n")
for _, r := range except32 {
if r >= 0x20000 {
log.Fatalf("%U too big for isNotPrint32\n", r)
}
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)
}
}