Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
84 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}-*/; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters