1 Star 1 Fork 0

LineOfSight / resob

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BsTrstruct_checks.go 13.41 KB
一键复制 编辑 原始数据 按行查看 历史
LineOfSight 提交于 2024-01-11 02:44 . resob_BsTrstruct
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
// Copyright 2023 @Author:LiuBin(LineOfSight) 1440383523@qq.com. All rights reserved.
// Licensed under the MulanPSL-2.0 License.
package resob
import (
"reflect"
"regexp"
"strings"
"unsafe"
)
const (
IDREGS = `^[\S]{0,513}$`
FIELDREGS = `^[\S ]{0,1000}$`
DESREGS = `^[\S ]{0,1000}$`
BTREGS = `^[a-zA-Z0-9\p{Han}][\w\p{Han}-_.:|]{0,128}$`
)
var (
__idreg__ = regexp.MustCompile(IDREGS)
__fieldreg__ = regexp.MustCompile(FIELDREGS)
__desreg__ = regexp.MustCompile(DESREGS)
__btreg__ = regexp.MustCompile(BTREGS)
__uint8reg__ = regexp.MustCompile(`^(\[)[0-9]*(\]uint8)$`)
)
// CheckFlagAndLen checks flag and lens mapped flag
func (cs *BsTr) CheckFlagAndLen() bool {
if cs.check_flag_and_len(BIT0) && cs.check_flag_and_len(BIT1) && cs.check_flag_and_len(BIT2) && cs.check_flag_and_len(BIT3) &&
cs.check_flag_and_len(BIT4) && cs.check_flag_and_len(BIT5) && cs.check_flag_and_len(BIT6) && cs.check_flag_and_len(BIT7) &&
cs.check_flag_and_len(BIT8) && cs.check_flag_and_len(BIT9) && cs.check_flag_and_len(BIT10) && cs.check_flag_and_len(BIT11) &&
cs.check_flag_and_len(BIT12) && cs.check_flag_and_len(BIT13) && cs.check_flag_and_len(BIT14) {
return true
}
return false
}
// CheckLens checks cs.lens
// Lens of a Bstr different from flag. Lens stats 15 types of golang, and also stats fields bsk/offs/tms of a Bstr
func (cs *BsTr) CheckLens() bool {
if cs.check_lens(BIT0) && cs.check_lens(BIT1) && cs.check_lens(BIT2) && cs.check_lens(BIT3) &&
cs.check_lens(BIT4) && cs.check_lens(BIT5) && cs.check_lens(BIT6) && cs.check_lens(BIT7) &&
cs.check_lens(BIT8) && cs.check_lens(BIT9) && cs.check_lens(BIT10) && cs.check_lens(BIT11) &&
cs.check_lens(BIT12) && cs.check_lens(BIT13) && cs.check_lens(BIT14) {
if cs.lens[BYTEKEYSN] == IntLens(len(cs.bsks)) && cs.lens[OFFSSN] == IntLens(len(cs.offs)) && cs.lens[TMSSN] == IntLens(len(cs.tms)) {
return true
}
return false
}
return false
}
func (cs *BsTr) check_lens(bit uint16) bool {
switch bit {
case BIT0:
if cs.lens[BOOLSN] == IntLens(len(cs.bools)) {
return true
}
case BIT1:
if cs.lens[INT8SN] == IntLens(len(cs.i8s)) {
return true
}
case BIT2:
if cs.lens[INT16SN] == IntLens(len(cs.i16s)) {
return true
}
case BIT3:
if cs.lens[INT32SN] == IntLens(len(cs.i32s)) {
return true
}
case BIT4:
if cs.lens[INT64SN] == IntLens(len(cs.i64s)) {
return true
}
case BIT5:
if cs.lens[UINT8SN] == IntLens(len(cs.ui8s)) {
return true
}
case BIT6:
if cs.lens[UINT16SN] == IntLens(len(cs.ui16s)) {
return true
}
case BIT7:
if cs.lens[UINT32SN] == IntLens(len(cs.ui32s)) {
return true
}
case BIT8:
if cs.lens[UINT64SN] == IntLens(len(cs.ui64s)) {
return true
}
case BIT9:
if cs.lens[FLOAT32SN] == IntLens(len(cs.f32s)) {
return true
}
case BIT10:
if cs.lens[FLOAT64SN] == IntLens(len(cs.f64s)) {
return true
}
case BIT11:
if cs.lens[COMPLEX64SN] == IntLens(len(cs.c64s)) {
return true
}
case BIT12:
if cs.lens[COMPLEX128SN] == IntLens(len(cs.c128s)) {
return true
}
case BIT13:
if cs.lens[STRINGSN] == IntLens(len(cs.ss)) {
return true
}
case BIT14:
if cs.lens[BYTESN] == IntLens(len(cs.bs)) {
return true
}
default:
return false
}
return false
}
// CheckDELETEFlag checks bit 15
func (cs *BsTr) CheckDELETEFlag() bool {
return cs.flag&BIT15 == BIT15
}
func (cs *BsTr) check_flag_and_len(bit uint16) bool {
switch cs.flag & bit {
case BIT0:
if cs.lens[BOOLSN] > 0 {
return true
}
case BIT1:
if cs.lens[INT8SN] > 0 {
return true
}
case BIT2:
if cs.lens[INT16SN] > 0 {
return true
}
case BIT3:
if cs.lens[INT32SN] > 0 {
return true
}
case BIT4:
if cs.lens[INT64SN] > 0 {
return true
}
case BIT5:
if cs.lens[UINT8SN] > 0 {
return true
}
case BIT6:
if cs.lens[UINT16SN] > 0 {
return true
}
case BIT7:
if cs.lens[UINT32SN] > 0 {
return true
}
case BIT8:
if cs.lens[UINT64SN] > 0 {
return true
}
case BIT9:
if cs.lens[FLOAT32SN] > 0 {
return true
}
case BIT10:
if cs.lens[FLOAT64SN] > 0 {
return true
}
case BIT11:
if cs.lens[COMPLEX64SN] > 0 {
return true
}
case BIT12:
if cs.lens[COMPLEX128SN] > 0 {
return true
}
case BIT13:
if cs.lens[STRINGSN] > 0 {
return true
}
case BIT14:
if cs.lens[BYTESN] > 0 {
return true
}
default:
switch bit {
case BIT0:
if cs.lens[BOOLSN] == 0 {
return true
}
case BIT1:
if cs.lens[INT8SN] == 0 {
return true
}
case BIT2:
if cs.lens[INT16SN] == 0 {
return true
}
case BIT3:
if cs.lens[INT32SN] == 0 {
return true
}
case BIT4:
if cs.lens[INT64SN] == 0 {
return true
}
case BIT5:
if cs.lens[UINT8SN] == 0 {
return true
}
case BIT6:
if cs.lens[UINT16SN] == 0 {
return true
}
case BIT7:
if cs.lens[UINT32SN] == 0 {
return true
}
case BIT8:
if cs.lens[UINT64SN] == 0 {
return true
}
case BIT9:
if cs.lens[FLOAT32SN] == 0 {
return true
}
case BIT10:
if cs.lens[FLOAT64SN] == 0 {
return true
}
case BIT11:
if cs.lens[COMPLEX64SN] == 0 {
return true
}
case BIT12:
if cs.lens[COMPLEX128SN] == 0 {
return true
}
case BIT13:
if cs.lens[STRINGSN] == 0 {
return true
}
case BIT14:
if cs.lens[BYTESN] == 0 {
return true
}
default:
return false
}
}
return false
}
// CheckLen checks cs.length
func (cs *BsTr) CheckLen() (int64, bool) {
olen := cs.length
rlen := cs.RecomputeLen()
if olen == rlen {
return rlen, true
}
return rlen, false
}
// RestatLens restats cs.lens
func (cs *BsTr) RestatLens() {
cs.lens[BOOLSN] = IntLens(len(cs.bools))
cs.lens[INT8SN] = IntLens(len(cs.i8s))
cs.lens[INT16SN] = IntLens(len(cs.i16s))
cs.lens[INT32SN] = IntLens(len(cs.i32s))
cs.lens[INT64SN] = IntLens(len(cs.i64s))
cs.lens[UINT8SN] = IntLens(len(cs.ui8s))
cs.lens[UINT16SN] = IntLens(len(cs.ui16s))
cs.lens[UINT32SN] = IntLens(len(cs.ui32s))
cs.lens[UINT64SN] = IntLens(len(cs.ui64s))
cs.lens[FLOAT32SN] = IntLens(len(cs.f32s))
cs.lens[FLOAT64SN] = IntLens(len(cs.f64s))
cs.lens[COMPLEX64SN] = IntLens(len(cs.c64s))
cs.lens[COMPLEX128SN] = IntLens(len(cs.c128s))
cs.lens[STRINGSN] = IntLens(len(cs.ss))
cs.lens[BYTESN] = IntLens(len(cs.bs))
cs.lens[BYTEKEYSN] = IntLens(len(cs.bsks))
cs.lens[OFFSSN] = IntLens(len(cs.offs))
cs.lens[TMSSN] = IntLens(len(cs.tms))
}
func (cs *BsTr) subRecomputeLen(l int, nids *[]string) int64 {
rlen := int64(0)
for i := 0; i < l; i++ {
rlen += int64(len((*nids)[i*3]) + len((*nids)[i*3+1]) + len((*nids)[i*3+2]))
}
return rlen
}
// RecomputeLen recomputes cs.length for write to io
func (cs *BsTr) RecomputeLen() int64 {
rlen := int64(0)
rlen += NUDECSLEN
rlen += int64(cs.bnlen + cs.tnlen)
rlen += int64(len(cs.i8s) + len(cs.ui8s) + len(cs.bools))
rlen += int64((len(cs.i16s) + len(cs.ui16s)) * 2)
rlen += int64((len(cs.i32s) + len(cs.ui32s) + len(cs.f32s)) * 4)
rlen += int64((len(cs.i64s) + len(cs.ui64s) + len(cs.f64s) + len(cs.c64s) + len(cs.offs) + len(cs.tms)) * 8)
rlen += int64(len(cs.c128s) * 16)
if !cs.nonids {
rlen += int64((len(cs.i8s) + len(cs.ui8s) + len(cs.bools)) * (int(unsafe.Sizeof(IntNidslen(0))) * 3))
rlen += int64((len(cs.i16s) + len(cs.ui16s)) * (int(unsafe.Sizeof(IntNidslen(0))) * 3))
rlen += int64((len(cs.i32s) + len(cs.ui32s) + len(cs.f32s)) * (int(unsafe.Sizeof(IntNidslen(0))) * 3))
rlen += int64((len(cs.i64s) + len(cs.ui64s) + len(cs.f64s) + len(cs.c64s)) * (int(unsafe.Sizeof(IntNidslen(0))) * 3))
rlen += int64(len(cs.c128s) * (int(unsafe.Sizeof(IntNidslen(0))) * 3))
rlen += int64((len(cs.ss) + len(cs.bs)) * int(unsafe.Sizeof(IntNidslen(0))*3))
rlen += cs.subRecomputeLen(len(cs.bools), &cs.boolnids)
rlen += cs.subRecomputeLen(len(cs.i8s), &cs.i8nids)
rlen += cs.subRecomputeLen(len(cs.i16s), &cs.i16nids)
rlen += cs.subRecomputeLen(len(cs.i32s), &cs.i32nids)
rlen += cs.subRecomputeLen(len(cs.i64s), &cs.i64nids)
rlen += cs.subRecomputeLen(len(cs.ui8s), &cs.ui8nids)
rlen += cs.subRecomputeLen(len(cs.ui16s), &cs.ui16nids)
rlen += cs.subRecomputeLen(len(cs.ui32s), &cs.ui32nids)
rlen += cs.subRecomputeLen(len(cs.ui64s), &cs.ui64nids)
rlen += cs.subRecomputeLen(len(cs.f32s), &cs.f64nids)
rlen += cs.subRecomputeLen(len(cs.f64s), &cs.f64nids)
rlen += cs.subRecomputeLen(len(cs.c64s), &cs.c64nids)
rlen += cs.subRecomputeLen(len(cs.c128s), &cs.c128nids)
rlen += cs.subRecomputeLen(len(cs.ss), &cs.snids)
rlen += cs.subRecomputeLen(len(cs.bs), &cs.bnids)
}
rlen += int64(len(cs.ss) * int(unsafe.Sizeof(IntSlens(0))))
rlen += int64(len(cs.bs) * int(unsafe.Sizeof(IntBlens(0))))
rlen += int64(len(cs.bsks) * int(unsafe.Sizeof(IntBlens(0))))
for i := 0; i < len(cs.ss); i++ {
rlen += int64(len(cs.ss[i]))
}
for i := 0; i < len(cs.bs); i++ {
rlen += int64(len(cs.bs[i]))
}
for i := 0; i < len(cs.bsks); i++ {
rlen += int64(len(cs.bsks[i]))
}
cs.length = rlen
return rlen
}
// JiSuanJieGouTiRealLen computes the real size of Bstr struct
// The real size is different from the length or the size that will be writen to io
func JiSuanJieGouTiRealLen(cs any) int64 {
t := reflect.TypeOf(cs)
v := reflect.ValueOf(cs)
n := t.NumField()
var rlen int64
rlen = 0
for k := 0; k < n; k++ {
switch v.Field(k).Kind() {
case reflect.Slice, reflect.Array:
l := v.Field(k).Len()
if l > 0 {
switch v.Field(k).Index(0).Type().String() == "string" || v.Field(k).Index(0).Type().String() == "[]uint8" {
case true:
for i := 0; i < l; i++ {
rlen += int64(v.Field(k).Index(i).Len())
}
default:
rlen += int64(int(v.Field(k).Index(0).Type().Size()) * l)
}
}
case reflect.String:
rlen += int64(len(v.Field(k).String()))
case reflect.Struct:
rlen += JiSuanJieGouTiRealLen(v.Field(k))
default:
rlen += int64(v.Field(k).Type().Size())
}
}
return rlen
}
// CheckBTExisted checks bn and tn existed or not
func (cs *BsTr) CheckBTExisted() bool {
if cs.bnlen == 0 || cs.tnlen == 0 || cs.bnlen != IntBTlen(len(cs.bn)) || cs.tnlen != IntBTlen(len(cs.tn)) {
return false
}
return true
}
// CheckBT checks bn and tn are named legally or not
func (cs *BsTr) CheckBT(bn, tn string) bool {
var b1, b2 bool
if len(bn) == 0 {
b1 = true
}
if len(tn) == 0 {
b2 = true
}
if b1 && b2 {
return true
}
if !b1 {
b1 = __btreg__.MatchString(bn)
}
if !b2 {
b2 = __btreg__.MatchString(tn)
}
if !b1 || !b2 {
return false
}
return true
}
// match id of nids
func (cs *BsTr) checkID(ID string) (bool, IntLens, any) {
var i IntLens
if ID == "" {
return true, KONGIDSN, nil //allow "",return -2
}
if cs.lens[BOOLSN] > 0 {
for i = 0; i < cs.lens[BOOLSN]; i++ {
if strings.Compare(ID, cs.boolnids[i*3]) == 0 {
return false, i, cs.bools[i]
}
}
}
if cs.lens[INT8SN] > 0 {
for i = 0; i < cs.lens[INT8SN]; i++ {
if strings.Compare(ID, cs.i8nids[i*3]) == 0 {
return false, i, cs.i8s[i]
}
}
}
if cs.lens[INT16SN] > 0 {
for i = 0; i < cs.lens[INT16SN]; i++ {
if strings.Compare(ID, cs.i16nids[i*3]) == 0 {
return false, i, cs.i16s[i]
}
}
}
if cs.lens[INT32SN] > 0 {
for i = 0; i < cs.lens[INT32SN]; i++ {
if strings.Compare(ID, cs.i32nids[i*3]) == 0 {
return false, i, cs.i32s[i]
}
}
}
if cs.lens[INT64SN] > 0 {
for i = 0; i < cs.lens[INT64SN]; i++ {
if strings.Compare(ID, cs.i64nids[i*3]) == 0 {
return false, i, cs.i64s[i]
}
}
}
if cs.lens[UINT8SN] > 0 {
for i = 0; i < cs.lens[UINT8SN]; i++ {
if strings.Compare(ID, cs.ui8nids[i*3]) == 0 {
return false, i, cs.ui8s[i]
}
}
}
if cs.lens[UINT16SN] > 0 {
for i = 0; i < cs.lens[UINT16SN]; i++ {
if strings.Compare(ID, cs.ui16nids[i*3]) == 0 {
return false, i, cs.ui16s[i]
}
}
}
if cs.lens[UINT32SN] > 0 {
for i = 0; i < cs.lens[UINT32SN]; i++ {
if strings.Compare(ID, cs.ui32nids[i*3]) == 0 {
return false, i, cs.ui32s[i]
}
}
}
if cs.lens[UINT64SN] > 0 {
for i = 0; i < cs.lens[UINT64SN]; i++ {
if strings.Compare(ID, cs.ui64nids[i*3]) == 0 {
return false, i, cs.ui64s[i]
}
}
}
if cs.lens[FLOAT32SN] > 0 {
for i = 0; i < cs.lens[FLOAT32SN]; i++ {
if strings.Compare(ID, cs.f32nids[i*3]) == 0 {
return false, i, cs.f32s[i]
}
}
}
if cs.lens[FLOAT64SN] > 0 {
for i = 0; i < cs.lens[FLOAT64SN]; i++ {
if strings.Compare(ID, cs.f64nids[i*3]) == 0 {
return false, i, cs.f64s[i]
}
}
}
if cs.lens[COMPLEX64SN] > 0 {
for i = 0; i < cs.lens[COMPLEX64SN]; i++ {
if strings.Compare(ID, cs.c64nids[i*3]) == 0 {
return false, i, cs.c64s[i]
}
}
}
if cs.lens[COMPLEX128SN] > 0 {
for i = 0; i < cs.lens[COMPLEX128SN]; i++ {
if strings.Compare(ID, cs.c128nids[i*3]) == 0 {
return false, i, cs.c128s[i]
}
}
}
if cs.lens[STRINGSN] > 0 {
for i = 0; i < cs.lens[STRINGSN]; i++ {
if strings.Compare(ID, cs.snids[i*3]) == 0 {
return false, i, cs.ss[i]
}
}
}
if cs.lens[BYTESN] > 0 {
for i = 0; i < cs.lens[BYTESN]; i++ {
if strings.Compare(ID, cs.bnids[i*3]) == 0 {
return false, i, cs.bs[i]
}
}
}
return true, -1, nil
}
// CheckID checks ID existed or not
func (cs *BsTr) CheckID(ID string) (bool, IntLens, any) {
return cs.checkID(ID)
}
func (cs *BsTr) check_nids(ID string, field string, description string) error {
var b1, b2, b3 bool
if len(ID) == 0 {
b1 = true
}
if len(field) == 0 {
b2 = true
}
if len(description) == 0 {
b3 = true
}
if b1 && b2 && b3 {
return nil
}
if !b1 {
b1 = __idreg__.MatchString(ID)
}
if !b2 {
b2 = __fieldreg__.MatchString(field)
}
if !b3 {
b3 = __desreg__.MatchString(description)
}
if !b1 || !b2 || !b3 {
return ErrRegexpdismatch
}
return nil
}
Go
1
https://gitee.com/lineofsight/resob.git
git@gitee.com:lineofsight/resob.git
lineofsight
resob
resob
5b7b5e40e6d1

搜索帮助