Small refactoring to use Family.CodeExtension.size when appropriate

This commit is contained in:
Abduqodiri Qurbonzoda
2020-04-15 02:25:08 +03:00
parent 0d5ec27ac4
commit 3a99b3d5a4
3 changed files with 4 additions and 5 deletions
@@ -292,7 +292,7 @@ object Elements : TemplateGroupBase() {
on(Platform.JS) { on(Platform.JS) {
inline(Inline.No) inline(Inline.No)
val size = if (f == CharSequences) "length" else "size" val size = f.code.size
body { body {
""" """
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, $size: $$size}") } return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, $size: $$size}") }
@@ -944,11 +944,10 @@ object Elements : TemplateGroupBase() {
} }
specialFor(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences) { specialFor(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences) {
body { body {
val size = if (family == CharSequences) "length" else "size"
""" """
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.") throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
return get(random.nextInt($size)) return get(random.nextInt(${f.code.size}))
""" """
} }
} }
@@ -966,7 +966,7 @@ object Generators : TemplateGroupBase() {
} }
body(CharSequences) { body(CharSequences) {
""" """
val size = ${if (f == CharSequences) "length" else "size" } - 1 val size = ${f.code.size} - 1
if (size < 1) return emptyList() if (size < 1) return emptyList()
val result = ArrayList<R>(size) val result = ArrayList<R>(size)
for (index in 0 until size) { for (index in 0 until size) {
@@ -162,7 +162,7 @@ object Snapshots : TemplateGroupBase() {
} }
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) {
""" """
return when (${ if (f == CharSequences) "length" else "size" }) { return when (${f.code.size}) {
0 -> emptyList() 0 -> emptyList()
1 -> listOf(this[0]) 1 -> listOf(this[0])
else -> this.toMutableList() else -> this.toMutableList()