-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii.go
56 lines (43 loc) · 1.01 KB
/
ascii.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
package main
import (
"crypto/rand"
"math/big"
)
// asciiStart define the first printable
// index in the ascii table.
const asciiStart = 33
// asciiEnd define the last printable
// character in the ascii table.
const asciiEnd = 93
// ASCII is the ascii generator.
// Could be used to generate string containing
// only ascii printable characters.
type ASCII struct{}
// NewASCII returns a new ASCII generator.
func NewASCII() Generator {
return ASCII{}
}
func generateASCIIChar() (byte, error) {
r, err := rand.Int(rand.Reader, big.NewInt(asciiEnd))
if err != nil {
return 0, err
}
c := asciiStart + r.Int64()
return byte(c), nil
}
// Generate generates a string of size length using only
// printable ascii characters.
func (a ASCII) Generate(length int) (string, error) {
if length < 0 {
return "", ErrNegativValue
}
res := make([]byte, 0, length)
for i := 0; i < length; i++ {
r, err := generateASCIIChar()
if err != nil {
return "", err
}
res = append(res, r)
}
return string(res), nil
}