-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
110 lines (97 loc) · 2.39 KB
/
common.go
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package common
import (
"encoding/binary"
"github.com/gorustyt/go-assimp/common/logger"
"math"
"unsafe"
)
type AiPropertyStore struct {
Sentinel uint8
}
// ------------------------------------------------------------------------------------
// Convert just one hex digit
// Return value is UINT_MAX if the input character is not a hex digit.
// ------------------------------------------------------------------------------------
func HexDigitToDecimal(in byte) (out uint) {
if in >= '0' && in <= '9' {
out = uint(in - '0')
} else if in >= 'a' && in <= 'f' {
out = 10 + uint(in-'a')
} else if in >= 'A' && in <= 'F' {
out = 10 + uint(in-'A')
return out
}
// return value is UINT_MAX if the input is not a hex digit
return out
}
func GetBytesOrder() binary.ByteOrder {
if IsLittleEndian() {
return binary.LittleEndian
}
return binary.BigEndian
}
func IsLittleEndian() bool {
n := 0x1234
return *(*byte)(unsafe.Pointer(&n)) == 0x34
}
// ------------------------------------------------------------------------------------
// Convert a string in decimal format to a number
// ------------------------------------------------------------------------------------
func Strtoul10(in string) (value int32, index int) {
for i, v := range in {
index = i
if v < '0' || v > '9' {
break
}
value = (value * 10) + (v - '0')
}
return value, index
}
// ------------------------------------------------------------------------------------
// signed variant of strtoul10
// ------------------------------------------------------------------------------------
func StrTol10(inData string) (value int32, index int) {
in := 0
inv := (inData[in] == '-')
if inv || inData[in] == '+' {
in++
}
value, index = Strtoul10(inData)
if inv {
if value < math.MaxInt32 {
value = -value
} else {
logger.WarnF("Converting the string \"%v \" into an inverted value resulted in overflow.", in)
}
}
return value, in
}
// find >=
func LowerBound(begin, end int, less func(index int) bool) int {
for begin < end {
half := begin + (end-begin)>>1
if less(half) {
begin = half + 1
} else {
end = half
}
}
return end
}
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64
}
func Max[T Number](a, b T) T {
if a <= b {
return b
}
return a
}
func Min[T Number](a, b T) T {
if a <= b {
return a
}
return b
}