mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-20 06:28:27 +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
|
||||
val countryNamesCache = mutableMapOf<String, String>()
|
||||
|
||||
private fun Ipv4Int(ip:String) = ip.takeWhile { it != '/' }.split('.').foldIndexed(0L) { i, acc, s ->
|
||||
val asInt = s.toLong()
|
||||
acc + (asInt shl (8 * (3-i)))
|
||||
private fun Ipv4Int(ip: String): Long {
|
||||
var result = 0L
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user