mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-13 20:24:00 +02:00
Improve TotalWords counter func
It is obviously more efficient when we do not care about the actual words. ``` BenchmarkTotalWords-4 100000 18795 ns/op 0 B/op 0 allocs/op BenchmarkTotalWordsOld-4 30000 46751 ns/op 6400 B/op 1 allocs/op ```
This commit is contained in:
@@ -384,8 +384,25 @@ func RenderBytes(ctx *RenderingContext) []byte {
|
||||
}
|
||||
}
|
||||
|
||||
// TotalWords returns an int of the total number of words in a given content.
|
||||
// TotalWords counts instance of one or more consecutive white space
|
||||
// characters, as defined by unicode.IsSpace, in s.
|
||||
// This is a cheaper way of word counting than the obvious len(strings.Fields(s)).
|
||||
func TotalWords(s string) int {
|
||||
n := 0
|
||||
inWord := false
|
||||
for _, r := range s {
|
||||
wasInWord := inWord
|
||||
inWord = !unicode.IsSpace(r)
|
||||
if inWord && !wasInWord {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// Old implementation only kept for benchmark comparison.
|
||||
// TODO(bep) remove
|
||||
func totalWordsOld(s string) int {
|
||||
return len(strings.Fields(s))
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user