-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric.go
More file actions
29 lines (23 loc) · 722 Bytes
/
numeric.go
File metadata and controls
29 lines (23 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 WoozyMasta
// Source: github.com/woozymasta/rap
package rap
import (
"fmt"
"math"
)
// u32ToInt converts uint32 to int with bounds check.
func u32ToInt(value uint32) (int, error) {
if uint64(value) > uint64(math.MaxInt) {
return 0, fmt.Errorf("%w: uint32 value overflows int=%d", ErrInvalidRAP, value)
}
return int(value), nil
}
// intToU32 converts int to uint32 with bounds check.
func intToU32(value int) (uint32, error) {
if value < 0 || uint64(value) > uint64(math.MaxUint32) {
return 0, fmt.Errorf("%w: int value overflows uint32=%d", ErrInvalidRAP, value)
}
//nolint:gosec // guarded by explicit range check above
return uint32(value), nil
}