github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/native/types/types.go (about) 1 /* 2 * Copyright 2021 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package types 18 19 import ( 20 `fmt` 21 `sync` 22 `unsafe` 23 ) 24 25 type ValueType int 26 type ParsingError uint 27 type SearchingError uint 28 29 // NOTE: !NOT MODIFIED ONLY. 30 // This definitions are followed in native/types.h. 31 32 const BufPaddingSize int = 64 33 34 const ( 35 V_EOF ValueType = 1 36 V_NULL ValueType = 2 37 V_TRUE ValueType = 3 38 V_FALSE ValueType = 4 39 V_ARRAY ValueType = 5 40 V_OBJECT ValueType = 6 41 V_STRING ValueType = 7 42 V_DOUBLE ValueType = 8 43 V_INTEGER ValueType = 9 44 _ ValueType = 10 // V_KEY_SEP 45 _ ValueType = 11 // V_ELEM_SEP 46 _ ValueType = 12 // V_ARRAY_END 47 _ ValueType = 13 // V_OBJECT_END 48 V_MAX 49 ) 50 51 const ( 52 // for native.Unquote() flags 53 B_DOUBLE_UNQUOTE = 0 54 B_UNICODE_REPLACE = 1 55 56 // for native.Value() flags 57 B_USE_NUMBER = 1 58 B_VALIDATE_STRING = 5 59 B_ALLOW_CONTROL = 31 60 ) 61 62 const ( 63 F_DOUBLE_UNQUOTE = 1 << B_DOUBLE_UNQUOTE 64 F_UNICODE_REPLACE = 1 << B_UNICODE_REPLACE 65 66 F_USE_NUMBER = 1 << B_USE_NUMBER 67 F_VALIDATE_STRING = 1 << B_VALIDATE_STRING 68 F_ALLOW_CONTROL = 1 << B_ALLOW_CONTROL 69 ) 70 71 const ( 72 MAX_RECURSE = 4096 73 ) 74 75 const ( 76 SPACE_MASK = (1 << ' ') | (1 << '\t') | (1 << '\r') | (1 << '\n') 77 ) 78 79 const ( 80 ERR_EOF ParsingError = 1 81 ERR_INVALID_CHAR ParsingError = 2 82 ERR_INVALID_ESCAPE ParsingError = 3 83 ERR_INVALID_UNICODE ParsingError = 4 84 ERR_INTEGER_OVERFLOW ParsingError = 5 85 ERR_INVALID_NUMBER_FMT ParsingError = 6 86 ERR_RECURSE_EXCEED_MAX ParsingError = 7 87 ERR_FLOAT_INFINITY ParsingError = 8 88 ERR_MISMATCH ParsingError = 9 89 ERR_INVALID_UTF8 ParsingError = 10 90 91 // error code used in ast 92 ERR_NOT_FOUND ParsingError = 33 93 ERR_UNSUPPORT_TYPE ParsingError = 34 94 ) 95 96 var _ParsingErrors = []string{ 97 0 : "ok", 98 ERR_EOF : "eof", 99 ERR_INVALID_CHAR : "invalid char", 100 ERR_INVALID_ESCAPE : "invalid escape char", 101 ERR_INVALID_UNICODE : "invalid unicode escape", 102 ERR_INTEGER_OVERFLOW : "integer overflow", 103 ERR_INVALID_NUMBER_FMT : "invalid number format", 104 ERR_RECURSE_EXCEED_MAX : "recursion exceeded max depth", 105 ERR_FLOAT_INFINITY : "float number is infinity", 106 ERR_MISMATCH : "mismatched type with value", 107 ERR_INVALID_UTF8 : "invalid UTF8", 108 } 109 110 func (self ParsingError) Error() string { 111 return "json: error when parsing input: " + self.Message() 112 } 113 114 func (self ParsingError) Message() string { 115 if int(self) < len(_ParsingErrors) { 116 return _ParsingErrors[self] 117 } else { 118 return fmt.Sprintf("unknown error %d", self) 119 } 120 } 121 122 type JsonState struct { 123 Vt ValueType 124 Dv float64 125 Iv int64 126 Ep int 127 Dbuf *byte 128 Dcap int 129 } 130 131 type StateMachine struct { 132 Sp int 133 Vt [MAX_RECURSE]int 134 } 135 136 var stackPool = sync.Pool{ 137 New: func()interface{}{ 138 return &StateMachine{} 139 }, 140 } 141 142 func NewStateMachine() *StateMachine { 143 return stackPool.Get().(*StateMachine) 144 } 145 146 func FreeStateMachine(fsm *StateMachine) { 147 stackPool.Put(fsm) 148 } 149 150 const MaxDigitNums = 800 151 152 var digitPool = sync.Pool{ 153 New: func() interface{} { 154 return (*byte)(unsafe.Pointer(&[MaxDigitNums]byte{})) 155 }, 156 } 157 158 func NewDbuf() *byte { 159 return digitPool.Get().(*byte) 160 } 161 162 func FreeDbuf(p *byte) { 163 digitPool.Put(p) 164 }