JS Regex.find does not throw on invalid start index #KT-36082

This commit is contained in:
Abduqodiri Qurbonzoda
2020-01-22 19:33:32 +03:00
parent c592201a43
commit ddcc3b39f7
2 changed files with 12 additions and 1 deletions
+7 -1
View File
@@ -70,9 +70,15 @@ public actual class Regex actual constructor(pattern: String, options: Set<Regex
*
* @param startIndex An index to start search with, by default 0. Must be not less than zero and not greater than `input.length()`
* @return An instance of [MatchResult] if match was found or `null` otherwise.
* @throws IndexOutOfBoundsException if [startIndex] is less than zero or greater than the length of the [input] char sequence.
*/
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun find(input: CharSequence, startIndex: Int = 0): MatchResult? = nativePattern.findNext(input.toString(), startIndex)
public actual fun find(input: CharSequence, startIndex: Int = 0): MatchResult? {
if (startIndex < 0 || startIndex > input.length) {
throw IndexOutOfBoundsException("Start index out of bounds: $startIndex, input length: ${input.length}")
}
return nativePattern.findNext(input.toString(), startIndex)
}
/**
* Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
+5
View File
@@ -48,6 +48,10 @@ class RegexTest {
val noMatch = last.next()
assertEquals(null, noMatch)
assertFailsWith<IndexOutOfBoundsException> { p.find(input, -1) }
assertFailsWith<IndexOutOfBoundsException> { p.find(input, input.length + 1) }
assertEquals(null, p.find(input, input.length))
}
@Test fun matchIgnoreCase() {
@@ -82,6 +86,7 @@ class RegexTest {
assertEquals(2, matches.size)
assertEquals("", pattern.findAll(input, input.length).single().value)
assertEquals("", pattern.find(input, input.length)?.value)
}
@Test fun matchGroups() {