diff --git a/pkg/structure/interface.gen.go b/pkg/structure/interface.gen.go index b9b806a..29cfec8 100644 --- a/pkg/structure/interface.gen.go +++ b/pkg/structure/interface.gen.go @@ -21,4 +21,6 @@ type StringXInterface interface { Reduce(int32) (string, error) Setbit(int32, bool) UpdateLength Getbit(int32) (bool, error) + Getrange(start, end int32) (string, error) + GetLength() int } diff --git a/pkg/structure/stringx/string.go b/pkg/structure/stringx/string.go index 9879237..0bbcb74 100644 --- a/pkg/structure/stringx/string.go +++ b/pkg/structure/stringx/string.go @@ -1,9 +1,10 @@ package stringx import ( + "strconv" + "gitee.com/timedb/wheatCache/pkg/errorx" "gitee.com/timedb/wheatCache/pkg/structure" - "strconv" ) type StringSingle struct { @@ -17,7 +18,7 @@ func NewStringSingle() structure.StringXInterface { } func (s *StringSingle) SizeByte() int64 { - return int64(s.val.GetLength()) + return int64(s.val.GetSize()) } // RollBack TODO 事务相关, V2 实现 @@ -105,3 +106,15 @@ func (s *StringSingle) Getbit(offer int32) (bool, error) { return b, err } + +func (s *StringSingle) Getrange(start, end int32) (string, error) { + b, err := s.val.SliceByString(int(start), int(end)) + if err != nil { + return "", err + } + return string(b), nil +} + +func (s *StringSingle) GetLength() int { + return s.val.GetLength() +} diff --git a/pkg/structure/value.go b/pkg/structure/value.go index 6d65fac..be578ae 100644 --- a/pkg/structure/value.go +++ b/pkg/structure/value.go @@ -3,9 +3,10 @@ package structure import ( "bytes" "encoding/binary" - "gitee.com/timedb/wheatCache/pkg/errorx" "math" "strconv" + + "gitee.com/timedb/wheatCache/pkg/errorx" ) // Value 提供一个基础的 动态类型 @@ -28,6 +29,10 @@ func (v *Value) GetLength() int { return len(v.val) } +func (v *Value) GetSize() int { + return v.length +} + func (v *Value) GetDynamicType() DynamicType { return v.onType } @@ -160,3 +165,17 @@ func (v *Value) GetByte(offset int) (bool, error) { return false, errorx.New("the maximum length is exceeded") } + +func (v *Value) SliceByString(start, end int) ([]byte, error) { + if v.onType != DynamicString { + return nil, errorx.New("not is string") + } + if start > end { + return nil, errorx.New("the end cannot be greater than the beginning") + } + if end > v.length { + return nil, errorx.New("the maximum index is exceeded, max index: %d", v.length) + } + + return v.val[start:end], nil +}