Skip to content

Commit

Permalink
refactor: use built-in min function (#1038)
Browse files Browse the repository at this point in the history
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 <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored Jan 6, 2025
1 parent 6ad807b commit a2a2e44
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 25 deletions.
7 changes: 0 additions & 7 deletions snappy/xerial/xerial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 4 additions & 11 deletions zip/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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],
Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions zip/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2a2e44

Please sign in to comment.