Перевод в uint64 и изменение библиотеки

This commit is contained in:
Anatoly Prohacky 2022-08-08 19:45:46 +10:00
parent 424a555d16
commit c0acd14e52
2 changed files with 44 additions and 85 deletions

View File

@ -1,84 +1,59 @@
package acl
import (
"errors"
"strconv"
)
const (
NullAttr string = ""
AllAttr string = "*"
)
// unite slice int to int64
func UniteAcl(a []uint64) uint64 {
var endacl uint64
for _, a0 := range a {
endacl = endacl | a0
}
type AclBit struct {
Acl int64
Atr string
return endacl
}
// Initialize acl bits and atributes
func NewAclBit(def int64, atr string) *AclBit {
acl := new(AclBit)
acl.Acl = def
acl.Atr = atr
return acl
}
// Set acl bits and atributes
func (a *AclBit) Set(acl int64, atr string) {
a.Acl = acl
a.Atr = atr
}
// Set array acl bits and atributes
func (a *AclBit) SetArray(acl []AclBit) {
var endacl int64
//unite slice atr to string
func UniteAtr(atr []string) string {
var endatr string
lenacl := len(acl)
for _, a0 := range acl {
endacl = endacl | a0.Acl
if a0.Atr != NullAttr {
endatr = endatr + a0.Atr
if lenacl > 1 {
lenatr := len(atr)
for _, a0 := range atr {
if a0 != "" {
endatr = endatr + a0
if lenatr > 1 {
endatr = endatr + ","
}
}
lenacl--
lenatr--
}
a.Acl = endacl
a.Atr = endatr
return endatr
}
// set acl bits
func (a *AclBit) SetAcl(acl int64) {
a.Acl = acl
}
// set acl atributes
func (a *AclBit) SetAtr(atr string) {
a.Atr = atr
}
// get current acl
func (a *AclBit) Get() *AclBit {
return a
// unite number bits to int64
func UniteBitsToInt(n ...uint64) uint64 {
var i64 uint64
for _, n0 := range n {
i64 = SetBitTrue(i64, n0)
}
return i64
}
// set acl bit in true
func (a *AclBit) SetBitTrue(n int64) {
a.Acl = a.Acl | (1 << n)
func SetBitTrue(a, n uint64) uint64 {
return a | (1 << n)
}
// set acl bit in false
func (a *AclBit) SetBitFalse(n int64) {
a.Acl = a.Acl &^ (1 << n)
func SetBitFalse(a, n uint64) uint64 {
return a &^ (1 << n)
}
// get acl bit return true or false
func (a *AclBit) GetBit(n int64) bool {
var msk int64 = 1 << n
if (a.Acl & msk) == msk {
// verify bit return true or false
func VerifyBit(a, n uint64) bool {
var msk uint64 = 1 << n
if (a & msk) == msk {
return true
} else {
return false
@ -86,16 +61,17 @@ func (a *AclBit) GetBit(n int64) bool {
}
// converting acl bits in string
func (a *AclBit) StringAcl() string {
return strconv.FormatInt(int64(a.Acl), 2)
// converting acl bits to string
func ConvertToString(a uint64) string {
return strconv.FormatUint(a, 2)
}
// converting string bits to int64
func ConvertToInt(str string) (int64, error) {
i64, err := strconv.ParseInt(str, 2, 64)
func ConvertToInt(str string) *uint64 {
ui64, err := strconv.ParseUint(str, 2, 64)
if err != nil {
return 0, errors.New("string not bit formats")
return nil
}
return i64, nil
return &ui64
}

25
main.go
View File

@ -7,26 +7,9 @@ import (
)
func main() {
acl1 := acl.NewAclBit(1, acl.AllAttr)
acl2 := acl.NewAclBit(2, acl.NullAttr)
acl3 := acl.NewAclBit(1, "finance")
acl4 := acl.NewAclBit(8, "nofinance")
var a []acl.AclBit
a = append(a, *acl1, *acl2, *acl3, *acl4)
// randoms := 0
// for i := 0; i <= 16; i++ {
// fmt.Println(randoms, ":", acl1.StringAcl())
// randoms = rand.Intn(64)
// acl1.SetBitTrue(randoms)
// }
a := acl.UniteBitsToInt(0, 1, 2, 3, 63)
fmt.Println(a)
aclend := acl.NewAclBit(0, acl.NullAttr)
aclend.SetArray(a)
fmt.Println(aclend.StringAcl())
fmt.Println(aclend.Atr)
fmt.Println(aclend.GetBit(0))
fmt.Println(aclend.GetBit(1))
fmt.Println(aclend.GetBit(2))
fmt.Println(aclend.GetBit(3))
fmt.Println(acl.ConvertToString(a))
b := fmt.Sprintf("%b", a)
fmt.Println(b)
}