rand: string()

pull/5842/head
Alexander Medvednikov 2020-07-15 21:36:06 +02:00
parent 4b0ded0475
commit c563168d69
4 changed files with 22 additions and 4 deletions

View File

@ -128,3 +128,15 @@ pub fn f32_in_range(min, max f32) f32 {
pub fn f64_in_range(min, max f64) f64 { pub fn f64_in_range(min, max f64) f64 {
return default_rng.f64_in_range(min, max) return default_rng.f64_in_range(min, max)
} }
const (
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
)
pub fn string(len int) string {
mut buf := malloc(len)
for i in 0..len {
buf[i] = chars[intn(chars.len)]
}
return string(buf, len)
}

View File

@ -4,7 +4,6 @@ module strings
// random returns a random string with `n` characters // random returns a random string with `n` characters
/* /*
// TODO
pub fn random(n int) string { pub fn random(n int) string {
buf := vmalloc(n) buf := vmalloc(n)
for i in 0..n { for i in 0..n {

View File

@ -167,7 +167,7 @@ Did you forget to add vlib to the path? (Use @vlib for default vlib)')
} }
pub fn (v &Builder) get_user_files() []string { pub fn (v &Builder) get_user_files() []string {
if v.pref.path in ['vlib/builtin', 'vlib/strconv'] { if v.pref.path in ['vlib/builtin', 'vlib/strconv', 'vlib/strings', 'vlib/hash'] {
// get_builtin_files() has already added the builtin files: // get_builtin_files() has already added the builtin files:
v.log('Skipping user files.') v.log('Skipping user files.')
return [] return []

View File

@ -90,9 +90,16 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) {
} }
} else { } else {
if !(it.is_pub || g.pref.is_debug) { if !(it.is_pub || g.pref.is_debug) {
// Private functions need to marked as static so that they are not exportable in the
// binaries
if g.pref.build_mode != .build_module {
// if !(g.pref.build_mode == .build_module && g.is_builtin_mod) {
// If we are building vlib/builtin, we need all private functions like array_get
// to be public, so that all V programs can access them.
g.write('static ') g.write('static ')
g.definitions.write('static ') g.definitions.write('static ')
} }
}
fn_header := if msvc_attrs.len > 0 { '$type_name $msvc_attrs ${name}(' } else { '$type_name ${name}(' } fn_header := if msvc_attrs.len > 0 { '$type_name $msvc_attrs ${name}(' } else { '$type_name ${name}(' }
g.definitions.write(fn_header) g.definitions.write(fn_header)
g.write(fn_header) g.write(fn_header)