src/java.base/share/classes/java/util/Formatter.java

Print this page
rev 10977 : 8065070: (fmt) Avoid creating substrings when building FormatSpecifier
Reviewed-by: duke


2607     public enum BigDecimalLayoutForm {
2608         /**
2609          * Format the {@code BigDecimal} in computerized scientific notation.
2610          */
2611         SCIENTIFIC,
2612 
2613         /**
2614          * Format the {@code BigDecimal} as a decimal number.
2615          */
2616         DECIMAL_FLOAT
2617     };
2618 
2619     private class FormatSpecifier implements FormatString {
2620         private int index = -1;
2621         private Flags f = Flags.NONE;
2622         private int width;
2623         private int precision;
2624         private boolean dt = false;
2625         private char c;
2626 
2627         private int index(String s) {
2628             if (s != null) {
2629                 try {
2630                     index = Integer.parseInt(s.substring(0, s.length() - 1));

2631                 } catch (NumberFormatException x) {
2632                     assert(false);
2633                 }
2634             } else {
2635                 index = 0;
2636             }
2637             return index;
2638         }
2639 
2640         public int index() {
2641             return index;
2642         }
2643 
2644         private Flags flags(String s, int start, int end) {
2645             f = Flags.parse(s, start, end);
2646             if (f.contains(Flags.PREVIOUS))
2647                 index = -1;
2648             return f;
2649         }
2650 
2651         private int width(String s) {
2652             width = -1;
2653             if (s != null) {
2654                 try {
2655                     width  = Integer.parseInt(s);
2656                     if (width < 0)
2657                         throw new IllegalFormatWidthException(width);
2658                 } catch (NumberFormatException x) {
2659                     assert(false);
2660                 }
2661             }
2662             return width;
2663         }
2664 
2665         private int precision(String s) {
2666             precision = -1;
2667             if (s != null) {
2668                 try {
2669                     // remove the '.'
2670                     precision = Integer.parseInt(s.substring(1));
2671                     if (precision < 0)
2672                         throw new IllegalFormatPrecisionException(precision);
2673                 } catch (NumberFormatException x) {
2674                     assert(false);
2675                 }
2676             }
2677             return precision;
2678         }
2679 
2680         private char conversion(char conv) {
2681             c = conv;
2682             if (!dt) {
2683                 if (!Conversion.isValid(c)) {
2684                     throw new UnknownFormatConversionException(String.valueOf(c));
2685                 }
2686                 if (Character.isUpperCase(c)) {
2687                     f.add(Flags.UPPERCASE);
2688                     c = Character.toLowerCase(c);
2689                 }
2690                 if (Conversion.isText(c)) {
2691                     index = -2;
2692                 }
2693             }
2694             return c;
2695         }
2696 
2697         FormatSpecifier(String s, Matcher m) {
2698             int idx = 1;
2699 
2700             index(m.group(idx++));
2701             flags(s, m.start(idx), m.end(idx++));
2702             width(m.group(idx++));
2703             precision(m.group(idx++));
2704 
2705             int tTStart = m.start(idx);
2706             int tTEnd = m.end(idx++);
2707             if (tTStart != -1 && tTEnd != -1) {
2708                 dt = true;
2709                 if (tTStart == tTEnd - 1 && s.charAt(tTStart) == 'T') {
2710                     f.add(Flags.UPPERCASE);
2711                 }
2712             }
2713 
2714             conversion(s.charAt(m.start(idx)));
2715 
2716             if (dt)
2717                 checkDateTime();
2718             else if (Conversion.isGeneral(c))
2719                 checkGeneral();
2720             else if (Conversion.isCharacter(c))
2721                 checkCharacter();
2722             else if (Conversion.isInteger(c))
2723                 checkInteger();




2607     public enum BigDecimalLayoutForm {
2608         /**
2609          * Format the {@code BigDecimal} in computerized scientific notation.
2610          */
2611         SCIENTIFIC,
2612 
2613         /**
2614          * Format the {@code BigDecimal} as a decimal number.
2615          */
2616         DECIMAL_FLOAT
2617     };
2618 
2619     private class FormatSpecifier implements FormatString {
2620         private int index = -1;
2621         private Flags f = Flags.NONE;
2622         private int width;
2623         private int precision;
2624         private boolean dt = false;
2625         private char c;
2626 
2627         private int index(String s, int start, int end) {
2628             if (start >= 0) {
2629                 try {
2630                     // remove the matched trailing $
2631                     index = Integer.parseInt(s, start, end - 1, 10);
2632                 } catch (NumberFormatException x) {
2633                     assert(false);
2634                 }
2635             } else {
2636                 index = 0;
2637             }
2638             return index;
2639         }
2640 
2641         public int index() {
2642             return index;
2643         }
2644 
2645         private Flags flags(String s, int start, int end) {
2646             f = Flags.parse(s, start, end);
2647             if (f.contains(Flags.PREVIOUS))
2648                 index = -1;
2649             return f;
2650         }
2651 
2652         private int width(String s, int start, int end) {
2653             width = -1;
2654             if (start >= 0) {
2655                 try {
2656                     width = Integer.parseInt(s, start, end, 10);
2657                     if (width < 0)
2658                         throw new IllegalFormatWidthException(width);
2659                 } catch (NumberFormatException x) {
2660                     assert(false);
2661                 }
2662             }
2663             return width;
2664         }
2665 
2666         private int precision(String s, int start, int end) {
2667             precision = -1;
2668             if (start >= 0) {
2669                 try {
2670                     // remove the '.'
2671                     precision = Integer.parseInt(s, start + 1, end, 10);
2672                     if (precision < 0)
2673                         throw new IllegalFormatPrecisionException(precision);
2674                 } catch (NumberFormatException x) {
2675                     assert(false);
2676                 }
2677             }
2678             return precision;
2679         }
2680 
2681         private char conversion(char conv) {
2682             c = conv;
2683             if (!dt) {
2684                 if (!Conversion.isValid(c)) {
2685                     throw new UnknownFormatConversionException(String.valueOf(c));
2686                 }
2687                 if (Character.isUpperCase(c)) {
2688                     f.add(Flags.UPPERCASE);
2689                     c = Character.toLowerCase(c);
2690                 }
2691                 if (Conversion.isText(c)) {
2692                     index = -2;
2693                 }
2694             }
2695             return c;
2696         }
2697 
2698         FormatSpecifier(String s, Matcher m) {
2699             int idx = 1;
2700 
2701             index(s, m.start(idx), m.end(idx++));
2702             flags(s, m.start(idx), m.end(idx++));
2703             width(s, m.start(idx), m.end(idx++));
2704             precision(s, m.start(idx), m.end(idx++));
2705 
2706             int tTStart = m.start(idx);
2707             int tTEnd = m.end(idx++);
2708             if (tTStart != -1 && tTEnd != -1) {
2709                 dt = true;
2710                 if (tTStart == tTEnd - 1 && s.charAt(tTStart) == 'T') {
2711                     f.add(Flags.UPPERCASE);
2712                 }
2713             }
2714 
2715             conversion(s.charAt(m.start(idx)));
2716 
2717             if (dt)
2718                 checkDateTime();
2719             else if (Conversion.isGeneral(c))
2720                 checkGeneral();
2721             else if (Conversion.isCharacter(c))
2722                 checkCharacter();
2723             else if (Conversion.isInteger(c))
2724                 checkInteger();