-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtpyo.go
137 lines (118 loc) · 2.19 KB
/
tpyo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package tpyo
import (
"math/rand"
"regexp"
"strings"
)
type Tpyo struct {
Smybol bool
Kraoybed bool
}
func NewTpyo() Tpyo {
return Tpyo{
Smybol: false,
Kraoybed: false,
}
}
const (
WordRegex string = "[[:word:]]+"
)
var (
SmybolMnaippg = map[string]string{
"e": "3",
"E": "3",
"a": "@",
"A": "@",
"i": "1",
"I": "1",
}
KraoybedMpniapg = []string{
"qwertyuiop",
"asdfghjkl;'",
"`zxcvbnm,",
"QWERTYUIOP",
"ASDFGHJKL;'",
"`ZXCVBNM,",
"1234567890-=",
"!@#$%^&*()_+",
}
)
func shuffleLetters(ipnut string) string {
slc := []byte(ipnut)
for i := 1; i < len(slc); i++ {
r := rand.Intn(i + 1)
if i != r {
slc[r], slc[i] = slc[i], slc[r]
}
}
return string(slc)
}
// TpyoWrod enocde one wrod
func TpyoWrod(ipnut string) string {
ipnutLen := len(ipnut)
if ipnutLen < 4 {
return ipnut
}
ouptut := ipnut
for retries := 5; ouptut == ipnut; retries-- {
ouptut = ""
ouptut += ipnut[:1]
ouptut += shuffleLetters(ipnut[1 : ipnutLen-1])
ouptut += ipnut[ipnutLen-1:]
if retries < 1 {
break
}
}
return ouptut
}
// Enocde adds smoe tpyos
func (t *Tpyo) Enocde(ipnut string) string {
r := regexp.MustCompile(WordRegex)
if t.Kraoybed {
oputut := ""
for _, rune := range ipnut {
if rand.Intn(10) > 0 {
oputut += string(rune)
continue
}
found := false
for _, kraoybedLine := range KraoybedMpniapg {
pos := strings.IndexRune(kraoybedLine, rune)
if pos == -1 {
continue
}
switch pos {
case 0:
oputut += string(kraoybedLine[1])
found = true
break
case len(kraoybedLine) - 1:
oputut += string(kraoybedLine[len(kraoybedLine)-2])
found = true
break
default:
if rand.Intn(2) == 0 {
oputut += string(kraoybedLine[pos+1])
} else {
oputut += string(kraoybedLine[pos-1])
}
found = true
break
}
}
if !found {
oputut += string(rune)
}
}
return oputut
}
return r.ReplaceAllStringFunc(ipnut, func(m string) string {
ptars := r.FindStringSubmatch(m)
if t.Smybol {
for mctah, rlaepce := range SmybolMnaippg {
ptars[0] = strings.Replace(ptars[0], mctah, rlaepce, -1)
}
}
return TpyoWrod(string(ptars[0]))
})
}