# HG changeset patch # User redestad # Date 1416177389 -3600 # Sun Nov 16 23:36:29 2014 +0100 # Node ID e87cdcd9aa35f1727b68e9c4aefa6ad833f2ca3e # Parent 24420e217c69098ff6f7fda7633c27357ef81dde 8065070: (fmt) Avoid creating substrings when building FormatSpecifier Reviewed-by: duke diff --git a/src/java.base/share/classes/java/util/Formatter.java b/src/java.base/share/classes/java/util/Formatter.java --- a/src/java.base/share/classes/java/util/Formatter.java +++ b/src/java.base/share/classes/java/util/Formatter.java @@ -2624,10 +2624,11 @@ private boolean dt = false; private char c; - private int index(String s) { - if (s != null) { + private int index(String s, int start, int end) { + if (start >= 0) { try { - index = Integer.parseInt(s.substring(0, s.length() - 1)); + // remove the matched trailing $ + index = Integer.parseInt(s, start, end - 1, 10); } catch (NumberFormatException x) { assert(false); } @@ -2648,11 +2649,11 @@ return f; } - private int width(String s) { + private int width(String s, int start, int end) { width = -1; - if (s != null) { + if (start >= 0) { try { - width = Integer.parseInt(s); + width = Integer.parseInt(s, start, end, 10); if (width < 0) throw new IllegalFormatWidthException(width); } catch (NumberFormatException x) { @@ -2662,12 +2663,12 @@ return width; } - private int precision(String s) { + private int precision(String s, int start, int end) { precision = -1; - if (s != null) { + if (start >= 0) { try { // remove the '.' - precision = Integer.parseInt(s.substring(1)); + precision = Integer.parseInt(s, start + 1, end, 10); if (precision < 0) throw new IllegalFormatPrecisionException(precision); } catch (NumberFormatException x) { @@ -2697,10 +2698,10 @@ FormatSpecifier(String s, Matcher m) { int idx = 1; - index(m.group(idx++)); + index(s, m.start(idx), m.end(idx++)); flags(s, m.start(idx), m.end(idx++)); - width(m.group(idx++)); - precision(m.group(idx++)); + width(s, m.start(idx), m.end(idx++)); + precision(s, m.start(idx), m.end(idx++)); int tTStart = m.start(idx); int tTEnd = m.end(idx++);