Add contains extension for mixed types for open ranges and open-closed range specializations

#KT-52932
This commit is contained in:
Ilya Gorbunov
2022-06-21 21:21:48 +03:00
committed by Space
parent 9574040f85
commit 8584fe2725
2 changed files with 212 additions and 2 deletions
@@ -176,7 +176,7 @@ object RangeOps : TemplateGroupBase() {
body { "return until(to)" }
}
val f_contains = fn("contains(value: Primitive)").byTwoPrimitives {
val f_containsMixedClosed = fn("contains(value: Primitive)").byTwoPrimitives {
include(Ranges, numericCombinations)
filter { _, (rangeType, itemType) -> rangeType != itemType }
} builderWith { (rangeType, itemType) ->
@@ -200,6 +200,40 @@ object RangeOps : TemplateGroupBase() {
}
}
val f_containsMixedOpenAndPrimitive = fn("contains(value: Primitive)").byTwoPrimitives {
include(OpenRanges, numericCombinations)
include(RangesOfPrimitives, numericCombinations.filter { (rangeType, _) -> rangeType in rangePrimitives })
filter { _, (rangeType, itemType) ->
rangeType != itemType && rangeType.isIntegral() == itemType.isIntegral() &&
rangeType != PrimitiveType.Float
}
} builderWith { (rangeType, itemType) ->
operator()
specialFor(OpenRanges) {
since("1.7")
annotation("@ExperimentalStdlibApi")
}
signature("contains(value: $itemType)")
check(rangeType.isNumeric() == itemType.isNumeric()) { "Required rangeType and itemType both to be numeric or both not, got: $rangeType, $itemType" }
platformName("${rangeType.name.decapitalize()}RangeContains")
returns("Boolean")
doc { "Checks if the specified [value] belongs to this range." }
body {
if (shouldCheckForConversionOverflow(fromType = itemType, toType = rangeType))
"return value.to${rangeType}ExactOrNull().let { if (it != null) contains(it) else false }"
else
"return contains(value.to$rangeType())"
}
specialFor(RangesOfPrimitives) {
inlineOnly()
body {
"return (this as ClosedRange<$rangeType>).contains(value)"
}
}
}
val f_contains_nullable = fn("contains(element: T?)") {
include(RangesOfPrimitives, rangePrimitives)
} builder {