rand: add comments for rand.uuid_v4, apply some more perf tweaks

pull/5983/head
Delyan Angelov 2020-07-25 21:39:54 +03:00
parent ee3ebed23c
commit 6fcc6cdc75
1 changed files with 20 additions and 11 deletions

View File

@ -146,30 +146,39 @@ pub fn string(len int) string {
// rand.uuid_v4 generate a completely random UUID (v4)
// See https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
pub fn uuid_v4() string {
buflen := 36
mut buf := malloc(37)
mut i_buf := 0
for i_buf < 36 {
mut x := default_rng.u64()
mut x := u64(0)
mut d := byte(0)
for i_buf < buflen {
mut c := 0
for c < 16 && i_buf < 36 {
mut d := byte(x) & 0x0F
x = default_rng.u64()
// do most of the bit manipulation at once:
x &= 0x0F0F0F0F0F0F0F0F
x += 0x3030303030303030
// write the ASCII codes to the buffer:
for c < 8 && i_buf < buflen {
d = byte(x)
unsafe {
buf[i_buf] = if d > 9 { d + 87 } else { d + 48 }
buf[i_buf] = if d > 0x39 { d + 0x27 } else { d }
}
i_buf++
c++
x = x >> 4
x = x >> 8
}
}
n := ((byte(default_rng.u64()) & 0x0F) & 0x03) + 0x08
// there are still some random bits in x:
x = x >> 8
d = byte(x)
unsafe {
buf[19] = if d > 0x39 { d + 0x27 } else { d }
buf[8] = `-`
buf[13] = `-`
buf[14] = `4`
buf[18] = `-`
buf[19] = if n > 9 { n + 87 } else { n + 48 }
buf[23] = `-`
buf[36] = 0
buf[14] = `4`
buf[buflen] = 0
}
return string(buf, 36)
return string(buf, buflen)
}