Skip to content

Commit a2a2e44

Browse files
authored
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 <[email protected]>
1 parent 6ad807b commit a2a2e44

File tree

3 files changed

+4
-25
lines changed

3 files changed

+4
-25
lines changed

snappy/xerial/xerial.go

-7
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,6 @@ func EncodeBetter(dst, src []byte) []byte {
115115
return dst
116116
}
117117

118-
func min(x, y int) int {
119-
if x < y {
120-
return x
121-
}
122-
return y
123-
}
124-
125118
const (
126119
sizeOffset = 16
127120
sizeBytes = 4

zip/writer.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ func writeHeader(w io.Writer, h *header) error {
406406
// flags.
407407
if h.raw && !h.hasDataDescriptor() {
408408
b.uint32(h.CRC32)
409-
b.uint32(uint32(min64(h.CompressedSize64, uint32max)))
410-
b.uint32(uint32(min64(h.UncompressedSize64, uint32max)))
409+
b.uint32(uint32(min(h.CompressedSize64, uint32max)))
410+
b.uint32(uint32(min(h.UncompressedSize64, uint32max)))
411411
} else {
412412
// When this package handle the compression, these values are
413413
// always written to the trailing data descriptor.
@@ -427,13 +427,6 @@ func writeHeader(w io.Writer, h *header) error {
427427
return err
428428
}
429429

430-
func min64(x, y uint64) uint64 {
431-
if x < y {
432-
return x
433-
}
434-
return y
435-
}
436-
437430
// CreateRaw adds a file to the zip archive using the provided [FileHeader] and
438431
// returns a [Writer] to which the file contents should be written. The file's
439432
// 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) {
445438
return nil, err
446439
}
447440

448-
fh.CompressedSize = uint32(min64(fh.CompressedSize64, uint32max))
449-
fh.UncompressedSize = uint32(min64(fh.UncompressedSize64, uint32max))
441+
fh.CompressedSize = uint32(min(fh.CompressedSize64, uint32max))
442+
fh.UncompressedSize = uint32(min(fh.UncompressedSize64, uint32max))
450443

451444
h := &header{
452445
FileHeader: fh,

zip/zip_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,6 @@ func (r *rleBuffer) Write(p []byte) (n int, err error) {
197197
return len(p), nil
198198
}
199199

200-
func min(x, y int64) int64 {
201-
if x < y {
202-
return x
203-
}
204-
return y
205-
}
206-
207200
func memset(a []byte, b byte) {
208201
if len(a) == 0 {
209202
return

0 commit comments

Comments
 (0)