mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Move implementations to SemVersionInternal
This commit is contained in:
parent
7a533d4667
commit
9c2a0abda5
@ -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
|
@JvmStatic
|
||||||
private fun String.parseRule(): SemVersion.RangeRequirement {
|
private fun String.parseRule(): SemVersion.RangeRequirement {
|
||||||
val trimmed = trim()
|
val trimmed = trim()
|
||||||
|
@ -68,14 +68,6 @@ public data class SemVersion internal constructor(
|
|||||||
)
|
)
|
||||||
|
|
||||||
public companion object {
|
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],
|
* 解析一个版本号, 将会返回一个 [SemVersion],
|
||||||
* 如果发生解析错误将会抛出一个 [IllegalArgumentException] 或者 [NumberFormatException]
|
* 如果发生解析错误将会抛出一个 [IllegalArgumentException] 或者 [NumberFormatException]
|
||||||
@ -97,45 +89,7 @@ public data class SemVersion internal constructor(
|
|||||||
*/
|
*/
|
||||||
@Throws(IllegalArgumentException::class, NumberFormatException::class)
|
@Throws(IllegalArgumentException::class, NumberFormatException::class)
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
public fun parse(version: String): SemVersion {
|
public fun parse(version: String): SemVersion = SemVersionInternal.parse(version)
|
||||||
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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析一条依赖需求描述, 在无法解析的时候抛出 [IllegalArgumentException]
|
* 解析一条依赖需求描述, 在无法解析的时候抛出 [IllegalArgumentException]
|
||||||
@ -162,9 +116,7 @@ public data class SemVersion internal constructor(
|
|||||||
*/
|
*/
|
||||||
@Throws(IllegalArgumentException::class)
|
@Throws(IllegalArgumentException::class)
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
public fun parseRangeRequirement(requirement: String): RangeRequirement {
|
public fun parseRangeRequirement(requirement: String): RangeRequirement = SemVersionInternal.parseRangeRequirement(requirement)
|
||||||
return SemVersionInternal.parseRangeRequirement(requirement)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @see [RangeRequirement.check] */
|
/** @see [RangeRequirement.check] */
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
Loading…
Reference in New Issue
Block a user