Skip to content

Commit

Permalink
[regex] Add missing exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmat192 committed Nov 23, 2018
1 parent f8bbd5e commit aa0f0f1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
28 changes: 17 additions & 11 deletions backend.native/tests/harmony_regex/MatchResultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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))
}
}
2 changes: 1 addition & 1 deletion runtime/src/main/kotlin/kotlin/text/Regex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/main/kotlin/kotlin/text/regex/CharClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/main/kotlin/kotlin/text/regex/Pattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions runtime/src/main/kotlin/kotlin/text/regex/Quantifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit aa0f0f1

Please sign in to comment.