diff options
Diffstat (limited to 'src/main/kotlin/util/regex.kt')
-rw-r--r-- | src/main/kotlin/util/regex.kt | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/main/kotlin/util/regex.kt b/src/main/kotlin/util/regex.kt index 78c90e8..a44435c 100644 --- a/src/main/kotlin/util/regex.kt +++ b/src/main/kotlin/util/regex.kt @@ -1,8 +1,14 @@ +@file:OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) + package moe.nea.firmament.util import java.util.regex.Matcher import java.util.regex.Pattern import org.intellij.lang.annotations.Language +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.experimental.ExperimentalTypeInference import kotlin.time.Duration import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds @@ -10,10 +16,14 @@ import kotlin.time.Duration.Companion.seconds inline fun <T> String.ifMatches(regex: Regex, block: (MatchResult) -> T): T? = regex.matchEntire(this)?.let(block) -inline fun <T> Pattern.useMatch(string: String, block: Matcher.() -> T): T? = - matcher(string) +inline fun <T> Pattern.useMatch(string: String, block: Matcher.() -> T): T? { + contract { + callsInPlace(block, InvocationKind.AT_MOST_ONCE) + } + return matcher(string) .takeIf(Matcher::matches) ?.let(block) +} @Language("RegExp") val TIME_PATTERN = "[0-9]+[ms]" |