@Test public void shouldThrowExceptionWhenParsingIntegerWhichCanOverFlow() { final String maxValuePlusOneDigit = Integer.MAX_VALUE + "1"; assertThrows(AsciiNumberFormatException.class, () -> parseIntAscii(maxValuePlusOneDigit, 0, maxValuePlusOneDigit.length()), maxValuePlusOneDigit); final String maxValuePlusOne = "2147483648"; assertThrows(AsciiNumberFormatException.class, () -> parseIntAscii(maxValuePlusOne, 0, maxValuePlusOne.length()), maxValuePlusOne); final String minValuePlusOneDigit = Integer.MIN_VALUE + "1"; assertThrows(AsciiNumberFormatException.class, () -> parseIntAscii(minValuePlusOneDigit, 0, minValuePlusOneDigit.length()), minValuePlusOneDigit); final String minValueMinusOne = "-2147483649"; assertThrows(AsciiNumberFormatException.class, () -> parseIntAscii(minValueMinusOne, 0, minValueMinusOne.length()), minValueMinusOne); }
public static int parseIntAscii(final CharSequence cs, final int index, final int length) { if (length <= 1) { return parseSingleDigit(cs, index, length); } final boolean hasSign = cs.charAt(index) == MINUS_SIGN; final int endExclusive = index + length; int i = hasSign ? index + 1 : index; if (length >= 10) { checkIntLimits(cs, index, length, i, hasSign); } int tally = 0; for (; i < endExclusive; i++) { tally = (tally * 10) + AsciiEncoding.getDigit(i, cs.charAt(i)); } if (hasSign) { tally = -tally; } return tally; }
AsciiEncoding { public static int parseIntAscii(final CharSequence cs, final int index, final int length) { if (length <= 1) { return parseSingleDigit(cs, index, length); } final boolean hasSign = cs.charAt(index) == MINUS_SIGN; final int endExclusive = index + length; int i = hasSign ? index + 1 : index; if (length >= 10) { checkIntLimits(cs, index, length, i, hasSign); } int tally = 0; for (; i < endExclusive; i++) { tally = (tally * 10) + AsciiEncoding.getDigit(i, cs.charAt(i)); } if (hasSign) { tally = -tally; } return tally; } }
AsciiEncoding { public static int parseIntAscii(final CharSequence cs, final int index, final int length) { if (length <= 1) { return parseSingleDigit(cs, index, length); } final boolean hasSign = cs.charAt(index) == MINUS_SIGN; final int endExclusive = index + length; int i = hasSign ? index + 1 : index; if (length >= 10) { checkIntLimits(cs, index, length, i, hasSign); } int tally = 0; for (; i < endExclusive; i++) { tally = (tally * 10) + AsciiEncoding.getDigit(i, cs.charAt(i)); } if (hasSign) { tally = -tally; } return tally; } private  AsciiEncoding(); }
AsciiEncoding { public static int parseIntAscii(final CharSequence cs, final int index, final int length) { if (length <= 1) { return parseSingleDigit(cs, index, length); } final boolean hasSign = cs.charAt(index) == MINUS_SIGN; final int endExclusive = index + length; int i = hasSign ? index + 1 : index; if (length >= 10) { checkIntLimits(cs, index, length, i, hasSign); } int tally = 0; for (; i < endExclusive; i++) { tally = (tally * 10) + AsciiEncoding.getDigit(i, cs.charAt(i)); } if (hasSign) { tally = -tally; } return tally; } private  AsciiEncoding(); static int endOffset(final int value); static int endOffset(final long value); static int getDigit(final int index, final byte value); static int getDigit(final int index, final char value); static int parseIntAscii(final CharSequence cs, final int index, final int length); static long parseLongAscii(final CharSequence cs, final int index, final int length); }
AsciiEncoding { public static int parseIntAscii(final CharSequence cs, final int index, final int length) { if (length <= 1) { return parseSingleDigit(cs, index, length); } final boolean hasSign = cs.charAt(index) == MINUS_SIGN; final int endExclusive = index + length; int i = hasSign ? index + 1 : index; if (length >= 10) { checkIntLimits(cs, index, length, i, hasSign); } int tally = 0; for (; i < endExclusive; i++) { tally = (tally * 10) + AsciiEncoding.getDigit(i, cs.charAt(i)); } if (hasSign) { tally = -tally; } return tally; } private  AsciiEncoding(); static int endOffset(final int value); static int endOffset(final long value); static int getDigit(final int index, final byte value); static int getDigit(final int index, final char value); static int parseIntAscii(final CharSequence cs, final int index, final int length); static long parseLongAscii(final CharSequence cs, final int index, final int length); static final byte[] MIN_INTEGER_VALUE; static final byte[] MIN_LONG_VALUE; static final byte MINUS_SIGN; static final byte ZERO; }