mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-12 20:13:59 +02:00
Handle views in combo with Ace base templates
As views looks like a regular template, but doesn't need a base template, we have to look inside it. Altough really not needed by this commit, reading the full file content into memory just to do a substring search is a waste. So this commit implements a `ReaderContains` func that in most cases should be much faster than doing an `ioutil.ReadAll` and `bytes.Contains`: ``` benchmark old ns/op new ns/op delta BenchmarkReaderContains 78452 20260 -74.18% benchmark old allocs new allocs delta BenchmarkReaderContains 46 20 -56.52% benchmark old bytes new bytes delta BenchmarkReaderContains 46496 1258 -97.29% ``` Fixes #999
This commit is contained in:
@@ -112,6 +112,42 @@ func BytesToReader(in []byte) io.Reader {
|
||||
return bytes.NewReader(in)
|
||||
}
|
||||
|
||||
// ReaderContains reports whether subslice is within r.
|
||||
func ReaderContains(r io.Reader, subslice []byte) bool {
|
||||
|
||||
if len(subslice) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
bufflen := len(subslice) * 4
|
||||
halflen := bufflen / 2
|
||||
buff := make([]byte, bufflen)
|
||||
var err error
|
||||
var n, i int
|
||||
|
||||
for {
|
||||
i++
|
||||
if i == 1 {
|
||||
n, err = io.ReadAtLeast(r, buff[:halflen], halflen)
|
||||
} else {
|
||||
if i != 2 {
|
||||
// shift left to catch overlapping matches
|
||||
copy(buff[:], buff[halflen:])
|
||||
}
|
||||
n, err = io.ReadAtLeast(r, buff[halflen:], halflen)
|
||||
}
|
||||
|
||||
if n > 0 && bytes.Contains(buff, subslice) {
|
||||
return true
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ThemeSet checks whether a theme is in use or not.
|
||||
func ThemeSet() bool {
|
||||
return viper.GetString("theme") != ""
|
||||
|
Reference in New Issue
Block a user