Skip to content

Commit b10d869

Browse files
committed
indexOf*, split, startsWith, endsWith: JS implementation
1 parent d724ce3 commit b10d869

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

js/js.libraries/src/core/char.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ package kotlin.js
1818

1919
// actually \s is enough to match all whitespace, but \xA0 added because of different regexp behavior of Rhino used in Selenium tests
2020
public fun Char.isWhitespace(): Boolean = toString().matches("[\\s\\xA0]")
21+
22+
native public fun Char.toLowerCase(): Char = noImpl
23+
24+
native public fun Char.toUpperCase(): Char = noImpl

js/js.libraries/src/core/string.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
package kotlin.js
22

3-
native public fun String.startsWith(s: String): Boolean = noImpl
4-
native public fun String.endsWith(s: String): Boolean = noImpl
5-
native public fun String.contains(s: String): Boolean = noImpl
6-
7-
native public fun String.startsWith(char: Char): Boolean = noImpl
8-
native public fun String.endsWith(char: Char): Boolean = noImpl
9-
native public fun String.contains(char: Char): Boolean = noImpl
10-
113
native public fun String.toUpperCase() : String = noImpl
124

135
native public fun String.toLowerCase() : String = noImpl
146

15-
native public fun String.indexOf(str : String) : Int = noImpl
16-
native public fun String.indexOf(str : String, fromIndex : Int) : Int = noImpl
7+
native("indexOf")
8+
public fun String.nativeIndexOf(str : String, fromIndex : Int) : Int = noImpl
179

18-
native public fun String.lastIndexOf(str: String) : Int = noImpl
19-
native public fun String.lastIndexOf(str : String, fromIndex : Int) : Int = noImpl
10+
native("lastIndexOf")
11+
public fun String.nativeLastIndexOf(str : String, fromIndex : Int) : Int = noImpl
2012

2113
library("splitString")
22-
public fun String.split(regex: String): Array<String> = noImpl
14+
public fun String.splitByRegex(regex: String): Array<String> = noImpl
2315

2416
library("splitString")
25-
public fun String.split(regex: String, limit: Int): Array<String> = noImpl
17+
public fun String.splitByRegex(regex: String, limit: Int): Array<String> = noImpl
2618

2719
native public fun String.substring(beginIndex : Int) : String = noImpl
2820
native public fun String.substring(beginIndex : Int, endIndex : Int) : String = noImpl

js/js.libraries/src/core/stringsCode.kt

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
11
package kotlin.js
22

3-
public inline fun String.lastIndexOf(ch : Char, fromIndex : Int) : Int = lastIndexOf(ch.toString(), fromIndex)
4-
public inline fun String.lastIndexOf(ch: Char) : Int = lastIndexOf(ch.toString())
3+
public inline fun String.nativeIndexOf(ch : Char, fromIndex : Int) : Int = nativeIndexOf(ch.toString(), fromIndex)
4+
public inline fun String.nativeLastIndexOf(ch : Char, fromIndex : Int) : Int = nativeLastIndexOf(ch.toString(), fromIndex)
5+
6+
/**
7+
* Returns `true` if this string starts with the specified prefix.
8+
*/
9+
public fun String.startsWith(prefix: String, ignoreCase: Boolean = false): Boolean =
10+
regionMatches(0, prefix, 0, prefix.length(), ignoreCase)
11+
12+
/**
13+
* Returns `true` if a substring of this string starting at the specified offset [thisOffset] starts with the specified prefix.
14+
*/
15+
public fun String.startsWith(prefix: String, thisOffset: Int, ignoreCase: Boolean = false): Boolean =
16+
regionMatches(thisOffset, prefix, 0, prefix.length(), ignoreCase)
17+
18+
/**
19+
* Returns `true` if this string ends with the specified suffix.
20+
*/
21+
public fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean =
22+
regionMatches(length() - suffix.length(), suffix, 0, suffix.length(), ignoreCase = true)
23+
524

6-
public inline fun String.indexOf(ch : Char) : Int = indexOf(ch.toString())
7-
public inline fun String.indexOf(ch : Char, fromIndex : Int) : Int = indexOf(ch.toString(), fromIndex)
825

926
public inline fun String.matches(regex : String) : Boolean {
1027
val result = this.match(regex)
1128
return result != null && result.size() > 0
1229
}
1330

31+
public fun String.equals(anotherString: String, ignoreCase: Boolean = false): Boolean =
32+
if (!ignoreCase)
33+
this == anotherString
34+
else
35+
this.toLowerCase() == anotherString.toLowerCase()
36+
37+
38+
public fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean {
39+
if ((otherOffset < 0) || (thisOffset < 0) || (thisOffset > length() - length)
40+
|| (otherOffset > other.length() - length)) {
41+
return false;
42+
}
43+
44+
return substring(thisOffset, thisOffset + length).equals(other.substring(otherOffset, otherOffset + length), ignoreCase)
45+
}
46+
47+
1448
/**
1549
* Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this
1650
*

js/js.translator/testData/kotlin_lib.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@
1717
(function (Kotlin) {
1818
"use strict";
1919

20-
// Shims for String
21-
String.prototype.startsWith = function (s) {
22-
return this.indexOf(s) === 0;
23-
};
24-
25-
String.prototype.endsWith = function (s) {
26-
return this.indexOf(s, this.length - s.length) !== -1;
27-
};
28-
29-
String.prototype.contains = function (s) {
30-
return this.indexOf(s) !== -1;
31-
};
32-
3320
// Kotlin stdlib
3421

3522
Kotlin.equals = function (obj1, obj2) {

0 commit comments

Comments
 (0)