aboutsummaryrefslogtreecommitdiffhomepage
path: root/tpl/strings
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-01-31 09:54:34 +0100
committerBjørn Erik Pedersen <[email protected]>2023-02-05 20:08:44 +0100
commitfce08904841ff9d70a1a0e0ca9e142636c937052 (patch)
tree0f52bdf26c750a2aa8d0a17d71feed893a49d961 /tpl/strings
parent87c78bd3e918f258bc1a4b0e8acdcec11b69bd35 (diff)
downloadhugo-fce08904841ff9d70a1a0e0ca9e142636c937052.tar.gz
hugo-fce08904841ff9d70a1a0e0ca9e142636c937052.zip
tpl/strings: Add strings.ContainsNonSpace
Diffstat (limited to 'tpl/strings')
-rw-r--r--tpl/strings/strings.go14
-rw-r--r--tpl/strings/strings_test.go21
2 files changed, 35 insertions, 0 deletions
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index a49451483..6c6e9b9d3 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -20,6 +20,7 @@ import (
"html/template"
"regexp"
"strings"
+ "unicode"
"unicode/utf8"
"github.com/gohugoio/hugo/common/text"
@@ -160,6 +161,19 @@ func (ns *Namespace) ContainsAny(s, chars any) (bool, error) {
return strings.ContainsAny(ss, sc), nil
}
+// ContainsNonSpace reports whether s contains any non-space characters as defined
+// by Unicode's White Space property,
+func (ns *Namespace) ContainsNonSpace(s any) bool {
+ ss := cast.ToString(s)
+
+ for _, r := range ss {
+ if !unicode.IsSpace(r) {
+ return true
+ }
+ }
+ return false
+}
+
// HasPrefix tests whether the input s begins with prefix.
func (ns *Namespace) HasPrefix(s, prefix any) (bool, error) {
ss, err := cast.ToStringE(s)
diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go
index 7e3960934..a230d4a48 100644
--- a/tpl/strings/strings_test.go
+++ b/tpl/strings/strings_test.go
@@ -145,6 +145,27 @@ func TestContainsAny(t *testing.T) {
}
}
+func TestContainsNonSpace(t *testing.T) {
+ t.Parallel()
+ c := qt.New(t)
+
+ for _, test := range []struct {
+ s any
+ expect bool
+ }{
+ {"", false},
+ {" ", false},
+ {" ", false},
+ {"\t", false},
+ {"\r", false},
+ {"a", true},
+ {" a", true},
+ {"a\n", true},
+ } {
+ c.Assert(ns.ContainsNonSpace(test.s), qt.Equals, test.expect)
+ }
+}
+
func TestCountRunes(t *testing.T) {
t.Parallel()
c := qt.New(t)