|
23 | 23 | import java.util.Objects; |
24 | 24 | import java.util.function.Consumer; |
25 | 25 |
|
26 | | -import org.apache.commons.lang3.CharUtils; |
27 | 26 | import org.apache.commons.lang3.StringUtils; |
28 | 27 | import org.apache.commons.lang3.Validate; |
29 | 28 |
|
@@ -109,7 +108,7 @@ private static <T> boolean accept(final Consumer<T> consumer, final T obj) { |
109 | 108 | try { |
110 | 109 | consumer.accept(obj); |
111 | 110 | return true; |
112 | | - } catch (Exception e) { |
| 111 | + } catch (final Exception e) { |
113 | 112 | return false; |
114 | 113 | } |
115 | 114 | } |
@@ -584,106 +583,12 @@ public static boolean isCreatable(final String str) { |
584 | 583 | if (StringUtils.isEmpty(str)) { |
585 | 584 | return false; |
586 | 585 | } |
587 | | - final char[] chars = str.toCharArray(); |
588 | | - int sz = chars.length; |
589 | | - boolean hasExp = false; |
590 | | - boolean hasDecPoint = false; |
591 | | - boolean allowSigns = false; |
592 | | - boolean foundDigit = false; |
593 | | - // deal with any possible sign up front |
594 | | - final int start = isSign(chars[0]) ? 1 : 0; |
595 | | - if (sz > start + 1 && chars[start] == '0' && !StringUtils.contains(str, '.')) { // leading 0, skip if is a decimal number |
596 | | - if (chars[start + 1] == 'x' || chars[start + 1] == 'X') { // leading 0x/0X |
597 | | - int i = start + 2; |
598 | | - if (i == sz) { |
599 | | - return false; // str == "0x" |
600 | | - } |
601 | | - // checking hex (it can't be anything else) |
602 | | - for (; i < chars.length; i++) { |
603 | | - if (!CharUtils.isHex(chars[i])) { |
604 | | - return false; |
605 | | - } |
606 | | - } |
607 | | - return true; |
608 | | - } |
609 | | - if (Character.isDigit(chars[start + 1])) { |
610 | | - // leading 0, but not hex, must be octal |
611 | | - int i = start + 1; |
612 | | - for (; i < chars.length; i++) { |
613 | | - if (!CharUtils.isOctal(chars[i])) { |
614 | | - return false; |
615 | | - } |
616 | | - } |
617 | | - return true; |
618 | | - } |
619 | | - } |
620 | | - sz--; // don't want to loop to the last char, check it afterwards |
621 | | - // for type qualifiers |
622 | | - int i = start; |
623 | | - // loop to the next to last char or to the last char if we need another digit to |
624 | | - // make a valid number (e.g. chars[0..5] = "1234E") |
625 | | - while (i < sz || i < sz + 1 && allowSigns && !foundDigit) { |
626 | | - if (CharUtils.isAsciiNumeric(chars[i])) { |
627 | | - foundDigit = true; |
628 | | - allowSigns = false; |
629 | | - } else if (chars[i] == '.') { |
630 | | - if (hasDecPoint || hasExp) { |
631 | | - // two decimal points or dec in exponent |
632 | | - return false; |
633 | | - } |
634 | | - hasDecPoint = true; |
635 | | - } else if (chars[i] == 'e' || chars[i] == 'E') { |
636 | | - // we've already taken care of hex. |
637 | | - if (hasExp) { |
638 | | - // two E's |
639 | | - return false; |
640 | | - } |
641 | | - if (!foundDigit) { |
642 | | - return false; |
643 | | - } |
644 | | - hasExp = true; |
645 | | - allowSigns = true; |
646 | | - } else if (isSign(chars[i])) { |
647 | | - if (!allowSigns) { |
648 | | - return false; |
649 | | - } |
650 | | - allowSigns = false; |
651 | | - foundDigit = false; // we need a digit after the E |
652 | | - } else { |
653 | | - return false; |
654 | | - } |
655 | | - i++; |
656 | | - } |
657 | | - if (i < chars.length) { |
658 | | - if (CharUtils.isAsciiNumeric(chars[i])) { |
659 | | - // no type qualifier, OK |
660 | | - return true; |
661 | | - } |
662 | | - if (chars[i] == 'e' || chars[i] == 'E') { |
663 | | - // can't have an E at the last byte |
664 | | - return false; |
665 | | - } |
666 | | - if (chars[i] == '.') { |
667 | | - if (hasDecPoint || hasExp) { |
668 | | - // two decimal points or dec in exponent |
669 | | - return false; |
670 | | - } |
671 | | - // single trailing decimal point after non-exponent is ok |
672 | | - return foundDigit; |
673 | | - } |
674 | | - if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) { |
675 | | - return foundDigit; |
676 | | - } |
677 | | - if (chars[i] == 'l' || chars[i] == 'L') { |
678 | | - // not allowing L with an exponent or decimal point |
679 | | - return foundDigit && !hasExp && !hasDecPoint; |
680 | | - } |
681 | | - // last character is illegal |
| 586 | + try { |
| 587 | + createNumber(str); |
| 588 | + return true; |
| 589 | + } catch (final RuntimeException e) { |
682 | 590 | return false; |
683 | 591 | } |
684 | | - // allowSigns is true iff the val ends in 'E' |
685 | | - // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass |
686 | | - return !allowSigns && foundDigit; |
687 | 592 | } |
688 | 593 |
|
689 | 594 | /** |
|
0 commit comments