Fail fast in Regex.findAll on an invalid startIndex #KT-28356

(cherry picked from commit eb3cb7a2beac49aeff7a4c312f744d0f28d980b7)
This commit is contained in:
Abduqodiri Qurbonzoda
2020-01-21 06:46:30 +03:00
committed by Vasily Levchenko
parent 3b248ba51b
commit 1a15483951
+10 -3
View File
@@ -155,10 +155,11 @@ public actual class Regex internal constructor(internal val nativePattern: Patte
*
* @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.
*/
actual fun find(input: CharSequence, startIndex: Int): MatchResult? {
if (startIndex < 0 || startIndex > input.length) {
throw IndexOutOfBoundsException("Start index out of bounds: $startIndex")
throw IndexOutOfBoundsException("Start index is out of bounds: $startIndex, input length: ${input.length}")
}
val matchResult = MatchResultImpl(input, this)
matchResult.mode = Mode.FIND
@@ -174,9 +175,15 @@ public actual class Regex internal constructor(internal val nativePattern: Patte
/**
* 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.
*/
actual fun findAll(input: CharSequence, startIndex: Int): Sequence<MatchResult>
= generateSequence({ find(input, startIndex) }, MatchResult::next)
actual fun findAll(input: CharSequence, startIndex: Int): Sequence<MatchResult> {
if (startIndex < 0 || startIndex > input.length) {
throw IndexOutOfBoundsException("Start index is 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.