Updated on 2026-08-14
This commit is contained in:
commit
7c7f07080e
1413 changed files with 24740 additions and 8757 deletions
|
|
@ -14,4 +14,6 @@ object StringsSigns {
|
|||
const val PERCENT = "%"
|
||||
const val THREE_STARS = "\u2217\u2217\u2217"
|
||||
const val PASSWORD_VISUAL_CHAR = '\u2022'
|
||||
const val APPROXIMATE = "≈"
|
||||
const val WHITE_SPACE = " "
|
||||
}
|
||||
|
|
@ -12,6 +12,9 @@ import kotlinx.coroutines.launch
|
|||
*/
|
||||
class JobHolder {
|
||||
|
||||
val isActive: Boolean
|
||||
get() = job?.isActive ?: false
|
||||
|
||||
private var job: Job? = null
|
||||
|
||||
/** Update current [JobHolder.job] and return new [job] */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.tangem.utils.extensions
|
||||
|
||||
private const val BYTES_IN_HEX = 2
|
||||
private const val RADIX = 16
|
||||
|
||||
/**
|
||||
* Converts a [ByteArray] to its hexadecimal string representation
|
||||
*
|
||||
* @return a [String] containing the hexadecimal representation of the byte array
|
||||
*/
|
||||
fun ByteArray.toHexString(): String = joinToString("") { "%02X".format(it) }
|
||||
|
||||
/**
|
||||
* Converts a hexadecimal [String] to a [ByteArray]
|
||||
*
|
||||
* @return a [ByteArray] representing the binary data of the hexadecimal string
|
||||
* @throws NumberFormatException if the string is not a valid hexadecimal representation
|
||||
*/
|
||||
fun String.hexToBytes(): ByteArray {
|
||||
val hexString = this.removePrefix("0x")
|
||||
|
||||
return ByteArray(size = hexString.length / BYTES_IN_HEX) { i ->
|
||||
Integer.parseInt(
|
||||
hexString.substring(
|
||||
startIndex = BYTES_IN_HEX * i,
|
||||
endIndex = BYTES_IN_HEX * i + BYTES_IN_HEX,
|
||||
),
|
||||
RADIX,
|
||||
).toByte()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely converts a hexadecimal [String] to a [ByteArray]
|
||||
*
|
||||
* @return a [ByteArray] if the conversion is successful, or `null` if an exception occurs
|
||||
*/
|
||||
fun String.hexToBytesOrNull(): ByteArray? = runCatching(String::hexToBytes).getOrNull()
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package com.tangem.utils.extensions
|
||||
|
||||
private const val DEEPLINK_VALIDATION_REGEX = "['\";<>()+\\\\]"
|
||||
private const val DEEPLINK_VALIDATION_REGEX = "[\";<>()+\\\\]"
|
||||
|
||||
/**
|
||||
* Check for malicious symbol in uri part
|
||||
|
|
|
|||
|
|
@ -0,0 +1,115 @@
|
|||
package com.tangem.utils.extensions
|
||||
|
||||
import com.google.common.truth.Truth
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class ByteArrayExtTest {
|
||||
|
||||
@Test
|
||||
fun `toHexString converts byte array to hexadecimal string`() {
|
||||
// Arrange
|
||||
val byteArray = byteArrayOf(0x0F, 0xA5.toByte(), 0xFF.toByte())
|
||||
|
||||
// Act
|
||||
val actual = byteArray.toHexString()
|
||||
|
||||
// Assert
|
||||
val expected = "0FA5FF"
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hexToBytes converts hexadecimal string to byte array`() {
|
||||
// Arrange
|
||||
val hexString = "0FA5FF"
|
||||
|
||||
// Act
|
||||
val actual = hexString.hexToBytes()
|
||||
|
||||
// Assert
|
||||
val expected = byteArrayOf(0x0F, 0xA5.toByte(), 0xFF.toByte())
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hexToBytes handles hex string with prefix`() {
|
||||
// Arrange
|
||||
val hexString = "0x0FA5FF"
|
||||
|
||||
// Act
|
||||
val actual = hexString.hexToBytes()
|
||||
|
||||
// Assert
|
||||
val expected = byteArrayOf(0x0F, 0xA5.toByte(), 0xFF.toByte())
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hexToBytes throws exception for invalid hex string`() {
|
||||
// Arrange
|
||||
val invalidHexString = "GHIJKL"
|
||||
|
||||
// Act
|
||||
val actual = runCatching { invalidHexString.hexToBytes() }.exceptionOrNull()
|
||||
|
||||
// Assert
|
||||
val expected = NumberFormatException()
|
||||
Truth.assertThat(actual).isInstanceOf(expected::class.java)
|
||||
Truth.assertThat(actual).hasMessageThat().isEqualTo("For input string: \"GH\" under radix 16")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hexToBytes handles empty string`() {
|
||||
// Arrange
|
||||
val emptyHexString = ""
|
||||
|
||||
// Act
|
||||
val actual = emptyHexString.hexToBytes()
|
||||
|
||||
// Assert
|
||||
Truth.assertThat(actual).isEqualTo(byteArrayOf())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hexToBytesOrNull returns byte array for valid hex string`() {
|
||||
// Arrange
|
||||
val hexString = "0FA5FF"
|
||||
|
||||
// Act
|
||||
val actual = hexString.hexToBytesOrNull()
|
||||
|
||||
// Assert
|
||||
val expected = byteArrayOf(0x0F, 0xA5.toByte(), 0xFF.toByte())
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hexToBytesOrNull returns null for invalid hex string`() {
|
||||
// Arrange
|
||||
val invalidHexString = "GHIJKL"
|
||||
|
||||
// Act
|
||||
val actual = invalidHexString.hexToBytesOrNull()
|
||||
|
||||
// Assert
|
||||
Truth.assertThat(actual).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hexToBytesOrNullHandlesEmptyString() {
|
||||
// Arrange
|
||||
val emptyHexString = ""
|
||||
|
||||
// Act
|
||||
val actual = emptyHexString.hexToBytesOrNull()
|
||||
|
||||
// Assert
|
||||
val expected = byteArrayOf()
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue