policy/acl/aclbit.go
Anatoly Prohacky a39010bcfc Edit v0.0.4
2023-03-05 18:40:21 +10:00

108 lines
1.6 KiB
Go

package acl
import (
"fmt"
"strconv"
)
type NumBit int64
type AclBit int64
// New AclBit
func New(n ...NumBit) AclBit {
var i64 AclBit
i64.SetTrue(n...)
return i64
}
// 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 (a *AclBit) SetTrue(n ...NumBit) {
for _, n0 := range n {
if veryNumBit(n0) {
*a = *a | (1 << n0)
}
}
}
// set acl bit in false
func (a *AclBit) SetFalse(n ...NumBit) {
for _, n0 := range n {
if veryNumBit(n0) {
*a = *a &^ (1 << n0)
}
}
}
// verify bit return true or false
func (a AclBit) Verify(n ...NumBit) bool {
for _, n0 := range n {
if veryNumBit(n0) {
var msk AclBit = 1 << n0
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
}
}
// verytify Num bit (max range 0 - 62 , summary 63 bit)
func veryNumBit(n NumBit) bool {
if n >= 0 && n < 63 {
return true
}
return false
}