summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/internal/decoder/optdec/helper.go
blob: 61faa6c806fe748bc03c39c5b5bd48f884133126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package optdec

import (
	"encoding/json"
	"strconv"

	"github.com/bytedance/sonic/internal/native"
	"github.com/bytedance/sonic/internal/utils"
	"github.com/bytedance/sonic/internal/native/types"
)


func SkipNumberFast(json string, start int) (int, bool) {
	// find the number ending, we parsed in native, it always valid
	pos := start
	for pos < len(json) && json[pos] != ']' && json[pos] != '}' && json[pos] != ',' {
		if json[pos] >= '0' && json[pos] <= '9' || json[pos] == '.' || json[pos] == '-' || json[pos] == '+' || json[pos] == 'e' || json[pos] == 'E' {
			pos += 1
		} else {
			break
		}
	}

	// if not found number, return false
	if pos == start {
		return pos, false
	}
	return pos, true
}


func isSpace(c byte) bool {
    return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}

// pos is the start index of the raw
func ValidNumberFast(raw string) bool {
	ret := utils.SkipNumber(raw, 0)
	if ret < 0 {
		return false
	}

	// check trailing chars
	for ret < len(raw) {
		return false
	}

	return true
}

func SkipOneFast2(json string, pos *int) (int, error) {
	// find the number ending, we parsed in sonic-cpp, it always valid
	start := native.SkipOneFast(&json, pos)
	if start < 0 {
		return -1, error_syntax(*pos, json, types.ParsingError(-start).Error())
	}
	return start, nil
}

func SkipOneFast(json string, pos int) (string, error) {
	// find the number ending, we parsed in sonic-cpp, it always valid
	start := native.SkipOneFast(&json, &pos)
	if start < 0 {
		// TODO: details error code
		return "", error_syntax(pos, json, types.ParsingError(-start).Error())
	}
	return json[start:pos], nil
}

func ParseI64(raw string) (int64, error) {
	i64, err := strconv.ParseInt(raw, 10, 64)
	if err != nil {
		return 0, err
	}
	return i64, nil
}

func ParseBool(raw string) (bool, error) {
	var b bool
	err := json.Unmarshal([]byte(raw), &b)
	if err != nil {
		return false, err
	}
	return b, nil
}

func ParseU64(raw string) (uint64, error) {
	u64, err := strconv.ParseUint(raw, 10, 64)
	if err != nil {
		return 0, err
	}
	return u64, nil
}

func ParseF64(raw string) (float64, error) {
	f64, err := strconv.ParseFloat(raw, 64)
	if err != nil {
		return 0, err
	}
	return f64, nil
}

func Unquote(raw string) (string, error) {
	var u string
	err := json.Unmarshal([]byte(raw), &u)
	if err != nil {
		return "", err
	}
	return u, nil
}