Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-07-03 11:34:40 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-07-03 11:35:46 +0300
commit028b356787426dbc190ce9868fbc9a6400c2996e (patch)
tree6ac2e3ad9d7a1acd3da7dbc0b542b9fa04fed92b /tpl/strings
parente9f87c4e3feee937d05504763935805fec26213c (diff)
tpl/strings: Add strings.Count
Fixes #7453
Diffstat (limited to 'tpl/strings')
-rw-r--r--tpl/strings/init.go7
-rw-r--r--tpl/strings/strings.go15
2 files changed, 22 insertions, 0 deletions
diff --git a/tpl/strings/init.go b/tpl/strings/init.go
index 7e638e6fc..fe0cff83b 100644
--- a/tpl/strings/init.go
+++ b/tpl/strings/init.go
@@ -51,6 +51,13 @@ func init() {
[][2]string{},
)
+ ns.AddMethodMapping(ctx.Count,
+ nil,
+ [][2]string{
+ {`{{"aabab" | strings.Count "a" }}`, `3`},
+ },
+ )
+
ns.AddMethodMapping(ctx.FindRE,
[]string{"findRE"},
[][2]string{
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index e807fe6fa..d78234e41 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"html/template"
+ "strings"
_strings "strings"
"unicode/utf8"
@@ -90,6 +91,20 @@ func (ns *Namespace) CountWords(s interface{}) (int, error) {
return counter, nil
}
+// Count counts the number of non-overlapping instances of substr in s.
+// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
+func (ns *Namespace) Count(substr, s interface{}) (int, error) {
+ substrs, err := cast.ToStringE(substr)
+ if err != nil {
+ return 0, _errors.Wrap(err, "Failed to convert substr to string")
+ }
+ ss, err := cast.ToStringE(s)
+ if err != nil {
+ return 0, _errors.Wrap(err, "Failed to convert s to string")
+ }
+ return strings.Count(ss, substrs), nil
+}
+
// Chomp returns a copy of s with all trailing newline characters removed.
func (ns *Namespace) Chomp(s interface{}) (interface{}, error) {
ss, err := cast.ToStringE(s)