github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/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 (
    33  	V_EOF     ValueType = 1
    34  	V_NULL    ValueType = 2
    35  	V_TRUE    ValueType = 3
    36  	V_FALSE   ValueType = 4
    37  	V_ARRAY   ValueType = 5
    38  	V_OBJECT  ValueType = 6
    39  	V_STRING  ValueType = 7
    40  	V_DOUBLE  ValueType = 8
    41  	V_INTEGER ValueType = 9
    42  	_         ValueType = 10 // V_KEY_SEP
    43  	_         ValueType = 11 // V_ELEM_SEP
    44  	_         ValueType = 12 // V_ARRAY_END
    45  	_         ValueType = 13 // V_OBJECT_END
    46  	V_MAX
    47  )
    48  
    49  const (
    50  	// for native.Unquote() flags
    51  	B_DOUBLE_UNQUOTE  = 0
    52  	B_UNICODE_REPLACE = 1
    53  
    54  	// for native.Value() flags
    55  	B_USE_NUMBER      = 1
    56  	B_VALIDATE_STRING = 5
    57  	B_ALLOW_CONTROL   = 31
    58  )
    59  
    60  const (
    61  	F_DOUBLE_UNQUOTE  = 1 << B_DOUBLE_UNQUOTE
    62  	F_UNICODE_REPLACE = 1 << B_UNICODE_REPLACE
    63  
    64  	F_USE_NUMBER      = 1 << B_USE_NUMBER
    65  	F_VALIDATE_STRING = 1 << B_VALIDATE_STRING
    66  	F_ALLOW_CONTROL   = 1 << B_ALLOW_CONTROL
    67  )
    68  
    69  const (
    70  	MAX_RECURSE = 4096
    71  )
    72  
    73  const (
    74  	SPACE_MASK = (1 << ' ') | (1 << '\t') | (1 << '\r') | (1 << '\n')
    75  )
    76  
    77  const (
    78  	ERR_EOF                ParsingError = 1
    79  	ERR_INVALID_CHAR       ParsingError = 2
    80  	ERR_INVALID_ESCAPE     ParsingError = 3
    81  	ERR_INVALID_UNICODE    ParsingError = 4
    82  	ERR_INTEGER_OVERFLOW   ParsingError = 5
    83  	ERR_INVALID_NUMBER_FMT ParsingError = 6
    84  	ERR_RECURSE_EXCEED_MAX ParsingError = 7
    85  	ERR_FLOAT_INFINITY     ParsingError = 8
    86  	ERR_MISMATCH           ParsingError = 9
    87  	ERR_INVALID_UTF8       ParsingError = 10
    88  
    89  	// error code used in ast
    90  	ERR_NOT_FOUND      ParsingError = 33
    91  	ERR_UNSUPPORT_TYPE ParsingError = 34
    92  )
    93  
    94  var _ParsingErrors = []string{
    95  	0:                      "ok",
    96  	ERR_EOF:                "eof",
    97  	ERR_INVALID_CHAR:       "invalid char",
    98  	ERR_INVALID_ESCAPE:     "invalid escape char",
    99  	ERR_INVALID_UNICODE:    "invalid unicode escape",
   100  	ERR_INTEGER_OVERFLOW:   "integer overflow",
   101  	ERR_INVALID_NUMBER_FMT: "invalid number format",
   102  	ERR_RECURSE_EXCEED_MAX: "recursion exceeded max depth",
   103  	ERR_FLOAT_INFINITY:     "float number is infinity",
   104  	ERR_MISMATCH:           "mismatched type with value",
   105  	ERR_INVALID_UTF8:       "invalid UTF8",
   106  }
   107  
   108  func (self ParsingError) Error() string {
   109  	return "json: error when parsing input: " + self.Message()
   110  }
   111  
   112  func (self ParsingError) Message() string {
   113  	if int(self) < len(_ParsingErrors) {
   114  		return _ParsingErrors[self]
   115  	} else {
   116  		return fmt.Sprintf("unknown error %d", self)
   117  	}
   118  }
   119  
   120  type JsonState struct {
   121  	Vt   ValueType
   122  	Dv   float64
   123  	Iv   int64
   124  	Ep   int
   125  	Dbuf *byte
   126  	Dcap int
   127  }
   128  
   129  type StateMachine struct {
   130  	Sp int
   131  	Vt [MAX_RECURSE]int
   132  }
   133  
   134  var stackPool = sync.Pool{
   135  	New: func() interface{} {
   136  		return &StateMachine{}
   137  	},
   138  }
   139  
   140  func NewStateMachine() *StateMachine {
   141  	return stackPool.Get().(*StateMachine)
   142  }
   143  
   144  func FreeStateMachine(fsm *StateMachine) {
   145  	stackPool.Put(fsm)
   146  }
   147  
   148  const MaxDigitNums = 800
   149  
   150  var digitPool = sync.Pool{
   151  	New: func() interface{} {
   152  		return (*byte)(unsafe.Pointer(&[MaxDigitNums]byte{}))
   153  	},
   154  }
   155  
   156  func NewDbuf() *byte {
   157  	return digitPool.Get().(*byte)
   158  }
   159  
   160  func FreeDbuf(p *byte) {
   161  	digitPool.Put(p)
   162  }