stdlib: Throw NoSuchElementException in range iterators
This commit is contained in:
@@ -21,15 +21,16 @@ package kotlin.ranges
|
|||||||
* @property step the number by which the value is incremented on each step.
|
* @property step the number by which the value is incremented on each step.
|
||||||
*/
|
*/
|
||||||
internal class CharProgressionIterator(first: Char, last: Char, val step: Int) : CharIterator() {
|
internal class CharProgressionIterator(first: Char, last: Char, val step: Int) : CharIterator() {
|
||||||
private var next = first.toInt()
|
|
||||||
private val finalElement = last.toInt()
|
private val finalElement = last.toInt()
|
||||||
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
||||||
|
private var next = if (hasNext) first.toInt() else finalElement
|
||||||
|
|
||||||
override fun hasNext(): Boolean = hasNext
|
override fun hasNext(): Boolean = hasNext
|
||||||
|
|
||||||
override fun nextChar(): Char {
|
override fun nextChar(): Char {
|
||||||
val value = next
|
val value = next
|
||||||
if (value == finalElement) {
|
if (value == finalElement) {
|
||||||
|
if (!hasNext) throw kotlin.NoSuchElementException()
|
||||||
hasNext = false
|
hasNext = false
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -44,15 +45,16 @@ internal class CharProgressionIterator(first: Char, last: Char, val step: Int) :
|
|||||||
* @property step the number by which the value is incremented on each step.
|
* @property step the number by which the value is incremented on each step.
|
||||||
*/
|
*/
|
||||||
internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : IntIterator() {
|
internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : IntIterator() {
|
||||||
private var next = first
|
|
||||||
private val finalElement = last
|
private val finalElement = last
|
||||||
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
||||||
|
private var next = if (hasNext) first else finalElement
|
||||||
|
|
||||||
override fun hasNext(): Boolean = hasNext
|
override fun hasNext(): Boolean = hasNext
|
||||||
|
|
||||||
override fun nextInt(): Int {
|
override fun nextInt(): Int {
|
||||||
val value = next
|
val value = next
|
||||||
if (value == finalElement) {
|
if (value == finalElement) {
|
||||||
|
if (!hasNext) throw kotlin.NoSuchElementException()
|
||||||
hasNext = false
|
hasNext = false
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -67,15 +69,16 @@ internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : In
|
|||||||
* @property step the number by which the value is incremented on each step.
|
* @property step the number by which the value is incremented on each step.
|
||||||
*/
|
*/
|
||||||
internal class LongProgressionIterator(first: Long, last: Long, val step: Long) : LongIterator() {
|
internal class LongProgressionIterator(first: Long, last: Long, val step: Long) : LongIterator() {
|
||||||
private var next = first
|
|
||||||
private val finalElement = last
|
private val finalElement = last
|
||||||
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
||||||
|
private var next = if (hasNext) first else finalElement
|
||||||
|
|
||||||
override fun hasNext(): Boolean = hasNext
|
override fun hasNext(): Boolean = hasNext
|
||||||
|
|
||||||
override fun nextLong(): Long {
|
override fun nextLong(): Long {
|
||||||
val value = next
|
val value = next
|
||||||
if (value == finalElement) {
|
if (value == finalElement) {
|
||||||
|
if (!hasNext) throw kotlin.NoSuchElementException()
|
||||||
hasNext = false
|
hasNext = false
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
Reference in New Issue
Block a user