mirror of
https://github.com/gohugoio/hugo.git
synced 2024-07-07 01:32:15 +00:00
tpl/strings: Add strings.ContainsNonSpace
This commit is contained in:
parent
87c78bd3e9
commit
fce0890484
|
@ -20,6 +20,7 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/common/text"
|
"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
|
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.
|
// HasPrefix tests whether the input s begins with prefix.
|
||||||
func (ns *Namespace) HasPrefix(s, prefix any) (bool, error) {
|
func (ns *Namespace) HasPrefix(s, prefix any) (bool, error) {
|
||||||
ss, err := cast.ToStringE(s)
|
ss, err := cast.ToStringE(s)
|
||||||
|
|
|
@ -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) {
|
func TestCountRunes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
c := qt.New(t)
|
c := qt.New(t)
|
||||||
|
|
Loading…
Reference in a new issue