JS: Array.toString is const, Array.contentToString is shallow, Array.contentDeepToString is deep and self-reference aware
#KT-13582
This commit is contained in:
@@ -646,7 +646,7 @@ public infix fun <T> Array<out T>.contentDeepEquals(other: Array<out T>): Boolea
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun <T> Array<out T>.contentToString(): String {
|
||||
@@ -654,7 +654,7 @@ public fun <T> Array<out T>.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun ByteArray.contentToString(): String {
|
||||
@@ -662,7 +662,7 @@ public fun ByteArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun ShortArray.contentToString(): String {
|
||||
@@ -670,7 +670,7 @@ public fun ShortArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun IntArray.contentToString(): String {
|
||||
@@ -678,7 +678,7 @@ public fun IntArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun LongArray.contentToString(): String {
|
||||
@@ -686,7 +686,7 @@ public fun LongArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun FloatArray.contentToString(): String {
|
||||
@@ -694,7 +694,7 @@ public fun FloatArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun DoubleArray.contentToString(): String {
|
||||
@@ -702,7 +702,7 @@ public fun DoubleArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun BooleanArray.contentToString(): String {
|
||||
@@ -710,7 +710,7 @@ public fun BooleanArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
* Returns a string representation of the contents of the specified array as if it is a [List].
|
||||
*/
|
||||
@library("arrayToString")
|
||||
public fun CharArray.contentToString(): String {
|
||||
@@ -718,11 +718,13 @@ public fun CharArray.contentToString(): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of this array as if it is [List].
|
||||
*
|
||||
* Returns a string representation of the contents of this array as if it is a [List].
|
||||
* Nested arrays are treated as lists too.
|
||||
*
|
||||
* If any of arrays contains itself on any nesting level that reference
|
||||
* is rendered as `"[...]"` to prevent recursion.
|
||||
*/
|
||||
@library("arrayToString")
|
||||
@library("arrayDeepToString")
|
||||
public fun <T> Array<out T>.contentDeepToString(): String {
|
||||
return noImpl
|
||||
}
|
||||
@@ -801,8 +803,8 @@ public fun CharArray.contentHashCode(): Int {
|
||||
|
||||
/**
|
||||
* Returns a hash code based on the contents of this array as if it is [List].
|
||||
*
|
||||
* Nested arrays are treated as lists too.
|
||||
*
|
||||
* If any of arrays contains itself on any nesting level the behavior is undefined.
|
||||
*/
|
||||
@library("arrayDeepHashCode")
|
||||
|
||||
+17
-2
@@ -86,7 +86,7 @@
|
||||
return "null";
|
||||
}
|
||||
else if (Array.isArray(o)) {
|
||||
return Kotlin.arrayToString(o);
|
||||
return "[...]";
|
||||
}
|
||||
else {
|
||||
return o.toString();
|
||||
@@ -97,6 +97,21 @@
|
||||
return "[" + a.map(Kotlin.toString).join(", ") + "]";
|
||||
};
|
||||
|
||||
Kotlin.arrayDeepToString = function (a, visited) {
|
||||
visited = visited || [a];
|
||||
return "[" + a.map(function(e) {
|
||||
if (Array.isArray(e) && visited.indexOf(e) < 0) {
|
||||
visited.push(e);
|
||||
var result = Kotlin.arrayDeepToString(e, visited);
|
||||
visited.pop();
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return Kotlin.toString(e);
|
||||
}
|
||||
}).join(", ") + "]";
|
||||
};
|
||||
|
||||
Kotlin.compareTo = function (a, b) {
|
||||
var typeA = typeof a;
|
||||
var typeB = typeof a;
|
||||
@@ -398,7 +413,7 @@
|
||||
var result = 1;
|
||||
for (var i = 0, n = arr.length; i < n; i++) {
|
||||
var e = arr[i];
|
||||
result = ((31 * result | 0) + (Array.isArray(e) ? Kotlin.arrayDeepHashCode(e) : Kotlin.hashCode(arr[i]))) | 0;
|
||||
result = ((31 * result | 0) + (Array.isArray(e) ? Kotlin.arrayDeepHashCode(e) : Kotlin.hashCode(e))) | 0;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user