JS backend: fixed String.split for the case when using regexp.

Added(overload) String.split with limit parameter.
This commit is contained in:
Zalim Bashorov
2013-08-06 17:24:48 +04:00
parent 0341d875a9
commit 9b09d5f6df
4 changed files with 46 additions and 5 deletions
+6 -3
View File
@@ -3,15 +3,18 @@ package java.lang
import java.io.IOException
import js.library
library("splitString")
public fun String.split(regex : String) : Array<String> = js.noImpl
library("splitString")
public fun String.split(regex : String, limit: Int) : Array<String> = 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<String> = js.noImpl
library
public class IllegalArgumentException(message: String? = null) : Exception() {}
@@ -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();
}
}
@@ -0,0 +1,34 @@
package foo
// todo drop
class Pair<F, S>(val first: F, val second: S)
fun <F, S> F.to(second: S): Pair<F, S> = 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"
}
+2 -2
View File
@@ -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) {