From 3a99b3d5a481bdab226758c1024f0f4f200f2c9a Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Wed, 15 Apr 2020 02:25:08 +0300 Subject: [PATCH] Small refactoring to use Family.CodeExtension.size when appropriate --- libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt | 5 ++--- .../tools/kotlin-stdlib-gen/src/templates/Generators.kt | 2 +- libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index e223c47c1a2..486a71d479b 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -292,7 +292,7 @@ object Elements : TemplateGroupBase() { on(Platform.JS) { inline(Inline.No) - val size = if (f == CharSequences) "length" else "size" + val size = f.code.size body { """ return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, $size: $$size}") } @@ -944,11 +944,10 @@ object Elements : TemplateGroupBase() { } specialFor(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences) { body { - val size = if (family == CharSequences) "length" else "size" """ if (isEmpty()) throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.") - return get(random.nextInt($size)) + return get(random.nextInt(${f.code.size})) """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 302760960c2..9826eafe482 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -966,7 +966,7 @@ object Generators : TemplateGroupBase() { } body(CharSequences) { """ - val size = ${if (f == CharSequences) "length" else "size" } - 1 + val size = ${f.code.size} - 1 if (size < 1) return emptyList() val result = ArrayList(size) for (index in 0 until size) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index b444584825e..841ea4cf31f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -162,7 +162,7 @@ object Snapshots : TemplateGroupBase() { } body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { """ - return when (${ if (f == CharSequences) "length" else "size" }) { + return when (${f.code.size}) { 0 -> emptyList() 1 -> listOf(this[0]) else -> this.toMutableList()