Move allocations later in the function.

This commit is contained in:
Dmitry Chestnykh 2015-01-21 21:40:45 +01:00
parent 69450cc2a9
commit 87c54b9ba5

View File

@ -53,13 +53,13 @@ func NewLenChars(length int, chars []byte) string {
if length == 0 { if length == 0 {
return "" return ""
} }
b := make([]byte, length)
r := make([]byte, length+(length/4)) // storage for random bytes.
clen := len(chars) clen := len(chars)
if clen > 256 { if clen > 256 {
panic("uniuri: maximum length of charset for NewLenChars is 256") panic("uniuri: maximum length of charset for NewLenChars is 256")
} }
maxrb := 256 - (256 % clen) maxrb := 256 - (256 % clen)
b := make([]byte, length)
r := make([]byte, length+(length/4)) // storage for random bytes.
i := 0 i := 0
for { for {
if _, err := rand.Read(r); err != nil { if _, err := rand.Read(r); err != nil {