JS Regex.find does not throw on invalid start index #KT-36082
This commit is contained in:
@@ -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()`
|
* @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.
|
* @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")
|
@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].
|
* Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
|
||||||
|
|||||||
@@ -48,6 +48,10 @@ class RegexTest {
|
|||||||
|
|
||||||
val noMatch = last.next()
|
val noMatch = last.next()
|
||||||
assertEquals(null, noMatch)
|
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() {
|
@Test fun matchIgnoreCase() {
|
||||||
@@ -82,6 +86,7 @@ class RegexTest {
|
|||||||
assertEquals(2, matches.size)
|
assertEquals(2, matches.size)
|
||||||
|
|
||||||
assertEquals("", pattern.findAll(input, input.length).single().value)
|
assertEquals("", pattern.findAll(input, input.length).single().value)
|
||||||
|
assertEquals("", pattern.find(input, input.length)?.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun matchGroups() {
|
@Test fun matchGroups() {
|
||||||
|
|||||||
Reference in New Issue
Block a user