-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharset_rule.go
More file actions
30 lines (26 loc) · 857 Bytes
/
charset_rule.go
File metadata and controls
30 lines (26 loc) · 857 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
package randomstring
import "strings"
// CharsetRuleFunc modify a charset and returns it
type CharsetRuleFunc func(charset string) string
// NewIncludeCharset returns a new charset rule func which add chars to charset
func NewIncludeCharset(chars string) CharsetRuleFunc {
return CharsetRuleFunc(func(charset string) string {
return charset + chars
})
}
// NewExcludeCharset returns a new charset rule func which removes chars from charset
func NewExcludeCharset(chars string) CharsetRuleFunc {
return CharsetRuleFunc(func(charset string) string {
sCharset := strings.Split(charset, "")
sChars := strings.Split(chars, "")
for _, char := range sChars {
for idx := range sCharset {
if char == sCharset[idx] {
sCharset = append(sCharset[:idx], sCharset[idx+1:]...)
break
}
}
}
return strings.Join(sCharset, "")
})
}