From eb2a976f322886dc7a48d8ca195963c42d0fe42d Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 18 Nov 2015 05:38:31 +0300 Subject: [PATCH] Minor: use EnumSet to hold options of regex. --- .../stdlib/src/kotlin/text/regex/RegexJVM.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt index a179d69af4e..50d3f9c7dbf 100644 --- a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt +++ b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt @@ -16,8 +16,9 @@ package kotlin.text -import java.util.regex.Pattern +import java.util.* import java.util.regex.Matcher +import java.util.regex.Pattern private interface FlagEnum { public val value: Int @@ -25,8 +26,15 @@ private interface FlagEnum { } private fun Iterable.toInt(): Int = this.fold(0, { value, option -> value or option.value }) -private fun fromInt(value: Int, allValues: Array): Set = - allValues.filter({ value and it.mask == it.value }).toSet() +private inline fun fromInt(value: Int): Set where T : FlagEnum, T: Enum = + Collections.unmodifiableSet(EnumSet.allOf(T::class.java).apply { + // TODO: use removeAll with predicate + with (iterator()) { + while (hasNext()) + if (next().let { value and it.mask != it.value }) + remove() + } + }) /** * Provides enumeration values to use to set regular expression options. @@ -106,7 +114,7 @@ public class Regex internal constructor(private val nativePattern: Pattern) { get() = nativePattern.pattern() /** The set of options that were used to create this regular expression. */ - public val options: Set = fromInt(nativePattern.flags(), RegexOption.values()) + public val options: Set = fromInt(nativePattern.flags()) @Deprecated("To get the Matcher from java.util.regex.Pattern use toPattern to convert string to Pattern, or migrate to Regex API") public fun matcher(input: CharSequence): Matcher = nativePattern.matcher(input)