string: last_index ?int

pull/3067/head
Alexander Medvednikov 2019-12-12 21:44:52 +03:00
parent 8e1c27d129
commit b8f728590b
5 changed files with 28 additions and 29 deletions

View File

@ -533,7 +533,7 @@ pub fn (s string) index(p string) ?int {
} }
// KMP search // KMP search
pub fn (s string) index_kmp(p string) int { fn (s string) index_kmp(p string) int {
if p.len > s.len { if p.len > s.len {
return -1 return -1
} }
@ -572,9 +572,9 @@ pub fn (s string) index_any(chars string) int {
return -1 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 { if p.len > s.len {
return -1 return none
} }
mut i := s.len - p.len mut i := s.len - p.len
for i >= 0 { for i >= 0 {
@ -587,7 +587,7 @@ pub fn (s string) last_index(p string) int {
} }
i-- i--
} }
return -1 return none
} }
pub fn (s string) index_after(p string, start int) int { 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 { if p.len > s.len {
return false return false
} }
res := s.last_index(p) == s.len - p.len idx := s.last_index(p) or {
return res return false
}
return idx == s.len - p.len
} }
// TODO only works with ASCII // 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 { pub fn (s string) all_before_last(dot string) string {
pos := s.last_index(dot) pos := s.last_index(dot) or {
if pos == -1 {
return s return s
} }
return s.left(pos) return s.left(pos)
} }
pub fn (s string) all_after(dot string) string { pub fn (s string) all_after(dot string) string {
pos := s.last_index(dot) pos := s.last_index(dot) or {
if pos == -1 {
return s return s
} }
return s.right(pos + dot.len) return s.right(pos + dot.len)

View File

@ -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 // v volt/slack_test.v: compile all .v files to get the environment
// I need to implement user packages! TODO // I need to implement user packages! TODO
is_test_with_imports := dir.ends_with('_test.v') && 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 { if is_test_with_imports {
user_files << dir user_files << dir
pos := dir.last_index(os.path_separator) if pos := dir.last_index(os.path_separator) {
dir = dir[..pos] + os.path_separator// TODO why is this needed dir = dir[..pos] + os.path_separator// TODO why is this needed
}
} }
if dir.ends_with('.v') || dir.ends_with('.vsh') { if dir.ends_with('.v') || dir.ends_with('.vsh') {
// Just compile one file and get parent dir // Just compile one file and get parent dir

View File

@ -2137,7 +2137,10 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
// } // }
if is_indexer { if is_indexer {
l := p.cgen.cur_line.trim_space() 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.cgen.resetln(l[..fn_ph])
p.table.varg_access << VargAccess{ p.table.varg_access << VargAccess{
fn_name: p.cur_fn.name, fn_name: p.cur_fn.name,

View File

@ -551,7 +551,7 @@ struct ParseAuthorityRes {
} }
fn parse_authority(authority string) ?ParseAuthorityRes { fn parse_authority(authority string) ?ParseAuthorityRes {
i := authority.last_index('@') i := authority.last_index('@') or { -1 }
mut host := '' mut host := ''
mut zuser := user('') mut zuser := user('')
if i < 0 { if i < 0 {
@ -602,8 +602,7 @@ fn parse_host(host string) ?string {
if host.starts_with('[') { if host.starts_with('[') {
// parse an IP-Literal in RFC 3986 and RFC 6874. // parse an IP-Literal in RFC 3986 and RFC 6874.
// E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'. // E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'.
mut i := host.last_index(']') mut i := host.last_index(']') or {
if i < 0 {
return error(error_msg('parse_host: missing \']\' in host', '')) return error(error_msg('parse_host: missing \']\' in host', ''))
} }
mut colon_port := host[i+1..] mut colon_port := host[i+1..]
@ -629,9 +628,8 @@ fn parse_host(host string) ?string {
} }
return host1 + host2 + host3 return host1 + host2 + host3
} }
i = host.last_index(':') if idx := host.last_index(':') {
if i != -1 { colon_port = host[idx..]
colon_port = host[i..]
if !valid_optional_port(colon_port) { if !valid_optional_port(colon_port) {
return error(error_msg('parse_host: invalid port $colon_port after host ', '')) 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 == '' { if ref == '' {
full = base full = base
} else if ref[0] != `/` { } else if ref[0] != `/` {
i := base.last_index('/') i := base.last_index('/') or { -1 }
full = base[..i+1] + ref full = base[..i+1] + ref
} else { } else {
full = ref full = ref

View File

@ -569,8 +569,7 @@ fn print_c_errno() {
pub fn ext(path string) string { pub fn ext(path string) string {
pos := path.last_index('.') pos := path.last_index('.') or {
if pos == -1 {
return '' return ''
} }
return path[pos..] return path[pos..]
@ -582,16 +581,14 @@ pub fn dir(path string) string {
if path == '.' { if path == '.' {
return getwd() return getwd()
} }
pos := path.last_index(path_separator) pos := path.last_index(path_separator) or {
if pos == -1 {
return '.' return '.'
} }
return path[..pos] return path[..pos]
} }
fn path_sans_ext(path string) string { fn path_sans_ext(path string) string {
pos := path.last_index('.') pos := path.last_index('.') or {
if pos == -1 {
return path return path
} }
return path[..pos] return path[..pos]
@ -599,8 +596,7 @@ fn path_sans_ext(path string) string {
pub fn basedir(path string) string { pub fn basedir(path string) string {
pos := path.last_index(path_separator) pos := path.last_index(path_separator) or {
if pos == -1 {
return path return path
} }
return path[..pos ] // NB: *without* terminating / return path[..pos ] // NB: *without* terminating /