-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
stringutils.go
71 lines (58 loc) · 1.31 KB
/
stringutils.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* crunchy - find common flaws in passwords
* Copyright (c) 2017, Christian Muehlhaeuser <[email protected]>
*
* For license see LICENSE
*/
package crunchy
import (
"hash"
"strings"
"unicode"
"unicode/utf8"
)
// countUniqueChars returns the amount of unique runes in a string
func countUniqueChars(s string) int {
m := make(map[rune]struct{})
for _, c := range s {
c = unicode.ToLower(c)
if _, ok := m[c]; !ok {
m[c] = struct{}{}
}
}
return len(m)
}
// countSystematicChars returns how many runes in a string are part of a sequence ('abcdef', '654321')
func countSystematicChars(s string) int {
var x int
rs := []rune(s)
for i, c := range rs {
if i == 0 {
continue
}
if c == rs[i-1]+1 || c == rs[i-1]-1 {
x++
}
}
return x
}
// reverse returns the reversed form of a string
func reverse(s string) string {
var rs []rune
for len(s) > 0 {
r, size := utf8.DecodeLastRuneInString(s)
s = s[:len(s)-size]
rs = append(rs, r)
}
return string(rs)
}
// normalize returns the trimmed and lowercase version of a string
func normalize(s string) string {
return strings.TrimSpace(strings.ToLower(s))
}
// hashsum returns the hashed sum of a string
func hashsum(s string, hasher hash.Hash) string {
hasher.Reset()
_, _ = hasher.Write([]byte(s))
return string(hasher.Sum(nil))
}