mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-21 15:48:26 +00:00
Avoiding memory allocation since this is used in laarge sets
This commit is contained in:
parent
31669efc51
commit
7a209a852c
@ -17,9 +17,26 @@ class IP2Country private constructor(private val context: Context) {
|
|||||||
private val pathsBuiltEventReceiver: BroadcastReceiver
|
private val pathsBuiltEventReceiver: BroadcastReceiver
|
||||||
val countryNamesCache = mutableMapOf<String, String>()
|
val countryNamesCache = mutableMapOf<String, String>()
|
||||||
|
|
||||||
private fun Ipv4Int(ip:String) = ip.takeWhile { it != '/' }.split('.').foldIndexed(0L) { i, acc, s ->
|
private fun Ipv4Int(ip: String): Long {
|
||||||
val asInt = s.toLong()
|
var result = 0L
|
||||||
acc + (asInt shl (8 * (3-i)))
|
var currentValue = 0L
|
||||||
|
var octetIndex = 0
|
||||||
|
|
||||||
|
for (char in ip) {
|
||||||
|
if (char == '.' || char == '/') {
|
||||||
|
result = (result shl 8) or currentValue
|
||||||
|
currentValue = 0
|
||||||
|
octetIndex++
|
||||||
|
if (char == '/') break
|
||||||
|
} else {
|
||||||
|
currentValue = currentValue * 10 + (char - '0')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle the last octet
|
||||||
|
result = (result shl 8) or currentValue
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private val ipv4ToCountry by lazy {
|
private val ipv4ToCountry by lazy {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user