package eu.friedcutlet.yokan.services.extensions /** * Compares two software/source version strings according to Semantic Versioning principles * and upstream Mangayomi version comparison rules (commit ed4f411b). * * Precedence rules: * 1. Trims whitespace and strips leading 'v' or 'V'. * 2. Ignores build metadata (everything after '+'). * 3. Splits main release numbers from pre-release identifiers (after '-'). * 4. Compares main numeric segments numerically: 0.9.1 < 0.10.0 (not string or right-padded). * 5. Normalizes trailing zeroes: 0.9 == 0.9.0 == 0.9.0.0. * 6. Pre-release precedence: A release without a pre-release tag has higher precedence * than one with a pre-release tag (1.0.0 > 1.0.0-beta.1). * 7. When both have pre-releases, compares dot-separated pre-release tokens numerically if both * are digits, or lexicographically otherwise. */ object VersionComparison { private val LeadingV = Regex("^[vV]") private val AttachedPreRelease = Regex("""^(\d+(\.\d+)*)([a-zA-Z].*)$""") /** True when [latest] is known and newer than [installed], i.e. an update is available. */ fun isOutdated(installed: String, latest: String): Boolean = latest.isNotBlank() && compare(installed, latest) < 0 fun compare(version1: String, version2: String): Int { val v1 = sanitize(version1) val v2 = sanitize(version2) val (main1, pre1) = splitMainAndPreRelease(v1) val (main2, pre2) = splitMainAndPreRelease(v2) val parts1 = main1.split('.').map { it.toLongOrNull() ?: 0L } val parts2 = main2.split('.').map { it.toLongOrNull() ?: 0L } val maxLen = maxOf(parts1.size, parts2.size) for (i in 0 until maxLen) { val p1 = parts1.getOrElse(i) { 0L } val p2 = parts2.getOrElse(i) { 0L } val cmp = p1.compareTo(p2) if (cmp != 0) return cmp } // Main versions are equal. Check pre-release precedence. // SemVer rule: a normal version has higher precedence than a pre-release. if (pre1 == null && pre2 != null) return 1 if (pre1 != null && pre2 == null) return -1 if (pre1 != null && pre2 != null) { val preCmp = comparePreRelease(pre1, pre2) if (preCmp != 0) return preCmp } return 0 } private fun sanitize(raw: String): String { return raw.trim() .replaceFirst(LeadingV, "") .split('+') .first() // Drop build metadata .trim() } private fun splitMainAndPreRelease(version: String): Pair { val dashIndex = version.indexOf('-') return if (dashIndex != -1) { version.substring(0, dashIndex) to version.substring(dashIndex + 1) } else { // Check for attached letter suffix (e.g., 1.4b -> 1.4 and pre-release "b") val match = AttachedPreRelease.find(version) if (match != null) { match.groupValues[1] to match.groupValues[3] } else { version to null } } } private fun comparePreRelease(pre1: String, pre2: String): Int { val tokens1 = pre1.split('.') val tokens2 = pre2.split('.') val maxTokens = maxOf(tokens1.size, tokens2.size) for (i in 0 until maxTokens) { if (i >= tokens1.size) return -1 if (i >= tokens2.size) return 1 val t1 = tokens1[i] val t2 = tokens2[i] val num1 = t1.toLongOrNull() val num2 = t2.toLongOrNull() val tokenCmp = when { num1 != null && num2 != null -> num1.compareTo(num2) num1 != null && num2 == null -> -1 // Numeric has lower precedence than alphanumeric num1 == null && num2 != null -> 1 else -> t1.compareTo(t2) } if (tokenCmp != 0) return tokenCmp } return 0 } }