Reduce string allocations for plain strings
This commit is contained in:
parent
b835581a73
commit
7e6f7ce4cf
21
parser.go
21
parser.go
@ -219,7 +219,7 @@ func String(allowedQuotes string) Parser {
|
|||||||
var end int = ps.Pos + 1
|
var end int = ps.Pos + 1
|
||||||
|
|
||||||
inputLen := len(ps.Input)
|
inputLen := len(ps.Input)
|
||||||
result := &bytes.Buffer{}
|
var buf *bytes.Buffer
|
||||||
|
|
||||||
for end < inputLen {
|
for end < inputLen {
|
||||||
switch ps.Input[end] {
|
switch ps.Input[end] {
|
||||||
@ -229,6 +229,10 @@ func String(allowedQuotes string) Parser {
|
|||||||
return Node{}
|
return Node{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if buf == nil {
|
||||||
|
buf = bytes.NewBufferString(ps.Input[ps.Pos+1 : end])
|
||||||
|
}
|
||||||
|
|
||||||
c := ps.Input[end+1]
|
c := ps.Input[end+1]
|
||||||
if c == 'u' {
|
if c == 'u' {
|
||||||
if end+6 >= inputLen {
|
if end+6 >= inputLen {
|
||||||
@ -243,19 +247,26 @@ func String(allowedQuotes string) Parser {
|
|||||||
ps.Error.pos = end + 2
|
ps.Error.pos = end + 2
|
||||||
return Node{}
|
return Node{}
|
||||||
}
|
}
|
||||||
result.WriteRune(r)
|
buf.WriteRune(r)
|
||||||
end += 6
|
end += 6
|
||||||
} else {
|
} else {
|
||||||
result.WriteByte(c)
|
buf.WriteByte(c)
|
||||||
end += 2
|
end += 2
|
||||||
}
|
}
|
||||||
case quote:
|
case quote:
|
||||||
|
if buf == nil {
|
||||||
|
result := ps.Input[ps.Pos+1 : end]
|
||||||
|
ps.Pos = end + 1
|
||||||
|
return Node{Token: result}
|
||||||
|
}
|
||||||
ps.Pos = end + 1
|
ps.Pos = end + 1
|
||||||
return Node{Token: result.String()}
|
return Node{Token: buf.String()}
|
||||||
default:
|
default:
|
||||||
r, w := utf8.DecodeRuneInString(ps.Input[end:])
|
r, w := utf8.DecodeRuneInString(ps.Input[end:])
|
||||||
result.WriteRune(r)
|
|
||||||
end += w
|
end += w
|
||||||
|
if buf != nil {
|
||||||
|
buf.WriteRune(r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user