Skip to content

Commit

Permalink
improve string generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Aug 17, 2024
1 parent bf4eb5c commit 15129ee
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions gen_strings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fauxrpc

import (
"math"
"strconv"
"strings"

Expand All @@ -18,6 +19,10 @@ type StringHints struct {
Version bool
}

func randInt64GeometricDist(faker *gofakeit.Faker, p float64) int64 {
return int64(math.Floor(math.Log(faker.Float64()) / math.Log(1.0-p)))
}

func GenerateString(faker *gofakeit.Faker, hints StringHints) string {
if hints.Rules == nil {
switch {
Expand All @@ -34,7 +39,7 @@ func GenerateString(faker *gofakeit.Faker, hints StringHints) string {
case hints.Version:
return faker.AppVersion()
}
return faker.Word()
return faker.HipsterSentence(int(randInt64GeometricDist(faker, 0.5) + 1))
}

if hints.Rules.Const != nil {
Expand Down Expand Up @@ -97,5 +102,20 @@ func GenerateString(faker *gofakeit.Faker, hints StringHints) string {
}
}

return faker.Sentence(int(maxLen / uint64(4)))[minLen:maxLen]
return generateHipsterText(faker, minLen, maxLen)
}

func generateHipsterText(faker *gofakeit.Faker, minLen, maxLen uint64) string {
b := &strings.Builder{}
addMoreText := func() {
b.WriteString(faker.HipsterSentence(int(randInt64GeometricDist(faker, 0.5) + 1)))
}
addMoreText()
for uint64(b.Len()) < minLen {
addMoreText()
}
if uint64(b.Len()) > maxLen {
return b.String()[:maxLen-1]
}
return b.String()
}

0 comments on commit 15129ee

Please sign in to comment.