Debian Codegraph @

vendor/golang.org/x/text/internal/gen/code.go

raw
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package gen
6
7 import (
8 "bytes"
9 "encoding/gob"
10 "fmt"
11 "hash"
12 "hash/fnv"
13 "io"
14 "log"
15 "os"
16 "reflect"
17 "strings"
18 "unicode"
19 "unicode/utf8"
20 )
21
22 // This file contains utilities for generating code.
23
24 // TODO: other write methods like:
25 // - slices, maps, types, etc.
26
27 // CodeWriter is a utility for writing structured code. It computes the content
28 // hash and size of written content. It ensures there are newlines between
29 // written code blocks.
30 type CodeWriter struct {
31 buf bytes.Buffer
32 Size int
33 Hash hash.Hash32 // content hash
34 gob *gob.Encoder
35 // For comments we skip the usual one-line separator if they are followed by
36 // a code block.
37 skipSep bool
38 }
39
40 func (w *CodeWriter) Write(p []byte) (n int, err error) {
41 return w.buf.Write(p)
42 }
43
44 // NewCodeWriter returns a new CodeWriter.
45 func NewCodeWriter() *CodeWriter {
46 h := fnv.New32()
47 return &CodeWriter{Hash: h, gob: gob.NewEncoder(h)}
48 }
49
50 // WriteGoFile appends the buffer with the total size of all created structures
51 // and writes it as a Go file to the the given file with the given package name.
52 func (w *CodeWriter) WriteGoFile(filename, pkg string) {
53 f, err := os.Create(filename)
54 if err != nil {
55 log.Fatalf("Could not create file %s: %v", filename, err)
56 }
57 defer f.Close()
58 if _, err = w.WriteGo(f, pkg, ""); err != nil {
59 log.Fatalf("Error writing file %s: %v", filename, err)
60 }
61 }
62
63 // WriteVersionedGoFile appends the buffer with the total size of all created
64 // structures and writes it as a Go file to the the given file with the given
65 // package name and build tags for the current Unicode version,
66 func (w *CodeWriter) WriteVersionedGoFile(filename, pkg string) {
67 tags := buildTags()
68 if tags != "" {
69 filename = insertVersion(filename, UnicodeVersion())
70 }
71 f, err := os.Create(filename)
72 if err != nil {
73 log.Fatalf("Could not create file %s: %v", filename, err)
74 }
75 defer f.Close()
76 if _, err = w.WriteGo(f, pkg, tags); err != nil {
77 log.Fatalf("Error writing file %s: %v", filename, err)
78 }
79 }
80
81 // WriteGo appends the buffer with the total size of all created structures and
82 // writes it as a Go file to the the given writer with the given package name.
83 func (w *CodeWriter) WriteGo(out io.Writer, pkg, tags string) (n int, err error) {
84 sz := w.Size
85 w.WriteComment("Total table size %d bytes (%dKiB); checksum: %X\n", sz, sz/1024, w.Hash.Sum32())
86 defer w.buf.Reset()
87 return WriteGo(out, pkg, tags, w.buf.Bytes())
88 }
89
90 func (w *CodeWriter) printf(f string, x ...interface{}) {
91 fmt.Fprintf(w, f, x...)
92 }
93
94 func (w *CodeWriter) insertSep() {
95 if w.skipSep {
96 w.skipSep = false
97 return
98 }
99 // Use at least two newlines to ensure a blank space between the previous
100 // block. WriteGoFile will remove extraneous newlines.
101 w.printf("\n\n")
102 }
103
104 // WriteComment writes a comment block. All line starts are prefixed with "//".
105 // Initial empty lines are gobbled. The indentation for the first line is
106 // stripped from consecutive lines.
107 func (w *CodeWriter) WriteComment(comment string, args ...interface{}) {
108 s := fmt.Sprintf(comment, args...)
109 s = strings.Trim(s, "\n")
110
111 // Use at least two newlines to ensure a blank space between the previous
112 // block. WriteGoFile will remove extraneous newlines.
113 w.printf("\n\n// ")
114 w.skipSep = true
115
116 // strip first indent level.
117 sep := "\n"
118 for ; len(s) > 0 && (s[0] == '\t' || s[0] == ' '); s = s[1:] {
119 sep += s[:1]
120 }
121
122 strings.NewReplacer(sep, "\n// ", "\n", "\n// ").WriteString(w, s)
123
124 w.printf("\n")
125 }
126
127 func (w *CodeWriter) writeSizeInfo(size int) {
128 w.printf("// Size: %d bytes\n", size)
129 }
130
131 // WriteConst writes a constant of the given name and value.
132 func (w *CodeWriter) WriteConst(name string, x interface{}) {
133 w.insertSep()
134 v := reflect.ValueOf(x)
135
136 switch v.Type().Kind() {
137 case reflect.String:
138 w.printf("const %s %s = ", name, typeName(x))
139 w.WriteString(v.String())
140 w.printf("\n")
141 default:
142 w.printf("const %s = %#v\n", name, x)
143 }
144 }
145
146 // WriteVar writes a variable of the given name and value.
147 func (w *CodeWriter) WriteVar(name string, x interface{}) {
148 w.insertSep()
149 v := reflect.ValueOf(x)
150 oldSize := w.Size
151 sz := int(v.Type().Size())
152 w.Size += sz
153
154 switch v.Type().Kind() {
155 case reflect.String:
156 w.printf("var %s %s = ", name, typeName(x))
157 w.WriteString(v.String())
158 case reflect.Struct:
159 w.gob.Encode(x)
160 fallthrough
161 case reflect.Slice, reflect.Array:
162 w.printf("var %s = ", name)
163 w.writeValue(v)
164 w.writeSizeInfo(w.Size - oldSize)
165 default:
166 w.printf("var %s %s = ", name, typeName(x))
167 w.gob.Encode(x)
168 w.writeValue(v)
169 w.writeSizeInfo(w.Size - oldSize)
170 }
171 w.printf("\n")
172 }
173
174 func (w *CodeWriter) writeValue(v reflect.Value) {
175 x := v.Interface()
176 switch v.Kind() {
177 case reflect.String:
178 w.WriteString(v.String())
179 case reflect.Array:
180 // Don't double count: callers of WriteArray count on the size being
181 // added, so we need to discount it here.
182 w.Size -= int(v.Type().Size())
183 w.writeSlice(x, true)
184 case reflect.Slice:
185 w.writeSlice(x, false)
186 case reflect.Struct:
187 w.printf("%s{\n", typeName(v.Interface()))
188 t := v.Type()
189 for i := 0; i < v.NumField(); i++ {
190 w.printf("%s: ", t.Field(i).Name)
191 w.writeValue(v.Field(i))
192 w.printf(",\n")
193 }
194 w.printf("}")
195 default:
196 w.printf("%#v", x)
197 }
198 }
199
200 // WriteString writes a string literal.
201 func (w *CodeWriter) WriteString(s string) {
202 s = strings.Replace(s, `\`, `\\`, -1)
203 io.WriteString(w.Hash, s) // content hash
204 w.Size += len(s)
205
206 const maxInline = 40
207 if len(s) <= maxInline {
208 w.printf("%q", s)
209 return
210 }
211
212 // We will render the string as a multi-line string.
213 const maxWidth = 80 - 4 - len(`"`) - len(`" +`)
214
215 // When starting on its own line, go fmt indents line 2+ an extra level.
216 n, max := maxWidth, maxWidth-4
217
218 // As per https://golang.org/issue/18078, the compiler has trouble
219 // compiling the concatenation of many strings, s0 + s1 + s2 + ... + sN,
220 // for large N. We insert redundant, explicit parentheses to work around
221 // that, lowering the N at any given step: (s0 + s1 + ... + s63) + (s64 +
222 // ... + s127) + etc + (etc + ... + sN).
223 explicitParens, extraComment := len(s) > 128*1024, ""
224 if explicitParens {
225 w.printf(`(`)
226 extraComment = "; the redundant, explicit parens are for https://golang.org/issue/18078"
227 }
228
229 // Print "" +\n, if a string does not start on its own line.
230 b := w.buf.Bytes()
231 if p := len(bytes.TrimRight(b, " \t")); p > 0 && b[p-1] != '\n' {
232 w.printf("\"\" + // Size: %d bytes%s\n", len(s), extraComment)
233 n, max = maxWidth, maxWidth
234 }
235
236 w.printf(`"`)
237
238 for sz, p, nLines := 0, 0, 0; p < len(s); {
239 var r rune
240 r, sz = utf8.DecodeRuneInString(s[p:])
241 out := s[p : p+sz]
242 chars := 1
243 if !unicode.IsPrint(r) || r == utf8.RuneError || r == '"' {
244 switch sz {
245 case 1:
246 out = fmt.Sprintf("\\x%02x", s[p])
247 case 2, 3:
248 out = fmt.Sprintf("\\u%04x", r)
249 case 4:
250 out = fmt.Sprintf("\\U%08x", r)
251 }
252 chars = len(out)
253 }
254 if n -= chars; n < 0 {
255 nLines++
256 if explicitParens && nLines&63 == 63 {
257 w.printf("\") + (\"")
258 }
259 w.printf("\" +\n\"")
260 n = max - len(out)
261 }
262 w.printf("%s", out)
263 p += sz
264 }
265 w.printf(`"`)
266 if explicitParens {
267 w.printf(`)`)
268 }
269 }
270
271 // WriteSlice writes a slice value.
272 func (w *CodeWriter) WriteSlice(x interface{}) {
273 w.writeSlice(x, false)
274 }
275
276 // WriteArray writes an array value.
277 func (w *CodeWriter) WriteArray(x interface{}) {
278 w.writeSlice(x, true)
279 }
280
281 func (w *CodeWriter) writeSlice(x interface{}, isArray bool) {
282 v := reflect.ValueOf(x)
283 w.gob.Encode(v.Len())
284 w.Size += v.Len() * int(v.Type().Elem().Size())
285 name := typeName(x)
286 if isArray {
287 name = fmt.Sprintf("[%d]%s", v.Len(), name[strings.Index(name, "]")+1:])
288 }
289 if isArray {
290 w.printf("%s{\n", name)
291 } else {
292 w.printf("%s{ // %d elements\n", name, v.Len())
293 }
294
295 switch kind := v.Type().Elem().Kind(); kind {
296 case reflect.String:
297 for _, s := range x.([]string) {
298 w.WriteString(s)
299 w.printf(",\n")
300 }
301 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
302 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
303 // nLine and nBlock are the number of elements per line and block.
304 nLine, nBlock, format := 8, 64, "%d,"
305 switch kind {
306 case reflect.Uint8:
307 format = "%#02x,"
308 case reflect.Uint16:
309 format = "%#04x,"
310 case reflect.Uint32:
311 nLine, nBlock, format = 4, 32, "%#08x,"
312 case reflect.Uint, reflect.Uint64:
313 nLine, nBlock, format = 4, 32, "%#016x,"
314 case reflect.Int8:
315 nLine = 16
316 }
317 n := nLine
318 for i := 0; i < v.Len(); i++ {
319 if i%nBlock == 0 && v.Len() > nBlock {
320 w.printf("// Entry %X - %X\n", i, i+nBlock-1)
321 }
322 x := v.Index(i).Interface()
323 w.gob.Encode(x)
324 w.printf(format, x)
325 if n--; n == 0 {
326 n = nLine
327 w.printf("\n")
328 }
329 }
330 w.printf("\n")
331 case reflect.Struct:
332 zero := reflect.Zero(v.Type().Elem()).Interface()
333 for i := 0; i < v.Len(); i++ {
334 x := v.Index(i).Interface()
335 w.gob.EncodeValue(v)
336 if !reflect.DeepEqual(zero, x) {
337 line := fmt.Sprintf("%#v,\n", x)
338 line = line[strings.IndexByte(line, '{'):]
339 w.printf("%d: ", i)
340 w.printf(line)
341 }
342 }
343 case reflect.Array:
344 for i := 0; i < v.Len(); i++ {
345 w.printf("%d: %#v,\n", i, v.Index(i).Interface())
346 }
347 default:
348 panic("gen: slice elem type not supported")
349 }
350 w.printf("}")
351 }
352
353 // WriteType writes a definition of the type of the given value and returns the
354 // type name.
355 func (w *CodeWriter) WriteType(x interface{}) string {
356 t := reflect.TypeOf(x)
357 w.printf("type %s struct {\n", t.Name())
358 for i := 0; i < t.NumField(); i++ {
359 w.printf("\t%s %s\n", t.Field(i).Name, t.Field(i).Type)
360 }
361 w.printf("}\n")
362 return t.Name()
363 }
364
365 // typeName returns the name of the go type of x.
366 func typeName(x interface{}) string {
367 t := reflect.ValueOf(x).Type()
368 return strings.Replace(fmt.Sprint(t), "main.", "", 1)
369 }