Skip to content

Commit

Permalink
Move shared methods off of String.
Browse files Browse the repository at this point in the history
GWT's String class had a couple of shared methods that were
used in StringBuilder and Long. Moved these to a
StringHelper class.

Change-Id: I912746c2024a6528b8c362554e1093a1ca10a830
  • Loading branch information
dankurka authored and Gerrit Code Review committed Jul 20, 2015
1 parent 986d61a commit bdad33f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 55 deletions.
2 changes: 1 addition & 1 deletion dev/core/super/javaemul/internal/ArrayHelper.java
Expand Up @@ -60,7 +60,7 @@ private static void arraySplice(
}
}

private static native Object nativeArraySlice(Object arrayToSclice, int start, int end) /*-{
static native Object nativeArraySlice(Object arrayToSclice, int start, int end) /*-{
return arrayToSclice.slice(start, end);
}-*/;

Expand Down
63 changes: 63 additions & 0 deletions dev/core/super/javaemul/internal/StringHelper.java
@@ -0,0 +1,63 @@
/*
* Copyright 2015 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
* License for the specific language governing permissions and limitations under
* the License.
*/
package javaemul.internal;

/**
* StringHelper contains utility methods for String processing that are used by multiple classes
* in GWT's emul.
*/
public class StringHelper {
/**
* Checks that bounds are correct.
*
* @param legalCount the end of the legal range
* @param start must be >= 0
* @param end must be <= legalCount and must be >= start
* @throw StringIndexOutOfBoundsException if the range is not legal
* @skip
*/
public static void checkBounds(int legalCount, int start, int end) {
if (start < 0) {
throw new StringIndexOutOfBoundsException(start);
}
if (end < start) {
throw new StringIndexOutOfBoundsException(end - start);
}
if (end > legalCount) {
throw new StringIndexOutOfBoundsException(end);
}
}

public static String valueOf(char x[], int start, int end) {
// Work around function.prototype.apply call stack size limits:
// https://code.google.com/p/v8/issues/detail?id=2896
// Performance: http://jsperf.com/string-fromcharcode-test/13
int batchSize = ArrayHelper.ARRAY_PROCESS_BATCH_SIZE;
String s = "";
for (int batchStart = start; batchStart < end;) {
int batchEnd = Math.min(batchStart + batchSize, end);
Object slicedArray = ArrayHelper.nativeArraySlice(x, batchStart, batchEnd);
s += fromCharCode(slicedArray);
batchStart = batchEnd;
}
return s;
}

private static native String fromCharCode(Object array) /*-{
return String.fromCharCode.apply(null, array);
}-*/;
}

@@ -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,6 +15,8 @@
*/
package java.lang;

import javaemul.internal.StringHelper;

/**
* A base class to share implementation between {@link StringBuffer} and {@link StringBuilder}.
* <p>
Expand Down Expand Up @@ -61,8 +63,8 @@ public char charAt(int index) {
}

public void getChars(int srcStart, int srcEnd, char[] dst, int dstStart) {
String.__checkBounds(length(), srcStart, srcEnd);
String.__checkBounds(dst.length, dstStart, dstStart + (srcEnd - srcStart));
StringHelper.checkBounds(length(), srcStart, srcEnd);
StringHelper.checkBounds(dst.length, dstStart, dstStart + (srcEnd - srcStart));
while (srcStart < srcEnd) {
dst[dstStart++] = string.charAt(srcStart++);
}
Expand Down
16 changes: 9 additions & 7 deletions user/super/com/google/gwt/emul/java/lang/Long.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,6 +15,8 @@
*/
package java.lang;

import javaemul.internal.StringHelper;

/**
* Wraps a primitive <code>long</code> as an object.
*/
Expand Down Expand Up @@ -95,11 +97,11 @@ public static int numberOfTrailingZeros(long i) {
public static long parseLong(String s) throws NumberFormatException {
return parseLong(s, 10);
}

public static long parseLong(String s, int radix) throws NumberFormatException {
return __parseAndValidateLong(s, radix);
}

public static long reverse(long i) {
int high = (int) (i >>> 32);
int low = (int) i;
Expand Down Expand Up @@ -191,7 +193,7 @@ public static String toString(long value, int intRadix) {
buf[pos--] = Character.forDigit(-((int) value));
buf[pos] = '-';
}
return String.__valueOf(buf, pos, bufSize);
return StringHelper.valueOf(buf, pos, bufSize);
}

public static Long valueOf(long i) {
Expand Down Expand Up @@ -229,7 +231,7 @@ private static String toPowerOfTwoUnsignedString(long value, int shift) {
value >>>= shift;
} while (value != 0);

return String.__valueOf(buf, pos, bufSize);
return StringHelper.valueOf(buf, pos, bufSize);
}

private final transient long value;
Expand Down
46 changes: 4 additions & 42 deletions user/super/com/google/gwt/emul/java/lang/String.java
Expand Up @@ -21,8 +21,8 @@
import java.util.Comparator;
import java.util.Locale;

import javaemul.internal.ArrayHelper;
import javaemul.internal.HashCodes;
import javaemul.internal.StringHelper;
import javaemul.internal.annotations.DoNotInline;

/**
Expand Down Expand Up @@ -113,12 +113,12 @@ public static native String valueOf(char x) /*-{

public static String valueOf(char x[], int offset, int count) {
int end = offset + count;
__checkBounds(x.length, offset, end);
return __valueOf(x, offset, end);
StringHelper.checkBounds(x.length, offset, end);
return StringHelper.valueOf(x, offset, end);
}

public static String valueOf(char[] x) {
return __valueOf(x, 0, x.length);
return StringHelper.valueOf(x, 0, x.length);
}

public static String valueOf(double x) {
Expand All @@ -143,27 +143,6 @@ public static String valueOf(Object x) {

// CHECKSTYLE_OFF: This class has special needs.

/**
* Checks that bounds are correct.
*
* @param legalCount the end of the legal range
* @param start must be >= 0
* @param end must be <= legalCount and must be >= start
* @throw StringIndexOutOfBoundsException if the range is not legal
* @skip
*/
static void __checkBounds(int legalCount, int start, int end) {
if (start < 0) {
throw new StringIndexOutOfBoundsException(start);
}
if (end < start) {
throw new StringIndexOutOfBoundsException(end - start);
}
if (end > legalCount) {
throw new StringIndexOutOfBoundsException(end);
}
}

/**
* @skip
*/
Expand Down Expand Up @@ -195,23 +174,6 @@ static String __translateReplaceString(String replaceStr) {
return replaceStr;
}

static String __valueOf(char x[], int start, int end) {
return __valueOf(x, start, end, ArrayHelper.ARRAY_PROCESS_BATCH_SIZE);
}

private static native String __valueOf(char x[], int start, int end, int batchSize) /*-{
// Work around function.prototype.apply call stack size limits:
// https://code.google.com/p/v8/issues/detail?id=2896
// Performance: http://jsperf.com/string-fromcharcode-test/13
var s = "";
for (var batchStart = start; batchStart < end;) { // increment in block
var batchEnd = Math.min(batchStart + batchSize, end);
s += String.fromCharCode.apply(null, x.slice(batchStart, batchEnd));
batchStart = batchEnd;
}
return s;
}-*/;

/**
* @skip
*/
Expand Down

0 comments on commit bdad33f

Please sign in to comment.