-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues reported by golangsci-lint
- Loading branch information
1 parent
1285834
commit 02646bf
Showing
14 changed files
with
227 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package math | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"unsafe" | ||
|
||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
func Max[T constraints.Integer]() T { | ||
size := unsafe.Sizeof(T(0)) | ||
switch any(T(0)).(type) { | ||
case int, int8, int16, int32, int64: | ||
return T(1<<(size*8-1) - 1) // 2^(n-1) - 1 for signed integers | ||
case uint, uint8, uint16, uint32, uint64: | ||
return T(1<<(size*8) - 1) // 2^n - 1 for unsigned integers | ||
default: | ||
panic("unsupported type") | ||
} | ||
} | ||
|
||
func Min[T constraints.Integer]() T { | ||
size := unsafe.Sizeof(T(0)) | ||
switch any(T(0)).(type) { | ||
case int, int8, int16, int32, int64: | ||
return T(int64(-1) << (size*8 - 1)) // -2^(n-1) | ||
case uint, uint8, uint16, uint32, uint64: | ||
return T(0) | ||
default: | ||
panic("unsupported type") | ||
} | ||
} | ||
|
||
func SafeCastTo[T, F constraints.Integer](from F) (T, error) { | ||
if from > 0 && uint64(Max[T]()) < uint64(from) { | ||
return T(0), fmt.Errorf("value(%v) exceeds the maximum value for type(%v)", from, Max[T]()) | ||
} | ||
if from < 0 && int64(Min[T]()) > int64(from) { | ||
return T(0), fmt.Errorf("value(%v) exceeds the minimum value for type(%v)", from, Min[T]()) | ||
} | ||
return T(from), nil | ||
} | ||
|
||
func CastTo[T, F constraints.Integer](from F) T { | ||
return T(from) | ||
} | ||
|
||
func MustSafeCastTo[T, F constraints.Integer](from F) T { | ||
to, err := SafeCastTo[T](from) | ||
if err != nil { | ||
log.Panicf("value (%v) out of bounds for type %T", from, T(0)) | ||
} | ||
return to | ||
} |
Oops, something went wrong.