From ed8c71442b1a5aa86d1f96f366ecccd9abd9b7d3 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 5 Mar 2024 10:23:43 +0100 Subject: [PATCH] [stdlib] Use more idiomatic index range check in getOrElse/Null --- .../stdlib/common/src/generated/_Arrays.kt | 54 +++++++++---------- .../common/src/generated/_Collections.kt | 6 +-- .../stdlib/common/src/generated/_Strings.kt | 6 +-- .../stdlib/common/src/generated/_UArrays.kt | 24 ++++----- .../src/templates/Elements.kt | 9 ++-- 5 files changed, 51 insertions(+), 48 deletions(-) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 73883d72d3e..bbe67463692 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -653,7 +653,7 @@ public inline fun Array.elementAtOrElse(index: Int, defaultValue: (In contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -666,7 +666,7 @@ public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> B contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -679,7 +679,7 @@ public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -692,7 +692,7 @@ public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> In contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -705,7 +705,7 @@ public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> L contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -718,7 +718,7 @@ public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -731,7 +731,7 @@ public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -744,7 +744,7 @@ public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) - contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -757,7 +757,7 @@ public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> C contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1383,7 +1383,7 @@ public inline fun Array.getOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1394,7 +1394,7 @@ public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1405,7 +1405,7 @@ public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short) contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1416,7 +1416,7 @@ public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): In contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1427,7 +1427,7 @@ public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1438,7 +1438,7 @@ public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float) contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1449,7 +1449,7 @@ public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Doubl contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1460,7 +1460,7 @@ public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Bool contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1471,7 +1471,7 @@ public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -1480,7 +1480,7 @@ public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): * @sample samples.collections.Collections.Elements.getOrNull */ public fun Array.getOrNull(index: Int): T? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1489,7 +1489,7 @@ public fun Array.getOrNull(index: Int): T? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun ByteArray.getOrNull(index: Int): Byte? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1498,7 +1498,7 @@ public fun ByteArray.getOrNull(index: Int): Byte? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun ShortArray.getOrNull(index: Int): Short? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1507,7 +1507,7 @@ public fun ShortArray.getOrNull(index: Int): Short? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun IntArray.getOrNull(index: Int): Int? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1516,7 +1516,7 @@ public fun IntArray.getOrNull(index: Int): Int? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun LongArray.getOrNull(index: Int): Long? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1525,7 +1525,7 @@ public fun LongArray.getOrNull(index: Int): Long? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun FloatArray.getOrNull(index: Int): Float? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1534,7 +1534,7 @@ public fun FloatArray.getOrNull(index: Int): Float? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun DoubleArray.getOrNull(index: Int): Double? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1543,7 +1543,7 @@ public fun DoubleArray.getOrNull(index: Int): Double? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun BooleanArray.getOrNull(index: Int): Boolean? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -1552,7 +1552,7 @@ public fun BooleanArray.getOrNull(index: Int): Boolean? { * @sample samples.collections.Collections.Elements.getOrNull */ public fun CharArray.getOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 2d06d3401ac..f2152bc1715 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -131,7 +131,7 @@ public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in 0.. List.getOrElse(index: Int, defaultValue: (Int) -> T): T contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in 0.. List.getOrElse(index: Int, defaultValue: (Int) -> T): T * @sample samples.collections.Collections.Elements.getOrNull */ public fun List.getOrNull(index: Int): T? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in 0..= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -139,7 +139,7 @@ public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -148,7 +148,7 @@ public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char * @sample samples.collections.Collections.Elements.getOrNull */ public fun CharSequence.getOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 6ceb03956a5..7e348568964 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -327,7 +327,7 @@ public inline fun UIntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> U contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -342,7 +342,7 @@ public inline fun ULongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -357,7 +357,7 @@ public inline fun UByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -372,7 +372,7 @@ public inline fun UShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -705,7 +705,7 @@ public inline fun UIntArray.getOrElse(index: Int, defaultValue: (Int) -> UInt): contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -718,7 +718,7 @@ public inline fun ULongArray.getOrElse(index: Int, defaultValue: (Int) -> ULong) contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -731,7 +731,7 @@ public inline fun UByteArray.getOrElse(index: Int, defaultValue: (Int) -> UByte) contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -744,7 +744,7 @@ public inline fun UShortArray.getOrElse(index: Int, defaultValue: (Int) -> UShor contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in indices) get(index) else defaultValue(index) } /** @@ -755,7 +755,7 @@ public inline fun UShortArray.getOrElse(index: Int, defaultValue: (Int) -> UShor @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UIntArray.getOrNull(index: Int): UInt? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -766,7 +766,7 @@ public fun UIntArray.getOrNull(index: Int): UInt? { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun ULongArray.getOrNull(index: Int): ULong? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -777,7 +777,7 @@ public fun ULongArray.getOrNull(index: Int): ULong? { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UByteArray.getOrNull(index: Int): UByte? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** @@ -788,7 +788,7 @@ public fun UByteArray.getOrNull(index: Int): UByte? { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UShortArray.getOrNull(index: Int): UShort? { - return if (index >= 0 && index <= lastIndex) get(index) else null + return if (index in indices) get(index) else null } /** diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 917be8dacd8..19834b61315 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -378,12 +378,13 @@ object Elements : TemplateGroupBase() { } specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() + val indices = if (family == Lists) "0..= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in $indices) get(index) else defaultValue(index) """ } } @@ -395,12 +396,13 @@ object Elements : TemplateGroupBase() { doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." } returns("T") inlineOnly() + val indices = if (family == Lists) "0..= 0 && index <= lastIndex) get(index) else defaultValue(index) + return if (index in $indices) get(index) else defaultValue(index) """ } } @@ -454,9 +456,10 @@ object Elements : TemplateGroupBase() { doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or `null` if the [index] is out of bounds of this ${f.collection}." } sample("samples.collections.Collections.Elements.getOrNull") returns("T?") + val indices = if (family == Lists) "0..= 0 && index <= lastIndex) get(index) else null + return if (index in $indices) get(index) else null """ } }