From b50dd6090d2f6f12a8115fea354d384225574eaf Mon Sep 17 00:00:00 2001 From: ChaiShushan Date: Thu, 28 Aug 2014 15:04:03 -0700 Subject: [PATCH] compress/flate: use go generate to create fixedhuff.go LGTM=r R=golang-codereviews, r CC=golang-codereviews https://golang.org/cl/137750043 --- src/pkg/compress/flate/fixedhuff.go | 2 +- src/pkg/compress/flate/gen.go | 55 +++++++++++++++++++++-------- src/pkg/compress/flate/inflate.go | 2 ++ 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/pkg/compress/flate/fixedhuff.go b/src/pkg/compress/flate/fixedhuff.go index 9be3d53495..7df8b9a293 100644 --- a/src/pkg/compress/flate/fixedhuff.go +++ b/src/pkg/compress/flate/fixedhuff.go @@ -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, diff --git a/src/pkg/compress/flate/gen.go b/src/pkg/compress/flate/gen.go index 1427557f80..6288ecddd0 100644 --- a/src/pkg/compress/flate/gen.go +++ b/src/pkg/compress/flate/gen.go @@ -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 diff --git a/src/pkg/compress/flate/inflate.go b/src/pkg/compress/flate/inflate.go index ce4923eca3..769ef42997 100644 --- a/src/pkg/compress/flate/inflate.go +++ b/src/pkg/compress/flate/inflate.go @@ -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. -- 2.50.0