mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
absurlreplacer: remove superfluous code
``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 18381 17946 -2.37% BenchmarkAbsURLSrcset 19531 19236 -1.51% BenchmarkXMLAbsURLSrcset 19316 19046 -1.40% BenchmarkXMLAbsURL 9818 9561 -2.62% benchmark old allocs new allocs delta BenchmarkAbsURL 24 24 +0.00% BenchmarkAbsURLSrcset 29 29 +0.00% BenchmarkXMLAbsURLSrcset 27 27 +0.00% BenchmarkXMLAbsURL 12 12 +0.00% benchmark old bytes new bytes delta BenchmarkAbsURL 3139 3139 +0.00% BenchmarkAbsURLSrcset 2354 2374 +0.85% BenchmarkXMLAbsURLSrcset 2584 2574 -0.39% BenchmarkXMLAbsURL 1864 1871 +0.38% ``` See #1059
This commit is contained in:
parent
be96aacb11
commit
be4ca21746
1 changed files with 29 additions and 58 deletions
|
@ -17,11 +17,6 @@ const (
|
||||||
matchStateFull
|
matchStateFull
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
matchPrefixSrc int = iota
|
|
||||||
matchPrefixHref
|
|
||||||
)
|
|
||||||
|
|
||||||
type contentlexer struct {
|
type contentlexer struct {
|
||||||
content []byte
|
content []byte
|
||||||
|
|
||||||
|
@ -47,12 +42,12 @@ type prefix struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var prefixes = []*prefix{
|
var prefixes = []*prefix{
|
||||||
&prefix{r: []rune{'s', 'r', 'c', '='}, f: checkCandidateSrc},
|
&prefix{r: []rune{'s', 'r', 'c', '='}, f: checkCandidateBase},
|
||||||
|
&prefix{r: []rune{'h', 'r', 'e', 'f', '='}, f: checkCandidateBase},
|
||||||
&prefix{r: []rune{'s', 'r', 'c', 's', 'e', 't', '='}, f: checkCandidateSrcset},
|
&prefix{r: []rune{'s', 'r', 'c', 's', 'e', 't', '='}, f: checkCandidateSrcset},
|
||||||
&prefix{r: []rune{'h', 'r', 'e', 'f', '='}, f: checkCandidateHref}}
|
}
|
||||||
|
|
||||||
type absURLMatcher struct {
|
type absURLMatcher struct {
|
||||||
prefix int
|
|
||||||
match []byte
|
match []byte
|
||||||
quote []byte
|
quote []byte
|
||||||
replacementURL []byte
|
replacementURL []byte
|
||||||
|
@ -128,35 +123,34 @@ func (l *contentlexer) emit() {
|
||||||
l.start = l.pos
|
l.start = l.pos
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a absURLMatcher) isSourceType() bool {
|
func checkCandidateBase(l *contentlexer) {
|
||||||
return a.prefix == matchPrefixSrc
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkCandidateSrc(l *contentlexer) {
|
|
||||||
for _, m := range l.matchers {
|
for _, m := range l.matchers {
|
||||||
if !m.isSourceType() {
|
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
l.replaceSimple(m)
|
// check for schemaless URLs
|
||||||
}
|
posAfter := l.pos + len(m.match)
|
||||||
}
|
if posAfter >= len(l.content) {
|
||||||
|
return
|
||||||
func checkCandidateHref(l *contentlexer) {
|
|
||||||
for _, m := range l.matchers {
|
|
||||||
if m.isSourceType() {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
l.replaceSimple(m)
|
r, _ := utf8.DecodeRune(l.content[posAfter:])
|
||||||
|
if r == '/' {
|
||||||
|
// schemaless: skip
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if l.pos > l.start {
|
||||||
|
l.emit()
|
||||||
|
}
|
||||||
|
l.pos += len(m.match)
|
||||||
|
l.w.Write(m.quote)
|
||||||
|
l.w.Write(m.replacementURL)
|
||||||
|
l.start = l.pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkCandidateSrcset(l *contentlexer) {
|
func checkCandidateSrcset(l *contentlexer) {
|
||||||
// special case, not frequent (me think)
|
// special case, not frequent (me think)
|
||||||
for _, m := range l.matchers {
|
for _, m := range l.matchers {
|
||||||
if m.isSourceType() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
|
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -207,29 +201,6 @@ func checkCandidateSrcset(l *contentlexer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *contentlexer) replaceSimple(m absURLMatcher) {
|
|
||||||
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// check for schemaless URLs
|
|
||||||
posAfter := l.pos + len(m.match)
|
|
||||||
if posAfter >= len(l.content) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
r, _ := utf8.DecodeRune(l.content[posAfter:])
|
|
||||||
if r == '/' {
|
|
||||||
// schemaless: skip
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if l.pos > l.start {
|
|
||||||
l.emit()
|
|
||||||
}
|
|
||||||
l.pos += len(m.match)
|
|
||||||
l.w.Write(m.quote)
|
|
||||||
l.w.Write(m.replacementURL)
|
|
||||||
l.start = l.pos
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contentlexer) replace() {
|
func (l *contentlexer) replace() {
|
||||||
contentLength := len(l.content)
|
contentLength := len(l.content)
|
||||||
var r rune
|
var r rune
|
||||||
|
@ -308,15 +279,15 @@ func newAbsURLReplacer(baseURL string) *absURLReplacer {
|
||||||
|
|
||||||
return &absURLReplacer{
|
return &absURLReplacer{
|
||||||
htmlMatchers: []absURLMatcher{
|
htmlMatchers: []absURLMatcher{
|
||||||
{matchPrefixSrc, dqHTMLMatch, dqHTML, base},
|
{dqHTMLMatch, dqHTML, base},
|
||||||
{matchPrefixSrc, sqHTMLMatch, sqHTML, base},
|
{sqHTMLMatch, sqHTML, base},
|
||||||
{matchPrefixHref, dqHTMLMatch, dqHTML, base},
|
{dqHTMLMatch, dqHTML, base},
|
||||||
{matchPrefixHref, sqHTMLMatch, sqHTML, base}},
|
{sqHTMLMatch, sqHTML, base}},
|
||||||
xmlMatchers: []absURLMatcher{
|
xmlMatchers: []absURLMatcher{
|
||||||
{matchPrefixSrc, dqXMLMatch, dqXML, base},
|
{dqXMLMatch, dqXML, base},
|
||||||
{matchPrefixSrc, sqXMLMatch, sqXML, base},
|
{sqXMLMatch, sqXML, base},
|
||||||
{matchPrefixHref, dqXMLMatch, dqXML, base},
|
{dqXMLMatch, dqXML, base},
|
||||||
{matchPrefixHref, sqXMLMatch, sqXML, base},
|
{sqXMLMatch, sqXML, base},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue