Skip to content

Commit

Permalink
refactor unquote in string scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jul 25, 2021
1 parent 0e31567 commit 49a818b
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,22 +381,15 @@ func (l *lexer) validNumber() bool {
}

func (l *lexer) scanString(start int) (int, string) {
var (
quote bool
newline bool
str string
err error
)
unquote := func(src string, quote bool) (ret string, str string, err error) {
ret = src
var quote, newline bool
unquote := func(src string, quote bool) (string, error) {
if quote {
src = "\"" + src + "\""
}
if newline {
src = strings.ReplaceAll(src, "\n", "\\n")
}
str, err = strconv.Unquote(src)
return
return strconv.Unquote(src)
}
for i, m := l.offset, len(l.source); i < m; i++ {
ch := l.source[i]
Expand All @@ -409,15 +402,17 @@ func (l *lexer) scanString(start int) (int, string) {
if !quote {
if !l.inString {
l.offset = i + 1
l.token, str, err = unquote(l.source[start:l.offset], false)
l.token = l.source[start:l.offset]
str, err := unquote(l.token, false)
if err != nil {
return tokInvalid, ""
}
return tokString, str
}
if i > l.offset {
l.offset = i
l.token, str, err = unquote(l.source[start:l.offset], true)
l.token = l.source[start:l.offset]
str, err := unquote(l.token, true)
if err != nil {
return tokInvalid, ""
}
Expand All @@ -433,7 +428,8 @@ func (l *lexer) scanString(start int) (int, string) {
if l.inString {
if i > l.offset+1 {
l.offset = i - 1
l.token, str, err = unquote(l.source[start:l.offset], true)
l.token = l.source[start:l.offset]
str, err := unquote(l.token, true)
if err != nil {
return tokInvalid, ""
}
Expand Down

0 comments on commit 49a818b

Please sign in to comment.