Rename reverse to reversed.

#KT-8171
This commit is contained in:
Ilya Gorbunov
2015-08-10 15:54:09 +03:00
parent 56e12787a8
commit ca798d8d71
8 changed files with 170 additions and 69 deletions
+10 -10
View File
@@ -2372,13 +2372,13 @@ public inline fun String.last(predicate: (Char) -> Boolean): Char {
*/
public fun <T> Array<out T>.lastIndexOf(element: T): Int {
if (element == null) {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (this[index] == null) {
return index
}
}
} else {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2391,7 +2391,7 @@ public fun <T> Array<out T>.lastIndexOf(element: T): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun BooleanArray.lastIndexOf(element: Boolean): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2403,7 +2403,7 @@ public fun BooleanArray.lastIndexOf(element: Boolean): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun ByteArray.lastIndexOf(element: Byte): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2415,7 +2415,7 @@ public fun ByteArray.lastIndexOf(element: Byte): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun CharArray.lastIndexOf(element: Char): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2427,7 +2427,7 @@ public fun CharArray.lastIndexOf(element: Char): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun DoubleArray.lastIndexOf(element: Double): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2439,7 +2439,7 @@ public fun DoubleArray.lastIndexOf(element: Double): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun FloatArray.lastIndexOf(element: Float): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2451,7 +2451,7 @@ public fun FloatArray.lastIndexOf(element: Float): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun IntArray.lastIndexOf(element: Int): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2463,7 +2463,7 @@ public fun IntArray.lastIndexOf(element: Int): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun LongArray.lastIndexOf(element: Long): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -2475,7 +2475,7 @@ public fun LongArray.lastIndexOf(element: Long): Int {
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun ShortArray.lastIndexOf(element: Short): Int {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
+123 -35
View File
@@ -13,87 +13,95 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun <T> Array<out T>.reverse(): List<T> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun BooleanArray.reverse(): List<Boolean> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun ByteArray.reverse(): List<Byte> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun CharArray.reverse(): List<Char> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun DoubleArray.reverse(): List<Double> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun FloatArray.reverse(): List<Float> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun IntArray.reverse(): List<Int> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun LongArray.reverse(): List<Long> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun ShortArray.reverse(): List<Short> {
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun <T> Iterable<T>.reverse(): List<T> {
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()"))
public fun String.reverse(): String {
return reversed()
}
/**
* Returns a list with elements in reversed order.
*/
public fun <T> Array<out T>.reversed(): List<T> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
@@ -103,8 +111,88 @@ public fun ShortArray.reverse(): List<Short> {
/**
* Returns a list with elements in reversed order.
*/
public fun <T> Iterable<T>.reverse(): List<T> {
if (this is Collection<*> && isEmpty()) return emptyList()
public fun BooleanArray.reversed(): List<Boolean> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun ByteArray.reversed(): List<Byte> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun CharArray.reversed(): List<Char> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun DoubleArray.reversed(): List<Double> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun FloatArray.reversed(): List<Float> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun IntArray.reversed(): List<Int> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun LongArray.reversed(): List<Long> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun ShortArray.reversed(): List<Short> {
if (isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
}
/**
* Returns a list with elements in reversed order.
*/
public fun <T> Iterable<T>.reversed(): List<T> {
if (this is Collection && isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
@@ -113,7 +201,7 @@ public fun <T> Iterable<T>.reverse(): List<T> {
/**
* Returns a string with characters in reversed order.
*/
public fun String.reverse(): String {
public fun String.reversed(): String {
return StringBuilder().append(this).reverse().toString()
}
@@ -529,14 +529,14 @@ class ArraysTest {
}
test fun reverse() {
expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reverse() }
expect(listOf<Byte>(3, 2, 1)) { byteArrayOf(1, 2, 3).reverse() }
expect(listOf<Short>(3, 2, 1)) { shortArrayOf(1, 2, 3).reverse() }
expect(listOf<Long>(3, 2, 1)) { longArrayOf(1, 2, 3).reverse() }
expect(listOf(3F, 2F, 1F)) { floatArrayOf(1F, 2F, 3F).reverse() }
expect(listOf(3.0, 2.0, 1.0)) { doubleArrayOf(1.0, 2.0, 3.0).reverse() }
expect(listOf('3', '2', '1')) { charArrayOf('1', '2', '3').reverse() }
expect(listOf(false, false, true)) { booleanArrayOf(true, false, false).reverse() }
expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reversed() }
expect(listOf<Byte>(3, 2, 1)) { byteArrayOf(1, 2, 3).reversed() }
expect(listOf<Short>(3, 2, 1)) { shortArrayOf(1, 2, 3).reversed() }
expect(listOf<Long>(3, 2, 1)) { longArrayOf(1, 2, 3).reversed() }
expect(listOf(3F, 2F, 1F)) { floatArrayOf(1F, 2F, 3F).reversed() }
expect(listOf(3.0, 2.0, 1.0)) { doubleArrayOf(1.0, 2.0, 3.0).reversed() }
expect(listOf('3', '2', '1')) { charArrayOf('1', '2', '3').reversed() }
expect(listOf(false, false, true)) { booleanArrayOf(true, false, false).reversed() }
}
test fun drop() {
@@ -310,19 +310,19 @@ class CollectionTest {
test fun reverse() {
val data = listOf("foo", "bar")
val rev = data.reverse()
val rev = data.reversed()
assertEquals(listOf("bar", "foo"), rev)
}
test fun reverseFunctionShouldReturnReversedCopyForList() {
val list: List<Int> = listOf(2, 3, 1)
expect(listOf(1, 3, 2)) { list.reverse() }
expect(listOf(1, 3, 2)) { list.reversed() }
expect(listOf(2, 3, 1)) { list }
}
test fun reverseFunctionShouldReturnReversedCopyForIterable() {
val iterable: Iterable<Int> = listOf(2, 3, 1)
expect(listOf(1, 3, 2)) { iterable.reverse() }
expect(listOf(1, 3, 2)) { iterable.reversed() }
expect(listOf(2, 3, 1)) { iterable }
}
@@ -32,7 +32,7 @@ class ReversedViewsTest {
test fun testBehavior() {
val original = listOf(2L, 3L, Long.MAX_VALUE)
val reversed = original.reverse()
val reversed = original.reversed()
compare(reversed, original.asReversed()) {
listBehavior()
}
+7 -7
View File
@@ -131,9 +131,9 @@ class StringTest {
}
test fun reverse() {
assertEquals("dcba", "abcd".reverse())
assertEquals("4321", "1234".reverse())
assertEquals("", "".reverse())
assertEquals("dcba", "abcd".reversed())
assertEquals("4321", "1234".reversed())
assertEquals("", "".reversed())
}
test fun indices() {
@@ -409,12 +409,12 @@ class StringTest {
val substrings = listOf("rac", "ra")
assertEquals(2, string.indexOfAny(substrings))
assertEquals(9, string.indexOfAny(substrings, startIndex = 3))
assertEquals(2, string.indexOfAny(substrings.reverse()))
assertEquals(2, string.indexOfAny(substrings.reversed()))
assertEquals(-1, string.indexOfAny(substrings, 10))
assertEquals(9, string.lastIndexOfAny(substrings))
assertEquals(2, string.lastIndexOfAny(substrings, startIndex = 8))
assertEquals(2, string.lastIndexOfAny(substrings.reverse(), startIndex = 8))
assertEquals(2, string.lastIndexOfAny(substrings.reversed(), startIndex = 8))
assertEquals(-1, string.lastIndexOfAny(substrings, 1))
assertEquals(0, string.indexOfAny(listOf("dab", "")), "empty strings are not ignored")
@@ -439,12 +439,12 @@ class StringTest {
val substrings = listOf("rac", "ra")
assertEquals(2 to "rac", string.findAnyOf(substrings))
assertEquals(9 to "ra", string.findAnyOf(substrings, startIndex = 3))
assertEquals(2 to "ra", string.findAnyOf(substrings.reverse()))
assertEquals(2 to "ra", string.findAnyOf(substrings.reversed()))
assertEquals(null, string.findAnyOf(substrings, 10))
assertEquals(9 to "ra", string.findLastAnyOf(substrings))
assertEquals(2 to "rac", string.findLastAnyOf(substrings, startIndex = 8))
assertEquals(2 to "ra", string.findLastAnyOf(substrings.reverse(), startIndex = 8))
assertEquals(2 to "ra", string.findLastAnyOf(substrings.reversed(), startIndex = 8))
assertEquals(null, string.findLastAnyOf(substrings, 1))
assertEquals(0 to "", string.findAnyOf(listOf("dab", "")), "empty strings are not ignored")
@@ -89,13 +89,13 @@ fun elements(): List<GenericFunction> {
body(ArraysOfObjects) {
"""
if (element == null) {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (this[index] == null) {
return index
}
}
} else {
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -106,7 +106,7 @@ fun elements(): List<GenericFunction> {
}
body(ArraysOfPrimitives) {
"""
for (index in indices.reverse()) {
for (index in indices.reversed()) {
if (element == this[index]) {
return index
}
@@ -6,11 +6,24 @@ fun ordering(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("reverse()") {
deprecate("reverse will change its behavior soon. Use reversed() instead.")
deprecateReplacement("reversed()")
doc { "Returns a list with elements in reversed order." }
returns { "List<T>" }
body { """return reversed()""" }
include(Strings)
returns(Strings) { "SELF" }
exclude(Sequences)
}
templates add f("reversed()") {
doc { "Returns a list with elements in reversed order." }
returns { "List<T>" }
body {
"""
if (this is Collection<*> && isEmpty()) return emptyList()
if (this is Collection && isEmpty()) return emptyList()
val list = toArrayList()
Collections.reverse(list)
return list
@@ -27,7 +40,7 @@ fun ordering(): List<GenericFunction> {
}
doc(Strings) { "Returns a string with characters in reversed order." }
returns(Strings) { "String" }
returns(Strings) { "SELF" }
body(Strings) {
// TODO: Replace with StringBuilder(this) when JS can handle it
"""