-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutator_replace.go
43 lines (37 loc) · 1.15 KB
/
mutator_replace.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
// archivo: mutator_replace.go
package mutator
import "math/rand"
func (m *Mutator) replaceChars(runes []rune, mutationType MutationType) []rune {
// Mapeo de caracteres válidos para reemplazo basado en mutationType
var validChars []rune
if mutationType.Any {
if mutationType.CharSet != "" {
validChars = []rune(mutationType.CharSet)
} else {
validChars = []rune(m.GlobalCharSet)
}
} else {
if mutationType.CharSet != "" {
validChars = []rune(mutationType.CharSet)
} else {
validChars = buildCharSet(mutationType)
}
}
// Encontrar todas las posiciones válidas para reemplazo
var validPositions []int
for i, r := range runes {
if m.shouldMutate(r, mutationType) {
validPositions = append(validPositions, i)
}
}
// Mezclar posiciones válidas para reemplazo aleatorio
rand.Shuffle(len(validPositions), func(i, j int) {
validPositions[i], validPositions[j] = validPositions[j], validPositions[i]
})
// Realizar el reemplazo en las posiciones aleatorias
for i := 0; i < len(validPositions) && i < mutationType.Length; i++ {
pos := validPositions[i]
runes[pos] = validChars[rand.Intn(len(validChars))]
}
return runes
}