Skip to content

Commit

Permalink
more eligible decode
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jan 22, 2024
1 parent 8071847 commit 57ae213
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions ast/api_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,28 @@ func (self *Parser) getByPathNoValidate(path ...interface{}) (int, types.ValueTy
return start, t, 0
}

func DecodeString(src string, pos int) (ret int, v string) {
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 p.p, str
}
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), ""
return "", -int(err), true
} else {
return p.p, out
return out, p.p, true
}
default:
return -int(_ERR_UNSUPPORT_TYPE), ""
return "", -int(_ERR_UNSUPPORT_TYPE), false
}
}

Expand Down
10 changes: 6 additions & 4 deletions ast/api_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,23 @@ func (self *Parser) getByPathNoValidate(path ...interface{}) (int, types.ValueTy
}

//go:nocheckptr
func DecodeString(src string, pos int) (ret int, v string) {
func DecodeString(src string, pos int, needEsc bool) (v string, ret int, hasEsc bool) {
ret, ep := skipString(src, pos)
if ep == -1 {
(*rt.GoString)(unsafe.Pointer(&v)).Ptr = rt.IndexChar(src, pos+1)
(*rt.GoString)(unsafe.Pointer(&v)).Len = ret - pos - 2
return ret, v
return v, ret, false
} else if needEsc {
return src[pos+1:ret-1], ret, true
}

vv, ok := unquoteBytes(rt.Str2Mem(src[pos:ret]))
if !ok {
return -int(types.ERR_INVALID_CHAR), ""
return "", -int(types.ERR_INVALID_CHAR), true
}

runtime.KeepAlive(src)
return ret, rt.Mem2Str(vv)
return rt.Mem2Str(vv), ret, true
}

// ValidSyntax check if a json has a valid JSON syntax,
Expand Down
2 changes: 1 addition & 1 deletion ast/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func decodeFalse(src string, pos int) (ret int) {

func decodeBinary(src string, pos int) (ret int, v []byte) {
var vv string
ret, vv = DecodeString(src, pos)
vv, ret, _ = DecodeString(src, pos, true)
if ret < 0 {
return ret, nil
}
Expand Down
2 changes: 1 addition & 1 deletion ast/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ func (self Value) toFloat64() (float64, error) {
}

func (self Value) toString() (string, error) {
ret, str := DecodeString(self.js, 0)
str, ret, _ := DecodeString(self.js, 0, true)
if ret < 0 {
return "", NewParserObj(self.js).ExportError(types.ParsingError(-ret))
}
Expand Down

0 comments on commit 57ae213

Please sign in to comment.