1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import java.text.MessageFormat;
14 import java.util.Collection;
15
16 import org.eclipse.jgit.annotations.NonNull;
17 import org.eclipse.jgit.internal.JGitText;
18
19
20
21
22 public final class StringUtils {
23
24 private static final long KiB = 1024;
25
26 private static final long MiB = 1024 * KiB;
27
28 private static final long GiB = 1024 * MiB;
29
30 private static final char[] LC;
31
32 static {
33 LC = new char['Z' + 1];
34 for (char c = 0; c < LC.length; c++)
35 LC[c] = c;
36 for (char c = 'A'; c <= 'Z'; c++)
37 LC[c] = (char) ('a' + (c - 'A'));
38 }
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public static char toLowerCase(char c) {
53 return c <= 'Z' ? LC[c] : c;
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public static String toLowerCase(String in) {
70 final StringBuilder r = new StringBuilder(in.length());
71 for (int i = 0; i < in.length(); i++)
72 r.append(toLowerCase(in.charAt(i)));
73 return r.toString();
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public static String capitalize(String str) {
95 int strLen;
96 if (str == null || (strLen = str.length()) == 0) {
97 return str;
98 }
99 return new StringBuilder(strLen)
100 .append(Character.toTitleCase(str.charAt(0)))
101 .append(str.substring(1)).toString();
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static boolean equalsIgnoreCase(String a, String b) {
117 if (References.isSameObject(a, b)) {
118 return true;
119 }
120 if (a.length() != b.length())
121 return false;
122 for (int i = 0; i < a.length(); i++) {
123 if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
124 return false;
125 }
126 return true;
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public static int compareIgnoreCase(String a, String b) {
143 for (int i = 0; i < a.length() && i < b.length(); i++) {
144 int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
145 if (d != 0)
146 return d;
147 }
148 return a.length() - b.length();
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 public static int compareWithCase(String a, String b) {
165 for (int i = 0; i < a.length() && i < b.length(); i++) {
166 int d = a.charAt(i) - b.charAt(i);
167 if (d != 0)
168 return d;
169 }
170 return a.length() - b.length();
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public static boolean toBoolean(String stringValue) {
185 if (stringValue == null)
186 throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
187
188 final Boolean bool = toBooleanOrNull(stringValue);
189 if (bool == null)
190 throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
191
192 return bool.booleanValue();
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public static Boolean toBooleanOrNull(String stringValue) {
212 if (stringValue == null)
213 return null;
214
215 if (equalsIgnoreCase("yes", stringValue)
216 || equalsIgnoreCase("true", stringValue)
217 || equalsIgnoreCase("1", stringValue)
218 || equalsIgnoreCase("on", stringValue))
219 return Boolean.TRUE;
220 else if (equalsIgnoreCase("no", stringValue)
221 || equalsIgnoreCase("false", stringValue)
222 || equalsIgnoreCase("0", stringValue)
223 || equalsIgnoreCase("off", stringValue))
224 return Boolean.FALSE;
225 else
226 return null;
227 }
228
229
230
231
232
233
234
235
236
237
238 public static String join(Collection<String> parts, String separator) {
239 return StringUtils.join(parts, separator, separator);
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public static String join(Collection<String> parts, String separator,
256 String lastSeparator) {
257 StringBuilder sb = new StringBuilder();
258 int i = 0;
259 int lastIndex = parts.size() - 1;
260 for (String part : parts) {
261 sb.append(part);
262 if (i == lastIndex - 1) {
263 sb.append(lastSeparator);
264 } else if (i != lastIndex) {
265 sb.append(separator);
266 }
267 i++;
268 }
269 return sb.toString();
270 }
271
272 private StringUtils() {
273
274 }
275
276
277
278
279
280
281
282
283 public static boolean isEmptyOrNull(String stringValue) {
284 return stringValue == null || stringValue.length() == 0;
285 }
286
287
288
289
290
291
292
293
294
295 public static String replaceLineBreaksWithSpace(String in) {
296 char[] buf = new char[in.length()];
297 int o = 0;
298 for (int i = 0; i < buf.length; ++i) {
299 char ch = in.charAt(i);
300 switch (ch) {
301 case '\r':
302 if (i + 1 < buf.length && in.charAt(i + 1) == '\n') {
303 buf[o++] = ' ';
304 ++i;
305 } else
306 buf[o++] = ' ';
307 break;
308 case '\n':
309 buf[o++] = ' ';
310 break;
311 default:
312 buf[o++] = ch;
313 break;
314 }
315 }
316 return new String(buf, 0, o);
317 }
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339 public static long parseLongWithSuffix(@NonNull String value,
340 boolean positiveOnly)
341 throws NumberFormatException, StringIndexOutOfBoundsException {
342 String n = value.strip();
343 if (n.isEmpty()) {
344 throw new StringIndexOutOfBoundsException();
345 }
346 long mul = 1;
347 switch (n.charAt(n.length() - 1)) {
348 case 'g':
349 case 'G':
350 mul = GiB;
351 break;
352 case 'm':
353 case 'M':
354 mul = MiB;
355 break;
356 case 'k':
357 case 'K':
358 mul = KiB;
359 break;
360 default:
361 break;
362 }
363 if (mul > 1) {
364 n = n.substring(0, n.length() - 1).trim();
365 }
366 if (n.isEmpty()) {
367 throw new StringIndexOutOfBoundsException();
368 }
369 long number;
370 if (positiveOnly) {
371 number = Long.parseUnsignedLong(n);
372 if (number < 0) {
373 throw new NumberFormatException(
374 MessageFormat.format(JGitText.get().valueExceedsRange,
375 value, Long.class.getSimpleName()));
376 }
377 } else {
378 number = Long.parseLong(n);
379 }
380 if (mul == 1) {
381 return number;
382 }
383 try {
384 return Math.multiplyExact(mul, number);
385 } catch (ArithmeticException e) {
386 NumberFormatException nfe = new NumberFormatException(
387 e.getLocalizedMessage());
388 nfe.initCause(e);
389 throw nfe;
390 }
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413 public static int parseIntWithSuffix(@NonNull String value,
414 boolean positiveOnly)
415 throws NumberFormatException, StringIndexOutOfBoundsException {
416 try {
417 return Math.toIntExact(parseLongWithSuffix(value, positiveOnly));
418 } catch (ArithmeticException e) {
419 NumberFormatException nfe = new NumberFormatException(
420 MessageFormat.format(JGitText.get().valueExceedsRange,
421 value, Integer.class.getSimpleName()));
422 nfe.initCause(e);
423 throw nfe;
424 }
425 }
426
427
428
429
430
431
432
433
434
435
436
437 public static String formatWithSuffix(long value) {
438 if (value >= GiB && (value % GiB) == 0) {
439 return String.valueOf(value / GiB) + 'g';
440 }
441 if (value >= MiB && (value % MiB) == 0) {
442 return String.valueOf(value / MiB) + 'm';
443 }
444 if (value >= KiB && (value % KiB) == 0) {
445 return String.valueOf(value / KiB) + 'k';
446 }
447 return String.valueOf(value);
448 }
449 }