1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* 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 com.android.inputmethod.latin;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Locale;
public final class StringUtils {
public static final int CAPITALIZE_NONE = 0; // No caps, or mixed case
public static final int CAPITALIZE_FIRST = 1; // First only
public static final int CAPITALIZE_ALL = 2; // All caps
private StringUtils() {
// This utility class is not publicly instantiable.
}
public static int codePointCount(final String text) {
if (TextUtils.isEmpty(text)) return 0;
return text.codePointCount(0, text.length());
}
public static boolean containsInArray(final String key, final String[] array) {
for (final String element : array) {
if (key.equals(element)) return true;
}
return false;
}
public static boolean containsInCsv(final String key, final String csv) {
if (TextUtils.isEmpty(csv)) return false;
return containsInArray(key, csv.split(","));
}
public static String appendToCsvIfNotExists(final String key, final String csv) {
if (TextUtils.isEmpty(csv)) return key;
if (containsInCsv(key, csv)) return csv;
return csv + "," + key;
}
public static String removeFromCsvIfExists(final String key, final String csv) {
if (TextUtils.isEmpty(csv)) return "";
final String[] elements = csv.split(",");
if (!containsInArray(key, elements)) return csv;
final ArrayList<String> result = CollectionUtils.newArrayList(elements.length - 1);
for (final String element : elements) {
if (!key.equals(element)) result.add(element);
}
return TextUtils.join(",", result);
}
/**
* Find a string that start with specified prefix from an array.
*
* @param prefix a prefix string to find.
* @param array an string array to be searched.
* @return the rest part of the string that starts with the prefix.
* Returns null if it couldn't be found.
*/
public static String findPrefixedString(final String prefix, final String[] array) {
for (final String element : array) {
if (element.startsWith(prefix)) {
return element.substring(prefix.length());
}
}
return null;
}
/**
* Remove duplicates from an array of strings.
*
* This method will always keep the first occurrence of all strings at their position
* in the array, removing the subsequent ones.
*/
public static void removeDupes(final ArrayList<String> suggestions) {
if (suggestions.size() < 2) return;
int i = 1;
// Don't cache suggestions.size(), since we may be removing items
while (i < suggestions.size()) {
final String cur = suggestions.get(i);
// Compare each suggestion with each previous suggestion
for (int j = 0; j < i; j++) {
final String previous = suggestions.get(j);
if (TextUtils.equals(cur, previous)) {
suggestions.remove(i);
i--;
break;
}
}
i++;
}
}
public static String toTitleCase(final String s, final Locale locale) {
if (s.length() <= 1) {
// TODO: is this really correct? Shouldn't this be s.toUpperCase()?
return s;
}
// TODO: fix the bugs below
// - This does not work for Greek, because it returns upper case instead of title case.
// - It does not work for Serbian, because it fails to account for the "lj" character,
// which should be "Lj" in title case and "LJ" in upper case.
// - It does not work for Dutch, because it fails to account for the "ij" digraph, which
// are two different characters but both should be capitalized as "IJ" as if they were
// a single letter.
// - It also does not work with unicode surrogate code points.
return s.toUpperCase(locale).charAt(0) + s.substring(1);
}
private static final int[] EMPTY_CODEPOINTS = {};
public static int[] toCodePointArray(final String string) {
final int length = string.length();
if (length <= 0) {
return EMPTY_CODEPOINTS;
}
final int[] codePoints = new int[string.codePointCount(0, length)];
int destIndex = 0;
for (int index = 0; index < length; index = string.offsetByCodePoints(index, 1)) {
codePoints[destIndex] = string.codePointAt(index);
destIndex++;
}
return codePoints;
}
public static String[] parseCsvString(final String text) {
final int size = text.length();
if (size == 0) {
return null;
}
if (codePointCount(text) == 1) {
return text.codePointAt(0) == Constants.CSV_SEPARATOR ? null : new String[] { text };
}
ArrayList<String> list = null;
int start = 0;
for (int pos = 0; pos < size; pos++) {
final char c = text.charAt(pos);
if (c == Constants.CSV_SEPARATOR) {
// Skip empty entry.
if (pos - start > 0) {
if (list == null) {
list = CollectionUtils.newArrayList();
}
list.add(text.substring(start, pos));
}
// Skip comma
start = pos + 1;
} else if (c == Constants.CSV_ESCAPE) {
// Skip escape character and escaped character.
pos++;
}
}
final String remain = (size - start > 0) ? text.substring(start) : null;
if (list == null) {
return remain != null ? new String[] { remain } : null;
}
if (remain != null) {
list.add(remain);
}
return list.toArray(new String[list.size()]);
}
// This method assumes the text is not empty or null.
public static int getCapitalizationType(final String text) {
// If the first char is not uppercase, then the word is either all lower case or
// camel case, and in either case we return CAPITALIZE_NONE.
if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE;
final int len = text.length();
int capsCount = 1;
int letterCount = 1;
for (int i = 1; i < len; i = text.offsetByCodePoints(i, 1)) {
if (1 != capsCount && letterCount != capsCount) break;
final int codePoint = text.codePointAt(i);
if (Character.isUpperCase(codePoint)) {
++capsCount;
++letterCount;
} else if (Character.isLetter(codePoint)) {
// We need to discount non-letters since they may not be upper-case, but may
// still be part of a word (e.g. single quote or dash, as in "IT'S" or "FULL-TIME")
++letterCount;
}
}
// We know the first char is upper case. So we want to test if either every letter other
// than the first is lower case, or if they are all upper case. If the string is exactly
// one char long, then we will arrive here with letterCount 1, and this is correct, too.
if (1 == capsCount) return CAPITALIZE_FIRST;
return (letterCount == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE);
}
}
|