From 390d71ac8d312bebc8cfaca4d767e8c177d59e97 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Thu, 25 Jun 2015 15:20:23 +0300 Subject: [PATCH] JS/RTTI. Fix RegExpMatch --- js/js.libraries/src/core/regex.kt | 9 ++++----- js/js.libraries/src/core/regexp.kt | 6 +++++- js/js.libraries/test/core/RegExpTest.kt | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/js/js.libraries/src/core/regex.kt b/js/js.libraries/src/core/regex.kt index 95daa8c133a..0f8dac9f3e5 100644 --- a/js/js.libraries/src/core/regex.kt +++ b/js/js.libraries/src/core/regex.kt @@ -56,7 +56,7 @@ public class Regex(pattern: String, options: Set) { public fun matches(input: CharSequence): Boolean { nativePattern.reset() val match = nativePattern.exec(input.toString()) - return match != null && (match as RegExpMatch).index == 0 && nativePattern.lastIndex == input.length + return match != null && match.index == 0 && nativePattern.lastIndex == input.length } /** Indicates whether the regular expression can find at least one match in the specified [input]. */ @@ -191,8 +191,7 @@ private fun RegExp.findNext(input: String, from: Int): MatchResult? { this.lastIndex = from val match = exec(input) if (match == null) return null - val reMatch = match as RegExpMatch - val range = reMatch.index..lastIndex - 1 + val range = match.index..lastIndex - 1 return object : MatchResult { override val range: IntRange = range @@ -200,7 +199,7 @@ private fun RegExp.findNext(input: String, from: Int): MatchResult? { get() = match[0]!! override val groups: MatchGroupCollection = object : MatchGroupCollection { - override val size: Int get() = match.size + override val size: Int get() = match.length override fun isEmpty(): Boolean = size == 0 override fun contains(element: MatchGroup?): Boolean = this.any { it == element } @@ -227,4 +226,4 @@ private fun RegExp.findNext(input: String, from: Int): MatchResult? { override fun next(): MatchResult? = this@findNext.findNext(input, if (range.isEmpty()) range.start + 1 else range.endInclusive + 1) } -} \ No newline at end of file +} diff --git a/js/js.libraries/src/core/regexp.kt b/js/js.libraries/src/core/regexp.kt index 1fdfaa18001..fbd77f51701 100644 --- a/js/js.libraries/src/core/regexp.kt +++ b/js/js.libraries/src/core/regexp.kt @@ -21,7 +21,7 @@ package kotlin.text.js public fun test(str: String): Boolean = noImpl - public fun exec(str: String): Array? = noImpl + public fun exec(str: String): RegExpMatch? = noImpl public override fun toString(): String = noImpl @@ -43,4 +43,8 @@ public fun RegExp.reset() { @native public interface RegExpMatch { public val index: Int public val input: String + public val length: Int + + nativeGetter + public fun get(index: Int): String? } diff --git a/js/js.libraries/test/core/RegExpTest.kt b/js/js.libraries/test/core/RegExpTest.kt index 0e11d4deee4..d0a860d1a06 100644 --- a/js/js.libraries/test/core/RegExpTest.kt +++ b/js/js.libraries/test/core/RegExpTest.kt @@ -56,16 +56,16 @@ class RegExpTest { var re = RegExp("""(\w\d)(\w\d)""", "g") val m1: Array = re.exec(string)!! assertEquals(arrayOf("R2D2", "R2", "D2"), m1) - assertEquals(0, (m1 as RegExpMatch).index) + assertEquals(0, m1.index) assertEquals(4, re.lastIndex) val m2: Array = re.exec(string)!! assertEquals(arrayOf("A5D5", "A5", "D5"), m2) - assertEquals(string.indexOf(m2[0]!!), (m2 as RegExpMatch).index) + assertEquals(string.indexOf(m2[0]!!), m2.index) val noMatch = re.exec(string) assertEquals(null, noMatch) assertEquals(0, re.lastIndex) } -} \ No newline at end of file +}