forked from greenrobot/essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtils.java
284 lines (259 loc) · 9.72 KB
/
StringUtils.java
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (C) 2014 Markus Junginger, greenrobot (http://greenrobot.de)
*
* 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 de.greenrobot.common;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Utilities for working with strings, like splitting, url-encoding, and MD5 digests.
*
* @author markus
*/
public class StringUtils {
/** Splits a String based on a single character, which is usually faster than regex-based String.split(). */
public static String[] fastSplit(String string, char delimiter) {
List<String> list = new ArrayList<String>();
int size = string.length();
int start = 0;
for (int i = 0; i < size; i++) {
if (string.charAt(i) == delimiter) {
if (start < i) {
list.add(string.substring(start, i));
} else {
list.add("");
}
start = i + 1;
} else if (i == size - 1) {
list.add(string.substring(start, size));
}
}
String[] elements = new String[list.size()];
list.toArray(elements);
return elements;
}
/**
* URL-Encodes a given string using UTF-8 (some web pages have problems with UTF-8 and umlauts, consider
* {@link #encodeUrlIso(String)} also). No UnsupportedEncodingException to handle as it is dealt with in this
* method.
*/
public static String encodeUrl(String stringToEncode) {
try {
return URLEncoder.encode(stringToEncode, "UTF-8");
} catch (UnsupportedEncodingException e1) {
throw new RuntimeException(e1);
}
}
/**
* URL-encodes a given string using ISO-8859-1, which may work better with web pages and umlauts compared to UTF-8.
* No UnsupportedEncodingException to handle as it is dealt with in this method.
*/
public static String encodeUrlIso(String stringToEncode) {
try {
return URLEncoder.encode(stringToEncode, "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
throw new RuntimeException(e1);
}
}
/**
* URL-Decodes a given string using UTF-8. No UnsupportedEncodingException to handle as it is dealt with in this
* method.
*/
public static String decodeUrl(String stringToDecode) {
try {
return URLDecoder.decode(stringToDecode, "UTF-8");
} catch (UnsupportedEncodingException e1) {
throw new RuntimeException(e1);
}
}
/**
* URL-Decodes a given string using ISO-8859-1. No UnsupportedEncodingException to handle as it is dealt with in
* this method.
*/
public static String decodeUrlIso(String stringToDecode) {
try {
return URLDecoder.decode(stringToDecode, "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
throw new RuntimeException(e1);
}
}
/**
* Generates the MD5 digest for a given String based on UTF-8. The digest is padded with zeroes in the front if
* necessary.
*
* @return MD5 digest (32 characters).
*/
public static String generateMD5String(String stringToEncode) {
return generateDigestString(stringToEncode, "MD5", "UTF-8", 32);
}
/**
* Generates the SHA-1 digest for a given String based on UTF-8. The digest is padded with zeroes in the front if
* necessary. The SHA-1 algorithm is considers to produce less collisions than MD5.
*
* @return SHA-1 digest (40 characters).
*/
public static String generateSHA1String(String stringToEncode) {
return generateDigestString(stringToEncode, "SHA-1", "UTF-8", 40);
}
public static String
generateDigestString(String stringToEncode, String digestAlgo, String encoding, int lengthToPad) {
// Loosely inspired by http://workbench.cadenhead.org/news/1428/creating-md5-hashed-passwords-java
MessageDigest digester;
try {
digester = MessageDigest.getInstance(digestAlgo);
} catch (NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae);
}
try {
digester.update(stringToEncode.getBytes(encoding));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return toHexString(digester.digest(), lengthToPad);
}
public static String toHexString(byte[] bytes, int lengthToPad) {
BigInteger hash = new BigInteger(1, bytes);
String digest = hash.toString(16);
while (digest.length() < lengthToPad) {
digest = "0" + digest;
}
return digest;
}
/**
* Simple HTML/XML entity resolving: Only supports unicode enitities and a very limited number text represented
* entities (apos, quot, gt, lt, and amp). There are many more: http://www.w3.org/TR/REC-html40/sgml/dtd.html
*
* @param entity The entity name without & and ; (null throws NPE)
* @return Resolved entity or the entity itself if it could not be resolved.
*/
public static String resolveEntity(String entity) {
if (entity.length() > 1 && entity.charAt(0) == '#') {
if (entity.charAt(1) == 'x') {
return String.valueOf((char) Integer.parseInt(entity.substring(2), 16));
} else {
return String.valueOf((char) Integer.parseInt(entity.substring(1)));
}
} else if (entity.equals("apos")) {
return "'";
} else if (entity.equals("quot")) {
return "\"";
} else if (entity.equals("gt")) {
return ">";
} else if (entity.equals("lt")) {
return "<";
} else if (entity.equals("amp")) {
return "&";
} else {
return entity;
}
}
/**
* Cuts the string at the end if it's longer than maxLength and appends "..." to it. The length of the resulting
* string including "..." is always less or equal to the given maxLength. It's valid to pass a null text; in this
* case null is returned.
*/
public static String ellipsize(String text, int maxLength) {
if (text != null && text.length() > maxLength) {
return text.substring(0, maxLength - 3) + "...";
}
return text;
}
public static String[] splitLines(String text, boolean skipEmptyLines) {
if (skipEmptyLines) {
return text.split("[\n\r]+");
} else {
return text.split("\\r?\\n");
}
}
public static List<String> findLinesContaining(String text, String searchText) {
String[] splitLinesSkipEmpty = splitLines(text, true);
List<String> matching = new ArrayList<String>();
for (String line : splitLinesSkipEmpty) {
if (line.contains(searchText)) {
matching.add(line);
}
}
return matching;
}
/**
* Returns a concatenated string consisting of the given lines seperated by a new line character \n. The last line
* does not have a \n at the end.
*/
public static String concatLines(List<String> lines) {
StringBuilder builder = new StringBuilder();
int countMinus1 = lines.size() - 1;
for (int i = 0; i < countMinus1; i++) {
builder.append(lines.get(i)).append('\n');
}
if (!lines.isEmpty()) {
builder.append(lines.get(countMinus1));
}
return builder.toString();
}
public static String joinIterableOnComma(Iterable<?> iterable) {
if (iterable != null) {
StringBuilder buf = new StringBuilder();
Iterator<?> it = iterable.iterator();
while (it.hasNext()) {
buf.append(it.next());
if (it.hasNext()) {
buf.append(',');
}
}
return buf.toString();
} else {
return "";
}
}
public static String joinArrayOnComma(int[] array) {
if (array != null) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (i != 0) {
buf.append(',');
}
buf.append(array[i]);
}
return buf.toString();
} else {
return "";
}
}
public static String joinArrayOnComma(String[] array) {
if (array != null) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (i != 0) {
buf.append(',');
}
buf.append(array[i]);
}
return buf.toString();
} else {
return "";
}
}
// /** not really public yet, umlauts!? */
// public static boolean __isValidEmail(String email) {
// // TODO compile and cache pattern using a ThreadLocal
// return Pattern.matches("^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+", email);
// }
}