Skip to content

Commit cfb4e79

Browse files
authored
Use JS String includes/startsWith/endsWith (#10170)
Even when completely inlined, should produce smaller code than the previous emulation, and depending on the browser implementation this could perform slightly better as well.
1 parent 552c72e commit cfb4e79

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

user/super/com/google/gwt/emul/java/lang/String.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public String concat(String str) {
399399
}
400400

401401
public boolean contains(CharSequence s) {
402-
return indexOf(s.toString()) != -1;
402+
return asNativeString().includes(s.toString());
403403
}
404404

405405
public boolean contentEquals(CharSequence cs) {
@@ -411,9 +411,7 @@ public boolean contentEquals(StringBuffer sb) {
411411
}
412412

413413
public boolean endsWith(String suffix) {
414-
// If IE8 supported negative start index, we could have just used "-suffixlength".
415-
int suffixlength = suffix.length();
416-
return asNativeString().substr(length() - suffixlength, suffixlength).equals(suffix);
414+
return asNativeString().endsWith(suffix);
417415
}
418416

419417
// Marked with @DoNotInline because we don't have static eval for "==" yet.
@@ -691,11 +689,11 @@ public String[] split(String regex, int maxMatch) {
691689
}
692690

693691
public boolean startsWith(String prefix) {
694-
return startsWith(prefix, 0);
692+
return asNativeString().startsWith(prefix);
695693
}
696694

697695
public boolean startsWith(String prefix, int toffset) {
698-
return toffset >= 0 && asNativeString().substr(toffset, prefix.length()).equals(prefix);
696+
return asNativeString().startsWith(prefix, toffset);
699697
}
700698

701699
@Override
@@ -991,11 +989,16 @@ private static class NativeString {
991989
public static native String fromCharCode(char x);
992990
public int length;
993991
public native char charCodeAt(int index);
992+
public native boolean endsWith(String suffix);
994993
public native int indexOf(String str);
995994
public native int indexOf(String str, int startIndex);
995+
public native boolean includes(String str);
996+
public native boolean includes(String str, int startIndex);
996997
public native int lastIndexOf(String str);
997998
public native int lastIndexOf(String str, int start);
998999
public native String replace(NativeRegExp regex, String replace);
1000+
public native boolean startsWith(String prefix);
1001+
public native boolean startsWith(String prefix, int toffset);
9991002
public native String substr(int beginIndex);
10001003
public native String substr(int beginIndex, int len);
10011004
public native String toLocaleLowerCase();

0 commit comments

Comments
 (0)