srctree

Robin Linden parent bee9db81 be8d0ba2
Get rid of the String.toHex extension

It doesn't really add any value or clarity over toInt(radix = 16).

inlinesplit
ev/null added: 5, removed: 16, total 0
@@ -1,8 +0,0 @@
// SPDX-FileCopyrightText: 2021 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
package ltd.evilcorp.domain
 
private const val RADIX_HEX = 16
fun String.toHex() = this.toInt(RADIX_HEX)
 
domain/src/main/kotlin/tox/ToxIdValidator.kt added: 5, removed: 16, total 0
@@ -4,8 +4,6 @@
 
package ltd.evilcorp.domain.tox
 
import ltd.evilcorp.domain.toHex
 
private const val CHECKSUM_BLOCK_SIZE = 4
 
class ToxIdValidator {
@@ -21,7 +19,7 @@ class ToxIdValidator {
!toxID.string().matches(Regex("[0-9A-Fa-f]*")) -> Result.NOT_HEX
toxID.string().length != TOX_ID_LENGTH -> Result.INCORRECT_LENGTH
toxID.string().chunked(CHECKSUM_BLOCK_SIZE).map {
it.toHex()
it.toInt(radix = 16)
}.fold(0) { b1, b2 -> b1 xor b2 } != 0 -> Result.INVALID_CHECKSUM
else -> Result.NO_ERROR
}
 
domain/src/main/kotlin/tox/ToxUtil.kt added: 5, removed: 16, total 0
@@ -15,9 +15,8 @@ import ltd.evilcorp.core.vo.ConnectionStatus
import ltd.evilcorp.core.vo.FileKind
import ltd.evilcorp.core.vo.MessageType
import ltd.evilcorp.core.vo.UserStatus
import ltd.evilcorp.domain.toHex
 
fun String.hexToBytes(): ByteArray = chunked(2).map { it.uppercase().toHex().toByte() }.toByteArray()
fun String.hexToBytes(): ByteArray = chunked(2).map { it.uppercase().toInt(radix = 16).toByte() }.toByteArray()
fun ByteArray.bytesToHex(): String = this.joinToString("") { "%02X".format(it) }
fun ToxUserStatus.toUserStatus(): UserStatus = UserStatus.values()[this.ordinal]
fun ToxConnection.toConnectionStatus(): ConnectionStatus = ConnectionStatus.values()[this.ordinal]