jdk/src/java.base/share/classes/java/util/Formatter.java
changeset 27731 3cba975130c0
parent 26733 bb835fd801b6
child 28302 5de69b0d3a84
equal deleted inserted replaced
27730:e739f0725baa 27731:3cba975130c0
  2622         private int width;
  2622         private int width;
  2623         private int precision;
  2623         private int precision;
  2624         private boolean dt = false;
  2624         private boolean dt = false;
  2625         private char c;
  2625         private char c;
  2626 
  2626 
  2627         private int index(String s) {
  2627         private int index(String s, int start, int end) {
  2628             if (s != null) {
  2628             if (start >= 0) {
  2629                 try {
  2629                 try {
  2630                     index = Integer.parseInt(s.substring(0, s.length() - 1));
  2630                     // skip the trailing '$'
       
  2631                     index = Integer.parseInt(s, start, end - 1, 10);
  2631                 } catch (NumberFormatException x) {
  2632                 } catch (NumberFormatException x) {
  2632                     assert(false);
  2633                     assert(false);
  2633                 }
  2634                 }
  2634             } else {
  2635             } else {
  2635                 index = 0;
  2636                 index = 0;
  2646             if (f.contains(Flags.PREVIOUS))
  2647             if (f.contains(Flags.PREVIOUS))
  2647                 index = -1;
  2648                 index = -1;
  2648             return f;
  2649             return f;
  2649         }
  2650         }
  2650 
  2651 
  2651         private int width(String s) {
  2652         private int width(String s, int start, int end) {
  2652             width = -1;
  2653             width = -1;
  2653             if (s != null) {
  2654             if (start >= 0) {
  2654                 try {
  2655                 try {
  2655                     width  = Integer.parseInt(s);
  2656                     width = Integer.parseInt(s, start, end, 10);
  2656                     if (width < 0)
  2657                     if (width < 0)
  2657                         throw new IllegalFormatWidthException(width);
  2658                         throw new IllegalFormatWidthException(width);
  2658                 } catch (NumberFormatException x) {
  2659                 } catch (NumberFormatException x) {
  2659                     assert(false);
  2660                     assert(false);
  2660                 }
  2661                 }
  2661             }
  2662             }
  2662             return width;
  2663             return width;
  2663         }
  2664         }
  2664 
  2665 
  2665         private int precision(String s) {
  2666         private int precision(String s, int start, int end) {
  2666             precision = -1;
  2667             precision = -1;
  2667             if (s != null) {
  2668             if (start >= 0) {
  2668                 try {
  2669                 try {
  2669                     // remove the '.'
  2670                     // skip the leading '.'
  2670                     precision = Integer.parseInt(s.substring(1));
  2671                     precision = Integer.parseInt(s, start + 1, end, 10);
  2671                     if (precision < 0)
  2672                     if (precision < 0)
  2672                         throw new IllegalFormatPrecisionException(precision);
  2673                         throw new IllegalFormatPrecisionException(precision);
  2673                 } catch (NumberFormatException x) {
  2674                 } catch (NumberFormatException x) {
  2674                     assert(false);
  2675                     assert(false);
  2675                 }
  2676                 }
  2693             }
  2694             }
  2694             return c;
  2695             return c;
  2695         }
  2696         }
  2696 
  2697 
  2697         FormatSpecifier(String s, Matcher m) {
  2698         FormatSpecifier(String s, Matcher m) {
  2698             int idx = 1;
  2699             index(s, m.start(1), m.end(1));
  2699 
  2700             flags(s, m.start(2), m.end(2));
  2700             index(m.group(idx++));
  2701             width(s, m.start(3), m.end(3));
  2701             flags(s, m.start(idx), m.end(idx++));
  2702             precision(s, m.start(4), m.end(4));
  2702             width(m.group(idx++));
  2703 
  2703             precision(m.group(idx++));
  2704             int tTStart = m.start(5);
  2704 
  2705             if (tTStart >= 0) {
  2705             int tTStart = m.start(idx);
       
  2706             int tTEnd = m.end(idx++);
       
  2707             if (tTStart != -1 && tTEnd != -1) {
       
  2708                 dt = true;
  2706                 dt = true;
  2709                 if (tTStart == tTEnd - 1 && s.charAt(tTStart) == 'T') {
  2707                 if (s.charAt(tTStart) == 'T') {
  2710                     f.add(Flags.UPPERCASE);
  2708                     f.add(Flags.UPPERCASE);
  2711                 }
  2709                 }
  2712             }
  2710             }
  2713 
  2711             conversion(s.charAt(m.start(6)));
  2714             conversion(s.charAt(m.start(idx)));
       
  2715 
  2712 
  2716             if (dt)
  2713             if (dt)
  2717                 checkDateTime();
  2714                 checkDateTime();
  2718             else if (Conversion.isGeneral(c))
  2715             else if (Conversion.isGeneral(c))
  2719                 checkGeneral();
  2716                 checkGeneral();