KT-4347 add Array extension indexOf to stdlib

This commit is contained in:
Jon Renner
2013-12-26 13:48:58 +08:00
committed by Andrey Breslav
parent 0a552dbb04
commit 6781231411
12 changed files with 208 additions and 0 deletions
@@ -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()
}