Commit fa1a87ec authored by Mygod's avatar Mygod

Symmetrize

parent 56319ce0
......@@ -57,8 +57,6 @@ class Subnet(val address: InetAddress, val prefixSize: Int) : Comparable<Subnet>
}
}
fun Byte.toPositiveInt() = toInt() and 0xFF
fun matches(b: Immutable) = matches(b.a)
fun matches(b: ByteArray): Boolean {
if (a.size != b.size) return false
......@@ -67,14 +65,13 @@ class Subnet(val address: InetAddress, val prefixSize: Int) : Comparable<Subnet>
if (a[i] != b[i]) return false
++i
}
val mask = 256 - (1 shl i * 8 + 8 - prefixSize)
return i * 8 == prefixSize || a[i].toPositiveInt() == b[i].toPositiveInt() and mask
return i * 8 == prefixSize || a[i] == (b[i].toInt() and -(1 shl i * 8 + 8 - prefixSize)).toByte()
}
}
fun toImmutable() = Immutable(address.address.also {
var i = prefixSize / 8
if (prefixSize % 8 > 0) {
it[i] = (it[i].toInt() and 256 - (1 shl i * 8 + 8 - prefixSize)).toByte()
it[i] = (it[i].toInt() and -(1 shl i * 8 + 8 - prefixSize)).toByte()
++i
}
while (i < it.size) it[i++] = 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment