forked from p53841790/wheat-cache
fix(value): add fix value get range slice
This commit is contained in:
parent
953daca82c
commit
cf4b24ea86
|
@ -3,6 +3,7 @@ package structure
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -175,12 +176,34 @@ func (v *Value) GetByte(offset int) (bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Value) SliceByString(start, end int) ([]byte, error) {
|
func (v *Value) SliceByString(start, end int) ([]byte, error) {
|
||||||
if v.onType != DynamicString {
|
|
||||||
return nil, errorx.New("not is string")
|
|
||||||
}
|
|
||||||
if start > end {
|
if start > end {
|
||||||
return nil, errorx.New("the end cannot be greater than the beginning")
|
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 {
|
if end > v.length {
|
||||||
return nil, errorx.New("the maximum index is exceeded, max index: %d", v.length)
|
return nil, errorx.New("the maximum index is exceeded, max index: %d", v.length)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue