Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions pkg/jsonskip/jsonutil_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@
package jsonskip

import (
"bytes"
"encoding/json"
"errors"
"github.com/bytedance/sonic/ast"

"github.com/superdb/super/pkg/byteconv"
)

// Skip skips a single JSON value in b, returning the offset of its first
// non-whitespace byte and the offset just past its end. If b does not
// begin with a complete JSON value, start is negative.
//
// sonic's decoder.Skip is unavailable on js/wasm, but a Searcher with an
// empty path uses sonic's pure-Go skipper and returns the raw value.
func Skip(b []byte) (int, int) {
dec := json.NewDecoder(bytes.NewReader(b))
if err := dec.Decode(new(json.RawMessage)); err != nil {
var offset int
var serr *json.SyntaxError
if errors.As(err, &serr) {
offset = int(serr.Offset)
s := byteconv.UnsafeString(b)
node, err := ast.NewSearcher(s).GetByPath()
if err != nil {
return -1, 0
}
raw, err := node.Raw()
if err != nil {
return -1, 0
}
start := firstNonSpace(b)
return start, start + len(raw)
}

func firstNonSpace(b []byte) int {
for i, c := range b {
switch c {
case ' ', '\t', '\n', '\r':
default:
return i
}
return -1, offset
}
return 0, int(dec.InputOffset())
return len(b)
}
Loading