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

47 lines
902 B
Go

package corpus
import (
"fmt"
"math/rand"
"strings"
)
func MakeCorpus(n int) string {
// Generate data
lineLength := 100
var lines []string
for i := 1; i <= n; i++ {
lines = append(lines, makeLine(i, lineLength))
}
// Randomize
for i := range lines {
j := rand.Intn(i + 1)
lines[i], lines[j] = lines[j], lines[i]
}
return strings.Join(lines, "\n")
}
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)
}