Skip to content

Commit

Permalink
tpl/strings: strings.RuneCount
Browse files Browse the repository at this point in the history
  • Loading branch information
theory authored and bep committed Jun 4, 2018
1 parent c311529 commit 019bd55
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
28 changes: 28 additions & 0 deletions docs/content/en/functions/strings.RuneCount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: strings.RuneCount
description: Determines the number of runes in a string.
godocref:
date: 2018-06-01
publishdate: 2018-06-01
lastmod: 2018-06-01
categories: [functions]
menu:
docs:
parent: "functions"
keywords: [counting, character count, length, rune length, rune count]
signature: ["strings.RuneCount INPUT"]
workson: []
hugoversion:
relatedfuncs: ["len", "countrunes"]
deprecated: false
aliases: []
---

In contrast with `strings.CountRunes` function, which strips HTML and whitespace before counting runes, `strings.RuneCount` simply counts all the runes in a string. It relies on the Go [`utf8.RuneCountInString`] function.

```
{{ "Hello, 世界" | strings.RuneCount }}
<!-- outputs a content length of 9 runes. -->
```

[`utf8.RuneCount`]: https://golang.org/pkg/unicode/utf8/#RuneCount
20 changes: 19 additions & 1 deletion docs/data/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3146,7 +3146,25 @@
"Aliases": [
"countrunes"
],
"Examples": []
"Examples": [
[
"{{ \"Hello, 世界\" | countrunes }}",
"8"
]
]
},
"RuneCount": {
"Description": "RuneCount returns the number of runes in s",
"Args": [
"s"
],
"Aliases": [],
"Examples": [
[
"{{ \"Hello, 世界\" | strings.RuneCount }}",
"9"
]
]
},
"CountWords": {
"Description": "CountWords returns the approximate word count in s.",
Expand Down
5 changes: 5 additions & 0 deletions tpl/strings/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func init() {
[][2]string{},
)

ns.AddMethodMapping(ctx.RuneCount,
nil,
[][2]string{},
)

ns.AddMethodMapping(ctx.CountWords,
[]string{"countwords"},
[][2]string{},
Expand Down
9 changes: 9 additions & 0 deletions tpl/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func (ns *Namespace) CountRunes(s interface{}) (int, error) {
return counter, nil
}

// RuneCount returns the number of runes in s.
func (ns *Namespace) RuneCount(s interface{}) (int, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return 0, fmt.Errorf("Failed to convert content to string: %s", err)
}
return utf8.RuneCountInString(ss), nil
}

// CountWords returns the approximate word count in s.
func (ns *Namespace) CountWords(s interface{}) (int, error) {
ss, err := cast.ToStringE(s)
Expand Down
27 changes: 27 additions & 0 deletions tpl/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,33 @@ func TestCountRunes(t *testing.T) {
}
}

func TestRuneCount(t *testing.T) {
t.Parallel()

for i, test := range []struct {
s interface{}
expect interface{}
}{
{"foo bar", 7},
{"旁边", 2},
{`<div class="test">旁边</div>`, 26},
// errors
{tstNoStringer{}, false},
} {
errMsg := fmt.Sprintf("[%d] %v", i, test.s)

result, err := ns.RuneCount(test.s)

if b, ok := test.expect.(bool); ok && !b {
require.Error(t, err, errMsg)
continue
}

require.NoError(t, err, errMsg)
assert.Equal(t, test.expect, result, errMsg)
}
}

func TestCountWords(t *testing.T) {
t.Parallel()

Expand Down

2 comments on commit 019bd55

@kaushalmodi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, just noticed this. The naming is very confusing IMO.. RuneCount vs CountRunes?? Really? How can anyone remember which is which?

I'd suggest having a more descriptive name for the new function to distinguish from the other.

@bep
Copy link
Member

@bep bep commented on 019bd55 Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaushalmodi we have had this discussion. And for future reference, I almost never read comments on merged (i.e. done) branches.

Please sign in to comment.