Skip to content

Commit

Permalink
Small refactoring to use Family.CodeExtension.size when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
Abduqodiri Qurbonzoda committed Apr 19, 2020
1 parent 0d5ec27 commit 3a99b3d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}") }
Expand Down Expand Up @@ -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}))
"""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>(size)
for (index in 0 until size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3a99b3d

Please sign in to comment.