Update Math interval

This commit is contained in:
Karlatemp 2020-09-21 12:31:12 +08:00
parent 1c909ae752
commit 00781c215a
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 33 additions and 12 deletions

View File

@ -19,9 +19,8 @@ import kotlin.math.min
internal object SemVersionInternal {
private val directVersion = """^[0-9]+(\.[0-9]+)+(|[\-+].+)$""".toRegex()
private val versionSelect = """^[0-9]+(\.[0-9]+)*\.x$""".toRegex()
private val versionRange = """([0-9]+(\.[0-9]+)+(|[\-+].+))\s*\-\s*([0-9]+(\.[0-9]+)+(|[\-+].+))""".toRegex()
private val versionMathRange =
"""\[([0-9]+(\.[0-9]+)+(|[\-+].+))\s*\,\s*([0-9]+(\.[0-9]+)+(|[\-+].+))\]""".toRegex()
"""([\[\(])([0-9]+(\.[0-9]+)+(|[\-+].+))\s*\,\s*([0-9]+(\.[0-9]+)+(|[\-+].+))([\]\)])""".toRegex()
private val versionRule = """^((\>\=)|(\<\=)|(\=)|(\>)|(\<))\s*([0-9]+(\.[0-9]+)+(|[\-+].+))$""".toRegex()
private val SEM_VERSION_REGEX =
@ -95,17 +94,35 @@ internal object SemVersionInternal {
override fun test(version: SemVersion): Boolean = regex.matches(version.toString())
}
}
(versionRange.matchEntire(trimmed) ?: versionMathRange.matchEntire(trimmed))?.let { range ->
var start = SemVersion.invoke(range.groupValues[1])
var end = SemVersion.invoke(range.groupValues[4])
versionMathRange.matchEntire(trimmed)?.let { range ->
// 1 mode
// 2 first
// 5 sec
// 8 type
var typeStart = range.groupValues[1][0]
var typeEnd = range.groupValues[8][0]
var start = SemVersion.invoke(range.groupValues[2])
var end = SemVersion.invoke(range.groupValues[5])
if (start > end) {
val c = end
end = start
start = c
val x = typeEnd
typeEnd = typeStart
typeStart = x
}
val a: (SemVersion) -> Boolean = when (typeStart) {
'[', ']' -> ({ start <= it })
'(', ')' -> ({ start < it })
else -> throw AssertionError()
}
val b: (SemVersion) -> Boolean = when (typeEnd) {
'[', ']' -> ({ it <= end })
'(', ')' -> ({ it < end })
else -> throw AssertionError()
}
val compareRange = start..end
return object : SemVersion.Requirement {
override fun test(version: SemVersion): Boolean = version in compareRange
override fun test(version: SemVersion): Boolean = a(version) && b(version)
}
}
versionRule.matchEntire(trimmed)?.let { result ->

View File

@ -72,14 +72,17 @@ internal class TestSemVersion {
.assertFalse("2.1")
.assert("1.2.5").assert("1.2.0").assertFalse("1.2")
.assertFalse("1.0.0")
SemVersion.parseRangeRequirement("1.0.0 - 114.514.1919")
.assert("1.0.0")
.assert("114.514").assert("114.514.1919")
.assertFalse("0.0.1")
.assertFalse("4444.4444")
SemVersion.parseRangeRequirement("[1.0.0, 19190.0]")
.assert("1.0.0").assertFalse("0.1.0")
.assert("19190.0").assertFalse("19198.10")
SemVersion.parseRangeRequirement("[1.0.0, 2.0.0)")
.assert("1.0.0").assert("1.2.3").assertFalse("2.0.0")
SemVersion.parseRangeRequirement("(2.0.0, 1.0.0]")
.assert("1.0.0").assert("1.2.3").assertFalse("2.0.0")
SemVersion.parseRangeRequirement("(2.0.0, 1.0.0)")
.assertFalse("1.0.0").assert("1.2.3").assertFalse("2.0.0")
SemVersion.parseRangeRequirement("(1.0.0, 2.0.0)")
.assertFalse("1.0.0").assert("1.2.3").assertFalse("2.0.0")
SemVersion.parseRangeRequirement(" >= 1.0.0")
.assert("1.0.0")
.assert("114.514.1919")
@ -103,6 +106,7 @@ internal class TestSemVersion {
assertInvalid("{")
assertInvalid("}")
assertInvalid("")
assertInvalid("1.2.3 - 3.2.1")
assertInvalid("1.5.78 &&")
assertInvalid("|| 1.0.0")
}