Fail fast in Regex.findAll on an invalid startIndex #KT-28356
This commit is contained in:
@@ -77,11 +77,17 @@ public actual class Regex actual constructor(pattern: String, options: Set<Regex
|
|||||||
/**
|
/**
|
||||||
* 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].
|
||||||
*
|
*
|
||||||
|
* @throws IndexOutOfBoundsException if [startIndex] is less than zero or greater than the length of the [input] char sequence.
|
||||||
|
*
|
||||||
* @sample samples.text.Regexps.findAll
|
* @sample samples.text.Regexps.findAll
|
||||||
*/
|
*/
|
||||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||||
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> =
|
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> {
|
||||||
generateSequence({ find(input, startIndex) }, { match -> match.next() })
|
if (startIndex < 0 || startIndex > input.length) {
|
||||||
|
throw IndexOutOfBoundsException("Start index out of bounds: $startIndex, input length: ${input.length}")
|
||||||
|
}
|
||||||
|
return generateSequence({ find(input, startIndex) }, { match -> match.next() })
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to match the entire [input] CharSequence against the pattern.
|
* Attempts to match the entire [input] CharSequence against the pattern.
|
||||||
|
|||||||
@@ -122,11 +122,17 @@ internal constructor(private val nativePattern: Pattern) : Serializable {
|
|||||||
/**
|
/**
|
||||||
* 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].
|
||||||
*
|
*
|
||||||
|
* @throws IndexOutOfBoundsException if [startIndex] is less than zero or greater than the length of the [input] char sequence.
|
||||||
|
*
|
||||||
* @sample samples.text.Regexps.findAll
|
* @sample samples.text.Regexps.findAll
|
||||||
*/
|
*/
|
||||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||||
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> =
|
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> {
|
||||||
generateSequence({ find(input, startIndex) }, MatchResult::next)
|
if (startIndex < 0 || startIndex > input.length) {
|
||||||
|
throw IndexOutOfBoundsException("Start index out of bounds: $startIndex, input length: ${input.length}")
|
||||||
|
}
|
||||||
|
return generateSequence({ find(input, startIndex) }, MatchResult::next)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to match the entire [input] CharSequence against the pattern.
|
* Attempts to match the entire [input] CharSequence against the pattern.
|
||||||
|
|||||||
@@ -67,6 +67,10 @@ class RegexTest {
|
|||||||
assertEquals(expected.drop(1), pattern.findAll(input, startIndex = 3).map { it.value }.toList())
|
assertEquals(expected.drop(1), pattern.findAll(input, startIndex = 3).map { it.value }.toList())
|
||||||
|
|
||||||
assertEquals(listOf(0..2, 4..6, 8..10), matches.map { it.range }.toList())
|
assertEquals(listOf(0..2, 4..6, 8..10), matches.map { it.range }.toList())
|
||||||
|
|
||||||
|
assertFailsWith<IndexOutOfBoundsException> { pattern.findAll(input, -1) }
|
||||||
|
assertFailsWith<IndexOutOfBoundsException> { pattern.findAll(input, input.length + 1) }
|
||||||
|
assertEquals(emptyList(), pattern.findAll(input, input.length).toList())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun matchAllSequence() {
|
@Test fun matchAllSequence() {
|
||||||
@@ -76,6 +80,8 @@ class RegexTest {
|
|||||||
assertEquals(input, matches[0].value)
|
assertEquals(input, matches[0].value)
|
||||||
assertEquals(input, matches.joinToString("") { it.value })
|
assertEquals(input, matches.joinToString("") { it.value })
|
||||||
assertEquals(2, matches.size)
|
assertEquals(2, matches.size)
|
||||||
|
|
||||||
|
assertEquals("", pattern.findAll(input, input.length).single().value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun matchGroups() {
|
@Test fun matchGroups() {
|
||||||
|
|||||||
Reference in New Issue
Block a user