From c0acd14e521d125d15144cd5f137b4f6d6ba8912 Mon Sep 17 00:00:00 2001 From: Anatoly Prohacky Date: Mon, 8 Aug 2022 19:45:46 +1000 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B2=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=B2=20uint64=20=D0=B8=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- acl/aclbit.go | 104 +++++++++++++++++++------------------------------- main.go | 25 ++---------- 2 files changed, 44 insertions(+), 85 deletions(-) diff --git a/acl/aclbit.go b/acl/aclbit.go index cff5ad1..cfff7dc 100644 --- a/acl/aclbit.go +++ b/acl/aclbit.go @@ -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 } diff --git a/main.go b/main.go index 8160319..4207cd9 100644 --- a/main.go +++ b/main.go @@ -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) }