-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed.go
More file actions
33 lines (28 loc) · 720 Bytes
/
feed.go
File metadata and controls
33 lines (28 loc) · 720 Bytes
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
package main
import (
"math/rand"
"strings"
)
type randomRuneFeed struct {
runes []rune
}
func (r randomRuneFeed) Read(p []byte) (int, error) {
return strings.NewReader(string(r.runes[rand.Intn(len(r.runes))])).Read(p)
}
type randomRuneRangeFeed struct {
from, to rune
maxRunes int
rangeSize int
offset int
}
func (r *randomRuneRangeFeed) Read(p []byte) (int, error) {
if r.rangeSize == 0 {
r.rangeSize = int(r.to) - int(r.from) + 1
if r.maxRunes == 0 {
r.maxRunes = r.rangeSize
}
r.maxRunes = min(r.maxRunes, r.rangeSize)
r.offset = rand.Intn(r.rangeSize)
}
return strings.NewReader(string(r.from + rune((rand.Intn(r.maxRunes)*r.rangeSize/r.maxRunes+r.offset)%r.rangeSize))).Read(p)
}