From aa0f0f169def3e59be05f42b5324ec90fe72308a Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Thu, 22 Nov 2018 13:11:07 +0700 Subject: [PATCH] [regex] Add missing exception messages --- .../tests/harmony_regex/MatchResultTest.kt | 28 +++++++++++-------- runtime/src/main/kotlin/kotlin/text/Regex.kt | 2 +- .../kotlin/kotlin/text/regex/CharClass.kt | 2 +- .../kotlin/text/regex/MatchResultImpl.kt | 2 +- .../main/kotlin/kotlin/text/regex/Pattern.kt | 2 +- .../kotlin/kotlin/text/regex/Quantifier.kt | 8 +++--- .../text/regex/sets/LeafQuantifierSet.kt | 2 +- 7 files changed, 26 insertions(+), 20 deletions(-) diff --git a/backend.native/tests/harmony_regex/MatchResultTest.kt b/backend.native/tests/harmony_regex/MatchResultTest.kt index 01af9e16946..46b070a8c97 100644 --- a/backend.native/tests/harmony_regex/MatchResultTest.kt +++ b/backend.native/tests/harmony_regex/MatchResultTest.kt @@ -435,6 +435,19 @@ class MatchResultTest { assertFalse(regex.containsMatchIn(str)) } + /** + * Regression test for HARMONY-3360 + */ + @Test fun testGeneralPunctuationCategory() { + val s = arrayOf(",", "!", "\"", "#", "%", "&", "'", "(", ")", "-", ".", "/") + val regexp = "\\p{P}" + + for (i in s.indices) { + val regex = Regex(regexp) + assertTrue(regex.containsMatchIn(s[i])) + } + } + /** * Regression test for https://github.com/JetBrains/kotlin-native/issues/2297 */ @@ -458,16 +471,9 @@ class MatchResultTest { assertTrue(Regex("[^a]").matches("b")) } - /** - * Regression test for HARMONY-3360 - */ - @Test fun testGeneralPunctuationCategory() { - val s = arrayOf(",", "!", "\"", "#", "%", "&", "'", "(", ")", "-", ".", "/") - val regexp = "\\p{P}" - - for (i in s.indices) { - val regex = Regex(regexp) - assertTrue(regex.containsMatchIn(s[i])) - } + @Test fun kt28158() { + val comment = "😃😃😃😃😃😃" + val regex = Regex("(.{3,})\\1+", RegexOption.IGNORE_CASE) + assertTrue(comment.contains(regex)) } } \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/text/Regex.kt b/runtime/src/main/kotlin/kotlin/text/Regex.kt index 46945a2f26a..97ed91d2209 100644 --- a/runtime/src/main/kotlin/kotlin/text/Regex.kt +++ b/runtime/src/main/kotlin/kotlin/text/Regex.kt @@ -157,7 +157,7 @@ public actual class Regex internal constructor(internal val nativePattern: Patte */ actual fun find(input: CharSequence, startIndex: Int): MatchResult? { if (startIndex < 0 || startIndex > input.length) { - throw IndexOutOfBoundsException() // TODO: Add a message. + throw IndexOutOfBoundsException("Start index out of bounds: $startIndex") } val matchResult = MatchResultImpl(input, this) matchResult.mode = Mode.FIND diff --git a/runtime/src/main/kotlin/kotlin/text/regex/CharClass.kt b/runtime/src/main/kotlin/kotlin/text/regex/CharClass.kt index 751f528ebaf..11328d457e0 100644 --- a/runtime/src/main/kotlin/kotlin/text/regex/CharClass.kt +++ b/runtime/src/main/kotlin/kotlin/text/regex/CharClass.kt @@ -213,7 +213,7 @@ internal class CharClass(val ignoreCase: Boolean = false, negative: Boolean = fa fun add(start: Int, end: Int): CharClass { if (start > end) - throw IllegalArgumentException() + throw IllegalArgumentException("Incorrect range of symbols (start > end)") val minSurrogate = Char.MIN_SURROGATE.toInt() val maxSurrogate = Char.MAX_SURROGATE.toInt() if (ignoreCase) { diff --git a/runtime/src/main/kotlin/kotlin/text/regex/MatchResultImpl.kt b/runtime/src/main/kotlin/kotlin/text/regex/MatchResultImpl.kt index ef58b6ede24..91d29ec75e3 100644 --- a/runtime/src/main/kotlin/kotlin/text/regex/MatchResultImpl.kt +++ b/runtime/src/main/kotlin/kotlin/text/regex/MatchResultImpl.kt @@ -218,7 +218,7 @@ constructor (internal val input: CharSequence, private fun checkGroup(group: Int) { if (group < 0 || group > groupCount) { - throw IndexOutOfBoundsException() + throw IndexOutOfBoundsException("Group index out of bounds: $group") } } diff --git a/runtime/src/main/kotlin/kotlin/text/regex/Pattern.kt b/runtime/src/main/kotlin/kotlin/text/regex/Pattern.kt index 7f0811be3a4..b56877ccc18 100644 --- a/runtime/src/main/kotlin/kotlin/text/regex/Pattern.kt +++ b/runtime/src/main/kotlin/kotlin/text/regex/Pattern.kt @@ -58,7 +58,7 @@ internal class Pattern(val pattern: String, flags: Int = 0) { /** Compiles the given pattern */ init { if (flags != 0 && flags or flagsBitMask != flagsBitMask) { - throw IllegalArgumentException() + throw IllegalArgumentException("Invalid match flags value") } startNode = processExpression(-1, this.flags, null) diff --git a/runtime/src/main/kotlin/kotlin/text/regex/Quantifier.kt b/runtime/src/main/kotlin/kotlin/text/regex/Quantifier.kt index 2bd57c93996..8532dcd51ba 100644 --- a/runtime/src/main/kotlin/kotlin/text/regex/Quantifier.kt +++ b/runtime/src/main/kotlin/kotlin/text/regex/Quantifier.kt @@ -26,17 +26,17 @@ import kotlin.IllegalArgumentException /** * Represents RE quantifier; contains two fields responsible for min and max number of repetitions. - * Negative value for maximum number of repetition represents infinity(i.e. +,*) + * -1 as a maximum number of repetition represents infinity(i.e. +,*). */ internal class Quantifier(val min: Int, val max: Int = min) : SpecialToken() { init { if (min < 0 || max < -1) { - throw IllegalArgumentException() + throw IllegalArgumentException("Incorrect quantifier value: $this") } } - override fun toString() = "{$min, ${if (max == -1) "" else max}}" + override fun toString() = "{$min, ${if (max == INF) "" else max}}" override val type: Type = SpecialToken.Type.QUANTIFIER @@ -51,7 +51,7 @@ internal class Quantifier(val min: Int, val max: Int = min) : SpecialToken() { Lexer.QUANT_STAR, Lexer.QUANT_STAR_P, Lexer.QUANT_STAR_R -> starQuantifier Lexer.QUANT_ALT, Lexer.QUANT_ALT_P, Lexer.QUANT_ALT_R -> altQuantifier Lexer.QUANT_PLUS, Lexer.QUANT_PLUS_P, Lexer.QUANT_PLUS_R -> plusQuantifier - else -> throw IllegalArgumentException() + else -> throw IllegalArgumentException("Unknown quantifier token: $token") } } } diff --git a/runtime/src/main/kotlin/kotlin/text/regex/sets/LeafQuantifierSet.kt b/runtime/src/main/kotlin/kotlin/text/regex/sets/LeafQuantifierSet.kt index 3e898982095..2aa103bc235 100644 --- a/runtime/src/main/kotlin/kotlin/text/regex/sets/LeafQuantifierSet.kt +++ b/runtime/src/main/kotlin/kotlin/text/regex/sets/LeafQuantifierSet.kt @@ -88,7 +88,7 @@ open internal class LeafQuantifierSet(var quantifier: Quantifier, get() = super.innerSet set(innerSet) { if (innerSet !is LeafSet) - throw RuntimeException() + throw RuntimeException("Internal Error") super.innerSet = innerSet } }