Fast-fail check

This commit is contained in:
Him188 2020-01-29 20:21:53 +08:00
parent 625d48f3a5
commit 1cd1a19dbe

View File

@ -5,6 +5,7 @@ package net.mamoe.mirai.utils.io
import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.String
import kotlinx.io.core.use
import net.mamoe.mirai.utils.checkOffsetAndLength
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@ -16,6 +17,7 @@ import kotlin.jvm.JvmSynthetic
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
@UseExperimental(ExperimentalUnsignedTypes::class)
fun List<Byte>.toUHexString(separator: String = " ", offset: Int = 0, length: Int = this.size - offset): String {
this.checkOffsetAndLength(offset, length)
if (length == 0) {
return ""
}
@ -36,6 +38,7 @@ fun List<Byte>.toUHexString(separator: String = " ", offset: Int = 0, length: In
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
@UseExperimental(ExperimentalUnsignedTypes::class)
fun ByteArray.toUHexString(separator: String = " ", offset: Int = 0, length: Int = this.size - offset): String {
this.checkOffsetAndLength(offset, length)
if (length == 0) {
return ""
}
@ -72,7 +75,8 @@ fun UByteArray.toUHexString(separator: String = " ", offset: Int = 0, length: In
}
}
fun ByteArray.encodeToString(): String = String(this)
@Suppress("NOTHING_TO_INLINE")
inline fun ByteArray.encodeToString(): String = String(this)
fun ByteArray.toReadPacket(offset: Int = 0, length: Int = this.size) = ByteReadPacket(this, offset = offset, length = length)