disbrowser/src/main/java/com/smallhacker/disbrowser/asm/VagueNumber.kt

43 lines
1.3 KiB
Kotlin
Raw Normal View History

2019-01-07 18:19:37 +00:00
package com.smallhacker.disbrowser.asm
2019-01-11 03:19:08 +00:00
inline class VagueNumber(private val valueAndCertainty: ULong) {
private constructor(value: UInt, certainty: UInt) : this(value.toULong() or (certainty.toULong() shl 32))
constructor(value: UInt) : this(value, 0xFFFF_FFFFu)
constructor() : this(0u, 0u)
2019-01-07 18:19:37 +00:00
2019-01-11 03:19:08 +00:00
val value get() = valueAndCertainty.toUInt()
val certainty get() = (valueAndCertainty shr 32).toUInt()
fun withBits(value: UInt) = VagueNumber(this.value or value, this.certainty or value)
fun withoutBits(value: UInt) = VagueNumber(this.value and value.inv(), this.certainty or value)
2019-01-07 18:19:37 +00:00
val certain: Boolean
2019-01-11 03:19:08 +00:00
get() = certainty == 0xFFFF_FFFFu
2019-01-07 18:19:37 +00:00
2019-01-11 03:19:08 +00:00
fun get(mask: UInt): UInt? {
2019-01-07 18:19:37 +00:00
if ((certainty and mask) != mask) {
return null
}
return value and mask
}
2019-01-11 03:19:08 +00:00
fun getBoolean(mask: UInt): Boolean? {
2019-01-07 18:19:37 +00:00
val value = get(mask) ?: return null
return value == mask
}
override fun toString(): String {
2019-01-11 03:19:08 +00:00
var i = 1u shl 31
2019-01-07 18:19:37 +00:00
val out = StringBuilder()
2019-01-11 03:19:08 +00:00
while (i != 0u) {
2019-01-07 18:19:37 +00:00
val b = getBoolean(i)
when (b) {
true -> out.append('1')
false -> out.append('0')
null -> out.append('?')
}
2019-01-11 03:19:08 +00:00
i = i shr 1
2019-01-07 18:19:37 +00:00
}
return out.toString()
}
}