rand: add rand.ascii (#8675)

pull/8680/head
cbracketdash 2021-02-10 22:32:40 -08:00 committed by GitHub
parent d4f6488afd
commit c636a7080d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -168,6 +168,21 @@ pub fn hex(len int) string {
return unsafe { buf.vstring_with_len(len) }
}
const (
ascii_chars = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz{|}~'
)
// ascii returns a random string of the printable ASCII characters with length `len`.
pub fn ascii(len int) string {
mut buf := malloc(len)
for i in 0 .. len {
unsafe {
buf[i] = rand.ascii_chars[intn(rand.ascii_chars.len)]
}
}
return unsafe { buf.vstring_with_len(len) }
}
// uuid_v4 generates a random (v4) UUID
// See https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
pub fn uuid_v4() string {