src/share/classes/java/lang/Integer.java
Print this page
rev 9925 : 8041972: Add improved parse/format methods for Long/Integer
Contributed-by: redestad
@@ -525,18 +525,43 @@
* specified radix.
* @exception NumberFormatException if the {@code String}
* does not contain a parsable {@code int}.
*/
public static int parseInt(String s, int radix)
- throws NumberFormatException
- {
+ throws NumberFormatException {
+ return parseInt(s, radix, 0);
+ }
+
+ /**
+ * Extend upon Integer.parseInt(String, int) by providing a start offset to enable parsing
+ * substrings without creating intermediary objects
+ * @see java.lang.Integer#parseInt(String, int)
+ *
+ * @param start the start position in s to parse from, inclusive
+ */
+ static int parseInt(CharSequence s, int radix, int start)
+ throws NumberFormatException {
+ if (s == null) {
+ throw new NumberFormatException("null");
+ }
+ return parseInt(s, radix, start, s.length());
+ }
+
+ /**
+ * Extend upon Integer.parseInt(String, int) by providing a start and end offset to enable parsing
+ * substrings without creating intermediary objects
+ * @see java.lang.Integer#parseInt(String, int)
+ * @param start the start position in s to parse from, inclusive
+ * @param end the end position in s to parse to, exclusive
+ */
+ static int parseInt(CharSequence s, int radix, int start, int end)
+ throws NumberFormatException {
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
-
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
@@ -547,49 +572,64 @@
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
+ if (start < 0 || start > end) {
+ throw new NumberFormatException("start position out of bounds");
+ }
+
+ if (start == end) {
+ throw NumberFormatException.forInputString("");
+ }
+
+ if (end > s.length()) {
+ throw new NumberFormatException("end position out of bounds");
+ }
+
int result = 0;
boolean negative = false;
- int i = 0, len = s.length();
+ int i = start;
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
- if (len > 0) {
- char firstChar = s.charAt(0);
+ char firstChar = s.charAt(start);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
- } else if (firstChar != '+')
- throw NumberFormatException.forInputString(s);
+ } else if (firstChar != '+') {
+ throw NumberFormatException.forInputString(
+ s.subSequence(start, end).toString());
+ }
- if (len == 1) // Cannot have lone "+" or "-"
- throw NumberFormatException.forInputString(s);
+ if (end == start + 1) { // Cannot have lone "+" or "-"
+ throw NumberFormatException.forInputString(
+ s.subSequence(start, end).toString());
+ }
i++;
}
multmin = limit / radix;
- while (i < len) {
+ while (i < end) {
// Accumulating negatively avoids surprises near MAX_VALUE
- digit = Character.digit(s.charAt(i++),radix);
+ digit = Character.digit(s.charAt(i++), radix);
if (digit < 0) {
- throw NumberFormatException.forInputString(s);
+ throw NumberFormatException.forInputString(
+ s.subSequence(start, end).toString());
}
if (result < multmin) {
- throw NumberFormatException.forInputString(s);
+ throw NumberFormatException.forInputString(
+ s.subSequence(start, end).toString());
}
result *= radix;
if (result < limit + digit) {
- throw NumberFormatException.forInputString(s);
+ throw NumberFormatException.forInputString(
+ s.subSequence(start, end).toString());
}
result -= digit;
}
- } else {
- throw NumberFormatException.forInputString(s);
- }
return negative ? result : -result;
}
/**
* Parses the string argument as a signed decimal integer. The
@@ -655,38 +695,63 @@
* does not contain a parsable {@code int}.
* @since 1.8
*/
public static int parseUnsignedInt(String s, int radix)
throws NumberFormatException {
+ return parseUnsignedInt(s, radix, 0);
+ }
+
+ /**
+ * Extend upon parseUnsignedInt(String, int) by providing a start offset
+ * to enable parsing substrings without creating intermediary objects
+ * @see java.lang.Integer#parseUnsignedInt(String, int)
+ * @param start the start position in s to parse from, inclusive
+ */
+ static int parseUnsignedInt(CharSequence s, int radix, int start)
+ throws NumberFormatException {
if (s == null) {
throw new NumberFormatException("null");
}
+ return parseUnsignedInt(s, radix, start, s.length());
+ }
- int len = s.length();
+ /**
+ * Extend upon parseUnsignedInt(String, int) by providing a start and end
+ * offset to enable parsing substrings without creating intermediary objects
+ * @see java.lang.Integer#parseUnsignedInt(String, int)
+ * @param start the start position in s to parse from, inclusive
+ * @param end the end position in s to parse to, exclusive
+ */
+ static int parseUnsignedInt(CharSequence s, int radix, int start, int end)
+ throws NumberFormatException {
+ if (s == null) {
+ throw new NumberFormatException("null");
+ }
+ int len = end - start;
if (len > 0) {
- char firstChar = s.charAt(0);
+ char firstChar = s.charAt(start);
if (firstChar == '-') {
throw new
NumberFormatException(String.format("Illegal leading minus sign " +
"on unsigned string %s.", s));
} else {
if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
(radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
- return parseInt(s, radix);
+ return parseInt(s, radix, start, end);
} else {
- long ell = Long.parseLong(s, radix);
+ long ell = Long.parseLong(s, radix, start, end);
if ((ell & 0xffff_ffff_0000_0000L) == 0) {
return (int) ell;
} else {
throw new
NumberFormatException(String.format("String value %s exceeds " +
"range of unsigned int.", s));
}
}
}
} else {
- throw NumberFormatException.forInputString(s);
+ throw NumberFormatException.forInputString(s.toString());
}
}
/**
* Parses the string argument as an unsigned decimal integer. The