forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
109 lines (96 loc) · 2.7 KB
/
random.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Package random contains functions for generating random values
package random
import (
"math"
"math/rand"
"regexp"
"time"
"unicode"
"github.com/pkg/errors"
)
// Rnd -
var Rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
// Default set, matches "[a-zA-Z0-9_.-]"
const defaultSet = "-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
// StringRE - Generate a random string that matches a given regular
// expression. Defaults to "[a-zA-Z0-9_.-]"
func StringRE(count int, match string) (r string, err error) {
var chars = []rune(defaultSet)
if match != "" {
chars, err = matchChars(match)
if err != nil {
return "", err
}
}
return rndString(count, chars)
}
// StringBounds returns a random string of characters with a codepoint
// between the lower and upper bounds. Only valid characters are returned
// and if a range is given where no valid characters can be found, an error
// will be returned.
func StringBounds(count int, lower, upper rune) (r string, err error) {
chars := filterRange(lower, upper)
if len(chars) == 0 {
return "", errors.Errorf("No printable codepoints found between U%#q and U%#q.", lower, upper)
}
return rndString(count, chars)
}
// produce a string containing a random selection of given characters
func rndString(count int, chars []rune) (string, error) {
s := make([]rune, count)
for i := range s {
s[i] = chars[Rnd.Intn(len(chars))]
}
return string(s), nil
}
func filterRange(lower, upper rune) []rune {
out := []rune{}
for r := lower; r <= upper; r++ {
if unicode.IsGraphic(r) {
out = append(out, r)
}
}
return out
}
func matchChars(match string) ([]rune, error) {
r, err := regexp.Compile(match)
if err != nil {
return nil, err
}
candidates := filterRange(0, unicode.MaxRune)
out := []rune{}
for _, c := range candidates {
if r.MatchString(string(c)) {
out = append(out, c)
}
}
return out, nil
}
// Item -
func Item(items []interface{}) (interface{}, error) {
if len(items) == 0 {
return nil, errors.Errorf("expected a non-empty array or slice")
}
if len(items) == 1 {
return items[0], nil
}
n := Rnd.Intn(len(items))
return items[n], nil
}
// Number -
func Number(min, max int64) (int64, error) {
if min > max {
return 0, errors.Errorf("min must not be greater than max (was %d, %d)", min, max)
}
if min == math.MinInt64 {
min++
}
if max-min >= (math.MaxInt64 >> 1) {
return 0, errors.Errorf("spread between min and max too high - must not be greater than 63-bit maximum (%d - %d = %d)", max, min, max-min)
}
return Rnd.Int63n(max-min+1) + min, nil
}
// Float - For now this is really just a wrapper around `rand.Float64`
func Float(min, max float64) (float64, error) {
return min + Rnd.Float64()*(max-min), nil
}