am f32b2d5c: Merge "Add a utility method to StringUtils."

* commit 'f32b2d5c5edc858d19a08785e04d2255d3c88091':
  Add a utility method to StringUtils.
main
Keisuke Kuroyanagi 2014-02-04 05:53:11 -08:00 committed by Android Git Automerger
commit b298ef29f2
3 changed files with 25 additions and 24 deletions

View File

@ -22,7 +22,6 @@ import android.util.SparseArray;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.makedict.Word;
import com.android.inputmethod.latin.settings.NativeSuggestOptions; import com.android.inputmethod.latin.settings.NativeSuggestOptions;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.JniUtils; import com.android.inputmethod.latin.utils.JniUtils;
@ -352,12 +351,7 @@ public final class BinaryDictionary extends Dictionary {
public GetNextWordPropertyResult getNextWordProperty(final int token) { public GetNextWordPropertyResult getNextWordProperty(final int token) {
final int[] codePoints = new int[MAX_WORD_LENGTH]; final int[] codePoints = new int[MAX_WORD_LENGTH];
final int nextToken = getNextWordNative(mNativeDict, token, codePoints); final int nextToken = getNextWordNative(mNativeDict, token, codePoints);
int len = 0; final String word = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints);
// codePoints is null-terminated if its length is shorter than the array length.
while (len < MAX_WORD_LENGTH && codePoints[len] != 0) {
++len;
}
final String word = new String(codePoints, 0, len);
return new GetNextWordPropertyResult(getWordProperty(word), nextToken); return new GetNextWordPropertyResult(getWordProperty(word), nextToken);
} }

View File

@ -46,7 +46,7 @@ public final class StringUtils {
public static String newSingleCodePointString(int codePoint) { public static String newSingleCodePointString(int codePoint) {
if (Character.charCount(codePoint) == 1) { if (Character.charCount(codePoint) == 1) {
// Optimization: avoid creating an temporary array for characters that are // Optimization: avoid creating a temporary array for characters that are
// represented by a single char value // represented by a single char value
return String.valueOf((char) codePoint); return String.valueOf((char) codePoint);
} }
@ -205,6 +205,24 @@ public final class StringUtils {
return codePoints; return codePoints;
} }
/**
* Construct a String from a code point array
*
* @param codePoints a code point array that is null terminated when its logical length is
* shorter than the array length.
* @return a string constructed from the code point array.
*/
public static String getStringFromNullTerminatedCodePointArray(final int[] codePoints) {
int stringLength = codePoints.length;
for (int i = 0; i < codePoints.length; i++) {
if (codePoints[i] == 0) {
stringLength = i;
break;
}
}
return new String(codePoints, 0 /* offset */, stringLength);
}
// This method assumes the text is not null. For the empty string, it returns CAPITALIZE_NONE. // This method assumes the text is not null. For the empty string, it returns CAPITALIZE_NONE.
public static int getCapitalizationType(final String text) { public static int getCapitalizationType(final String text) {
// If the first char is not uppercase, then the word is either all lower case or // If the first char is not uppercase, then the word is either all lower case or

View File

@ -61,15 +61,6 @@ public class WordProperty {
} }
} }
private static int getCodePointCount(final int[] codePoints) {
for (int i = 0; i < codePoints.length; i++) {
if (codePoints[i] == 0) {
return i;
}
}
return codePoints.length;
}
// This represents invalid word when the probability is BinaryDictionary.NOT_A_PROBABILITY. // This represents invalid word when the probability is BinaryDictionary.NOT_A_PROBABILITY.
public WordProperty(final int[] codePoints, final boolean isNotAWord, public WordProperty(final int[] codePoints, final boolean isNotAWord,
final boolean isBlacklisted, final boolean hasBigram, final boolean isBlacklisted, final boolean hasBigram,
@ -77,7 +68,7 @@ public class WordProperty {
final ArrayList<int[]> bigramTargets, final ArrayList<int[]> bigramProbabilityInfo, final ArrayList<int[]> bigramTargets, final ArrayList<int[]> bigramProbabilityInfo,
final ArrayList<int[]> shortcutTargets, final ArrayList<int[]> shortcutTargets,
final ArrayList<Integer> shortcutProbabilities) { final ArrayList<Integer> shortcutProbabilities) {
mCodePoints = new String(codePoints, 0 /* offset */, getCodePointCount(codePoints)); mCodePoints = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints);
mIsNotAWord = isNotAWord; mIsNotAWord = isNotAWord;
mIsBlacklisted = isBlacklisted; mIsBlacklisted = isBlacklisted;
mHasBigrams = hasBigram; mHasBigrams = hasBigram;
@ -86,9 +77,8 @@ public class WordProperty {
final int bigramTargetCount = bigramTargets.size(); final int bigramTargetCount = bigramTargets.size();
for (int i = 0; i < bigramTargetCount; i++) { for (int i = 0; i < bigramTargetCount; i++) {
final int[] bigramTargetCodePointArray = bigramTargets.get(i); final String bigramTargetString =
final String bigramTargetString = new String(bigramTargetCodePointArray, StringUtils.getStringFromNullTerminatedCodePointArray(bigramTargets.get(i));
0 /* offset */, getCodePointCount(bigramTargetCodePointArray));
final ProbabilityInfo bigramProbability = final ProbabilityInfo bigramProbability =
new ProbabilityInfo(bigramProbabilityInfo.get(i)); new ProbabilityInfo(bigramProbabilityInfo.get(i));
mBigramTargets.add( mBigramTargets.add(
@ -98,9 +88,8 @@ public class WordProperty {
final int shortcutTargetCount = shortcutTargets.size(); final int shortcutTargetCount = shortcutTargets.size();
for (int i = 0; i < shortcutTargetCount; i++) { for (int i = 0; i < shortcutTargetCount; i++) {
final int[] shortcutTargetCodePointArray = shortcutTargets.get(i); final String shortcutTargetString =
final String shortcutTargetString = new String(shortcutTargetCodePointArray, StringUtils.getStringFromNullTerminatedCodePointArray(shortcutTargets.get(i));
0 /* offset */, getCodePointCount(shortcutTargetCodePointArray));
mShortcutTargets.add( mShortcutTargets.add(
new WeightedString(shortcutTargetString, shortcutProbabilities.get(i))); new WeightedString(shortcutTargetString, shortcutProbabilities.get(i)));
} }