-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_rule.go
50 lines (45 loc) · 1.17 KB
/
output_rule.go
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
47
48
49
50
package randomstring
import (
"bytes"
"strings"
)
// OutputRuleFunc checks if the string meets the rule
type OutputRuleFunc func(str []byte, c byte) bool
// NewBeginWith returns a new output rule func that checks if str does start with a one of letters
func NewBeginWith(letters string) OutputRuleFunc {
return OutputRuleFunc(func(str []byte, c byte) bool {
if len(str) > 0 {
return true
}
if strings.IndexByte(letters, c) == -1 {
return false
}
return true
})
}
// NewNoDuplicateCharacters returns a new output rule func that checks if str doesn't have c
func NewNoDuplicateCharacters() OutputRuleFunc {
return OutputRuleFunc(func(str []byte, c byte) bool {
return bytes.IndexByte(str, c) == -1
})
}
// NewNoSequentialCharacters returns new output rule func that checks if str doesn't have n sequentials characters
func NewNoSequentialCharacters(n uint) OutputRuleFunc {
return OutputRuleFunc(func(str []byte, c byte) bool {
lStr := uint(len(str))
n1 := n - 1
if lStr < n1 {
return true
}
start := byte(n1)
valid := false
for i := lStr - n1; i < lStr; i++ {
if str[i] != c-start {
valid = true
break
}
start--
}
return valid
})
}