From 50151dacb3414eb05e610feb8dc23b64ef6e4eaa Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Wed, 9 Oct 2024 15:42:39 +1030 Subject: [PATCH] Use std lib binarySearch --- .../thoughtcrime/securesms/util/IP2Country.kt | 27 +++---------------- 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/IP2Country.kt b/app/src/main/java/org/thoughtcrime/securesms/util/IP2Country.kt index 7ec2de4e4c..d6b71b3151 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/IP2Country.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/IP2Country.kt @@ -12,6 +12,7 @@ import org.session.libsignal.utilities.ThreadUtils import java.io.DataInputStream import java.io.InputStream import java.io.InputStreamReader +import kotlin.math.absoluteValue private fun ipv4Int(ip: String): UInt = ip.split(".", "/", ",").take(4).fold(0U) { acc, s -> acc shl 8 or s.toUInt() } @@ -91,8 +92,8 @@ class IP2Country internal constructor( countryNamesCache[ip]?.let { return it } val ipInt = ipv4Int(ip) - val index = ips.fuzzyBinarySearch(ipInt) - val code = index?.let { codes[it] } + val index = ips.binarySearch(ipInt).let { it.takeIf { it >= 0 } ?: (it.absoluteValue - 2) } + val code = codes.getOrNull(index) val bestMatchCountry = countryToNames[code] if (bestMatchCountry != null) countryNamesCache[ip] = bestMatchCountry @@ -115,25 +116,3 @@ class IP2Country internal constructor( } // endregion } - -@OptIn(ExperimentalUnsignedTypes::class) -private fun UIntArray.fuzzyBinarySearch(target: UInt): Int? { - if (isEmpty()) return null - - var low = 0 - var high = size - 1 - - while (low <= high) { - val mid = (low + high) / 2 - val midValue = this[mid] - - when { - midValue == target -> return mid // Exact match found - midValue < target -> low = mid + 1 // Search in the right half - else -> high = mid - 1 // Search in the left half - } - } - - // If no exact match, return the largest index with value <= target - return if (high >= 0) high else null -}