From 9c2a0abda55f856d2d34e18157404b60e2850984 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 20 Sep 2020 14:09:48 +0800 Subject: [PATCH] Move implementations to SemVersionInternal --- .../internal/util/SemVersionInternal.kt | 49 +++++++++++++++++ .../mamoe/mirai/console/util/SemVersion.kt | 52 +------------------ 2 files changed, 51 insertions(+), 50 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 f880a8ecd..6d2da7ae5 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 @@ -32,6 +32,55 @@ internal object SemVersionInternal { } } + private val SEM_VERSION_REGEX = + """^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$""".toRegex() + + /** 解析核心版本号, eg: `1.0.0` -> IntArray[1, 0, 0] */ + @JvmStatic + private fun String.parseMainVersion(): IntArray = + split('.').map { it.toInt() }.toIntArray() + + + fun parse(version: String): SemVersion { + if (!SEM_VERSION_REGEX.matches(version)) { + throw IllegalArgumentException("`$version` not a valid version") + } + var mainVersionEnd = 0 + kotlin.run { + val iterator = version.iterator() + while (iterator.hasNext()) { + val next = iterator.next() + if (next == '-' || next == '+') { + break + } + mainVersionEnd++ + } + } + var identifier: String? = null + var metadata: String? = null + if (mainVersionEnd != version.length) { + when (version[mainVersionEnd]) { + '-' -> { + val metadataSplitter = version.indexOf('+', startIndex = mainVersionEnd) + if (metadataSplitter == -1) { + identifier = version.substring(mainVersionEnd + 1) + } else { + identifier = version.substring(mainVersionEnd + 1, metadataSplitter) + metadata = version.substring(metadataSplitter + 1) + } + } + '+' -> { + metadata = version.substring(mainVersionEnd + 1) + } + } + } + return SemVersion( + mainVersion = version.substring(0, mainVersionEnd).parseMainVersion(), + identifier = identifier, + metadata = metadata + ) + } + @JvmStatic private fun String.parseRule(): SemVersion.RangeRequirement { val trimmed = trim() 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 939fe7ea6..cc75c416e 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 @@ -68,14 +68,6 @@ public data class SemVersion internal constructor( ) public companion object { - private val SEM_VERSION_REGEX = - """^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$""".toRegex() - - /** 解析核心版本号, eg: `1.0.0` -> IntArray[1, 0, 0] */ - @JvmStatic - private fun String.parseMainVersion(): IntArray = - split('.').map { it.toInt() }.toIntArray() - /** * 解析一个版本号, 将会返回一个 [SemVersion], * 如果发生解析错误将会抛出一个 [IllegalArgumentException] 或者 [NumberFormatException] @@ -97,45 +89,7 @@ public data class SemVersion internal constructor( */ @Throws(IllegalArgumentException::class, NumberFormatException::class) @JvmStatic - public fun parse(version: String): SemVersion { - if (!SEM_VERSION_REGEX.matches(version)) { - throw IllegalArgumentException("`$version` not a valid version") - } - var mainVersionEnd = 0 - kotlin.run { - val iterator = version.iterator() - while (iterator.hasNext()) { - val next = iterator.next() - if (next == '-' || next == '+') { - break - } - mainVersionEnd++ - } - } - var identifier: String? = null - var metadata: String? = null - if (mainVersionEnd != version.length) { - when (version[mainVersionEnd]) { - '-' -> { - val metadataSplitter = version.indexOf('+', startIndex = mainVersionEnd) - if (metadataSplitter == -1) { - identifier = version.substring(mainVersionEnd + 1) - } else { - identifier = version.substring(mainVersionEnd + 1, metadataSplitter) - metadata = version.substring(metadataSplitter + 1) - } - } - '+' -> { - metadata = version.substring(mainVersionEnd + 1) - } - } - } - return SemVersion( - mainVersion = version.substring(0, mainVersionEnd).parseMainVersion(), - identifier = identifier, - metadata = metadata - ) - } + public fun parse(version: String): SemVersion = SemVersionInternal.parse(version) /** * 解析一条依赖需求描述, 在无法解析的时候抛出 [IllegalArgumentException] @@ -162,9 +116,7 @@ public data class SemVersion internal constructor( */ @Throws(IllegalArgumentException::class) @JvmStatic - public fun parseRangeRequirement(requirement: String): RangeRequirement { - return SemVersionInternal.parseRangeRequirement(requirement) - } + public fun parseRangeRequirement(requirement: String): RangeRequirement = SemVersionInternal.parseRangeRequirement(requirement) /** @see [RangeRequirement.check] */ @JvmStatic