-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode.go
90 lines (73 loc) · 1.75 KB
/
mode.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
package performance
import (
"strconv"
"strings"
"pgregory.net/rand"
)
type LengthMode int
const (
SingleDigitMode LengthMode = iota
MediumLengthMode
LargeLengthMode
)
var (
mediumLength = [2]int{4, 5}
largeLength = [2]int{9, 10}
dotByte = byte('.')
num = [...]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
numBytes = [...]byte{0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39}
modes = [...]*mode{
{func() string {
return "." + num[rand.Intn(len(num))]
}},
{func() string {
zeroes := mediumLength[rand.Intn(2)]
trailing := numBytes[(rand.Intn(2)+9)%10]
buf := new(strings.Builder)
cache := make([]byte, zeroes)
buf.Grow(zeroes * 2)
for i := 0; i < zeroes; i++ {
cache[i] = trailing
}
buf.Write([]byte{dotByte, numBytes[rand.Intn(len(numBytes))]})
buf.Write(cache)
buf.WriteString(randNum(zeroes))
return buf.String()
}},
{func() string {
zeroes := largeLength[rand.Intn(2)]
endNums := mediumLength[rand.Intn(2)]
trailing := numBytes[(rand.Intn(2)+9)%10]
buf := new(strings.Builder)
cache := make([]byte, zeroes)
buf.Grow(endNums + zeroes)
for i := 0; i < zeroes; i++ {
cache[i] = trailing
}
buf.Write([]byte{dotByte, numBytes[rand.Intn(len(numBytes))]})
buf.Write(cache)
buf.WriteString(randNum(endNums))
return buf.String()
}},
}
)
func (l LengthMode) Index() int {
return [...]int{1, 11, 16}[l]
}
type mode struct {
f func() string
}
func (m *mode) float() float64 {
f, _ := strconv.ParseFloat(m.f(), 64)
return f
}
func (m *mode) string() string {
return m.f()
}
func randNum(length int) string {
buf := make([]byte, length)
for i := 0; i < length; i++ {
buf[i] = numBytes[rand.Intn(len(numBytes))]
}
return string(buf)
}