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

array: add index() method

This commit is contained in:
Don Alfons Nisnoni
2019-10-05 04:07:19 +08:00
committed by Alexander Medvednikov
parent 19c7b95d00
commit 68bcf6830c
2 changed files with 66 additions and 1 deletions

View File

@ -290,3 +290,42 @@ fn compare_ints(a, b &int) int {
pub fn (a mut []int) sort() {
a.sort_with_compare(compare_ints)
}
// Looking for an array index based on value.
// If there is, it will return the index and if not, it will return `-1`
// TODO: Implement for all types
pub fn (a []string) index(v string) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
pub fn (a []int) index(v int) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
pub fn (a []byte) index(v byte) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
pub fn (a []char) index(v char) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}