Перевод в 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 package acl
import ( import (
"errors"
"strconv" "strconv"
) )
const ( // unite slice int to int64
NullAttr string = "" func UniteAcl(a []uint64) uint64 {
AllAttr string = "*" var endacl uint64
) for _, a0 := range a {
endacl = endacl | a0
}
type AclBit struct { return endacl
Acl int64
Atr string
} }
// Initialize acl bits and atributes //unite slice atr to string
func NewAclBit(def int64, atr string) *AclBit { func UniteAtr(atr []string) string {
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
var endatr string var endatr string
lenacl := len(acl) lenatr := len(atr)
for _, a0 := range acl { for _, a0 := range atr {
endacl = endacl | a0.Acl if a0 != "" {
if a0.Atr != NullAttr { endatr = endatr + a0
endatr = endatr + a0.Atr if lenatr > 1 {
if lenacl > 1 {
endatr = endatr + "," endatr = endatr + ","
} }
} }
lenacl-- lenatr--
} }
a.Acl = endacl
a.Atr = endatr return endatr
} }
// set acl bits // unite number bits to int64
func (a *AclBit) SetAcl(acl int64) { func UniteBitsToInt(n ...uint64) uint64 {
a.Acl = acl var i64 uint64
} for _, n0 := range n {
i64 = SetBitTrue(i64, n0)
// set acl atributes }
func (a *AclBit) SetAtr(atr string) { return i64
a.Atr = atr
}
// get current acl
func (a *AclBit) Get() *AclBit {
return a
} }
// set acl bit in true // set acl bit in true
func (a *AclBit) SetBitTrue(n int64) { func SetBitTrue(a, n uint64) uint64 {
a.Acl = a.Acl | (1 << n) return a | (1 << n)
} }
// set acl bit in false // set acl bit in false
func (a *AclBit) SetBitFalse(n int64) { func SetBitFalse(a, n uint64) uint64 {
a.Acl = a.Acl &^ (1 << n) return a &^ (1 << n)
} }
// get acl bit return true or false // verify bit return true or false
func (a *AclBit) GetBit(n int64) bool { func VerifyBit(a, n uint64) bool {
var msk int64 = 1 << n var msk uint64 = 1 << n
if (a.Acl & msk) == msk { if (a & msk) == msk {
return true return true
} else { } else {
return false return false
@ -86,16 +61,17 @@ func (a *AclBit) GetBit(n int64) bool {
} }
// converting acl bits in string // converting acl bits to string
func (a *AclBit) StringAcl() string { func ConvertToString(a uint64) string {
return strconv.FormatInt(int64(a.Acl), 2) return strconv.FormatUint(a, 2)
} }
// converting string bits to int64 // converting string bits to int64
func ConvertToInt(str string) (int64, error) { func ConvertToInt(str string) *uint64 {
i64, err := strconv.ParseInt(str, 2, 64) ui64, err := strconv.ParseUint(str, 2, 64)
if err != nil { 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() { func main() {
acl1 := acl.NewAclBit(1, acl.AllAttr) a := acl.UniteBitsToInt(0, 1, 2, 3, 63)
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)
// }
fmt.Println(a) fmt.Println(a)
aclend := acl.NewAclBit(0, acl.NullAttr) fmt.Println(acl.ConvertToString(a))
aclend.SetArray(a) b := fmt.Sprintf("%b", a)
fmt.Println(aclend.StringAcl()) fmt.Println(b)
fmt.Println(aclend.Atr)
fmt.Println(aclend.GetBit(0))
fmt.Println(aclend.GetBit(1))
fmt.Println(aclend.GetBit(2))
fmt.Println(aclend.GetBit(3))
} }