Skip to content

Commit

Permalink
tpl/strings: Add strings.ContainsNonSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 5, 2023
1 parent 87c78bd commit fce0890
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tpl/strings/strings.go
Expand Up @@ -20,6 +20,7 @@ import (
"html/template"
"regexp"
"strings"
"unicode"
"unicode/utf8"

"github.com/gohugoio/hugo/common/text"
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tpl/strings/strings_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit fce0890

Please sign in to comment.