From 04c4676b7cd3de563e62e474841af206b631266a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 24 Mar 2024 20:31:00 +0900 Subject: [PATCH] internal/png: update with Go 1.22 --- internal/png/gen.go | 29 +++++++++++++++++++++++++++++ internal/png/stdlibreader.go | 16 ++++++++-------- internal/png/stdlibwriter.go | 6 +++--- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/internal/png/gen.go b/internal/png/gen.go index b28a2834e..4020ede23 100644 --- a/internal/png/gen.go +++ b/internal/png/gen.go @@ -24,6 +24,9 @@ import ( "go/token" "os" "path/filepath" + "regexp" + "runtime" + "strconv" "strings" exec "golang.org/x/sys/execabs" @@ -47,6 +50,20 @@ func pngFiles() ([]string, error) { } func run() error { + reVer := regexp.MustCompile(`^go1\.(\d+)(\.\d+)?$`) + verStr := runtime.Version() + m := reVer.FindStringSubmatch(verStr) + if m == nil { + return fmt.Errorf("png: unexpected Go version: %s", verStr) + } + ver, err := strconv.Atoi(m[1]) + if err != nil { + return err + } + if ver < 22 { + return fmt.Errorf("png: use Go 1.22 or newer") + } + dir, err := pngDir() if err != nil { return err @@ -115,6 +132,18 @@ func run() error { fmt.Fprintln(out, "// Code generated by gen.go. DO NOT EDIT.") fmt.Fprintln(out) format.Node(out, fset, tree) + + if f == "reader.go" { + // The min function was removed as of Go 1.22, but this is needed for old Go. + // TODO: Remove this when Go 1.21 is the minimum supported version. + fmt.Fprintln(out, ` +func min(a, b int) int { + if a < b { + return a + } + return b +}`) + } } return nil } diff --git a/internal/png/stdlibreader.go b/internal/png/stdlibreader.go index 6f8917735..d12bb769e 100644 --- a/internal/png/stdlibreader.go +++ b/internal/png/stdlibreader.go @@ -138,13 +138,6 @@ type UnsupportedError string func (e UnsupportedError) Error() string { return "png: unsupported feature: " + string(e) } -func min(a, b int) int { - if a < b { - return a - } - return b -} - func (d *decoder) parseIHDR(length uint32) error { if length != 13 { return FormatError("bad IHDR length") @@ -973,7 +966,7 @@ func (d *decoder) checkHeader() error { return nil } -// Decode reads a PNG image from r and returns it as an image.Image. +// Decode reads a PNG image from r and returns it as an [image.Image]. // The type of Image returned depends on the PNG contents. func Decode(r io.Reader) (image.Image, error) { d := &decoder{ @@ -1061,3 +1054,10 @@ func DecodeConfig(r io.Reader) (image.Config, error) { func init() { } + +func min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/internal/png/stdlibwriter.go b/internal/png/stdlibwriter.go index 95cd0d384..d169b4263 100644 --- a/internal/png/stdlibwriter.go +++ b/internal/png/stdlibwriter.go @@ -27,7 +27,7 @@ type Encoder struct { } // EncoderBufferPool is an interface for getting and returning temporary -// instances of the EncoderBuffer struct. This can be used to reuse buffers +// instances of the [EncoderBuffer] struct. This can be used to reuse buffers // when encoding multiple images. type EncoderBufferPool interface { Get() *EncoderBuffer @@ -192,7 +192,7 @@ func (e *encoder) writePLTEAndTRNS(p color.Palette) { // An encoder is an io.Writer that satisfies writes by writing PNG IDAT chunks, // including an 8-byte header and 4-byte CRC checksum per Write call. Such calls -// should be relatively infrequent, since writeIDATs uses a bufio.Writer. +// should be relatively infrequent, since writeIDATs uses a [bufio.Writer]. // // This method should only be called from writeIDATs (via writeImage). // No other code should treat an encoder as an io.Writer. @@ -588,7 +588,7 @@ func levelToZlib(l CompressionLevel) int { func (e *encoder) writeIEND() { e.writeChunk(nil, "IEND") } // Encode writes the Image m to w in PNG format. Any Image may be -// encoded, but images that are not image.NRGBA might be encoded lossily. +// encoded, but images that are not [image.NRGBA] might be encoded lossily. func Encode(w io.Writer, m image.Image) error { var e Encoder return e.Encode(w, m)