policy/acl/aclbit.go
Anatoly Prohacky 894bc6617c change v0.0.1
2023-03-04 16:58:02 +10:00

106 lines
1.5 KiB
Go

package acl
import (
"fmt"
"strconv"
)
type NumBit int64
type AclBit int64
// New AclBit
func New(n ...NumBit) AclBit {
var i64 AclBit
for _, n0 := range n {
i64 = SetTrue(i64, n0)
}
return i64
}
// verytify Num bit (max range 0 - 62 , summary 63 bit)
func veryNumBit(n NumBit) bool {
if n >= 0 && n < 63 {
return true
}
return false
}
// unite slice int to int64
func Unite(a ...AclBit) AclBit {
var endacl AclBit
for _, a0 := range a {
endacl = endacl | a0
}
return endacl
}
// set acl bit in true
func SetTrue(a AclBit, n NumBit) AclBit {
if veryNumBit(n) {
return a | (1 << n)
}
return a
}
// set acl bit in false
func SetFalse(a AclBit, n NumBit) AclBit {
if veryNumBit(n) {
return a &^ (1 << n)
}
return a
}
// verify bit return true or false
func Verify(a AclBit, n NumBit) bool {
if veryNumBit(n) {
var msk AclBit = 1 << n
if (a & msk) == msk {
return true
}
}
return false
}
// converting acl bits to string
func (a AclBit) String() string {
return strconv.FormatInt(int64(a), 2)
}
// converting string bits to int64
func (a AclBit) Int64() int64 {
return int64(a)
}
// converting string bits to int64
func Int64(i any) int64 {
var i64 int64
var err error
switch i.(type) {
case string:
str := fmt.Sprintf("%s", i)
i64, err = strconv.ParseInt(str, 2, 64)
if err != nil {
return -1
}
return i64
case AclBit:
i64 = int64(i.(AclBit))
return i64
case int64:
i64 = i.(int64)
return i64
case uint64:
i64 = int64(i.(uint64))
return i64
default:
i64 = -1
return i64
}
}