refactor: use the built-in max/min to simplify the code

Signed-off-by: hongmengning <hongmengning@outlook.com>
This commit is contained in:
hongmengning 2025-08-12 15:40:21 +08:00
parent cbbf686ecc
commit d7661b44f6
4 changed files with 9 additions and 36 deletions

View file

@ -27,10 +27,7 @@ func XORBytes(dst, a, b []byte) int {
// fastXORBytes xors in bulk. It only works on architectures that support // fastXORBytes xors in bulk. It only works on architectures that support
// unaligned read/writes. // unaligned read/writes.
func fastXORBytes(dst, a, b []byte) int { func fastXORBytes(dst, a, b []byte) int {
n := len(a) n := min(len(b), len(a))
if len(b) < n {
n = len(b)
}
w := n / wordSize w := n / wordSize
if w > 0 { if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst)) dw := *(*[]uintptr)(unsafe.Pointer(&dst))
@ -49,10 +46,7 @@ func fastXORBytes(dst, a, b []byte) int {
// safeXORBytes xors one by one. It works on all architectures, independent if // safeXORBytes xors one by one. It works on all architectures, independent if
// it supports unaligned read/writes or not. // it supports unaligned read/writes or not.
func safeXORBytes(dst, a, b []byte) int { func safeXORBytes(dst, a, b []byte) int {
n := len(a) n := min(len(b), len(a))
if len(b) < n {
n = len(b)
}
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]
} }
@ -71,10 +65,7 @@ func ANDBytes(dst, a, b []byte) int {
// fastANDBytes ands in bulk. It only works on architectures that support // fastANDBytes ands in bulk. It only works on architectures that support
// unaligned read/writes. // unaligned read/writes.
func fastANDBytes(dst, a, b []byte) int { func fastANDBytes(dst, a, b []byte) int {
n := len(a) n := min(len(b), len(a))
if len(b) < n {
n = len(b)
}
w := n / wordSize w := n / wordSize
if w > 0 { if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst)) dw := *(*[]uintptr)(unsafe.Pointer(&dst))
@ -93,10 +84,7 @@ func fastANDBytes(dst, a, b []byte) int {
// safeANDBytes ands one by one. It works on all architectures, independent if // safeANDBytes ands one by one. It works on all architectures, independent if
// it supports unaligned read/writes or not. // it supports unaligned read/writes or not.
func safeANDBytes(dst, a, b []byte) int { func safeANDBytes(dst, a, b []byte) int {
n := len(a) n := min(len(b), len(a))
if len(b) < n {
n = len(b)
}
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
dst[i] = a[i] & b[i] dst[i] = a[i] & b[i]
} }
@ -115,10 +103,7 @@ func ORBytes(dst, a, b []byte) int {
// fastORBytes ors in bulk. It only works on architectures that support // fastORBytes ors in bulk. It only works on architectures that support
// unaligned read/writes. // unaligned read/writes.
func fastORBytes(dst, a, b []byte) int { func fastORBytes(dst, a, b []byte) int {
n := len(a) n := min(len(b), len(a))
if len(b) < n {
n = len(b)
}
w := n / wordSize w := n / wordSize
if w > 0 { if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst)) dw := *(*[]uintptr)(unsafe.Pointer(&dst))
@ -137,10 +122,7 @@ func fastORBytes(dst, a, b []byte) int {
// safeORBytes ors one by one. It works on all architectures, independent if // safeORBytes ors one by one. It works on all architectures, independent if
// it supports unaligned read/writes or not. // it supports unaligned read/writes or not.
func safeORBytes(dst, a, b []byte) int { func safeORBytes(dst, a, b []byte) int {
n := len(a) n := min(len(b), len(a))
if len(b) < n {
n = len(b)
}
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
dst[i] = a[i] | b[i] dst[i] = a[i] | b[i]
} }

View file

@ -31,10 +31,7 @@ func Raise(max uint64) (uint64, error) {
return 0, err return 0, err
} }
// Try to update the limit to the max allowance // Try to update the limit to the max allowance
limit.Cur = limit.Max limit.Cur = min(limit.Max, max)
if limit.Cur > max {
limit.Cur = max
}
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
return 0, err return 0, err
} }

View file

@ -146,10 +146,7 @@ func DecodeBig(input string) (*big.Int, error) {
words := make([]big.Word, len(raw)/bigWordNibbles+1) words := make([]big.Word, len(raw)/bigWordNibbles+1)
end := len(raw) end := len(raw)
for i := range words { for i := range words {
start := end - bigWordNibbles start := max(end-bigWordNibbles, 0)
if start < 0 {
start = 0
}
for ri := start; ri < end; ri++ { for ri := start; ri < end; ri++ {
nib := decodeNibble(raw[ri]) nib := decodeNibble(raw[ri])
if nib == badNibble { if nib == badNibble {

View file

@ -179,10 +179,7 @@ func (b *Big) UnmarshalText(input []byte) error {
words := make([]big.Word, len(raw)/bigWordNibbles+1) words := make([]big.Word, len(raw)/bigWordNibbles+1)
end := len(raw) end := len(raw)
for i := range words { for i := range words {
start := end - bigWordNibbles start := max(end-bigWordNibbles, 0)
if start < 0 {
start = 0
}
for ri := start; ri < end; ri++ { for ri := start; ri < end; ri++ {
nib := decodeNibble(raw[ri]) nib := decodeNibble(raw[ri])
if nib == badNibble { if nib == badNibble {