Skip to content

Commit a87da43

Browse files
committed
arrayIterators: Restore index to the previous value after NoSuchElementException has been thrown
Relates to #KT-17453
1 parent 731dcf5 commit a87da43

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

core/runtime.jvm/src/kotlin/jvm/internal/ArrayIterator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package kotlin.jvm.internal
1919
private class ArrayIterator<T>(val array: Array<T>) : Iterator<T> {
2020
private var index = 0
2121
override fun hasNext() = index < array.size
22-
override fun next() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
22+
override fun next() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
2323
}
2424

2525
public fun <T> iterator(array: Array<T>): Iterator<T> = ArrayIterator(array)

core/runtime.jvm/src/kotlin/jvm/internal/ArrayIterators.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,49 @@ package kotlin.jvm.internal
2121
private class ArrayByteIterator(private val array: ByteArray) : ByteIterator() {
2222
private var index = 0
2323
override fun hasNext() = index < array.size
24-
override fun nextByte() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
24+
override fun nextByte() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
2525
}
2626

2727
private class ArrayCharIterator(private val array: CharArray) : CharIterator() {
2828
private var index = 0
2929
override fun hasNext() = index < array.size
30-
override fun nextChar() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
30+
override fun nextChar() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
3131
}
3232

3333
private class ArrayShortIterator(private val array: ShortArray) : ShortIterator() {
3434
private var index = 0
3535
override fun hasNext() = index < array.size
36-
override fun nextShort() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
36+
override fun nextShort() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
3737
}
3838

3939
private class ArrayIntIterator(private val array: IntArray) : IntIterator() {
4040
private var index = 0
4141
override fun hasNext() = index < array.size
42-
override fun nextInt() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
42+
override fun nextInt() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
4343
}
4444

4545
private class ArrayLongIterator(private val array: LongArray) : LongIterator() {
4646
private var index = 0
4747
override fun hasNext() = index < array.size
48-
override fun nextLong() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
48+
override fun nextLong() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
4949
}
5050

5151
private class ArrayFloatIterator(private val array: FloatArray) : FloatIterator() {
5252
private var index = 0
5353
override fun hasNext() = index < array.size
54-
override fun nextFloat() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
54+
override fun nextFloat() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
5555
}
5656

5757
private class ArrayDoubleIterator(private val array: DoubleArray) : DoubleIterator() {
5858
private var index = 0
5959
override fun hasNext() = index < array.size
60-
override fun nextDouble() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
60+
override fun nextDouble() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
6161
}
6262

6363
private class ArrayBooleanIterator(private val array: BooleanArray) : BooleanIterator() {
6464
private var index = 0
6565
override fun hasNext() = index < array.size
66-
override fun nextBoolean() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }
66+
override fun nextBoolean() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }
6767
}
6868

6969
public fun iterator(array: ByteArray): ByteIterator = ArrayByteIterator(array)

generators/src/org/jetbrains/kotlin/generators/builtins/arrayIterators.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class GenerateArrayIterators(out: PrintWriter) : BuiltInsSourceGenerator(out) {
2929
out.println("private class Array${s}Iterator(private val array: ${s}Array) : ${s}Iterator() {")
3030
out.println(" private var index = 0")
3131
out.println(" override fun hasNext() = index < array.size")
32-
out.println(" override fun next$s() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { throw NoSuchElementException(e.message) }")
32+
out.println(" override fun next$s() = try { array[index++] } catch (e: ArrayIndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) }")
3333
out.println("}")
3434
out.println()
3535
}

0 commit comments

Comments
 (0)