From ca798d8d71e5ce7d5a8341c2b2f682b555d0e90e Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 10 Aug 2015 15:54:09 +0300 Subject: [PATCH] Rename reverse to reversed. #KT-8171 --- libraries/stdlib/src/generated/_Elements.kt | 20 +-- libraries/stdlib/src/generated/_Ordering.kt | 158 ++++++++++++++---- .../stdlib/test/collections/ArraysTest.kt | 16 +- .../stdlib/test/collections/CollectionTest.kt | 6 +- .../test/collections/ReversedViewsTest.kt | 2 +- libraries/stdlib/test/text/StringTest.kt | 14 +- .../src/templates/Elements.kt | 6 +- .../src/templates/Ordering.kt | 17 +- 8 files changed, 170 insertions(+), 69 deletions(-) diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt index 0543cecf361..7e591419c02 100644 --- a/libraries/stdlib/src/generated/_Elements.kt +++ b/libraries/stdlib/src/generated/_Elements.kt @@ -2372,13 +2372,13 @@ public inline fun String.last(predicate: (Char) -> Boolean): Char { */ public fun Array.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 Array.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 } diff --git a/libraries/stdlib/src/generated/_Ordering.kt b/libraries/stdlib/src/generated/_Ordering.kt index 71445697032..57187317b1c 100644 --- a/libraries/stdlib/src/generated/_Ordering.kt +++ b/libraries/stdlib/src/generated/_Ordering.kt @@ -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 Array.reverse(): List { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun Iterable.reverse(): 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 String.reverse(): String { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +public fun Array.reversed(): List { if (isEmpty()) return emptyList() val list = toArrayList() Collections.reverse(list) @@ -103,8 +111,88 @@ public fun ShortArray.reverse(): List { /** * Returns a list with elements in reversed order. */ -public fun Iterable.reverse(): List { - if (this is Collection<*> && isEmpty()) return emptyList() +public fun BooleanArray.reversed(): List { + 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 { + 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 { + 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 { + 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 { + 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 { + 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 { + 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 { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun Iterable.reversed(): List { + if (this is Collection && isEmpty()) return emptyList() val list = toArrayList() Collections.reverse(list) return list @@ -113,7 +201,7 @@ public fun Iterable.reverse(): List { /** * Returns a string with characters in reversed order. */ -public fun String.reverse(): String { +public fun String.reversed(): String { return StringBuilder().append(this).reverse().toString() } diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index bcb478f92f7..277926a6532 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -529,14 +529,14 @@ class ArraysTest { } test fun reverse() { - expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reverse() } - expect(listOf(3, 2, 1)) { byteArrayOf(1, 2, 3).reverse() } - expect(listOf(3, 2, 1)) { shortArrayOf(1, 2, 3).reverse() } - expect(listOf(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(3, 2, 1)) { byteArrayOf(1, 2, 3).reversed() } + expect(listOf(3, 2, 1)) { shortArrayOf(1, 2, 3).reversed() } + expect(listOf(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() { diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index c6bf8ce019c..c968a6f15c7 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -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 = 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 = listOf(2, 3, 1) - expect(listOf(1, 3, 2)) { iterable.reverse() } + expect(listOf(1, 3, 2)) { iterable.reversed() } expect(listOf(2, 3, 1)) { iterable } } diff --git a/libraries/stdlib/test/collections/ReversedViewsTest.kt b/libraries/stdlib/test/collections/ReversedViewsTest.kt index 40a67d90fd3..32da294d589 100644 --- a/libraries/stdlib/test/collections/ReversedViewsTest.kt +++ b/libraries/stdlib/test/collections/ReversedViewsTest.kt @@ -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() } diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 9a32cb17e15..e37b8339489 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -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") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 1c7fdb630f4..527195bf7ee 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -89,13 +89,13 @@ fun elements(): List { 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 { } body(ArraysOfPrimitives) { """ - for (index in indices.reverse()) { + for (index in indices.reversed()) { if (element == this[index]) { return index } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index 35cefb9b6ef..16cb715517e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -6,11 +6,24 @@ fun ordering(): List { val templates = arrayListOf() 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" } + 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" } 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 { } 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 """