mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
rand: add PRNG.fill_buffer_from_set/2 (#21037)
This commit is contained in:
parent
4221522c0a
commit
a1c6377ab6
5 changed files with 143 additions and 31 deletions
|
@ -93,6 +93,7 @@ fn internal_ulid_at_millisecond(mut rng PRNG, unix_time_milli u64) string {
|
|||
}
|
||||
}
|
||||
|
||||
@[direct_array_access]
|
||||
fn internal_string_from_set(mut rng PRNG, charset string, len int) string {
|
||||
if len == 0 {
|
||||
return ''
|
||||
|
@ -100,7 +101,7 @@ fn internal_string_from_set(mut rng PRNG, charset string, len int) string {
|
|||
mut buf := unsafe { malloc_noscan(len + 1) }
|
||||
for i in 0 .. len {
|
||||
unsafe {
|
||||
buf[i] = charset[intn(charset.len) or { 0 }]
|
||||
buf[i] = charset[rng.u32() % charset.len]
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
|
@ -109,6 +110,19 @@ fn internal_string_from_set(mut rng PRNG, charset string, len int) string {
|
|||
return unsafe { buf.vstring_with_len(len) }
|
||||
}
|
||||
|
||||
@[direct_array_access]
|
||||
fn internal_fill_buffer_from_set(mut rng PRNG, charset string, mut buf []u8) {
|
||||
if buf.len == 0 {
|
||||
return
|
||||
}
|
||||
blen := buf.len
|
||||
for i in 0 .. blen {
|
||||
unsafe {
|
||||
buf[i] = charset[rng.u32() % charset.len]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deinit() {
|
||||
unsafe {
|
||||
default_rng.free() // free the implementation
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue