diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 2d9f6d7aa0..17886c8411 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -533,7 +533,7 @@ pub fn (s string) index(p string) ?int { } // KMP search -pub fn (s string) index_kmp(p string) int { +fn (s string) index_kmp(p string) int { if p.len > s.len { return -1 } @@ -572,9 +572,9 @@ pub fn (s string) index_any(chars string) int { return -1 } -pub fn (s string) last_index(p string) int { +pub fn (s string) last_index(p string) ?int { if p.len > s.len { - return -1 + return none } mut i := s.len - p.len for i >= 0 { @@ -587,7 +587,7 @@ pub fn (s string) last_index(p string) int { } i-- } - return -1 + return none } pub fn (s string) index_after(p string, start int) int { @@ -672,8 +672,10 @@ pub fn (s string) ends_with(p string) bool { if p.len > s.len { return false } - res := s.last_index(p) == s.len - p.len - return res + idx := s.last_index(p) or { + return false + } + return idx == s.len - p.len } // TODO only works with ASCII @@ -1069,16 +1071,14 @@ pub fn (s string) all_before(dot string) string { } pub fn (s string) all_before_last(dot string) string { - pos := s.last_index(dot) - if pos == -1 { + pos := s.last_index(dot) or { return s } return s.left(pos) } pub fn (s string) all_after(dot string) string { - pos := s.last_index(dot) - if pos == -1 { + pos := s.last_index(dot) or { return s } return s.right(pos + dot.len) diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index f7c297ef04..3c4be47b02 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -759,11 +759,13 @@ pub fn (v &V) get_user_files() []string { // v volt/slack_test.v: compile all .v files to get the environment // I need to implement user packages! TODO is_test_with_imports := dir.ends_with('_test.v') && - (dir.contains('${os.path_separator}volt') || dir.contains('${os.path_separator}c2volt'))// TODO + (dir.contains('${os.path_separator}volt') || + dir.contains('${os.path_separator}c2volt'))// TODO if is_test_with_imports { user_files << dir - pos := dir.last_index(os.path_separator) - dir = dir[..pos] + os.path_separator// TODO why is this needed + if pos := dir.last_index(os.path_separator) { + dir = dir[..pos] + os.path_separator// TODO why is this needed + } } if dir.ends_with('.v') || dir.ends_with('.vsh') { // Just compile one file and get parent dir diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index df291faaa6..8f3fa00a2d 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -2137,7 +2137,10 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { // } if is_indexer { l := p.cgen.cur_line.trim_space() - index_val := l[l.last_index(' ')..].trim_space() + idx := l.last_index(' ') or { + panic('idx') + } + index_val := l[idx..].trim_space() p.cgen.resetln(l[..fn_ph]) p.table.varg_access << VargAccess{ fn_name: p.cur_fn.name, diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index 3745c7ab50..5f6c262435 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -551,7 +551,7 @@ struct ParseAuthorityRes { } fn parse_authority(authority string) ?ParseAuthorityRes { - i := authority.last_index('@') + i := authority.last_index('@') or { -1 } mut host := '' mut zuser := user('') if i < 0 { @@ -602,8 +602,7 @@ fn parse_host(host string) ?string { if host.starts_with('[') { // parse an IP-Literal in RFC 3986 and RFC 6874. // E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'. - mut i := host.last_index(']') - if i < 0 { + mut i := host.last_index(']') or { return error(error_msg('parse_host: missing \']\' in host', '')) } mut colon_port := host[i+1..] @@ -629,9 +628,8 @@ fn parse_host(host string) ?string { } return host1 + host2 + host3 } - i = host.last_index(':') - if i != -1 { - colon_port = host[i..] + if idx := host.last_index(':') { + colon_port = host[idx..] if !valid_optional_port(colon_port) { return error(error_msg('parse_host: invalid port $colon_port after host ', '')) } @@ -910,7 +908,7 @@ fn resolve_path(base, ref string) string { if ref == '' { full = base } else if ref[0] != `/` { - i := base.last_index('/') + i := base.last_index('/') or { -1 } full = base[..i+1] + ref } else { full = ref diff --git a/vlib/os/os.v b/vlib/os/os.v index 2144af8d68..1bff1bf629 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -569,8 +569,7 @@ fn print_c_errno() { pub fn ext(path string) string { - pos := path.last_index('.') - if pos == -1 { + pos := path.last_index('.') or { return '' } return path[pos..] @@ -582,16 +581,14 @@ pub fn dir(path string) string { if path == '.' { return getwd() } - pos := path.last_index(path_separator) - if pos == -1 { + pos := path.last_index(path_separator) or { return '.' } return path[..pos] } fn path_sans_ext(path string) string { - pos := path.last_index('.') - if pos == -1 { + pos := path.last_index('.') or { return path } return path[..pos] @@ -599,8 +596,7 @@ fn path_sans_ext(path string) string { pub fn basedir(path string) string { - pos := path.last_index(path_separator) - if pos == -1 { + pos := path.last_index(path_separator) or { return path } return path[..pos ] // NB: *without* terminating /