1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.commons.util;
20
21 import java.util.ArrayList;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public final class StringUtils
37 {
38 private StringUtils()
39 {
40
41 }
42
43
44
45
46
47
48
49
50
51
52
53 public static boolean isFloatNoExponent(String str)
54 {
55 int len = str.length();
56 if (len == 0)
57 {
58 return false;
59 }
60
61
62 char c = str.charAt(0);
63 int i = ((c == '-') || (c == '+')) ? 1 : 0;
64
65
66 if (i >= len)
67 {
68 return false;
69 }
70
71 boolean decimalPointFound = false;
72
73 do
74 {
75 c = str.charAt(i);
76 if (c == '.')
77 {
78
79 if (decimalPointFound)
80 {
81 return false;
82 }
83
84 decimalPointFound = true;
85 }
86 else if (!Character.isDigit(c))
87 {
88 return false;
89 }
90
91 i++;
92 }
93 while (i < len);
94
95 return true;
96 }
97
98 public static boolean isFloatWithOptionalExponent(String str)
99 {
100 int len = str.length();
101 if (len == 0)
102 {
103 return false;
104 }
105
106
107 char c = str.charAt(0);
108 int i = ((c == '-') || (c == '+')) ? 1 : 0;
109
110
111 if (i >= len)
112 {
113 return false;
114 }
115
116 boolean exponentFound = false;
117 boolean decimalPointFound = false;
118
119 do
120 {
121 c = str.charAt(i);
122 switch (c)
123 {
124 case '.':
125
126
127 if (decimalPointFound || exponentFound)
128 {
129 return false;
130 }
131 decimalPointFound = true;
132
133 break;
134
135 case 'e':
136 case 'E':
137
138
139 if (exponentFound)
140 {
141 return false;
142 }
143 exponentFound = true;
144
145
146 c = str.charAt(i + 1);
147
148 if ((c == '-') || (c == '+'))
149 {
150 i++;
151 }
152
153 break;
154
155 default:
156 if (!Character.isDigit(c))
157 {
158 return false;
159 }
160 }
161
162 i++;
163 }
164 while (i < len);
165
166 return true;
167 }
168
169 public static boolean isInteger(String str)
170 {
171 int len = str.length();
172 if (len == 0)
173 {
174 return false;
175 }
176
177
178 char c = str.charAt(0);
179 int i = ((c == '-') || (c == '+')) ? 1 : 0;
180
181
182 if (i >= len)
183 {
184 return false;
185 }
186
187 do
188 {
189 if (!Character.isDigit(str.charAt(i)))
190 {
191 return false;
192 }
193 i++;
194 }
195 while (i < len);
196
197 return true;
198 }
199
200 public static boolean isUnsignedInteger(String str)
201 {
202 int len = str.length();
203 if (len == 0)
204 {
205 return false;
206 }
207
208 for (int i = 0; i < len; i++)
209 {
210 if (!Character.isDigit(str.charAt(i)))
211 {
212 return false;
213 }
214 }
215
216 return true;
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230 public static String dequote(String str, char quote)
231 {
232
233 if (str == null)
234 {
235 return null;
236 }
237
238 return dequote(str, 0, str.length(), quote);
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261 public static String dequote(String str, int begin, int end, char quote)
262 {
263
264 if (begin == end)
265 {
266 return "";
267 }
268
269 int end_ = str.indexOf(quote, begin);
270
271
272
273 if (end_ < 0)
274 {
275 return str.substring(begin, end);
276 }
277
278 StringBuffer sb = new StringBuffer(end - begin);
279 int begin_ = begin;
280 for (; (end_ >= 0) && (end_ < end);
281 end_ = str.indexOf(quote, begin_ = end_ + 2))
282 {
283 if (((end_ + 1) >= end) || (str.charAt(end_ + 1) != quote))
284 {
285 throw new IllegalArgumentException(
286 "Internal quote not doubled in string '"
287 + str.substring(begin, end) + "'");
288 }
289
290 sb.append(substring(str, begin_, end_)).append(quote);
291 }
292
293 return sb.append(substring(str, begin_, end)).toString();
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308 public static String dequoteFull(String str, char quote)
309 {
310 if (str == null)
311 {
312 return null;
313 }
314
315 return dequoteFull(str, 0, str.length(), quote);
316 }
317
318 public static String dequoteFull(String str, int begin, int end, char quote)
319 {
320
321 if (begin == end)
322 {
323 return "";
324 }
325
326
327 if (str.charAt(begin) != quote)
328 {
329 return str.substring(begin, end);
330 }
331
332 int _end = end - 1;
333 if ((str.length() < 2) || (str.charAt(_end) != quote))
334 {
335 throw new IllegalArgumentException(
336 "Closing quote missing in string '"
337 + substring(str, begin, end) + "'");
338 }
339
340 return dequote(str, begin + 1, _end, quote);
341 }
342
343 public static String replace(String str, String repl, String with)
344 {
345 int lastindex = 0;
346 int pos = str.indexOf(repl);
347
348
349
350 if (pos < 0)
351 {
352 return str;
353 }
354
355 int len = repl.length();
356 int lendiff = with.length() - repl.length();
357 StringBuffer out =
358 new StringBuffer((lendiff <= 0) ? str.length()
359 : (str.length() + (10 * lendiff)));
360 for (; pos >= 0; pos = str.indexOf(repl, lastindex = pos + len))
361 {
362 out.append(substring(str, lastindex, pos)).append(with);
363 }
364
365 return out.append(substring(str, lastindex, str.length())).toString();
366 }
367
368 public static String replace(String str, char repl, String with)
369 {
370 int pos = str.indexOf(repl);
371
372
373
374 if (pos < 0)
375 {
376 return str;
377 }
378
379 int len = str.length();
380 int lendiff = with.length() - 1;
381 StringBuffer out =
382 new StringBuffer((lendiff <= 0) ? str.length()
383 : (str.length() + (10 * lendiff)));
384 int lastindex = 0;
385 for (; pos >= 0; pos = str.indexOf(repl, lastindex = pos + 1))
386 {
387 out.append(substring(str, lastindex, pos)).append(with);
388 }
389
390 return out.append(substring(str, lastindex, len)).toString();
391 }
392
393 public static StringBuffer replace(
394 StringBuffer out, String s, String repl, String with)
395 {
396 int lastindex = 0;
397 int len = repl.length();
398 for (int index = s.indexOf(repl); index >= 0;
399 index = s.indexOf(repl, lastindex = index + len))
400 {
401
402 out.append(substring(s, lastindex, index)).append(with);
403 }
404
405 return out.append(substring(s, lastindex, len));
406 }
407
408
409
410
411
412
413
414
415
416
417 public static String[] splitLongString(String str, char separator)
418 {
419 int len;
420 if (str == null || (len = str.length()) == 0)
421 {
422 return ArrayUtils.EMPTY_STRING_ARRAY;
423 }
424
425 int oldPos = 0;
426 ArrayList list = new ArrayList();
427 for (
428 int pos = str.indexOf(separator); pos >= 0;
429 pos = str.indexOf(separator, (oldPos = (pos + 1))))
430 {
431 list.add(substring(str, oldPos, pos));
432 }
433
434 list.add(substring(str, oldPos, len));
435
436 return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 public static String[] splitLongString(
453 String str, char separator, char quote)
454 {
455 int len;
456 if (str == null || (len = str.length()) == 0)
457 {
458 return ArrayUtils.EMPTY_STRING_ARRAY;
459 }
460
461 int oldPos = 0;
462 ArrayList list = new ArrayList();
463 for (int pos = 0; pos < len; oldPos = ++pos)
464 {
465
466 while ((pos < len) && (str.charAt(pos) == quote))
467 {
468 pos = str.indexOf(quote, pos + 1) + 1;
469
470 if (pos == 0)
471 {
472 throw new IllegalArgumentException(
473 "Closing quote missing in string '" + str + "'");
474 }
475 }
476
477 boolean quoted;
478
479 if (pos != oldPos)
480 {
481 quoted = true;
482
483 if ((pos < len) && (str.charAt(pos) != separator))
484 {
485 throw new IllegalArgumentException(
486 "Separator must follow closing quote in string '"
487 + str + "'");
488 }
489 }
490 else
491 {
492 quoted = false;
493 pos = str.indexOf(separator, pos);
494 if (pos < 0)
495 {
496 pos = len;
497 }
498 }
499
500 list.add(
501 quoted ? dequote(str, oldPos + 1, pos - 1, quote)
502 : substring(str, oldPos, pos));
503 }
504
505 return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
506 }
507
508
509
510
511
512
513
514
515
516
517
518 public static String[] splitShortString(String str, char separator)
519 {
520 int len;
521 if (str == null || (len = str.length()) == 0)
522 {
523 return ArrayUtils.EMPTY_STRING_ARRAY;
524 }
525
526 int lastTokenIndex = 0;
527
528
529
530 for (int pos = str.indexOf(separator);
531 pos >= 0; pos = str.indexOf(separator, pos + 1))
532 {
533 lastTokenIndex++;
534 }
535
536
537 String[] list = new String[lastTokenIndex + 1];
538
539 int oldPos = 0;
540
541
542 for (
543 int pos = str.indexOf(separator), i = 0; pos >= 0;
544 pos = str.indexOf(separator, (oldPos = (pos + 1))))
545 {
546 list[i++] = substring(str, oldPos, pos);
547 }
548
549 list[lastTokenIndex] = substring(str, oldPos, len);
550
551 return list;
552 }
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568 public static String[] splitShortString(
569 String str, char separator, char quote)
570 {
571 int len;
572 if (str == null || (len = str.length()) == 0)
573 {
574 return ArrayUtils.EMPTY_STRING_ARRAY;
575 }
576
577
578
579 int tokenCount = 0;
580 for (int pos = 0; pos < len; pos++)
581 {
582 tokenCount++;
583
584 int oldPos = pos;
585
586
587 while ((pos < len) && (str.charAt(pos) == quote))
588 {
589 pos = str.indexOf(quote, pos + 1) + 1;
590
591
592 if (pos == 0)
593 {
594 throw new IllegalArgumentException(
595 "Closing quote missing in string '" + str + "'");
596 }
597 }
598
599 if (pos != oldPos)
600 {
601 if ((pos < len) && (str.charAt(pos) != separator))
602 {
603 throw new IllegalArgumentException(
604 "Separator must follow closing quote in strng '"
605 + str + "'");
606 }
607 }
608 else
609 {
610 pos = str.indexOf(separator, pos);
611 if (pos < 0)
612 {
613 break;
614 }
615 }
616 }
617
618
619 if (str.charAt(len - 1) == separator)
620 {
621 tokenCount++;
622 }
623
624
625 String[] list = new String[tokenCount];
626
627
628
629
630 tokenCount--;
631
632 int oldPos = 0;
633 for (int pos = 0, i = 0; i < tokenCount; i++, oldPos = ++pos)
634 {
635 boolean quoted;
636
637
638 while (str.charAt(pos) == quote)
639 {
640 pos = str.indexOf(quote, pos + 1) + 1;
641 }
642
643 if (pos != oldPos)
644 {
645 quoted = true;
646
647 if (str.charAt(pos) != separator)
648 {
649 throw new IllegalArgumentException(
650 "Separator must follow closing quote in strng '"
651 + str + "'");
652 }
653 }
654 else
655 {
656 quoted = false;
657 pos = str.indexOf(separator, pos);
658 }
659
660 list[i] =
661 quoted ? dequote(str, oldPos + 1, pos - 1, quote)
662 : substring(str, oldPos, pos);
663 }
664
665 list[tokenCount] = dequoteFull(str, oldPos, len, quote);
666
667 return list;
668 }
669
670 public static String substring(String str, int begin, int end)
671 {
672 if (begin == end)
673 {
674 return "";
675 }
676
677 return str.substring(begin, end);
678 }
679
680 public static String[] trim(String[] strings)
681 {
682 if (strings == null)
683 {
684 return null;
685 }
686
687 for (int i = 0, len = strings.length; i < len; i++)
688 {
689 strings[i] = strings[i].trim();
690 }
691
692 return strings;
693 }
694
695
696
697
698
699
700
701
702
703
704 public static int minIndex(int a, int b)
705 {
706 return (a < 0) ? b
707 : (b < 0) ? a
708 : (a < b) ? a : b;
709 }
710 }