JS/RTTI. Fix RegExpMatch
This commit is contained in:
committed by
Alexey Andreev
parent
fcd941ad49
commit
390d71ac8d
@@ -56,7 +56,7 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
|
|||||||
public fun matches(input: CharSequence): Boolean {
|
public fun matches(input: CharSequence): Boolean {
|
||||||
nativePattern.reset()
|
nativePattern.reset()
|
||||||
val match = nativePattern.exec(input.toString())
|
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]. */
|
/** 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
|
this.lastIndex = from
|
||||||
val match = exec(input)
|
val match = exec(input)
|
||||||
if (match == null) return null
|
if (match == null) return null
|
||||||
val reMatch = match as RegExpMatch
|
val range = match.index..lastIndex - 1
|
||||||
val range = reMatch.index..lastIndex - 1
|
|
||||||
|
|
||||||
return object : MatchResult {
|
return object : MatchResult {
|
||||||
override val range: IntRange = range
|
override val range: IntRange = range
|
||||||
@@ -200,7 +199,7 @@ private fun RegExp.findNext(input: String, from: Int): MatchResult? {
|
|||||||
get() = match[0]!!
|
get() = match[0]!!
|
||||||
|
|
||||||
override val groups: MatchGroupCollection = object : MatchGroupCollection {
|
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 isEmpty(): Boolean = size == 0
|
||||||
|
|
||||||
override fun contains(element: MatchGroup?): Boolean = this.any { it == element }
|
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)
|
override fun next(): MatchResult? = this@findNext.findNext(input, if (range.isEmpty()) range.start + 1 else range.endInclusive + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ package kotlin.text.js
|
|||||||
|
|
||||||
public fun test(str: String): Boolean = noImpl
|
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
|
public override fun toString(): String = noImpl
|
||||||
|
|
||||||
@@ -43,4 +43,8 @@ public fun RegExp.reset() {
|
|||||||
@native public interface RegExpMatch {
|
@native public interface RegExpMatch {
|
||||||
public val index: Int
|
public val index: Int
|
||||||
public val input: String
|
public val input: String
|
||||||
|
public val length: Int
|
||||||
|
|
||||||
|
nativeGetter
|
||||||
|
public fun get(index: Int): String?
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,16 +56,16 @@ class RegExpTest {
|
|||||||
var re = RegExp("""(\w\d)(\w\d)""", "g")
|
var re = RegExp("""(\w\d)(\w\d)""", "g")
|
||||||
val m1: Array<out String?> = re.exec(string)!!
|
val m1: Array<out String?> = re.exec(string)!!
|
||||||
assertEquals(arrayOf("R2D2", "R2", "D2"), m1)
|
assertEquals(arrayOf("R2D2", "R2", "D2"), m1)
|
||||||
assertEquals(0, (m1 as RegExpMatch).index)
|
assertEquals(0, m1.index)
|
||||||
assertEquals(4, re.lastIndex)
|
assertEquals(4, re.lastIndex)
|
||||||
|
|
||||||
val m2: Array<out String?> = re.exec(string)!!
|
val m2: Array<out String?> = re.exec(string)!!
|
||||||
assertEquals(arrayOf("A5D5", "A5", "D5"), m2)
|
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)
|
val noMatch = re.exec(string)
|
||||||
assertEquals(null, noMatch)
|
assertEquals(null, noMatch)
|
||||||
assertEquals(0, re.lastIndex)
|
assertEquals(0, re.lastIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user