From 9b09d5f6dfe9456c1aba3d278ae39ea77e82909b Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Tue, 6 Aug 2013 17:24:48 +0400 Subject: [PATCH] JS backend: fixed String.split for the case when using regexp. Added(overload) String.split with limit parameter. --- js/js.libraries/src/core/javalang.kt | 9 +++-- .../k2js/test/semantics/StringTest.java | 4 +++ .../expression/string/cases/stringSplit.kt | 34 +++++++++++++++++++ js/js.translator/testFiles/kotlin_lib.js | 4 +-- 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 js/js.translator/testFiles/expression/string/cases/stringSplit.kt diff --git a/js/js.libraries/src/core/javalang.kt b/js/js.libraries/src/core/javalang.kt index 13e781825f3..dbff2c6f5aa 100644 --- a/js/js.libraries/src/core/javalang.kt +++ b/js/js.libraries/src/core/javalang.kt @@ -3,15 +3,18 @@ package java.lang import java.io.IOException import js.library +library("splitString") +public fun String.split(regex : String) : Array = js.noImpl + +library("splitString") +public fun String.split(regex : String, limit: Int) : Array = js.noImpl + library open public class Exception(message: String? = null) : Throwable() {} library open public class RuntimeException(message: String? = null) : Exception(message) {} -library("splitString") -public fun String.split(regex : String) : Array = js.noImpl - library public class IllegalArgumentException(message: String? = null) : Exception() {} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java index f12ebc858f7..66cab1d42f7 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java @@ -80,4 +80,8 @@ public final class StringTest extends AbstractExpressionTest { assertFalse(filePath + " should not contain toString calls", text.contains("toString")); } } + + public void testStringSplit() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/testFiles/expression/string/cases/stringSplit.kt b/js/js.translator/testFiles/expression/string/cases/stringSplit.kt new file mode 100644 index 00000000000..5685dfe1b3d --- /dev/null +++ b/js/js.translator/testFiles/expression/string/cases/stringSplit.kt @@ -0,0 +1,34 @@ +package foo + +// todo drop +class Pair(val first: F, val second: S) +fun F.to(second: S): Pair = Pair(this, second) + +fun box(): String { + val testInput = "test data\t1foo 2 bar" + val tests = array( + " " to array("test", "data\t1foo", "", "2", "bar"), + "\\s+" to array("test", "data", "1foo", "2", "bar"), + "[sd]" to array("te", "t ", "ata\t1foo 2 bar"), + "[\\d]" to array("test data\t", "foo ", " bar") + ) + + for (test in tests) { + val regexp = test.first + val expected = test.second + val result = testInput.split(regexp) + + if (result != expected) return "Wrong result for '$regexp' -- Expected: $expected | Actual: $result" + } + + for (test in tests) { + val regexp = test.first + val limit = 2 + val expected = Array(limit) { test.second[it] } + val result = testInput.split(regexp, limit) + + if (result != expected) return "Wrong result for '$regexp' -- Expected: $expected | Actual: $result" + } + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index 597a89a6c71..2244398b899 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -482,8 +482,8 @@ String.prototype.contains = function (s) { } ); - Kotlin.splitString = function (str, regex) { - return str.split(regex); + Kotlin.splitString = function (str, regex, limit) { + return str.split(new RegExp(regex), limit); }; Kotlin.nullArray = function (size) {