Skip to content

Commit 6bfa4b5

Browse files
committed
Use stdlib toHexString
1 parent 93aebc2 commit 6bfa4b5

3 files changed

Lines changed: 9 additions & 26 deletions

File tree

kable-core/src/commonMain/kotlin/ByteArray.kt

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,6 @@ package com.juul.kable
33
import com.juul.kable.Endianness.BigEndian
44
import com.juul.kable.Endianness.LittleEndian
55

6-
/**
7-
* Copied from Tuulbox.
8-
* https://github.com/JuulLabs/tuulbox/blob/fde4eb74d2aeb37b6aaac2002bccc553adc02c5a/encoding/src/commonMain/kotlin/HexString.kt
9-
*/
10-
internal fun ByteArray.toHexString(
11-
separator: String? = null,
12-
prefix: String? = null,
13-
lowerCase: Boolean = false,
14-
): String {
15-
if (isEmpty()) return ""
16-
val hexCode = if (lowerCase) "0123456789abcdef" else "0123456789ABCDEF"
17-
val capacity = size * (2 + (prefix?.length ?: 0)) + (size - 1) * (separator?.length ?: 0)
18-
val r = StringBuilder(capacity)
19-
for (b in this) {
20-
if (separator != null && r.isNotEmpty()) r.append(separator)
21-
if (prefix != null) r.append(prefix)
22-
r.append(hexCode[b.toInt() shr 4 and 0xF])
23-
r.append(hexCode[b.toInt() and 0xF])
24-
}
25-
return r.toString()
26-
}
27-
286
internal fun ByteArray.toShort(): Int {
297
require(size == 2) { "ByteArray must be size of 2 to be converted to a short, was $size" }
308
return this[0] and 0xFF shl 8 or (this[1] and 0xFF)

kable-core/src/commonMain/kotlin/Filter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.juul.kable
33
import com.juul.kable.Filter.Name.Exact
44
import com.juul.kable.Filter.Name.Prefix
55
import kotlin.experimental.and
6+
import kotlin.text.HexFormat.Companion.UpperCase
67
import kotlin.uuid.Uuid
78

89
/**
@@ -126,7 +127,7 @@ public sealed class Filter {
126127
}
127128

128129
override fun toString(): String =
129-
"ManufacturerData(id=$id, data=${data?.toHexString()}, dataMask=${dataMask?.toHexString()})"
130+
"ManufacturerData(id=$id, data=${data?.toHexString(UpperCase)}, dataMask=${dataMask?.toHexString()})"
130131
}
131132

132133
/**

kable-core/src/commonMain/kotlin/logs/Hex.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.juul.kable.logs
22

3-
import com.juul.kable.toHexString
4-
53
public val Hex: Logging.DataProcessor = Hex()
64

75
public class HexBuilder internal constructor() {
@@ -15,7 +13,13 @@ public class HexBuilder internal constructor() {
1513

1614
public fun Hex(init: HexBuilder.() -> Unit = {}): Logging.DataProcessor {
1715
val config = HexBuilder().apply(init)
16+
val format = HexFormat {
17+
upperCase = !config.lowerCase
18+
bytes {
19+
byteSeparator = config.separator
20+
}
21+
}
1822
return Logging.DataProcessor { data, _, _, _, _ ->
19-
data.toHexString(separator = config.separator, lowerCase = config.lowerCase)
23+
data.toHexString(format)
2024
}
2125
}

0 commit comments

Comments
 (0)