[WASM] Add std methods for collections

This commit is contained in:
Igor Yakovlev
2021-12-21 12:58:35 +01:00
committed by igoriakovlev
parent c9a92d71ae
commit e58d4163ad
8 changed files with 42 additions and 54 deletions
@@ -123,3 +123,18 @@ public actual inline fun <reified T> Collection<T>.toTypedArray(): Array<T> {
@Suppress("UNCHECKED_CAST")
return result as Array<T>
}
/**
* Returns a new array which is a copy of the original array with new elements filled with null values.
*/
internal fun <E> Array<E>.copyOfNulls(newSize: Int): Array<E?> = copyOfNulls(0, newSize)
internal fun <E> Array<E>.copyOfNulls(fromIndex: Int, toIndex: Int): Array<E?> {
val newSize = toIndex - fromIndex
if (newSize < 0) {
throw IllegalArgumentException("$fromIndex > $toIndex")
}
val result = @Suppress("TYPE_PARAMETER_AS_REIFIED") arrayOfNulls<E>(newSize)
this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size))
return result
}