-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.go
More file actions
46 lines (40 loc) · 706 Bytes
/
Copy pathcount.go
File metadata and controls
46 lines (40 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"strconv"
"strings"
"unicode/utf8"
"github.com/dustin/go-humanize"
)
func countBytes(s string, human bool) string {
if human {
return humanize.Bytes(uint64(len(s)))
}
return strconv.Itoa(len(s))
}
func countChars(str string, chars ...rune) int {
if len(chars) > 0 {
char := chars[0]
count := 0
for _, c := range str {
if c == char {
count++
}
}
return count
}
return utf8.RuneCountInString(str)
}
func countWords(str string, words ...string) int {
wordList := strings.Fields(str)
if len(words) > 0 {
word := words[0]
count := 0
for _, w := range wordList {
if w == word {
count++
}
}
return count
}
return len(wordList)
}