Skip to content

Commit 352cb0e

Browse files
committed
refactor: add more platforms
1 parent 1037432 commit 352cb0e

File tree

7 files changed

+73
-89
lines changed

7 files changed

+73
-89
lines changed

build.gradle.kts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ kotlin {
4646
js {
4747
browser()
4848
}
49+
50+
macosX64()
51+
macosArm64()
52+
iosSimulatorArm64()
53+
iosX64()
54+
iosArm64()
55+
56+
watchosArm32()
57+
watchosArm64()
58+
watchosX64()
59+
watchosSimulatorArm64()
60+
tvosSimulatorArm64()
61+
tvosX64()
62+
tvosArm64()
63+
64+
linuxX64()
65+
linuxArm64()
66+
mingwX64()
67+
68+
4969
sourceSets {
5070
all {
5171
languageSettings {

gradle.properties

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
kotlin.code.style=official
2-
org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
31
org.gradle.configuration-cache=true
2+
org.gradle.caching=true
3+
org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
4+
kotlin.daemon.jvmargs=-Xmx6g
5+
kotlin.native.enableKlibsCrossCompilation=true
6+
org.gradle.jvmargs=-Xmx2g

src/commonMain/kotlin/space/iseki/bencoding/BinaryStringStrategy.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package space.iseki.bencoding
22

33
import kotlinx.serialization.descriptors.SerialDescriptor
4-
import space.iseki.bencoding.internal.bytes2StringIso88591
54
import kotlin.io.encoding.ExperimentalEncodingApi
65

76
/**
@@ -39,7 +38,7 @@ enum class BinaryStringStrategy {
3938
strategy = strategy,
4039
options = options,
4140
base64 = { kotlin.io.encoding.Base64.encode(decodeByteArray()) },
42-
raw = { decodeRaw(decodeByteArray()) },
41+
raw = { bytes2StringIso88591(decodeByteArray()) },
4342
)
4443

4544
context(BencodeEncoder)
@@ -85,11 +84,7 @@ enum class BinaryStringStrategy {
8584
return bytes
8685
}
8786

88-
private fun decodeRaw(bytes: ByteArray): String {
89-
return bytes2StringIso88591(bytes) ?: decodeRawFallback(bytes)
90-
}
91-
92-
private fun decodeRawFallback(bytes: ByteArray): String {
87+
private fun bytes2StringIso88591(bytes: ByteArray): String {
9388
val chars = CharArray(bytes.size)
9489
for (i in bytes.indices) {
9590
chars[i] = (bytes[i].toInt() and 0xff).toChar()

src/commonMain/kotlin/space/iseki/bencoding/internal/BytesLexer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class BytesLexer(private val bytes: ByteArray) : CommonLexer() {
1414
while (i < bytes.size && (bytes[i] in '0'.code..'9'.code || bytes[i] == '-'.code.toByte())) i++
1515
if (i == pos) decodeError("invalid number")
1616
try {
17-
return bytes2Long(bytes, pos, i - pos)
17+
return bytes.copyOfRange(pos, i).decodeToString().toLong()
1818
} catch (e: NumberFormatException) {
1919
decodeError("invalid number")
2020
} finally {

src/commonMain/kotlin/space/iseki/bencoding/internal/Utils.kt

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,55 @@ package space.iseki.bencoding.internal
33
import kotlinx.serialization.ExperimentalSerializationApi
44
import kotlinx.serialization.descriptors.SerialDescriptor
55
import space.iseki.bencoding.BinaryString
6-
7-
internal expect fun bytes2Long(bytes: ByteArray, off: Int, len: Int): Long
8-
internal expect fun bytes2StringIso88591(bytes: ByteArray, off: Int = 0, len: Int = bytes.size): String?
6+
import kotlin.math.max
97

108
@OptIn(ExperimentalSerializationApi::class)
119
internal fun SerialDescriptor.binaryStringAnnotation(childIndex: Int) =
1210
getElementAnnotations(childIndex).firstOrNull { it is BinaryString } as BinaryString?
1311

14-
internal expect fun createBytesWriter0(): BWriter
1512
internal fun createBytesWriter(): BWriter = createBytesWriter0()
1613

14+
internal fun createBytesWriter0(): BWriter = object : BWriter {
15+
private var pos = 0
16+
private fun ensureSize(delta: Int) {
17+
val newLen = pos + delta
18+
if (newLen < ba.size) {
19+
return
20+
}
21+
val target = ByteArray(newLength(ba.size, delta, ba.size))
22+
ba.copyInto(target)
23+
ba = target
24+
}
25+
26+
private var ba = ByteArray(32)
27+
override fun writeData(b: Int) {
28+
ensureSize(1)
29+
ba[pos++] = b.toByte()
30+
}
31+
32+
override fun writeData(b: ByteArray) {
33+
ensureSize(b.size)
34+
b.copyInto(ba, pos)
35+
pos += b.size
36+
}
37+
38+
override fun getByteArray(): ByteArray {
39+
return ba.sliceArray(0 until pos)
40+
}
41+
}
42+
43+
private fun newLength(oldLength: Int, minGrowth: Int, prefGrowth: Int): Int {
44+
val prefLength = (oldLength + max(minGrowth, prefGrowth)) // might overflow
45+
return if (0 < prefLength && prefLength <= Int.MAX_VALUE - 8) {
46+
prefLength
47+
} else {
48+
val minLength = oldLength + minGrowth
49+
return if (minLength < 0) { // overflow
50+
throw Error("Required array length $oldLength + $minGrowth is too large")
51+
} else if (minLength <= Int.MAX_VALUE - 8) {
52+
Int.MAX_VALUE - 8
53+
} else {
54+
minLength
55+
}
56+
}
57+
}

src/jsMain/kotlin/space/iseki/bencoding/internal/Utils.js.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/jvmMain/kotlin/space/iseki/bencoding/internal/Utils.jvm.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)