-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace bytearray creation to more optimal and more readable method.
- Loading branch information
Showing
25 changed files
with
119 additions
and
38 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
lib/src/commonMain/kotlin/maryk/lib/bytes/combineToByteArray.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package maryk.lib.bytes | ||
|
||
fun combineToByteArray(vararg elements: Any): ByteArray { | ||
val totalSize = elements.sumOf { element -> | ||
when (element) { | ||
is ByteArray -> element.size | ||
is Byte -> 1 | ||
else -> throw IllegalArgumentException("Unsupported type: ${element::class.simpleName}") | ||
} | ||
} | ||
|
||
val result = ByteArray(totalSize) | ||
var offset = 0 | ||
|
||
elements.forEach { element -> | ||
when (element) { | ||
is ByteArray -> { | ||
element.copyInto(result, offset) | ||
offset += element.size | ||
} | ||
is Byte -> { | ||
result[offset] = element | ||
offset++ | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} |
48 changes: 48 additions & 0 deletions
48
lib/src/commonTest/kotlin/maryk/lib/bytes/CombineToByteArrayTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package maryk.lib.bytes | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.expect | ||
|
||
class CombineToByteArrayTest { | ||
@Test | ||
fun testCombineByteArrays() { | ||
val result = combineToByteArray( | ||
byteArrayOf(1, 2, 3), | ||
byteArrayOf(4, 5), | ||
byteArrayOf(6) | ||
) | ||
assertContentEquals(byteArrayOf(1, 2, 3, 4, 5, 6), result) | ||
} | ||
|
||
@Test | ||
fun testCombineBytes() { | ||
val result = combineToByteArray(1.toByte(), 2.toByte(), 3.toByte()) | ||
assertContentEquals(byteArrayOf(1, 2, 3), result) | ||
} | ||
|
||
@Test | ||
fun testCombineMixed() { | ||
val result = combineToByteArray( | ||
byteArrayOf(1, 2), | ||
3.toByte(), | ||
byteArrayOf(4, 5), | ||
6.toByte() | ||
) | ||
assertContentEquals(byteArrayOf(1, 2, 3, 4, 5, 6), result) | ||
} | ||
|
||
@Test | ||
fun testEmptyInput() { | ||
val result = combineToByteArray() | ||
expect(0) { result.size } | ||
} | ||
|
||
@Test | ||
fun testUnsupportedType() { | ||
assertFailsWith<IllegalArgumentException> { | ||
combineToByteArray(byteArrayOf(1, 2), "unsupported") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.