From a2a2e44979319a246e154d8a4b52bb3de5d4ead6 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Tue, 7 Jan 2025 00:30:09 +0800 Subject: [PATCH] refactor: use built-in `min` function (#1038) We can use the built-in `min` function since Go 1.21. Reference: https://go.dev/ref/spec#Min_and_max Signed-off-by: Eng Zer Jun --- snappy/xerial/xerial.go | 7 ------- zip/writer.go | 15 ++++----------- zip/zip_test.go | 7 ------- 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/snappy/xerial/xerial.go b/snappy/xerial/xerial.go index a4a8fb638d..7b7ed2ee26 100644 --- a/snappy/xerial/xerial.go +++ b/snappy/xerial/xerial.go @@ -115,13 +115,6 @@ func EncodeBetter(dst, src []byte) []byte { return dst } -func min(x, y int) int { - if x < y { - return x - } - return y -} - const ( sizeOffset = 16 sizeBytes = 4 diff --git a/zip/writer.go b/zip/writer.go index b85bb91d86..e33df2431c 100644 --- a/zip/writer.go +++ b/zip/writer.go @@ -406,8 +406,8 @@ func writeHeader(w io.Writer, h *header) error { // flags. if h.raw && !h.hasDataDescriptor() { b.uint32(h.CRC32) - b.uint32(uint32(min64(h.CompressedSize64, uint32max))) - b.uint32(uint32(min64(h.UncompressedSize64, uint32max))) + b.uint32(uint32(min(h.CompressedSize64, uint32max))) + b.uint32(uint32(min(h.UncompressedSize64, uint32max))) } else { // When this package handle the compression, these values are // always written to the trailing data descriptor. @@ -427,13 +427,6 @@ func writeHeader(w io.Writer, h *header) error { return err } -func min64(x, y uint64) uint64 { - if x < y { - return x - } - return y -} - // CreateRaw adds a file to the zip archive using the provided [FileHeader] and // returns a [Writer] to which the file contents should be written. The file's // contents must be written to the io.Writer before the next call to [Writer.Create], @@ -445,8 +438,8 @@ func (w *Writer) CreateRaw(fh *FileHeader) (io.Writer, error) { return nil, err } - fh.CompressedSize = uint32(min64(fh.CompressedSize64, uint32max)) - fh.UncompressedSize = uint32(min64(fh.UncompressedSize64, uint32max)) + fh.CompressedSize = uint32(min(fh.CompressedSize64, uint32max)) + fh.UncompressedSize = uint32(min(fh.UncompressedSize64, uint32max)) h := &header{ FileHeader: fh, diff --git a/zip/zip_test.go b/zip/zip_test.go index cf7a73b450..cc3e18bdd4 100644 --- a/zip/zip_test.go +++ b/zip/zip_test.go @@ -197,13 +197,6 @@ func (r *rleBuffer) Write(p []byte) (n int, err error) { return len(p), nil } -func min(x, y int64) int64 { - if x < y { - return x - } - return y -} - func memset(a []byte, b byte) { if len(a) == 0 { return