From 20c8a73421b6dd7b38c57ace886ac2efd4323fa8 Mon Sep 17 00:00:00 2001 From: Anatoly Prohacky Date: Fri, 12 Aug 2022 21:45:06 +1000 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=20=D1=82=D0=B8=D0=BF=D0=B0=D0=BC=20?= =?UTF-8?q?=D0=B8=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- acl/aclbit.go | 115 +++++++++++++++++++++++++++++--------------------- atr/atrib.go | 25 +++++++++++ main.go | 10 ++++- 3 files changed, 101 insertions(+), 49 deletions(-) create mode 100644 atr/atrib.go diff --git a/acl/aclbit.go b/acl/aclbit.go index cfff7dc..42f6560 100644 --- a/acl/aclbit.go +++ b/acl/aclbit.go @@ -1,12 +1,34 @@ 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 = SetBitTrue(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 UniteAcl(a []uint64) uint64 { - var endacl uint64 +func UniteAcl(a []AclBit) AclBit { + var endacl AclBit for _, a0 := range a { endacl = endacl | a0 } @@ -14,64 +36,63 @@ func UniteAcl(a []uint64) uint64 { return endacl } -//unite slice atr to string -func UniteAtr(atr []string) string { - var endatr string - lenatr := len(atr) - for _, a0 := range atr { - if a0 != "" { - endatr = endatr + a0 - if lenatr > 1 { - endatr = endatr + "," - } - } - lenatr-- - } - - return endatr -} - -// 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 SetBitTrue(a, n uint64) uint64 { - return a | (1 << n) +func SetBitTrue(a AclBit, n NumBit) AclBit { + if veryNumBit(n) { + return a | (1 << n) + } + + return a } // set acl bit in false -func SetBitFalse(a, n uint64) uint64 { - return a &^ (1 << n) +func SetBitFalse(a AclBit, n NumBit) AclBit { + if veryNumBit(n) { + return a &^ (1 << n) + } + + return a } // 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 +func VerifyBit(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 ConvertToString(a uint64) string { - return strconv.FormatUint(a, 2) +func String(a AclBit) string { + return strconv.FormatInt(int64(a), 2) } // converting string bits to int64 -func ConvertToInt(str string) *uint64 { - ui64, err := strconv.ParseUint(str, 2, 64) - if err != nil { - return nil +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 } - - return &ui64 } diff --git a/atr/atrib.go b/atr/atrib.go new file mode 100644 index 0000000..963bd2d --- /dev/null +++ b/atr/atrib.go @@ -0,0 +1,25 @@ +package atr + +type Atr string + +// New Atr +func New(a string) Atr { + return Atr(a) +} + +//Glue slice Atr to Atr +func Glue(atr []Atr) Atr { + var endatr Atr + lenatr := len(atr) + for _, a0 := range atr { + if a0 != "" { + endatr = endatr + a0 + if lenatr > 1 { + endatr = endatr + "," + } + } + lenatr-- + } + + return endatr +} diff --git a/main.go b/main.go index 4207cd9..236be03 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,15 @@ import ( ) func main() { - a := acl.UniteBitsToInt(0, 1, 2, 3, 63) + a := acl.New(63, 63, 63, 3, 62, 62, 64, 64, 5654, 5435, -12234) fmt.Println(a) - fmt.Println(acl.ConvertToString(a)) + str := acl.String(a) + fmt.Println(str) + fmt.Println(acl.Int64(str)) b := fmt.Sprintf("%b", a) fmt.Println(b) + fmt.Println(acl.VerifyBit(a, 63)) + fmt.Println(acl.VerifyBit(a, 62)) + fmt.Println(acl.VerifyBit(a, -1)) + fmt.Println(acl.VerifyBit(a, 3)) }