Yokan / rebuild
Course 1 8624561

VersionComparison.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/extensions/VersionComparison.kt

implementation106 lines
1package eu.friedcutlet.yokan.services.extensions23/**4 * Compares two software/source version strings according to Semantic Versioning principles5 * and upstream Mangayomi version comparison rules (commit ed4f411b).6 *7 * Precedence rules:8 * 1. Trims whitespace and strips leading 'v' or 'V'.9 * 2. Ignores build metadata (everything after '+').10 * 3. Splits main release numbers from pre-release identifiers (after '-').11 * 4. Compares main numeric segments numerically: 0.9.1 < 0.10.0 (not string or right-padded).12 * 5. Normalizes trailing zeroes: 0.9 == 0.9.0 == 0.9.0.0.13 * 6. Pre-release precedence: A release without a pre-release tag has higher precedence14 *    than one with a pre-release tag (1.0.0 > 1.0.0-beta.1).15 * 7. When both have pre-releases, compares dot-separated pre-release tokens numerically if both16 *    are digits, or lexicographically otherwise.17 */18object VersionComparison {19    private val LeadingV = Regex("^[vV]")20    private val AttachedPreRelease = Regex("""^(\d+(\.\d+)*)([a-zA-Z].*)$""")2122    /** True when [latest] is known and newer than [installed], i.e. an update is available. */23    fun isOutdated(installed: String, latest: String): Boolean =24        latest.isNotBlank() && compare(installed, latest) < 02526    fun compare(version1: String, version2: String): Int {27        val v1 = sanitize(version1)28        val v2 = sanitize(version2)2930        val (main1, pre1) = splitMainAndPreRelease(v1)31        val (main2, pre2) = splitMainAndPreRelease(v2)3233        val parts1 = main1.split('.').map { it.toLongOrNull() ?: 0L }34        val parts2 = main2.split('.').map { it.toLongOrNull() ?: 0L }3536        val maxLen = maxOf(parts1.size, parts2.size)37        for (i in 0 until maxLen) {38            val p1 = parts1.getOrElse(i) { 0L }39            val p2 = parts2.getOrElse(i) { 0L }40            val cmp = p1.compareTo(p2)41            if (cmp != 0) return cmp42        }4344        // Main versions are equal. Check pre-release precedence.45        // SemVer rule: a normal version has higher precedence than a pre-release.46        if (pre1 == null && pre2 != null) return 147        if (pre1 != null && pre2 == null) return -148        if (pre1 != null && pre2 != null) {49            val preCmp = comparePreRelease(pre1, pre2)50            if (preCmp != 0) return preCmp51        }5253        return 054    }5556    private fun sanitize(raw: String): String {57        return raw.trim()58            .replaceFirst(LeadingV, "")59            .split('+')60            .first() // Drop build metadata61            .trim()62    }6364    private fun splitMainAndPreRelease(version: String): Pair<String, String?> {65        val dashIndex = version.indexOf('-')66        return if (dashIndex != -1) {67            version.substring(0, dashIndex) to version.substring(dashIndex + 1)68        } else {69            // Check for attached letter suffix (e.g., 1.4b -> 1.4 and pre-release "b")70            val match = AttachedPreRelease.find(version)71            if (match != null) {72                match.groupValues[1] to match.groupValues[3]73            } else {74                version to null75            }76        }77    }7879    private fun comparePreRelease(pre1: String, pre2: String): Int {80        val tokens1 = pre1.split('.')81        val tokens2 = pre2.split('.')82        val maxTokens = maxOf(tokens1.size, tokens2.size)8384        for (i in 0 until maxTokens) {85            if (i >= tokens1.size) return -186            if (i >= tokens2.size) return 18788            val t1 = tokens1[i]89            val t2 = tokens2[i]9091            val num1 = t1.toLongOrNull()92            val num2 = t2.toLongOrNull()9394            val tokenCmp = when {95                num1 != null && num2 != null -> num1.compareTo(num2)96                num1 != null && num2 == null -> -1 // Numeric has lower precedence than alphanumeric97                num1 == null && num2 != null -> 198                else -> t1.compareTo(t2)99            }100101            if (tokenCmp != 0) return tokenCmp102        }103104        return 0105    }106}107

Search chapters, reference and source filenames.