From 1a15483951dc1c019d3aa737e927d68356a18b7e Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Tue, 21 Jan 2020 06:46:30 +0300 Subject: [PATCH] Fail fast in Regex.findAll on an invalid startIndex #KT-28356 (cherry picked from commit eb3cb7a2beac49aeff7a4c312f744d0f28d980b7) --- runtime/src/main/kotlin/kotlin/text/Regex.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/text/Regex.kt b/runtime/src/main/kotlin/kotlin/text/Regex.kt index 4356661ece6..423a3b33191 100644 --- a/runtime/src/main/kotlin/kotlin/text/Regex.kt +++ b/runtime/src/main/kotlin/kotlin/text/Regex.kt @@ -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 - = generateSequence({ find(input, startIndex) }, MatchResult::next) + actual fun findAll(input: CharSequence, startIndex: Int): Sequence { + 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.