JS/RTTI. Fix RegExpMatch

This commit is contained in:
Alexey Tsvetkov
2015-06-25 15:20:23 +03:00
committed by Alexey Andreev
parent fcd941ad49
commit 390d71ac8d
3 changed files with 12 additions and 9 deletions
+4 -5
View File
@@ -56,7 +56,7 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
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)
}
}
}
+5 -1
View File
@@ -21,7 +21,7 @@ package kotlin.text.js
public fun test(str: String): Boolean = noImpl
public fun exec(str: String): Array<String?>? = 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?
}
+3 -3
View File
@@ -56,16 +56,16 @@ class RegExpTest {
var re = RegExp("""(\w\d)(\w\d)""", "g")
val m1: Array<out String?> = 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<out String?> = 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)
}
}
}