forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
56 lines (46 loc) · 721 Bytes
/
utils.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
package main
import (
"math"
"strings"
"unsafe"
)
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func minNonZeroInt(a, b int) int {
switch {
case a == 0:
return b
case b == 0:
return a
}
return minInt(a, b)
}
func roundToInt(a float64) int {
return int(math.Round(a))
}
func scaleInt(a int, scale float64) int {
if a == 0 {
return 0
}
return roundToInt(float64(a) * scale)
}
func trimAfter(s string, sep byte) string {
i := strings.IndexByte(s, sep)
if i < 0 {
return s
}
return s[:i]
}
func ptrToBytes(ptr unsafe.Pointer, size int) []byte {
return (*[math.MaxInt32]byte)(ptr)[:int(size):int(size)]
}