]> Cypherpunks repositories - gostls13.git/commitdiff
compress/flate: use go generate to create fixedhuff.go
authorChaiShushan <chaishushan@gmail.com>
Thu, 28 Aug 2014 22:04:03 +0000 (15:04 -0700)
committerRob Pike <r@golang.org>
Thu, 28 Aug 2014 22:04:03 +0000 (15:04 -0700)
LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/137750043

src/pkg/compress/flate/fixedhuff.go
src/pkg/compress/flate/gen.go
src/pkg/compress/flate/inflate.go

index 9be3d534954be4395260a02eb712fadbc81aa3a3..7df8b9a293f796637caa09df0678ea43bb2be702 100644 (file)
@@ -4,7 +4,7 @@
 
 package flate
 
-// autogenerated by gen.go, DO NOT EDIT
+// autogenerated by go run gen.go -output fixedhuff.go, DO NOT EDIT
 
 var fixedHuffmanDecoder = huffmanDecoder{
        7,
index 1427557f807e265cc84c006104450b5ae5dd83c8..6288ecddd0e601948b2f9ea6f8622c7a04ee7351 100644 (file)
@@ -7,14 +7,21 @@
 // This program generates fixedhuff.go
 // Invoke as
 //
-//      go run gen.go |gofmt >fixedhuff.go
+//     go run gen.go -output fixedhuff.go
 
 package main
 
 import (
+       "bytes"
+       "flag"
        "fmt"
+       "go/format"
+       "io/ioutil"
+       "log"
 )
 
+var filename = flag.String("output", "fixedhuff.go", "output file name")
+
 const maxCodeLen = 16
 
 // Note: the definition of the huffmanDecoder struct is copied from
@@ -113,6 +120,8 @@ func (h *huffmanDecoder) init(bits []int) bool {
 }
 
 func main() {
+       flag.Parse()
+
        var h huffmanDecoder
        var bits [288]int
        initReverseByte()
@@ -129,27 +138,43 @@ func main() {
                bits[i] = 8
        }
        h.init(bits[:])
-       fmt.Println("package flate")
-       fmt.Println()
-       fmt.Println("// autogenerated by gen.go, DO NOT EDIT")
-       fmt.Println()
-       fmt.Println("var fixedHuffmanDecoder = huffmanDecoder{")
-       fmt.Printf("\t%d,\n", h.min)
-       fmt.Println("\t[huffmanNumChunks]uint32{")
+
+       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.Fprintln(&buf, "package flate")
+       fmt.Fprintln(&buf)
+       fmt.Fprintln(&buf, "// autogenerated by go run gen.go -output fixedhuff.go, DO NOT EDIT")
+       fmt.Fprintln(&buf)
+       fmt.Fprintln(&buf, "var fixedHuffmanDecoder = huffmanDecoder{")
+       fmt.Fprintf(&buf, "\t%d,\n", h.min)
+       fmt.Fprintln(&buf, "\t[huffmanNumChunks]uint32{")
        for i := 0; i < huffmanNumChunks; i++ {
                if i&7 == 0 {
-                       fmt.Printf("\t\t")
+                       fmt.Fprintf(&buf, "\t\t")
                } else {
-                       fmt.Printf(" ")
+                       fmt.Fprintf(&buf, " ")
                }
-               fmt.Printf("0x%04x,", h.chunks[i])
+               fmt.Fprintf(&buf, "0x%04x,", h.chunks[i])
                if i&7 == 7 {
-                       fmt.Println()
+                       fmt.Fprintln(&buf)
                }
        }
-       fmt.Println("\t},")
-       fmt.Println("\tnil, 0,")
-       fmt.Println("}")
+       fmt.Fprintln(&buf, "\t},")
+       fmt.Fprintln(&buf, "\tnil, 0,")
+       fmt.Fprintln(&buf, "}")
+
+       data, err := format.Source(buf.Bytes())
+       if err != nil {
+               log.Fatal(err)
+       }
+       err = ioutil.WriteFile(*filename, data, 0644)
+       if err != nil {
+               log.Fatal(err)
+       }
 }
 
 var reverseByte [256]byte
index ce4923eca3738f6a00ed0ce99439534dafe0bf14..769ef4299790e6437e7d66b47494b162ccd877ea 100644 (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 gen.go -output fixedhuff.go
+
 // Package flate implements the DEFLATE compressed data format, described in
 // RFC 1951.  The gzip and zlib packages implement access to DEFLATE-based file
 // formats.