Temporary workaround due to KT-7502.

Correct usage of joinToString.
Test for passing multiple options.
This commit is contained in:
Ilya Gorbunov
2015-04-18 01:31:26 +03:00
parent 81325208be
commit 260553d516
2 changed files with 11 additions and 3 deletions
+5 -3
View File
@@ -50,7 +50,7 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
public val pattern: String = pattern
/** The set of options that were used to create this regular expression. */
public val options: Set<RegexOption> = options.toSet()
private val nativePattern: RegExp = RegExp(pattern, options.map { it.value }.joinToString() + "g")
private val nativePattern: RegExp = RegExp(pattern, options.map { it.value }.joinToString(separator = "") + "g")
/** Indicates whether the regular expression matches the entire [input]. */
public fun matches(input: CharSequence): Boolean {
@@ -115,8 +115,10 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
*
* @param replacement A replacement expression that can include substitutions. See [Matcher.appendReplacement] for details.
*/
public fun replaceFirst(input: CharSequence, replacement: String): String =
input.toString().nativeReplace(RegExp(pattern, options.map { it.value }.joinToString()), replacement)
public fun replaceFirst(input: CharSequence, replacement: String): String {
val nonGlobalOptions = options.map { it.value }.joinToString(separator = "")
return input.toString().nativeReplace(RegExp(pattern, nonGlobalOptions), replacement)
}
/**
* Splits this string around matches of the given regular expression.
+6
View File
@@ -84,6 +84,12 @@ class RegexTest {
assertEquals("bye", m2.groups[2]?.value)
}
test fun matchMultiline() {
val regex = "^[a-z]*$".toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
val matchedValues = regex.matchAll("test\r\nLine").map { it.value }.toList()
assertEquals(listOf("test", "Line"), matchedValues)
}
test fun escapeLiteral() {
val literal = """[-\/\\^$*+?.()|[\]{}]"""
assertTrue(Regex.fromLiteral(literal).matches(literal))