Commit b50dd609 authored by ChaiShushan's avatar ChaiShushan Committed by Rob Pike

compress/flate: use go generate to create fixedhuff.go

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/137750043
parent 5db510f1
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
package flate package flate
// autogenerated by gen.go, DO NOT EDIT // autogenerated by go run gen.go -output fixedhuff.go, DO NOT EDIT
var fixedHuffmanDecoder = huffmanDecoder{ var fixedHuffmanDecoder = huffmanDecoder{
7, 7,
......
...@@ -7,14 +7,21 @@ ...@@ -7,14 +7,21 @@
// This program generates fixedhuff.go // This program generates fixedhuff.go
// Invoke as // Invoke as
// //
// go run gen.go |gofmt >fixedhuff.go // go run gen.go -output fixedhuff.go
package main package main
import ( import (
"bytes"
"flag"
"fmt" "fmt"
"go/format"
"io/ioutil"
"log"
) )
var filename = flag.String("output", "fixedhuff.go", "output file name")
const maxCodeLen = 16 const maxCodeLen = 16
// Note: the definition of the huffmanDecoder struct is copied from // Note: the definition of the huffmanDecoder struct is copied from
...@@ -113,6 +120,8 @@ func (h *huffmanDecoder) init(bits []int) bool { ...@@ -113,6 +120,8 @@ func (h *huffmanDecoder) init(bits []int) bool {
} }
func main() { func main() {
flag.Parse()
var h huffmanDecoder var h huffmanDecoder
var bits [288]int var bits [288]int
initReverseByte() initReverseByte()
...@@ -129,27 +138,43 @@ func main() { ...@@ -129,27 +138,43 @@ func main() {
bits[i] = 8 bits[i] = 8
} }
h.init(bits[:]) h.init(bits[:])
fmt.Println("package flate")
fmt.Println() var buf bytes.Buffer
fmt.Println("// autogenerated by gen.go, DO NOT EDIT")
fmt.Println() fmt.Fprintf(&buf, `// Copyright 2013 The Go Authors. All rights reserved.
fmt.Println("var fixedHuffmanDecoder = huffmanDecoder{") // Use of this source code is governed by a BSD-style
fmt.Printf("\t%d,\n", h.min) // license that can be found in the LICENSE file.`+"\n\n")
fmt.Println("\t[huffmanNumChunks]uint32{")
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++ { for i := 0; i < huffmanNumChunks; i++ {
if i&7 == 0 { if i&7 == 0 {
fmt.Printf("\t\t") fmt.Fprintf(&buf, "\t\t")
} else { } 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 { if i&7 == 7 {
fmt.Println() fmt.Fprintln(&buf)
} }
} }
fmt.Println("\t},") fmt.Fprintln(&buf, "\t},")
fmt.Println("\tnil, 0,") fmt.Fprintln(&buf, "\tnil, 0,")
fmt.Println("}") 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 var reverseByte [256]byte
......
...@@ -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 gen.go -output fixedhuff.go
// Package flate implements the DEFLATE compressed data format, described in // Package flate implements the DEFLATE compressed data format, described in
// RFC 1951. The gzip and zlib packages implement access to DEFLATE-based file // RFC 1951. The gzip and zlib packages implement access to DEFLATE-based file
// formats. // formats.
......
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