From 62527f0ed0ce4e2d676341c3bfd884eb9c98c72e Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sun, 20 Sep 2020 22:48:54 +0800 Subject: [PATCH] Extract SemVersion.major, minor, patch --- .../internal/util/SemVersionInternal.kt | 19 ++++++------ .../mamoe/mirai/console/util/SemVersion.kt | 30 ++++++++++++------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt index 02cec4490..2cbe56ec4 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt @@ -69,8 +69,11 @@ internal object SemVersionInternal { } } } + val mainVersion = version.substring(0, mainVersionEnd).parseMainVersion() return SemVersion( - mainVersion = version.substring(0, mainVersionEnd).parseMainVersion(), + major = mainVersion[0], + minor = mainVersion[1], + patch = mainVersion.getOrNull(2), identifier = identifier, metadata = metadata ) @@ -176,16 +179,12 @@ internal object SemVersionInternal { // If $this equals $other (without metadata), // return same. - if (other.mainVersion.contentEquals(source.mainVersion) && source.identifier == other.identifier) { - return 0 - } - fun IntArray.getSafe(index: Int) = getOrElse(index) { 0 } - // Compare main-version - for (index in 0 until (max(source.mainVersion.size, other.mainVersion.size))) { - val result = source.mainVersion.getSafe(index).compareTo(other.mainVersion.getSafe(index)) - if (result != 0) return result - } + + source.major.compareTo(other.major).takeUnless { it == 0 }?.let { return it } + source.minor.compareTo(other.minor).takeUnless { it == 0 }?.let { return it } + (source.patch ?: 0).compareTo(other.patch ?: 0).takeUnless { it == 0 }?.let { return it } + // If main-versions are same. var identifier0 = source.identifier var identifier1 = other.identifier diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt index 095280b90..bed586514 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt @@ -32,10 +32,13 @@ import net.mamoe.mirai.console.util.SemVersion.Requirement * * 解析示例: * - * `1.0.0-M4+c25733b8` 将会解析出三个内容, mainVersion (核心版本号), [identifier] (先行版本号) 和 [metadata] (元数据). + * `1.0.0-M4+c25733b8` 将会解析出下面的内容, + * [major] (主本号), [minor] (次版本号), [patch] (修订号), [identifier] (先行版本号) 和 [metadata] (元数据). * ``` * SemVersion( - * mainVersion = IntArray [1, 0, 0], + * major = 1, + * minor = 0, + * patch = 0, * identifier = "M4" * metadata = "c25733b8" * ) @@ -53,8 +56,12 @@ public data class SemVersion * @see SemVersion.invoke 字符串解析 */ internal constructor( - /** 核心版本号, 由主版本号, 次版本号和修订号组成, 其中修订号不一定存在 */ - public val mainVersion: IntArray, + /** 主版本号 */ + public val major: Int, + /** 次版本号 */ + public val minor: Int, + /** 修订号 */ + public val patch: Int?, /** 先行版本号识别符 */ public val identifier: String? = null, /** 版本号元数据, 不参与版本号对比([compareTo]), 但是参与版本号严格对比([equals]) */ @@ -151,14 +158,14 @@ internal constructor( @Transient private val toString: String by lazy(LazyThreadSafetyMode.NONE) { buildString { - mainVersion.joinTo(this, ".") + append(major) + append('.').append(minor) + patch?.let { append('.').append(it) } identifier?.let { identifier -> - append('-') - append(identifier) + append('-').append(identifier) } metadata?.let { metadata -> - append('+') - append(metadata) + append('+').append(metadata) } } } @@ -169,7 +176,7 @@ internal constructor( * 将 [SemVersion] 转为 Kotlin data class 风格的 [String] */ public fun toStructuredString(): String { - return "SemVersion(mainVersion=${mainVersion.contentToString()}, identifier=$identifier, metadata=$metadata)" + return "SemVersion(major=$major, minor=$minor, patch=$patch, identifier=$identifier, metadata=$metadata)" } override fun equals(other: Any?): Boolean { @@ -182,7 +189,8 @@ internal constructor( } override fun hashCode(): Int { - var result = mainVersion.contentHashCode() + var result = major shl minor + result *= (patch ?: 1) result = 31 * result + (identifier?.hashCode() ?: 0) result = 31 * result + (metadata?.hashCode() ?: 0) return result