mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
string: last_index ?int
This commit is contained in:
parent
8e1c27d129
commit
b8f728590b
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
12
vlib/os/os.v
12
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 /
|
||||
|
Loading…
Reference in New Issue
Block a user