From 9853323157fcecffda9f0b82044d10c2b002df65 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Sep 2019 15:07:18 +0300 Subject: [PATCH] array: make ".contains()" private, use "in" everywhere --- compiler/fn.v | 5 ++++- compiler/main.v | 43 ++++++++++++++++++++++--------------------- compiler/parser.v | 8 ++++---- compiler/table.v | 6 +++--- vlib/builtin/string.v | 4 ++-- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/compiler/fn.v b/compiler/fn.v index 134d5b554a..41e0f85745 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -423,7 +423,7 @@ fn (p mut Parser) fn_decl() { } if f.name == 'main' || f.name == 'WinMain' { p.genln('init_consts();') - if p.table.imports.contains('os') { + if 'os' in p.table.imports { if f.name == 'main' { p.genln('os__args = os__init_os_args(argc, argv);') } @@ -604,6 +604,9 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type string) { if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod { + if f.name == 'contains' { + println('use `value in numbers` instead of `numbers.contains(value)`') + } p.error('function `$f.name` is private') } p.calling_c = f.is_c diff --git a/compiler/main.v b/compiler/main.v index fbbdceb8b3..47f824362d 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -232,10 +232,11 @@ fn (v mut V) compile() { v.generate_hotcode_reloading_declarations() - imports_json := v.table.imports.contains('json') + imports_json := 'json' in v.table.imports // TODO remove global UI hack - if v.os == .mac && ((v.pref.build_mode == .embed_vlib && v.table.imports.contains('ui')) || - (v.pref.build_mode == .build_module && v.dir.contains('/ui'))) { + if v.os == .mac && ((v.pref.build_mode == .embed_vlib && 'ui' in + v.table.imports) || (v.pref.build_mode == .build_module && + v.dir.contains('/ui'))) { cgen.genln('id defaultFont = 0; // main.v') } // We need the cjson header for all the json decoding user will do in default mode @@ -250,13 +251,13 @@ fn (v mut V) compile() { // TODO //cgen.genln('i64 total_m = 0; // For counting total RAM allocated') cgen.genln('int g_test_ok = 1; ') - if v.table.imports.contains('json') { + if 'json' in v.table.imports { cgen.genln(' #define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key)) ') } } - if os.args.contains('-debug_alloc') { + if '-debug_alloc' in os.args { cgen.genln('#define DEBUG_ALLOC 1') } cgen.genln('/*================================== FNS =================================*/') @@ -648,7 +649,7 @@ fn new_v(args[]string) &V { mut out_name := get_arg(joined_args, 'o', 'a.out') mut dir := args.last() - if args.contains('run') { + if 'run' in args { dir = get_all_after(joined_args, 'run', '') } if dir.ends_with('/') { @@ -685,7 +686,7 @@ fn new_v(args[]string) &V { */ } // TODO embed_vlib is temporarily the default mode. It's much slower. - else if !args.contains('-embed_vlib') { + else if !('-embed_vlib' in args) { build_mode = .embed_vlib } // @@ -784,24 +785,24 @@ fn new_v(args[]string) &V { rdir := os.realpath( dir ) rdir_name := os.filename( rdir ) - obfuscate := args.contains('-obf') - is_repl:=args.contains('-repl') + obfuscate := '-obf' in args + is_repl := '-repl' in args pref := &Preferences { is_test: is_test is_script: is_script - is_so: args.contains('-shared') - is_prod: args.contains('-prod') - is_verbose: args.contains('-verbose') - is_debuggable: args.contains('-g') // -debuggable implys debug - is_debug: args.contains('-debug') || args.contains('-g') + is_so: '-shared' in args + is_prod: '-prod' in args + is_verbose: '-verbose' in args + is_debuggable: '-g' in args + is_debug: '-debug' in args || '-g' in args obfuscate: obfuscate - is_prof: args.contains('-prof') - is_live: args.contains('-live') - sanitize: args.contains('-sanitize') - nofmt: args.contains('-nofmt') - show_c_cmd: args.contains('-show_c_cmd') - translated: args.contains('translated') - is_run: args.contains('run') + is_prof: '-prof' in args + is_live: '-live' in args + sanitize: '-sanitize' in args + nofmt: '-nofmt' in args + show_c_cmd: '-show_c_cmd' in args + translated: 'translated' in args + is_run: 'run' in args is_repl: is_repl build_mode: build_mode cflags: cflags diff --git a/compiler/parser.v b/compiler/parser.v index 20ecd0ac0f..6975d39ccf 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -176,7 +176,7 @@ fn (p mut Parser) parse(pass Pass) { for p.tok == .key_import && p.peek() != .key_const { p.imports() } - if p.table.imports.contains('builtin') { + if 'builtin' in p.table.imports { p.error('module `builtin` cannot be imported') } // save file import table @@ -365,7 +365,7 @@ fn (p mut Parser) import_statement() { // add import to file scope import table p.import_table.register_alias(mod_alias, mod) // Make sure there are no duplicate imports - if p.table.imports.contains(mod) { + if mod in p.table.imports { return } p.log('adding import $mod') @@ -2888,7 +2888,7 @@ fn (p mut Parser) struct_init(typ string, is_c_struct_init bool) string { if !p.first_pass() && !t.has_field(field) { p.error('`$t.name` has no field `$field`') } - if inited_fields.contains(field) { + if field in inited_fields { p.error('already initialized field `$field` in `$t.name`') } f := t.find_field(field) @@ -2914,7 +2914,7 @@ fn (p mut Parser) struct_init(typ string, is_c_struct_init bool) string { for i, field in t.fields { // println('### field.name') // Skip if this field has already been assigned to - if inited_fields.contains(field.name) { + if field.name in inited_fields { continue } field_typ := field.typ diff --git a/compiler/table.v b/compiler/table.v index 62e8a79d5d..da8e79594e 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -245,7 +245,7 @@ fn new_table(obfuscate bool) &Table { // If `name` is a reserved C keyword, returns `v_name` instead. fn (t mut Table) var_cgen_name(name string) string { - if CReserved.contains(name) { + if name in CReserved { return 'v_$name' } else { @@ -254,7 +254,7 @@ fn (t mut Table) var_cgen_name(name string) string { } fn (t mut Table) register_module(mod string) { - if t.modules.contains(mod) { + if mod in t.modules { return } t.modules << mod @@ -710,7 +710,7 @@ fn (table mut Table) cgen_name(f &Fn) string { // Avoid name conflicts (with things like abs(), print() etc). // Generate b_abs(), b_print() // TODO duplicate functionality - if f.mod == 'builtin' && CReserved.contains(f.name) { + if f.mod == 'builtin' && f.name in CReserved { return 'v_$name' } // Obfuscate but skip certain names diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index ad9bae4a41..21feeb3aaf 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -529,7 +529,7 @@ pub fn (s string) find_between(start, end string) string { } // TODO generic -pub fn (ar []string) contains(val string) bool { +fn (ar []string) contains(val string) bool { for s in ar { if s == val { return true @@ -539,7 +539,7 @@ pub fn (ar []string) contains(val string) bool { } // TODO generic -pub fn (ar []int) contains(val int) bool { +fn (ar []int) contains(val int) bool { for i, s in ar { if s == val { return true