Skip to content

Commit

Permalink
Faster hex validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Feb 21, 2024
1 parent d982f02 commit 5cd1c5a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.vitorpamplona.quartz.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.encoders.HexValidator
import junit.framework.TestCase.assertEquals
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -65,4 +66,9 @@ class HexBenchmark {

benchmarkRule.measureRepeated { assertEquals(testHex, fr.acinq.secp256k1.Hex.encode(bytes)) }
}

@Test
fun isHex() {
benchmarkRule.measureRepeated { HexValidator.isHex(testHex) }
}
}
26 changes: 3 additions & 23 deletions quartz/src/main/java/com/vitorpamplona/quartz/encoders/HexUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,9 @@ fun HexKey.hexToByteArray(): ByteArray {
object HexValidator {
private fun isHexChar(c: Char): Boolean {
return when (c) {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'A',
'B',
'C',
'D',
'E',
'F',
-> true
in '0'..'9' -> true
in 'a'..'f' -> true
in 'A'..'F' -> true
else -> false
}
}
Expand Down

0 comments on commit 5cd1c5a

Please sign in to comment.