Collection.toString() should not throw if it contains itself
Merge-request: KT-MR-10591 Merged-by: Abduqodiri Qurbonzoda <abduqodiri.qurbonzoda@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
6fc02c3408
commit
f152fa537d
@@ -182,7 +182,7 @@ actual class ArrayList<E> private constructor(
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return backingArray.subarrayContentToString(offset, length)
|
||||
return backingArray.subarrayContentToString(offset, length, this)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
||||
@@ -23,13 +23,18 @@ internal fun checkCopyOfRangeArguments(fromIndex: Int, toIndex: Int, size: Int)
|
||||
/**
|
||||
* Returns a string representation of the contents of the subarray of the specified array as if it is [List].
|
||||
*/
|
||||
internal inline fun <T> Array<out T>.subarrayContentToString(offset: Int, length: Int): String {
|
||||
internal inline fun <T> Array<out T>.subarrayContentToString(offset: Int, length: Int, thisCollection: Collection<T>): String {
|
||||
val sb = StringBuilder(2 + length * 3)
|
||||
sb.append("[")
|
||||
var i = 0
|
||||
while (i < length) {
|
||||
if (i > 0) sb.append(", ")
|
||||
sb.append(this[offset + i])
|
||||
val nextElement = this[offset + i]
|
||||
if (nextElement === thisCollection) {
|
||||
sb.append("(this Collection)")
|
||||
} else {
|
||||
sb.append(nextElement)
|
||||
}
|
||||
i++
|
||||
}
|
||||
sb.append("]")
|
||||
|
||||
@@ -634,10 +634,10 @@ actual class HashMap<K, V> private constructor(
|
||||
if (index >= map.length) throw NoSuchElementException()
|
||||
lastIndex = index++
|
||||
val key = map.keysArray[lastIndex]
|
||||
if (key == map) sb.append("(this Map)") else sb.append(key)
|
||||
if (key === map) sb.append("(this Map)") else sb.append(key)
|
||||
sb.append('=')
|
||||
val value = map.valuesArray!![lastIndex]
|
||||
if (value == map) sb.append("(this Map)") else sb.append(value)
|
||||
if (value === map) sb.append("(this Map)") else sb.append(value)
|
||||
initNext()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user