api_compat.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // +build !amd64,!arm64 go1.25 !go1.17 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. `github.com/bytedance/sonic/internal/compat`
  24. )
  25. func init() {
  26. compat.Warn("sonic/ast")
  27. }
  28. func quote(buf *[]byte, val string) {
  29. quoteString(buf, val)
  30. }
  31. // unquote unescapes an internal JSON string (it doesn't count quotas at the beginning and end)
  32. func unquote(src string) (string, types.ParsingError) {
  33. sp := rt.IndexChar(src, -1)
  34. out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
  35. if !ok {
  36. return "", types.ERR_INVALID_ESCAPE
  37. }
  38. return rt.Mem2Str(out), 0
  39. }
  40. func (self *Parser) decodeValue() (val types.JsonState) {
  41. e, v := decodeValue(self.s, self.p, self.dbuf == nil)
  42. if e < 0 {
  43. return v
  44. }
  45. self.p = e
  46. return v
  47. }
  48. func (self *Parser) skip() (int, types.ParsingError) {
  49. e, s := skipValue(self.s, self.p)
  50. if e < 0 {
  51. return self.p, types.ParsingError(-e)
  52. }
  53. self.p = e
  54. return s, 0
  55. }
  56. func (self *Parser) skipFast() (int, types.ParsingError) {
  57. e, s := skipValueFast(self.s, self.p)
  58. if e < 0 {
  59. return self.p, types.ParsingError(-e)
  60. }
  61. self.p = e
  62. return s, 0
  63. }
  64. func (self *Node) encodeInterface(buf *[]byte) error {
  65. out, err := json.Marshal(self.packAny())
  66. if err != nil {
  67. return err
  68. }
  69. *buf = append(*buf, out...)
  70. return nil
  71. }
  72. func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) {
  73. for _, p := range path {
  74. if idx, ok := p.(int); ok && idx >= 0 {
  75. if err := self.searchIndex(idx); err != 0 {
  76. return self.p, err
  77. }
  78. } else if key, ok := p.(string); ok {
  79. if err := self.searchKey(key); err != 0 {
  80. return self.p, err
  81. }
  82. } else {
  83. panic("path must be either int(>=0) or string")
  84. }
  85. }
  86. var start int
  87. var e types.ParsingError
  88. if validate {
  89. start, e = self.skip()
  90. } else {
  91. start, e = self.skipFast()
  92. }
  93. if e != 0 {
  94. return self.p, e
  95. }
  96. return start, 0
  97. }
  98. func validate_utf8(str string) bool {
  99. return utf8.ValidString(str)
  100. }