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

string: make index() return ?int instead of int/-1

This commit is contained in:
Alexander Medvednikov
2019-11-30 13:09:05 +03:00
parent cc2bd0bb68
commit 81d4f66fbb
11 changed files with 117 additions and 103 deletions

View File

@ -84,7 +84,7 @@ fn (table mut Table) parse_cflag(cflag string, mod string) ?bool {
mut fos := ''
mut name := ''
if flag.starts_with('linux') || flag.starts_with('darwin') || flag.starts_with('freebsd') || flag.starts_with('windows') {
pos := flag.index(' ')
pos := flag.index(' ') or { return none }
fos = flag[..pos].trim_space()
flag = flag[pos..].trim_space()
}
@ -101,16 +101,21 @@ fn (table mut Table) parse_cflag(cflag string, mod string) ?bool {
}
}
}
for i in [flag.index(' '), flag.index(',')] {
if index == -1 || (i != -1 && i < index) {
if i := flag.index(' ') {
if index == -1 || i < index {
index = i
}
}
if i := flag.index(',') {
if index == -1 || i < index {
index = i
}
}
if index != -1 && flag[index] == ` ` && flag[index+1] == `-` {
for f in allowed_flags {
i := index+f.len
if i < flag.len && f == flag[index..i] {
index = i
j := index+f.len
if j < flag.len && f == flag[index..j] {
index = j
break
}
}
@ -149,7 +154,7 @@ fn (table mut Table) parse_cflag(cflag string, mod string) ?bool {
fn (cflags []CFlag) c_options_before_target_msvc() string { return '' }
fn (cflags []CFlag) c_options_after_target_msvc() string { return '' }
fn (cflags []CFlag) c_options_before_target() string {
fn (cflags []CFlag) c_options_before_target() string {
// -I flags, optimization flags and so on
mut args:=[]string
for flag in cflags {
@ -185,7 +190,7 @@ fn (cflags []CFlag) c_options_without_object_files() string {
fn (cflags []CFlag) c_options_only_object_files() string {
mut args:=[]string
for flag in cflags {
if flag.value.ends_with('.o') || flag.value.ends_with('.obj') {
if flag.value.ends_with('.o') || flag.value.ends_with('.obj') {
args << flag.format()
}
}

View File

@ -39,7 +39,7 @@ fn (p mut Parser) comp_time() {
for {
if p.tok == .key_return {
p.returns = true
}
}
if p.tok == .lcbr {
stack++
} else if p.tok == .rcbr {
@ -114,7 +114,7 @@ fn (p mut Parser) comp_time() {
//p.gen('/* returns $p.returns */')
} else if p.tok == .key_else {
p.error('use `$' + 'else` instead of `else` in comptime if statements')
}
}
}
else if p.tok == .key_for {
p.next()
@ -207,7 +207,7 @@ fn (p mut Parser) chash() {
if !p.pref.building_v && !p.fileis('vlib') {
p.warn('C #includes will soon be removed from the language' +
'\ndefine the C structs and functions in V')
}
}
*/
if p.file_pcguard.len != 0 {
//println('p: $p.file_platform $p.file_pcguard')
@ -220,8 +220,8 @@ fn (p mut Parser) chash() {
}
// TODO remove after ui_mac.m is removed
else if hash.contains('embed') {
pos := hash.index('embed') + 5
file := hash[pos..]
pos := hash.index('embed') or { return }
file := hash[pos+5..]
//if p.pref.build_mode != .default_mode {
p.genln('#include $file')
//}
@ -262,13 +262,13 @@ fn (p mut Parser) comptime_method_call(typ Type) {
mut j := 0
for i, method in typ.methods {
if method.typ != 'void' {
continue
}
receiver := method.args[0]
if !p.expr_var.ptr {
p.error('`$p.expr_var.name` needs to be a reference')
}
}
amp := if receiver.is_mut && !p.expr_var.ptr { '&' } else { '' }
if j > 0 {
p.gen(' else ')
@ -290,7 +290,7 @@ fn (p mut Parser) comptime_method_call(typ Type) {
fn (p mut Parser) gen_array_str(typ Type) {
if typ.has_method('str') {
return
}
}
p.add_method(typ.name, Fn{
name: 'str'
typ: 'string'
@ -381,7 +381,7 @@ fn (p mut Parser) gen_array_filter(str_typ string, method_ph int) {
// V
a := [1,2,3,4]
b := a.filter(it % 2 == 0)
// C
array_int a = ...;
array_int tmp2 = new_array(0, 4, 4);
@ -421,7 +421,7 @@ fn (p mut Parser) gen_array_map(str_typ string, method_ph int) string {
// V
a := [1,2,3,4]
b := a.map(it * 2)
// C
array_int a = ...;
array_int tmp2 = new_array(0, 4, 4);

View File

@ -799,8 +799,7 @@ pub fn get_arg(joined_args, arg, def string) string {
pub fn get_param_after(joined_args, arg, def string) string {
key := '$arg '
mut pos := joined_args.index(key)
if pos == -1 {
mut pos := joined_args.index(key) or {
return def
}
pos += key.len

View File

@ -1068,9 +1068,12 @@ fn (p mut Parser) get_type() string {
//}
return 'void*'
}
/*
TODO this is not needed?
if typ.last_index('__') > typ.index('__') {
p.error('2 __ in gettype(): typ="$typ"')
}
*/
return typ
}
@ -1410,8 +1413,10 @@ fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
{
line := p.cgen.cur_line
vname := line[..pos].replace('=','') // TODO cgen line hack
p.cgen.resetln(line.replace(line[..line.index('=')+1], ''))
p.gen_handle_option_or_else(expr_type, vname, ph)
if idx := line.index('=') {
p.cgen.resetln(line.replace(line[..idx+1], ''))
p.gen_handle_option_or_else(expr_type, vname, ph)
}
}
else if expr_type[0]==`[` {
// assignment to a fixed_array `mut a:=[3]int a=[1,2,3]!!`
@ -3053,12 +3058,10 @@ fn (p mut Parser) defer_st() {
}
fn (p mut Parser) check_and_register_used_imported_type(typ_name string) {
us_idx := typ_name.index('__')
if us_idx != -1 {
arg_mod := typ_name[..us_idx]
if p.import_table.known_alias(arg_mod) {
p.import_table.register_used_import(arg_mod)
}
us_idx := typ_name.index('__') or { return }
arg_mod := typ_name[..us_idx]
if p.import_table.known_alias(arg_mod) {
p.import_table.register_used_import(arg_mod)
}
}

View File

@ -157,8 +157,10 @@ fn (p mut Parser) get_type2() Type {
typ = 'Option_$typ'
p.table.register_type_with_parent(typ, 'Option')
}
/*
if typ.last_index('__') > typ.index('__') {
p.error('2 __ in gettype(): typ="$typ"')
}
*/
return Type{name: typ, cat: cat}
}

View File

@ -931,8 +931,8 @@ fn (p &Parser) identify_typo(name string) string {
// compare just name part, some items are mod prefied
fn typo_compare_name_mod(a, b, b_mod string) f32 {
if a.len - b.len > 2 || b.len - a.len > 2 { return 0 }
auidx := a.index('__')
buidx := b.index('__')
auidx := a.index('__') or { -1 }
buidx := b.index('__') or { -1 }
a_mod := if auidx != -1 { mod_gen_name_rev(a[..auidx]) } else { '' }
a_name := if auidx != -1 { a[auidx+2..] } else { a }
b_name := if buidx != -1 { b[buidx+2..] } else { b }