KT-4347 add Array extension indexOf to stdlib
This commit is contained in:
committed by
Andrey Breslav
parent
0a552dbb04
commit
6781231411
@@ -25,5 +25,48 @@ fun arrays(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("indexOf(item: T)") {
|
||||
absentFor(PrimitiveArrays)
|
||||
isInline = false
|
||||
doc = "Returns first index of item, or -1 if the array does not contain item"
|
||||
returns("Int")
|
||||
body {
|
||||
"""
|
||||
if (item == null) {
|
||||
for (i in indices) {
|
||||
if (this[i] == null) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (i in indices) {
|
||||
if (item == this[i]) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
// implementation for PrimitiveArrays is separate from Arrays, because they cannot hold null elements
|
||||
templates add f("indexOf(item: T)") {
|
||||
absentFor(Arrays)
|
||||
isInline = false
|
||||
doc = "Returns first index of item, or -1 if the array does not contain item"
|
||||
returns("Int")
|
||||
body {
|
||||
"""
|
||||
for (i in indices) {
|
||||
if (item == this[i]) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
return templates.sort()
|
||||
}
|
||||
Reference in New Issue
Block a user