diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt index d67afde..bf0e32c 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt @@ -5,25 +5,6 @@ import kotlin.experimental.inv import kotlin.experimental.or import kotlin.experimental.xor -fun ByteArray.invInto( - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) { - val length = endIndex - startIndex - require(length >= 0) - require(startIndex in 0..(size - length)) - require(destinationOffset in 0..(destination.size - length)) - - repeat(length) { i -> - destination[destinationOffset + i] = this[startIndex + i].inv() - } -} - -fun ByteArray.inv() = - ByteArray(size).also { invInto(it) } - private inline fun ByteArray.combineInto( that: ByteArray, destination: ByteArray, @@ -53,6 +34,25 @@ private inline fun ByteArray.combine( return ByteArray(size).also { combineInto(that, it, combine = combine) } } +fun ByteArray.invInto( + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, +) { + val length = endIndex - startIndex + require(length >= 0) + require(startIndex in 0..(size - length)) + require(destinationOffset in 0..(destination.size - length)) + + repeat(length) { i -> + destination[destinationOffset + i] = this[startIndex + i].inv() + } +} + +fun ByteArray.inv(): ByteArray = + ByteArray(size).also { invInto(it) } + fun ByteArray.andInto( that: ByteArray, destination: ByteArray, @@ -62,7 +62,7 @@ fun ByteArray.andInto( thatStartIndex: Int = 0, ) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a and b } -infix fun ByteArray.and(that: ByteArray) = +infix fun ByteArray.and(that: ByteArray): ByteArray = combine(that) { a, b -> a and b } fun ByteArray.orInto( @@ -74,7 +74,7 @@ fun ByteArray.orInto( thatStartIndex: Int = 0, ) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b } -infix fun ByteArray.or(that: ByteArray) = +infix fun ByteArray.or(that: ByteArray): ByteArray = combine(that) { a, b -> a or b } fun ByteArray.xorInto( @@ -86,5 +86,5 @@ fun ByteArray.xorInto( thatStartIndex: Int = 0, ) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b } -infix fun ByteArray.xor(that: ByteArray) = +infix fun ByteArray.xor(that: ByteArray): ByteArray = combine(that) { a, b -> a xor b } diff --git a/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt index 4efd00f..4faff12 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.int import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(4).also { copyInto(it, order = order) } -fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(4).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt index 4e027d1..2eccdb5 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.intarray import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(size * 4).also { copyInto(it, order = order) } -fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(size * 4).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt index 604edba..754b57a 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.long import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(8).also { copyInto(it, order = order) } -fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(8).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt index eacb5ac..b98ae0e 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.longarray import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(size * 8).also { copyInto(it, order = order) } -fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(size * 8).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt index e9a5e0a..ad136d4 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt @@ -12,7 +12,7 @@ fun UByteArray.invInto( endIndex: Int = size, ) = asByteArray().invInto(destination.asByteArray(), destinationOffset, startIndex, endIndex) -fun UByteArray.inv() = +fun UByteArray.inv(): UByteArray = UByteArray(size).also { invInto(it) } fun UByteArray.andInto( @@ -31,7 +31,7 @@ fun UByteArray.andInto( thatStartIndex, ) -infix fun UByteArray.and(that: UByteArray) = +infix fun UByteArray.and(that: UByteArray): UByteArray = UByteArray(size).also { andInto(that, it) } fun UByteArray.orInto( @@ -50,7 +50,7 @@ fun UByteArray.orInto( thatStartIndex, ) -infix fun UByteArray.or(that: UByteArray) = +infix fun UByteArray.or(that: UByteArray): UByteArray = UByteArray(size).also { orInto(that, it) } fun UByteArray.xorInto( @@ -69,5 +69,5 @@ fun UByteArray.xorInto( thatStartIndex, ) -infix fun UByteArray.xor(that: UByteArray) = +infix fun UByteArray.xor(that: UByteArray): UByteArray = UByteArray(size).also { xorInto(that, it) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt index a5adfb9..714781f 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.uint import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(4).also { copyInto(it, order = order) } -fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(4).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt index dec4318..7b88e50 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.uintarray import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(size * 4).also { copyInto(it, order = order) } -fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(size * 4).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt index 1a931f4..4e26af5 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.ulong import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(8).also { copyInto(it, order = order) } -fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(8).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt index c412293..b80cd38 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.ulongarray import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) = +fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = ByteArray(size * 8).also { copyInto(it, order = order) } -fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = +fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = UByteArray(size * 8).also { copyInto(it, order = order) }