Commit 1dc0255b authored by Him188's avatar Him188

Enhance performance

parent 45bcd0fa
...@@ -102,23 +102,27 @@ fun UByte.fixToUHex(): String = if (this.toInt() in 0..9) "0${this.toString(16). ...@@ -102,23 +102,27 @@ fun UByte.fixToUHex(): String = if (this.toInt() in 0..9) "0${this.toString(16).
*/ */
fun String.hexToBytes(): ByteArray = fun String.hexToBytes(): ByteArray =
this.split(" ") this.split(" ")
.asSequence()
.filterNot { it.isEmpty() } .filterNot { it.isEmpty() }
.map { s -> s.toUByte(16).toByte() } .map { s -> s.toUByte(16).toByte() }
.toList()
.toByteArray() .toByteArray()
/** /**
* 每 2 char 为一组, 转换 Hex 为 [ByteArray] * 每 2 char 为一组, 转换 Hex 为 [ByteArray]
*/ */
fun String.chunkedHexToBytes(): ByteArray = fun String.chunkedHexToBytes(): ByteArray =
this.chunked(2).map { it.toUByte(16).toByte() }.toByteArray() this.asSequence().chunked(2).map { (it[0].toString() + it[1]).toUByte(16).toByte() }.toList().toByteArray()
/** /**
* 将无符号 Hex 转为 [UByteArray], 有根据 hex 的 [hashCode] 建立的缓存. * 将无符号 Hex 转为 [UByteArray], 有根据 hex 的 [hashCode] 建立的缓存.
*/ */
fun String.hexToUBytes(): UByteArray = fun String.hexToUBytes(): UByteArray =
this.split(" ") this.split(" ")
.asSequence()
.filterNot { it.isEmpty() } .filterNot { it.isEmpty() }
.map { s -> s.toUByte(16) } .map { s -> s.toUByte(16) }
.toList()
.toUByteArray() .toUByteArray()
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment