1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: add rand.ascii (#8675)

This commit is contained in:
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

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 {