Skip to content

Commit

Permalink
Add null checks to String methods.
Browse files Browse the repository at this point in the history
Also did some general cleanup and
fixed String tests to not be optimized by the compiler.

Change-Id: I503aebb5b60764e74406e408985bf57437e88498
  • Loading branch information
korzha committed Jun 23, 2016
1 parent b703afc commit fc52b53
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 151 deletions.
10 changes: 5 additions & 5 deletions user/super/com/google/gwt/emul/java/lang/Character.java
Expand Up @@ -230,7 +230,7 @@ public static boolean isBmpCodePoint(int codePoint) {
* TODO: correct Unicode handling.
*/
public static boolean isDigit(char c) {
return String.valueOf(c).nativeMatches(digitRegex());
return digitRegex().test(String.valueOf(c));
}

private static native NativeRegExp digitRegex() /*-{
Expand All @@ -245,7 +245,7 @@ public static boolean isHighSurrogate(char ch) {
* TODO: correct Unicode handling.
*/
public static boolean isLetter(char c) {
return String.valueOf(c).nativeMatches(leterRegex());
return leterRegex().test(String.valueOf(c));
}

private static native NativeRegExp leterRegex() /*-{
Expand All @@ -256,7 +256,7 @@ private static native NativeRegExp leterRegex() /*-{
* TODO: correct Unicode handling.
*/
public static boolean isLetterOrDigit(char c) {
return String.valueOf(c).nativeMatches(leterOrDigitRegex());
return leterOrDigitRegex().test(String.valueOf(c));
}

private static native NativeRegExp leterOrDigitRegex() /*-{
Expand Down Expand Up @@ -296,11 +296,11 @@ public static boolean isSpace(char c) {
}

public static boolean isWhitespace(char ch) {
return String.valueOf(ch).nativeMatches(whitespaceRegex());
return whitespaceRegex().test(String.valueOf(ch));
}

public static boolean isWhitespace(int codePoint) {
return String.fromCodePoint(codePoint).nativeMatches(whitespaceRegex());
return whitespaceRegex().test(String.fromCodePoint(codePoint));
}

// The regex would just be /\s/, but browsers handle non-breaking spaces inconsistently. Also,
Expand Down
57 changes: 22 additions & 35 deletions user/super/com/google/gwt/emul/java/lang/String.java
Expand Up @@ -210,13 +210,6 @@ private static String translateReplaceString(String replaceStr) {
return replaceStr;
}

private static native int compareTo(String thisStr, String otherStr) /*-{
if (thisStr == otherStr) {
return 0;
}
return thisStr < otherStr ? -1 : 1;
}-*/;

private static Charset getCharset(String charsetName) throws UnsupportedEncodingException {
try {
return Charset.forName(charsetName);
Expand Down Expand Up @@ -382,15 +375,15 @@ public int codePointCount(int beginIndex, int endIndex) {

@Override
public int compareTo(String other) {
return compareTo(this, other);
return JsUtils.compare(checkNotNull(this), checkNotNull(other));
}

public int compareToIgnoreCase(String other) {
return compareTo(toLowerCase(), other.toLowerCase());
return toLowerCase().compareTo(other.toLowerCase());
}

public String concat(String str) {
return this + str;
return checkNotNull(this) + checkNotNull(str);
}

public boolean contains(CharSequence s) {
Expand Down Expand Up @@ -421,18 +414,15 @@ public boolean equals(Object other) {
}

public boolean equalsIgnoreCase(String other) {
return equalsIgnoreCase(this, other);
}

private static native boolean equalsIgnoreCase(String s, String other) /*-{
checkNotNull(this);
if (other == null) {
return false;
}
if (s == other) {
if (equals(other)) {
return true;
}
return (s.length == other.length) && (s.toLowerCase() == other.toLowerCase());
}-*/;
return length() == other.length() && toLowerCase().equals(other.toLowerCase());
}

public byte[] getBytes() {
// default character set for GWT is UTF-8
Expand All @@ -450,7 +440,10 @@ public byte[] getBytes(Charset charset) {
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
checkCriticalStringBounds(srcBegin, srcEnd, length());
checkCriticalStringBounds(dstBegin, dstBegin + (srcEnd - srcBegin), dst.length);
getChars0(srcBegin, srcEnd, dst, dstBegin);
}

private void getChars0(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
while (srcBegin < srcEnd) {
dst[dstBegin++] = charAt(srcBegin++);
}
Expand All @@ -476,9 +469,9 @@ public int indexOf(String str) {
public int indexOf(String str, int startIndex) {
return asNativeString().indexOf(str, startIndex);
}

public String intern() {
return this;
return checkNotNull(this);
}

public boolean isEmpty() {
Expand Down Expand Up @@ -516,11 +509,7 @@ public int length() {
*/
public boolean matches(String regex) {
// We surround the regex with '^' and '$' because it must match the entire string.
return nativeMatches(new NativeRegExp("^(" + regex + ")$"));
}

boolean nativeMatches(NativeRegExp regex) {
return regex.test(this);
return new NativeRegExp("^(" + regex + ")$").test(this);
}

public int offsetByCodePoints(int index, int codePointOffset) {
Expand All @@ -529,13 +518,11 @@ public int offsetByCodePoints(int index, int codePointOffset) {

public boolean regionMatches(boolean ignoreCase, int toffset, String other,
int ooffset, int len) {
if (other == null) {
throw new NullPointerException();
}
checkNotNull(other);
if (toffset < 0 || ooffset < 0 || len <= 0) {
return false;
}
if (toffset + len > this.length() || ooffset + len > other.length()) {
if (toffset + len > length() || ooffset + len > other.length()) {
return false;
}

Expand Down Expand Up @@ -692,21 +679,21 @@ public boolean startsWith(String prefix, int toffset) {

@Override
public CharSequence subSequence(int beginIndex, int endIndex) {
return this.substring(beginIndex, endIndex);
return substring(beginIndex, endIndex);
}

public String substring(int beginIndex) {
return asNativeString().substr(beginIndex, this.length() - beginIndex);
return asNativeString().substr(beginIndex, length() - beginIndex);
}

public String substring(int beginIndex, int endIndex) {
return asNativeString().substr(beginIndex, endIndex - beginIndex);
}

public char[] toCharArray() {
int n = this.length();
int n = length();
char[] charArr = new char[n];
getChars(0, n, charArr, 0);
getChars0(0, n, charArr, 0);
return charArr;
}

Expand Down Expand Up @@ -833,15 +820,15 @@ private static class NativeString {
}

protected static String $create(String other) {
return other;
return checkNotNull(other);
}

protected static String $create(StringBuffer sb) {
return String.valueOf(sb);
return sb.toString();
}

protected static String $create(StringBuilder sb) {
return String.valueOf(sb);
return sb.toString();
}

@JsMethod
Expand Down
14 changes: 8 additions & 6 deletions user/super/com/google/gwt/emul/java/lang/StringBuffer.java
@@ -1,12 +1,12 @@
/*
* Copyright 2008 Google Inc.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Expand All @@ -15,9 +15,11 @@
*/
package java.lang;

import static javaemul.internal.InternalPreconditions.checkNotNull;

/**
* A fast way to create strings using multiple appends.
*
*
* This class is an exact clone of {@link StringBuilder} except for the name.
* Any change made to one should be mirrored in the other.
*/
Expand All @@ -28,7 +30,7 @@ public StringBuffer() {
}

public StringBuffer(CharSequence s) {
super(String.valueOf(s));
super(s.toString());
}

/**
Expand All @@ -41,7 +43,7 @@ public StringBuffer(int ignoredCapacity) {
}

public StringBuffer(String s) {
super(s);
super(checkNotNull(s));
}

public StringBuffer append(boolean x) {
Expand Down
14 changes: 8 additions & 6 deletions user/super/com/google/gwt/emul/java/lang/StringBuilder.java
@@ -1,12 +1,12 @@
/*
* Copyright 2008 Google Inc.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Expand All @@ -15,9 +15,11 @@
*/
package java.lang;

import static javaemul.internal.InternalPreconditions.checkNotNull;

/**
* A fast way to create strings using multiple appends.
*
*
* This class is an exact clone of {@link StringBuffer} except for the name.
* Any change made to one should be mirrored in the other.
*/
Expand All @@ -28,7 +30,7 @@ public StringBuilder() {
}

public StringBuilder(CharSequence s) {
super(String.valueOf(s));
super(s.toString());
}

/**
Expand All @@ -41,7 +43,7 @@ public StringBuilder(int ignoredCapacity) {
}

public StringBuilder(String s) {
super(s);
super(checkNotNull(s));
}

public StringBuilder append(boolean x) {
Expand Down
4 changes: 4 additions & 0 deletions user/super/com/google/gwt/emul/javaemul/internal/JsUtils.java
Expand Up @@ -20,6 +20,10 @@
*/
public class JsUtils {

public static native int compare(String a, String b) /*-{
return a == b ? 0 : (a < b ? -1 : 1);
}-*/;

public static native boolean isFinite(double d) /*-{
return isFinite(d);
}-*/;
Expand Down

0 comments on commit fc52b53

Please sign in to comment.