search.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. package ast
  17. import (
  18. `github.com/bytedance/sonic/internal/rt`
  19. `github.com/bytedance/sonic/internal/native/types`
  20. )
  21. type Searcher struct {
  22. parser Parser
  23. }
  24. func NewSearcher(str string) *Searcher {
  25. return &Searcher{
  26. parser: Parser{
  27. s: str,
  28. noLazy: false,
  29. },
  30. }
  31. }
  32. // GetByPathCopy search in depth from top json and returns a **Copied** json node at the path location
  33. func (self *Searcher) GetByPathCopy(path ...interface{}) (Node, error) {
  34. return self.getByPath(true, true, path...)
  35. }
  36. // GetByPathNoCopy search in depth from top json and returns a **Referenced** json node at the path location
  37. //
  38. // WARN: this search directly refer partial json from top json, which has faster speed,
  39. // may consumes more memory.
  40. func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
  41. return self.getByPath(false, true, path...)
  42. }
  43. func (self *Searcher) getByPath(copystring bool, validate bool, path ...interface{}) (Node, error) {
  44. var err types.ParsingError
  45. var start int
  46. self.parser.p = 0
  47. start, err = self.parser.getByPath(validate, path...)
  48. if err != 0 {
  49. // for compatibility with old version
  50. if err == types.ERR_NOT_FOUND {
  51. return Node{}, ErrNotExist
  52. }
  53. if err == types.ERR_UNSUPPORT_TYPE {
  54. panic("path must be either int(>=0) or string")
  55. }
  56. return Node{}, self.parser.syntaxError(err)
  57. }
  58. t := switchRawType(self.parser.s[start])
  59. if t == _V_NONE {
  60. return Node{}, self.parser.ExportError(err)
  61. }
  62. // copy string to reducing memory usage
  63. var raw string
  64. if copystring {
  65. raw = rt.Mem2Str([]byte(self.parser.s[start:self.parser.p]))
  66. } else {
  67. raw = self.parser.s[start:self.parser.p]
  68. }
  69. return newRawNode(raw, t), nil
  70. }
  71. // GetByPath searches a path and returns relaction and types of target
  72. func _GetByPath(src string, path ...interface{}) (start int, end int, typ int, err error) {
  73. p := NewParserObj(src)
  74. s, e := p.getByPath(false, path...)
  75. if e != 0 {
  76. // for compatibility with old version
  77. if e == types.ERR_NOT_FOUND {
  78. return -1, -1, 0, ErrNotExist
  79. }
  80. if e == types.ERR_UNSUPPORT_TYPE {
  81. panic("path must be either int(>=0) or string")
  82. }
  83. return -1, -1, 0, p.syntaxError(e)
  84. }
  85. t := switchRawType(p.s[s])
  86. if t == _V_NONE {
  87. return -1, -1, 0, ErrNotExist
  88. }
  89. if t == _V_NUMBER {
  90. p.p = 1 + backward(p.s, p.p-1)
  91. }
  92. return s, p.p, int(t), nil
  93. }
  94. // ValidSyntax check if a json has a valid JSON syntax,
  95. // while not validate UTF-8 charset
  96. func _ValidSyntax(json string) bool {
  97. p := NewParserObj(json)
  98. _, e := p.skip()
  99. if e != 0 {
  100. return false
  101. }
  102. if skipBlank(p.s, p.p) != -int(types.ERR_EOF) {
  103. return false
  104. }
  105. return true
  106. }
  107. // SkipFast skip a json value in fast-skip algs,
  108. // while not strictly validate JSON syntax and UTF-8 charset.
  109. func _SkipFast(src string, i int) (int, int, error) {
  110. p := NewParserObj(src)
  111. p.p = i
  112. s, e := p.skipFast()
  113. if e != 0 {
  114. return -1, -1, p.ExportError(e)
  115. }
  116. t := switchRawType(p.s[s])
  117. if t == _V_NONE {
  118. return -1, -1, ErrNotExist
  119. }
  120. if t == _V_NUMBER {
  121. p.p = 1 + backward(p.s, p.p-1)
  122. }
  123. return s, p.p, nil
  124. }