forked from elliotchance/c2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noarch.go
43 lines (35 loc) · 992 Bytes
/
noarch.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
// Package noarch contains low-level functions that apply to multiple platforms.
package noarch
// BoolToInt converts boolean value to an int, which is a common operation in C.
// 0 and 1 represent false and true respectively.
func BoolToInt(x bool) int {
if x {
return 1
}
return 0
}
// NotInt performs a logical not (!) on an integer and returns an integer.
func NotInt(x int) int {
if x == 0 {
return 1
}
return 0
}
// NotUint16 works the same as NotInt, but on a uint16.
func NotUint16(x uint16) uint16 {
if x == 0 {
return 1
}
return 0
}
// Ternary simulates the ternary (also known as the conditional operator). Go
// does not have the equivalent of using if statements as expressions or inline
// if statements. This function takes the true and false parts as closures to be
// sure that only the true or false condition is evaulated - to prevent side
// effects.
func Ternary(a bool, b, c func() interface{}) interface{} {
if a {
return b()
}
return c()
}