Simplify convention

This commit is contained in:
Him188 2020-02-29 18:04:39 +08:00
parent 1e9b34a636
commit fb5b52415c

View File

@ -35,10 +35,10 @@ fun Short.toByteArray(): ByteArray = with(toInt()) {
* 255 -> 00 00 00 FF * 255 -> 00 00 00 FF
*/ */
fun Int.toByteArray(): ByteArray = byteArrayOf( fun Int.toByteArray(): ByteArray = byteArrayOf(
(shr(24) and 0xFF).toByte(), ushr(24).toByte(),
(shr(16) and 0xFF).toByte(), ushr(16).toByte(),
(shr(8) and 0xFF).toByte(), ushr(8).toByte(),
(shr(0) and 0xFF).toByte() ushr(0).toByte()
) )
/** /**
@ -114,7 +114,8 @@ fun Byte.fixToUHex(): String = this.toUByte().fixToUHex()
/** /**
* 转无符号十六进制表示, 并补充首位 `0`. * 转无符号十六进制表示, 并补充首位 `0`.
*/ */
fun UByte.fixToUHex(): String = if (this.toInt() in 0..15) "0${this.toString(16).toUpperCase()}" else this.toString(16).toUpperCase() fun UByte.fixToUHex(): String =
if (this.toInt() in 0..15) "0${this.toString(16).toUpperCase()}" else this.toString(16).toUpperCase()
/** /**
* 将无符号 Hex 转为 [ByteArray], 有根据 hex [hashCode] 建立的缓存. * 将无符号 Hex 转为 [ByteArray], 有根据 hex [hashCode] 建立的缓存.
@ -143,7 +144,9 @@ fun String.chunkedHexToBytes(): ByteArray =
* 这个方法很累, 不建议经常使用. * 这个方法很累, 不建议经常使用.
*/ */
fun String.autoHexToBytes(): ByteArray = fun String.autoHexToBytes(): ByteArray =
this.replace("\n", "").replace(" ", "").asSequence().chunked(2).map { (it[0].toString() + it[1]).toUByte(16).toByte() }.toList().toByteArray() this.replace("\n", "").replace(" ", "").asSequence().chunked(2).map {
(it[0].toString() + it[1]).toUByte(16).toByte()
}.toList().toByteArray()
/** /**
* 将无符号 Hex 转为 [UByteArray], 有根据 hex [hashCode] 建立的缓存. * 将无符号 Hex 转为 [UByteArray], 有根据 hex [hashCode] 建立的缓存.
@ -188,16 +191,24 @@ fun getRandomString(length: Int, vararg charRanges: CharRange): String =
* 本函数将 4 [Byte] bits 连接得到 [Int] * 本函数将 4 [Byte] bits 连接得到 [Int]
*/ */
fun ByteArray.toUInt(): UInt = fun ByteArray.toUInt(): UInt =
(this[0].toUInt().and(255u) shl 24) + (this[1].toUInt().and(255u) shl 16) + (this[2].toUInt().and(255u) shl 8) + (this[3].toUInt().and(255u) shl 0) (this[0].toUInt().and(255u) shl 24) + (this[1].toUInt().and(255u) shl 16) + (this[2].toUInt().and(255u) shl 8) + (this[3].toUInt().and(
255u
) shl 0)
fun ByteArray.toUShort(): UShort = fun ByteArray.toUShort(): UShort =
((this[0].toUInt().and(255u) shl 8) + (this[1].toUInt().and(255u) shl 0)).toUShort() ((this[0].toUInt().and(255u) shl 8) + (this[1].toUInt().and(255u) shl 0)).toUShort()
fun ByteArray.toInt(): Int = fun ByteArray.toInt(): Int =
(this[0].toInt().and(255) shl 24) + (this[1].toInt().and(255) shl 16) + (this[2].toInt().and(255) shl 8) + (this[3].toInt().and(255) shl 0) (this[0].toInt().and(255) shl 24) + (this[1].toInt().and(255) shl 16) + (this[2].toInt().and(255) shl 8) + (this[3].toInt().and(
255
) shl 0)
/** /**
* [IoBuffer.Pool] [borrow][ObjectPool.borrow] 一个 [IoBuffer] 然后将 [this] 写入. * [IoBuffer.Pool] [borrow][ObjectPool.borrow] 一个 [IoBuffer] 然后将 [this] 写入.
* 注意回收 ([ObjectPool.recycle]) * 注意回收 ([ObjectPool.recycle])
*/ */
fun ByteArray.toIoBuffer(offset: Int = 0, length: Int = this.size - offset, pool: ObjectPool<IoBuffer> = IoBuffer.Pool): IoBuffer = pool.borrow().let { it.writeFully(this, offset, length); it } fun ByteArray.toIoBuffer(
offset: Int = 0,
length: Int = this.size - offset,
pool: ObjectPool<IoBuffer> = IoBuffer.Pool
): IoBuffer = pool.borrow().let { it.writeFully(this, offset, length); it }