Приведение к типам и разделение

This commit is contained in:
Anatoly Prohacky 2022-08-12 21:45:06 +10:00
parent c0acd14e52
commit 20c8a73421
3 changed files with 101 additions and 49 deletions

View File

@ -1,12 +1,34 @@
package acl package acl
import ( import (
"fmt"
"strconv" "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 // unite slice int to int64
func UniteAcl(a []uint64) uint64 { func UniteAcl(a []AclBit) AclBit {
var endacl uint64 var endacl AclBit
for _, a0 := range a { for _, a0 := range a {
endacl = endacl | a0 endacl = endacl | a0
} }
@ -14,64 +36,63 @@ func UniteAcl(a []uint64) uint64 {
return endacl 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 // set acl bit in true
func SetBitTrue(a, n uint64) uint64 { func SetBitTrue(a AclBit, n NumBit) AclBit {
if veryNumBit(n) {
return a | (1 << n) return a | (1 << n)
}
return a
} }
// set acl bit in false // set acl bit in false
func SetBitFalse(a, n uint64) uint64 { func SetBitFalse(a AclBit, n NumBit) AclBit {
if veryNumBit(n) {
return a &^ (1 << n) return a &^ (1 << n)
}
return a
} }
// verify bit return true or false // verify bit return true or false
func VerifyBit(a, n uint64) bool { func VerifyBit(a AclBit, n NumBit) bool {
var msk uint64 = 1 << n if veryNumBit(n) {
var msk AclBit = 1 << n
if (a & msk) == msk { if (a & msk) == msk {
return true return true
} else {
return false
} }
}
return false
} }
// converting acl bits to string // converting acl bits to string
func ConvertToString(a uint64) string { func String(a AclBit) string {
return strconv.FormatUint(a, 2) return strconv.FormatInt(int64(a), 2)
} }
// converting string bits to int64 // converting string bits to int64
func ConvertToInt(str string) *uint64 { func Int64(i any) int64 {
ui64, err := strconv.ParseUint(str, 2, 64) 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 { if err != nil {
return 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
} }

25
atr/atrib.go Normal file
View File

@ -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
}

10
main.go
View File

@ -7,9 +7,15 @@ import (
) )
func main() { 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(a)
fmt.Println(acl.ConvertToString(a)) str := acl.String(a)
fmt.Println(str)
fmt.Println(acl.Int64(str))
b := fmt.Sprintf("%b", a) b := fmt.Sprintf("%b", a)
fmt.Println(b) 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))
} }