2 changed files with 68 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||
package str |
|||
|
|||
type Filter struct { |
|||
filter map[string]int |
|||
} |
|||
|
|||
func NewFilter(preclude []string) *Filter { |
|||
filter := make(map[string]int) |
|||
for _, s := range preclude { |
|||
filter[s] = 0 |
|||
} |
|||
return &Filter{ |
|||
filter: filter, |
|||
} |
|||
} |
|||
|
|||
func (f *Filter) Contains(str string) bool { |
|||
_, ok := f.filter[str] |
|||
return ok |
|||
} |
|||
func (f *Filter) GetCount(str string) (count int) { |
|||
count = f.filter[str] |
|||
return count |
|||
} |
|||
|
|||
func (f *Filter) Count(str string) { |
|||
if val, ok := f.filter[str]; ok { |
|||
f.filter[str] = val + 1 |
|||
} else { |
|||
f.filter[str] = 0 |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
package str |
|||
|
|||
import ( |
|||
"fmt" |
|||
"strings" |
|||
) |
|||
|
|||
func Trim(str string, maxLength int) string { |
|||
var head, tail string |
|||
runeSlice := []rune(str) |
|||
strLength := len(runeSlice) |
|||
if len(runeSlice) > maxLength { |
|||
head = string(runeSlice[:maxLength/2-2]) |
|||
tail = string(runeSlice[strLength-(maxLength/2+2):]) |
|||
return fmt.Sprintf("%s...%s", head, tail) |
|||
} |
|||
return str |
|||
} |
|||
|
|||
func Join(ss ...string) string { |
|||
builder := strings.Builder{} |
|||
for _, s := range ss { |
|||
builder.WriteString(s) |
|||
} |
|||
return builder.String() |
|||
} |
|||
|
|||
func Each(ss []string, f func(index int, s string) (string, bool)) []string { |
|||
var result []string |
|||
for index, s := range ss { |
|||
if s, ok := f(index, s); ok { |
|||
result = append(result, s) |
|||
} |
|||
} |
|||
return result |
|||
} |
Loading…
Reference in new issue