Make ByteArray#asHexString less slow

the old code was creating formatters (running regex) for each byte...
This commit is contained in:
Jason Penilla 2023-11-20 17:14:36 -07:00
parent 47ec175e27
commit 87e8db20d0
No known key found for this signature in database
GPG key ID: 0E75A301420E48F8

View file

@ -311,12 +311,16 @@ fun InputStream.hash(algorithm: HashingAlgorithm, bufferSize: Int = 8192): ByteA
return digest.digest()
}
private val hexChars = "0123456789abcdef".toCharArray()
fun ByteArray.asHexString(): String {
val sb: StringBuilder = StringBuilder(size * 2)
for (aHash in this) {
sb.append("%02x".format(aHash.toInt() and 0xFF))
val chars = CharArray(2 * size)
forEachIndexed { i, byte ->
val unsigned = byte.toInt() and 0xFF
chars[2 * i] = hexChars[unsigned / 16]
chars[2 * i + 1] = hexChars[unsigned % 16]
}
return sb.toString()
return String(chars)
}
fun JavaToolchainService.defaultJavaLauncher(project: Project): Provider<JavaLauncher> {