-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringTransforms.go
127 lines (92 loc) · 3.36 KB
/
stringTransforms.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
package main
import (
"math/rand"
"time"
)
func padString(s string) string {
//leftChar := '['
var rightChar rune
// Initialize a slice containing the characters to choose from
leftMostChars := []rune{'{', '[', '(', '<'}
// Seed the random number generator
//rand.Seed(time.Now().UnixNano())
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Randomly select a character from the slice
leftChar := leftMostChars[r.Intn(len(leftMostChars))]
rightChar = matchCharacterPair(leftChar, rightChar)
// Add the left and right characters to the input string
paddedStr := string(leftChar) + s + string(rightChar)
//fmt.Printf(paddedStr)
return paddedStr
}
func matchCharacterPair(leftChar rune, rightChar rune) rune {
rightMostChars := []rune{'}', ']', ')', '>'}
if leftChar == '(' {
rightChar = ')'
} else if leftChar == '{' {
rightChar = '}'
} else if leftChar == '[' {
rightChar = ']'
} else if leftChar == '<' {
rightChar = '>'
} else {
rightChar = rightMostChars[rand.Intn(len(rightMostChars))]
}
return rightChar
}
func surroundString(input string) string {
// Only run this transformation routine about 25% of the times it is called
// Generate a random number between 0 and 3
//rand.Seed(time.Now().UnixNano())
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomNumber := r.Intn(4)
// Only perform the routines if the random number is 0
if randomNumber == 0 {
// Initialize a slice containing the valid surrounding characters
chars := []rune{'_', '-', '*', '&', '^', '%', '$', '#', '@', '!', '~', '?', '.'}
// Seed the random number generator
//rand.Seed(time.Now().UnixNano())
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Randomly select a character from the slice of valid characters
randChar := chars[r.Intn(len(chars))]
// Surround the input string with the randomly selected character
output := string(randChar) + input + string(randChar)
//fmt.Printf("Input string: %s\nOutput string: %s\n", input, output)
return output
} else {
return input
}
}
// const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
const charset = "1234567890"
func randomStringNumbers(length int) string {
// Seed the random number generator
//rand.Seed(time.Now().UnixNano())
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Generate a random string of the specified length
b := make([]byte, length)
for i := range b {
b[i] = charset[r.Intn(len(charset))]
}
return string(b)
}
func randPadString(input string) string {
// Determine the number of characters to pad
numPadding := requestedPasswordLength - len(input)
// If the input string is already longer than the desired length, return the input string
if numPadding <= 0 {
return input
}
// Generate random strings for the left and right padding
leftPadding := randomStringNumbers(numPadding / 2)
rightPadding := randomStringNumbers(numPadding - len(leftPadding))
// Concatenate the left and right padding with the input string
output := leftPadding + input + rightPadding
// Generate a random position to insert the input string
//rand.Seed(time.Now().UnixNano())
r := rand.New(rand.NewSource(time.Now().UnixNano()))
insertPos := r.Intn(len(output) - len(input) + 1)
// Insert the input string at the random position and return the result
output = output[:insertPos] + input + output[insertPos+len(input):]
return output
}