api_compat.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // +build !amd64 !go1.16 go1.22
  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/base64`
  20. `encoding/json`
  21. `fmt`
  22. `github.com/bytedance/sonic/internal/native/types`
  23. `github.com/bytedance/sonic/internal/rt`
  24. )
  25. func init() {
  26. println("WARNING: sonic only supports Go1.16~1.20 && CPU amd64, but your environment is not suitable")
  27. }
  28. func quote(buf *[]byte, val string) {
  29. quoteString(buf, val)
  30. }
  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 decodeBase64(src string) ([]byte, error) {
  40. return base64.StdEncoding.DecodeString(src)
  41. }
  42. func encodeBase64(src []byte) string {
  43. return base64.StdEncoding.EncodeToString(src)
  44. }
  45. func (self *Parser) decodeValue() (val types.JsonState) {
  46. e, v := decodeValue(self.s, self.p, self.dbuf == nil)
  47. if e < 0 {
  48. return v
  49. }
  50. self.p = e
  51. return v
  52. }
  53. func (self *Parser) skip() (int, types.ParsingError) {
  54. e, s := skipValue(self.s, self.p)
  55. if e < 0 {
  56. return self.p, types.ParsingError(-e)
  57. }
  58. self.p = e
  59. return s, 0
  60. }
  61. func (self *Parser) skipFast() (int, types.ParsingError) {
  62. e, s := skipValueFast(self.s, self.p)
  63. if e < 0 {
  64. return self.p, types.ParsingError(-e)
  65. }
  66. self.p = e
  67. return s, 0
  68. }
  69. func (self *Node) encodeInterface(buf *[]byte) error {
  70. out, err := json.Marshal(self.packAny())
  71. if err != nil {
  72. return err
  73. }
  74. *buf = append(*buf, out...)
  75. return nil
  76. }
  77. func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
  78. self.parser.p = 0
  79. var err types.ParsingError
  80. for _, p := range path {
  81. if idx, ok := p.(int); ok && idx >= 0 {
  82. if err = self.parser.searchIndex(idx); err != 0 {
  83. return Node{}, self.parser.ExportError(err)
  84. }
  85. } else if key, ok := p.(string); ok {
  86. if err = self.parser.searchKey(key); err != 0 {
  87. return Node{}, self.parser.ExportError(err)
  88. }
  89. } else {
  90. panic("path must be either int(>=0) or string")
  91. }
  92. }
  93. var start = self.parser.p
  94. if start, err = self.parser.skip(); err != 0 {
  95. return Node{}, self.parser.ExportError(err)
  96. }
  97. ns := len(self.parser.s)
  98. if self.parser.p > ns || start >= ns || start>=self.parser.p {
  99. return Node{}, fmt.Errorf("skip %d char out of json boundary", start)
  100. }
  101. t := switchRawType(self.parser.s[start])
  102. if t == _V_NONE {
  103. return Node{}, self.parser.ExportError(err)
  104. }
  105. return newRawNode(self.parser.s[start:self.parser.p], t), nil
  106. }