api_compat.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // +build !amd64,!arm64 go1.23 !go1.16 arm64,!go1.20
  2. /*
  3. * Copyright 2022 ByteDance Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package ast
  18. import (
  19. `encoding/json`
  20. `unicode/utf8`
  21. `github.com/bytedance/sonic/internal/native/types`
  22. `github.com/bytedance/sonic/internal/rt`
  23. )
  24. func init() {
  25. println("WARNING:(ast) sonic only supports Go1.16~1.22, but your environment is not suitable")
  26. }
  27. func quote(buf *[]byte, val string) {
  28. quoteString(buf, val)
  29. }
  30. // unquote unescapes a internal JSON string (it doesn't count quotas at the begining and end)
  31. func unquote(src string) (string, types.ParsingError) {
  32. sp := rt.IndexChar(src, -1)
  33. out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
  34. if !ok {
  35. return "", types.ERR_INVALID_ESCAPE
  36. }
  37. return rt.Mem2Str(out), 0
  38. }
  39. func (self *Parser) decodeValue() (val types.JsonState) {
  40. e, v := decodeValue(self.s, self.p, self.dbuf == nil)
  41. if e < 0 {
  42. return v
  43. }
  44. self.p = e
  45. return v
  46. }
  47. func (self *Parser) skip() (int, types.ParsingError) {
  48. e, s := skipValue(self.s, self.p)
  49. if e < 0 {
  50. return self.p, types.ParsingError(-e)
  51. }
  52. self.p = e
  53. return s, 0
  54. }
  55. func (self *Parser) skipFast() (int, types.ParsingError) {
  56. e, s := skipValueFast(self.s, self.p)
  57. if e < 0 {
  58. return self.p, types.ParsingError(-e)
  59. }
  60. self.p = e
  61. return s, 0
  62. }
  63. func (self *Node) encodeInterface(buf *[]byte) error {
  64. out, err := json.Marshal(self.packAny())
  65. if err != nil {
  66. return err
  67. }
  68. *buf = append(*buf, out...)
  69. return nil
  70. }
  71. func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) {
  72. for _, p := range path {
  73. if idx, ok := p.(int); ok && idx >= 0 {
  74. if err := self.searchIndex(idx); err != 0 {
  75. return self.p, err
  76. }
  77. } else if key, ok := p.(string); ok {
  78. if err := self.searchKey(key); err != 0 {
  79. return self.p, err
  80. }
  81. } else {
  82. panic("path must be either int(>=0) or string")
  83. }
  84. }
  85. var start int
  86. var e types.ParsingError
  87. if validate {
  88. start, e = self.skip()
  89. } else {
  90. start, e = self.skipFast()
  91. }
  92. if e != 0 {
  93. return self.p, e
  94. }
  95. return start, 0
  96. }
  97. func validate_utf8(str string) bool {
  98. return utf8.ValidString(str)
  99. }