fast-replace/go-fast-replace/internal/corpus/corpus.go

47 lines
902 B
Go
Raw Permalink Normal View History

2023-12-30 21:35:09 +00:00
package corpus
import (
"fmt"
"math/rand"
2023-12-31 17:18:29 +00:00
"strings"
2023-12-30 21:35:09 +00:00
)
2023-12-31 17:18:29 +00:00
func MakeCorpus(n int) string {
2023-12-30 21:35:09 +00:00
// Generate data
lineLength := 100
var lines []string
2023-12-31 17:18:29 +00:00
for i := 1; i <= n; i++ {
2023-12-30 21:35:09 +00:00
lines = append(lines, makeLine(i, lineLength))
}
// Randomize
for i := range lines {
j := rand.Intn(i + 1)
lines[i], lines[j] = lines[j], lines[i]
}
2023-12-31 17:18:29 +00:00
return strings.Join(lines, "\n")
2023-12-30 21:35:09 +00:00
}
func makeLine(line int, lineLength int) string {
var key string = fmt.Sprintf("%06d", line)
lineLength = lineLength - len(key)
var firstLen int = rand.Intn(lineLength)
var secondLen int = lineLength - firstLen
return randomString(firstLen) + key + randomString(secondLen)
}
func randomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyz")
var length int = len(letters)
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(length)]
}
return string(s)
}