2017-01-23 02:33:43 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-06-18 17:09:30 +02:00
|
|
|
"fmt"
|
2017-06-19 17:43:53 +02:00
|
|
|
"strconv"
|
2017-01-23 02:33:43 +02:00
|
|
|
)
|
|
|
|
|
2017-06-06 17:27:00 +02:00
|
|
|
type stringAny struct {
|
2017-01-26 10:41:49 +02:00
|
|
|
baseAny
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
2017-06-18 17:09:30 +02:00
|
|
|
func (any *stringAny) Get(path ...interface{}) Any {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return any
|
|
|
|
}
|
2018-02-19 08:18:42 +02:00
|
|
|
return &invalidAny{baseAny{}, fmt.Errorf("GetIndex %v from simple value", path)}
|
2017-06-18 17:09:30 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 10:41:49 +02:00
|
|
|
func (any *stringAny) Parse() *Iterator {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ValueType() ValueType {
|
2017-08-05 01:10:15 +02:00
|
|
|
return StringValue
|
2017-01-26 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 15:21:20 +02:00
|
|
|
func (any *stringAny) MustBeValid() Any {
|
|
|
|
return any
|
|
|
|
}
|
|
|
|
|
2017-01-26 10:41:49 +02:00
|
|
|
func (any *stringAny) LastError() error {
|
2017-06-18 16:40:18 +02:00
|
|
|
return nil
|
2017-01-26 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToBool() bool {
|
|
|
|
str := any.ToString()
|
2017-07-03 13:40:12 +02:00
|
|
|
if str == "0" {
|
2017-01-26 10:41:49 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, c := range str {
|
|
|
|
switch c {
|
|
|
|
case ' ', '\n', '\r', '\t':
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToInt() int {
|
2017-07-03 18:19:41 +02:00
|
|
|
return int(any.ToInt64())
|
|
|
|
|
2017-01-26 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToInt32() int32 {
|
2017-07-03 18:19:41 +02:00
|
|
|
return int32(any.ToInt64())
|
2017-01-26 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToInt64() int64 {
|
2017-07-03 18:19:41 +02:00
|
|
|
if any.val == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
flag := 1
|
|
|
|
startPos := 0
|
|
|
|
if any.val[0] == '+' || any.val[0] == '-' {
|
|
|
|
startPos = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if any.val[0] == '-' {
|
|
|
|
flag = -1
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:17:29 +02:00
|
|
|
endPos := startPos
|
2017-07-03 18:19:41 +02:00
|
|
|
for i := startPos; i < len(any.val); i++ {
|
|
|
|
if any.val[i] >= '0' && any.val[i] <= '9' {
|
|
|
|
endPos = i + 1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parsed, _ := strconv.ParseInt(any.val[startPos:endPos], 10, 64)
|
|
|
|
return int64(flag) * parsed
|
2017-01-26 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 10:55:32 +02:00
|
|
|
func (any *stringAny) ToUint() uint {
|
2017-07-03 18:19:41 +02:00
|
|
|
return uint(any.ToUint64())
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToUint32() uint32 {
|
2017-07-03 18:19:41 +02:00
|
|
|
return uint32(any.ToUint64())
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToUint64() uint64 {
|
2017-07-03 18:19:41 +02:00
|
|
|
if any.val == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
startPos := 0
|
2017-07-04 11:02:46 +02:00
|
|
|
|
|
|
|
if any.val[0] == '-' {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if any.val[0] == '+' {
|
2017-07-03 18:19:41 +02:00
|
|
|
startPos = 1
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:17:29 +02:00
|
|
|
endPos := startPos
|
2017-07-03 18:19:41 +02:00
|
|
|
for i := startPos; i < len(any.val); i++ {
|
|
|
|
if any.val[i] >= '0' && any.val[i] <= '9' {
|
|
|
|
endPos = i + 1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parsed, _ := strconv.ParseUint(any.val[startPos:endPos], 10, 64)
|
2017-01-29 10:55:32 +02:00
|
|
|
return parsed
|
|
|
|
}
|
|
|
|
|
2017-01-26 10:41:49 +02:00
|
|
|
func (any *stringAny) ToFloat32() float32 {
|
2017-07-04 18:23:00 +02:00
|
|
|
return float32(any.ToFloat64())
|
2017-01-26 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToFloat64() float64 {
|
2017-07-04 18:23:00 +02:00
|
|
|
if len(any.val) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// first char invalid
|
|
|
|
if any.val[0] != '+' && any.val[0] != '-' && (any.val[0] > '9' || any.val[0] < '0') {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract valid num expression from string
|
|
|
|
// eg 123true => 123, -12.12xxa => -12.12
|
|
|
|
endPos := 1
|
|
|
|
for i := 1; i < len(any.val); i++ {
|
2017-07-10 08:06:37 +02:00
|
|
|
if any.val[i] == '.' || any.val[i] == 'e' || any.val[i] == 'E' || any.val[i] == '+' || any.val[i] == '-' {
|
2017-07-04 18:23:00 +02:00
|
|
|
endPos = i + 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// end position is the first char which is not digit
|
|
|
|
if any.val[i] >= '0' && any.val[i] <= '9' {
|
|
|
|
endPos = i + 1
|
|
|
|
} else {
|
|
|
|
endPos = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parsed, _ := strconv.ParseFloat(any.val[:endPos], 64)
|
2017-07-04 16:28:24 +02:00
|
|
|
return parsed
|
2017-01-26 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) ToString() string {
|
|
|
|
return any.val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) WriteTo(stream *Stream) {
|
|
|
|
stream.WriteString(any.val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *stringAny) GetInterface() interface{} {
|
|
|
|
return any.val
|
2017-06-06 17:27:00 +02:00
|
|
|
}
|