fix(value): add fix value get range slice

This commit is contained in:
bandl 2021-11-28 19:32:59 +08:00
parent 953daca82c
commit cf4b24ea86
1 changed files with 26 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package structure
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"strconv"
@ -175,12 +176,34 @@ func (v *Value) GetByte(offset int) (bool, error) {
}
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 v.onType == DynamicInt {
ret, err := v.ToInt()
if err != nil {
return nil, err
}
value := strconv.Itoa(int(ret))
if end > len(value) {
return nil, errorx.New("the maximum index is exceeded, max index: %d", len(value))
}
return []byte(value[start:end]), nil
}
if v.onType == DynamicFloat {
ret, err := v.ToFloat64()
if err != nil {
return nil, err
}
value := fmt.Sprintf("%.2f", ret)
if end > len(value) {
return nil, errorx.New("the maximum index is exceeded, max index: %d", len(value))
}
return []byte(value[start:end]), nil
}
if end > v.length {
return nil, errorx.New("the maximum index is exceeded, max index: %d", v.length)
}