Breaking: change meaning of the parameters of ImmutableBlob.to(U)ByteArray

To be consistent with other subrange copying to* functions: String.toCharArray, Array.copyInto etc
This commit is contained in:
Ilya Gorbunov
2018-11-21 18:32:09 +03:00
committed by Nikolay Igotti
parent 6f6bfd15ed
commit 22ef0a1a4c
2 changed files with 22 additions and 17 deletions
+12 -11
View File
@@ -467,17 +467,18 @@ KInt Kotlin_NativePtrArray_getArrayLength(KConstRef thiz) {
return array->count_;
}
OBJ_GETTER(Kotlin_ImmutableBlob_toByteArray, KConstRef thiz, KInt start, KInt count) {
const ArrayHeader* array = thiz->array();
if (start < 0 || count < 0 || start > array->count_ - count) {
ThrowArrayIndexOutOfBoundsException();
}
ArrayHeader* result = AllocArrayInstance(
theByteArrayTypeInfo, count, OBJ_RESULT)->array();
memcpy(PrimitiveArrayAddressOfElementAt<KByte>(result, 0),
PrimitiveArrayAddressOfElementAt<KByte>(array, start),
count);
RETURN_OBJ(result->obj());
OBJ_GETTER(Kotlin_ImmutableBlob_toByteArray, KConstRef thiz, KInt startIndex, KInt endIndex) {
const ArrayHeader* array = thiz->array();
if (startIndex < 0 || endIndex > array->count_ || startIndex > endIndex) {
ThrowArrayIndexOutOfBoundsException();
}
KInt count = endIndex - startIndex;
ArrayHeader* result = AllocArrayInstance(
theByteArrayTypeInfo, count, OBJ_RESULT)->array();
memcpy(PrimitiveArrayAddressOfElementAt<KByte>(result, 0),
PrimitiveArrayAddressOfElementAt<KByte>(array, startIndex),
count);
RETURN_OBJ(result->obj());
}
KNativePtr Kotlin_ImmutableBlob_asCPointerImpl(KRef thiz, KInt offset) {
+10 -6
View File
@@ -42,19 +42,23 @@ private class ImmutableBlobIteratorImpl(val blob: ImmutableBlob) : ByteIterator(
}
/**
* Allocates new [ByteArray] and copies the data from blob starting from [start]
* and with [count] amount of bytes.
* Copies the data from this blob into a new [ByteArray].
*
* @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default.
* @param endIndex the end (exclusive) of the subrange to copy, size of this blob by default.
*/
@SymbolName("Kotlin_ImmutableBlob_toByteArray")
public external fun ImmutableBlob.toByteArray(start: Int = 0, count: Int = size): ByteArray
public external fun ImmutableBlob.toByteArray(startIndex: Int = 0, endIndex: Int = size): ByteArray
/**
* Allocates new [UByteArray] and copies the data from blob starting from [start]
* and with [count] amount of bytes.
* Copies the data from this blob into a new [UByteArray].
*
* @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default.
* @param endIndex the end (exclusive) of the subrange to copy, size of this blob by default.
*/
@ExperimentalUnsignedTypes
@SymbolName("Kotlin_ImmutableBlob_toByteArray")
public external fun ImmutableBlob.toUByteArray(start: Int = 0, count: Int = size): UByteArray
public external fun ImmutableBlob.toUByteArray(startIndex: Int = 0, endIndex: Int = size): UByteArray
/**
* Returns stable C pointer to data at certain [offset], useful as a way to pass resource