feat(string): add stringx option

This commit is contained in:
bandl 2021-10-20 21:35:15 +08:00
parent aed507fc9a
commit 56d69f63af
3 changed files with 37 additions and 3 deletions

View File

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

View File

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

View File

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