Extract SemVersion.major, minor, patch

This commit is contained in:
Karlatemp 2020-09-20 22:48:54 +08:00
parent 7a8944b1d6
commit 62527f0ed0
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 28 additions and 21 deletions

View File

@ -69,8 +69,11 @@ internal object SemVersionInternal {
} }
} }
} }
val mainVersion = version.substring(0, mainVersionEnd).parseMainVersion()
return SemVersion( return SemVersion(
mainVersion = version.substring(0, mainVersionEnd).parseMainVersion(), major = mainVersion[0],
minor = mainVersion[1],
patch = mainVersion.getOrNull(2),
identifier = identifier, identifier = identifier,
metadata = metadata metadata = metadata
) )
@ -176,16 +179,12 @@ internal object SemVersionInternal {
// If $this equals $other (without metadata), // If $this equals $other (without metadata),
// return same. // 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 // 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)) source.major.compareTo(other.major).takeUnless { it == 0 }?.let { return it }
if (result != 0) return result 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. // If main-versions are same.
var identifier0 = source.identifier var identifier0 = source.identifier
var identifier1 = other.identifier var identifier1 = other.identifier

View File

@ -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( * SemVersion(
* mainVersion = IntArray [1, 0, 0], * major = 1,
* minor = 0,
* patch = 0,
* identifier = "M4" * identifier = "M4"
* metadata = "c25733b8" * metadata = "c25733b8"
* ) * )
@ -53,8 +56,12 @@ public data class SemVersion
* @see SemVersion.invoke 字符串解析 * @see SemVersion.invoke 字符串解析
*/ */
internal constructor( internal constructor(
/** 核心版本号, 由主版本号, 次版本号和修订号组成, 其中修订号不一定存在 */ /** 主版本号 */
public val mainVersion: IntArray, public val major: Int,
/** 次版本号 */
public val minor: Int,
/** 修订号 */
public val patch: Int?,
/** 先行版本号识别符 */ /** 先行版本号识别符 */
public val identifier: String? = null, public val identifier: String? = null,
/** 版本号元数据, 不参与版本号对比([compareTo]), 但是参与版本号严格对比([equals]) */ /** 版本号元数据, 不参与版本号对比([compareTo]), 但是参与版本号严格对比([equals]) */
@ -151,14 +158,14 @@ internal constructor(
@Transient @Transient
private val toString: String by lazy(LazyThreadSafetyMode.NONE) { private val toString: String by lazy(LazyThreadSafetyMode.NONE) {
buildString { buildString {
mainVersion.joinTo(this, ".") append(major)
append('.').append(minor)
patch?.let { append('.').append(it) }
identifier?.let { identifier -> identifier?.let { identifier ->
append('-') append('-').append(identifier)
append(identifier)
} }
metadata?.let { metadata -> metadata?.let { metadata ->
append('+') append('+').append(metadata)
append(metadata)
} }
} }
} }
@ -169,7 +176,7 @@ internal constructor(
* [SemVersion] 转为 Kotlin data class 风格的 [String] * [SemVersion] 转为 Kotlin data class 风格的 [String]
*/ */
public fun toStructuredString(): 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 { override fun equals(other: Any?): Boolean {
@ -182,7 +189,8 @@ internal constructor(
} }
override fun hashCode(): Int { 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 + (identifier?.hashCode() ?: 0)
result = 31 * result + (metadata?.hashCode() ?: 0) result = 31 * result + (metadata?.hashCode() ?: 0)
return result return result