Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Mar 12, 2024
1 parent 477cfb8 commit 53894d2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 84 deletions.
72 changes: 0 additions & 72 deletions ast/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,75 +124,3 @@ func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
}
return start, 0
}

// GetByPath searches a path and returns relaction and types of target
func GetByPath(src string, path ...interface{}) (start int, end int, typ int, err error) {
p := NewParserObj(src)
s, e := p.getByPath(path...)
if e != 0 {
// for compatibility with old version
if e == types.ERR_NOT_FOUND {
return -1, -1, 0, ErrNotExist
}
if e == types.ERR_UNSUPPORT_TYPE {
panic("path must be either int(>=0) or string")
}
return -1, -1, 0, p.syntaxError(e)
}

t := switchRawType(p.s[s])
if t == _V_NONE {
return -1, -1, 0, ErrNotExist
}
return s, p.p, int(t), nil
}

// DecoderString decodes a JSON string from pos and return golang string.
// - needEsc indicates if to unescaped escaping chars
// - hasEsc tells if the returned string has escaping chars
func DecodeString(src string, pos int, needEsc bool) (v string, ret int, hasEsc bool) {
p := NewParserObj(src)
p.p = pos
switch val := p.decodeValue(); val.Vt {
case types.V_STRING:
str := p.s[val.Iv : p.p-1]
/* fast path: no escape sequence */
if val.Ep == -1 {
return str, p.p, false
} else if !needEsc {
return str, p.p, true
}
/* unquote the string */
out, err := Unquote(str)
/* check for errors */
if err != 0 {
return "", -int(err), true
} else {
return out, p.p, true
}
default:
return "", -int(_ERR_UNSUPPORT_TYPE), false
}
}

// ValidSyntax check if a json has a valid JSON syntax,
// while not validate UTF-8 charset
func ValidSyntax(json string) bool {
fsm := types.NewStateMachine()
p := 0
ret := native.ValidateOne(&json, &p, fsm, 0)
types.FreeStateMachine(fsm)

if ret < 0 {
return false
}

/* check for trailing spaces */
for ; p < len(json); p++ {
if !isSpace(json[p]) {
return false
}
}

return true
}
28 changes: 28 additions & 0 deletions ast/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,31 @@ func skipArray(src string, pos int) (ret int, start int) {
pos++
}
}

// DecoderString decodes a JSON string from pos and return golang string.
// - needEsc indicates if to unescaped escaping chars
// - hasEsc tells if the returned string has escaping chars
func DecodeString(src string, pos int, needEsc bool) (v string, ret int, hasEsc bool) {
p := NewParserObj(src)
p.p = pos
switch val := p.decodeValue(); val.Vt {
case types.V_STRING:
str := p.s[val.Iv : p.p-1]
/* fast path: no escape sequence */
if val.Ep == -1 {
return str, p.p, false
} else if !needEsc {
return str, p.p, true
}
/* unquote the string */
out, err := Unquote(str)
/* check for errors */
if err != 0 {
return "", -int(err), true
} else {
return out, p.p, true
}
default:
return "", -int(_ERR_UNSUPPORT_TYPE), false
}
}
12 changes: 0 additions & 12 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,15 +653,3 @@ func (self *Parser) ExportError(err types.ParsingError) error {
Code: err,
}.Description())
}

// SkipFast skip a json value in fast-skip algs,
// while not strictly validate JSON syntax and UTF-8 charset.
func SkipFast(src string, i int) (int, int, error) {
p := NewParserObj(src)
p.p = i
s, e := p.skipFast()
if e != 0 {
return -1, -1, p.ExportError(e)
}
return s, p.p, nil
}
42 changes: 42 additions & 0 deletions ast/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,45 @@ func (self *Searcher) getByPath(copystring bool, path ...interface{}) (Node, err
}
return newRawNode(raw, t), nil
}

// GetByPath searches a path and returns relaction and types of target
func GetByPath(src string, path ...interface{}) (start int, end int, typ int, err error) {
p := NewParserObj(src)
s, e := p.getByPath(path...)
if e != 0 {
// for compatibility with old version
if e == types.ERR_NOT_FOUND {
return -1, -1, 0, ErrNotExist
}
if e == types.ERR_UNSUPPORT_TYPE {
panic("path must be either int(>=0) or string")
}
return -1, -1, 0, p.syntaxError(e)
}

t := switchRawType(p.s[s])
if t == _V_NONE {
return -1, -1, 0, ErrNotExist
}
return s, p.p, int(t), nil
}

// ValidSyntax check if a json has a valid JSON syntax,
// while not validate UTF-8 charset
func ValidSyntax(json string) bool {
p := NewParserObj(json)
_, e := p.skip()
return e == 0
}

// SkipFast skip a json value in fast-skip algs,
// while not strictly validate JSON syntax and UTF-8 charset.
func SkipFast(src string, i int) (int, int, error) {
p := NewParserObj(src)
p.p = i
s, e := p.skipFast()
if e != 0 {
return -1, -1, p.ExportError(e)
}
return s, p.p, nil
}

0 comments on commit 53894d2

Please sign in to comment.