Compare commits
3 Commits
841bdfa21e
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
12ba3bf530
|
|||
|
b2d8048713
|
|||
|
c366c79d4c
|
@@ -1,6 +1,6 @@
|
|||||||
# encoding-kt
|
# encoding.kt
|
||||||
|
|
||||||
`encoding-kt` is a Kotlin Multiplatform library that provides common `ByteArray` encodings.
|
`encoding.kt` is a Kotlin Multiplatform library that provides common `ByteArray` encodings.
|
||||||
|
|
||||||
Supported encodings:
|
Supported encodings:
|
||||||
- Base2 (binary)
|
- Base2 (binary)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[versions]
|
[versions]
|
||||||
infendro = "1.0.0"
|
infendro = "1.1.0"
|
||||||
kotlin = "2.3.0"
|
kotlin = "2.3.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.infendro.encoding
|
|||||||
|
|
||||||
import com.infendro.encoding.exception.IllegalByteException
|
import com.infendro.encoding.exception.IllegalByteException
|
||||||
import com.infendro.encoding.util.align
|
import com.infendro.encoding.util.align
|
||||||
import com.infendro.encoding.util.divCeil
|
import com.infendro.encoding.util.ceilDiv
|
||||||
import com.infendro.encoding.util.lcm
|
import com.infendro.encoding.util.lcm
|
||||||
import com.infendro.encoding.util.log2
|
import com.infendro.encoding.util.log2
|
||||||
|
|
||||||
@@ -17,8 +17,8 @@ class StreamEncoding internal constructor(
|
|||||||
|
|
||||||
override fun encode(bytes: ByteArray): ByteArray {
|
override fun encode(bytes: ByteArray): ByteArray {
|
||||||
val size = when (padding) {
|
val size = when (padding) {
|
||||||
null -> (bytes.size * 8).divCeil(bits)
|
null -> (bytes.size * 8).ceilDiv(bits)
|
||||||
else -> (bytes.size * 8).divCeil(bits).align(block)
|
else -> (bytes.size * 8).ceilDiv(bits).align(block)
|
||||||
}
|
}
|
||||||
val encoded = ByteArray(size)
|
val encoded = ByteArray(size)
|
||||||
var i = 0
|
var i = 0
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ internal class BigInt private constructor(
|
|||||||
get() = BigInt(intArrayOf(0))
|
get() = BigInt(intArrayOf(0))
|
||||||
|
|
||||||
fun from(bytes: ByteArray): BigInt {
|
fun from(bytes: ByteArray): BigInt {
|
||||||
val words = IntArray(bytes.size.divCeil(4))
|
val words = IntArray(bytes.size.ceilDiv(4))
|
||||||
for (i in bytes.indices) {
|
for (i in bytes.indices) {
|
||||||
val (wi, bi) = (bytes.lastIndex - i).divRem(4)
|
val (wi, bi) = (bytes.lastIndex - i).divRem(4)
|
||||||
words[wi] = words[wi] or ((bytes[i].toInt() and 0xFF) shl (bi * 8))
|
words[wi] = words[wi] or ((bytes[i].toInt() and 0xFF) shl (bi * 8))
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package com.infendro.encoding.util
|
|||||||
|
|
||||||
import kotlin.math.log2
|
import kotlin.math.log2
|
||||||
|
|
||||||
internal fun Int.divCeil(n: Int): Int =
|
internal fun Int.ceilDiv(n: Int): Int =
|
||||||
(this + n - 1) / n
|
(this + n - 1) / n
|
||||||
|
|
||||||
internal fun Int.align(n: Int): Int =
|
internal fun Int.align(n: Int): Int =
|
||||||
divCeil(n) * n
|
ceilDiv(n) * n
|
||||||
|
|
||||||
internal fun log2(value: Int): Double =
|
internal fun log2(value: Int): Double =
|
||||||
log2(value.toDouble())
|
log2(value.toDouble())
|
||||||
|
|||||||
Reference in New Issue
Block a user