@Test public void shouldParseLong() { assertEquals(0L, parseLongAscii("0", 0, 1)); assertEquals(0L, parseLongAscii("-0", 0, 2)); assertEquals(7L, parseLongAscii("7", 0, 1)); assertEquals(-7L, parseLongAscii("-7", 0, 2)); assertEquals(33L, parseLongAscii("3333", 1, 2)); assertEquals(-123456789876543210L, parseLongAscii("-123456789876543210", 0, 19)); final String maxValueMinusOne = String.valueOf(Long.MAX_VALUE - 1); assertEquals(Long.MAX_VALUE - 1, parseLongAscii(maxValueMinusOne, 0, maxValueMinusOne.length())); final String maxValue = String.valueOf(Long.MAX_VALUE); assertEquals(Long.MAX_VALUE, parseLongAscii(maxValue, 0, maxValue.length())); final String minValuePlusOne = String.valueOf(Long.MIN_VALUE + 1); assertEquals(Long.MIN_VALUE + 1, parseLongAscii(minValuePlusOne, 0, minValuePlusOne.length())); final String minValue = String.valueOf(Long.MIN_VALUE); assertEquals(Long.MIN_VALUE, parseLongAscii(minValue, 0, minValue.length())); }
public static long parseLongAscii(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 >= 19) { checkLongLimits(cs, index, length, i, hasSign); } long tally = 0; for (; i < endExclusive; i++) { tally = (tally * 10) + AsciiEncoding.getDigit(i, cs.charAt(i)); } if (hasSign) { tally = -tally; } return tally; }
AsciiEncoding { public static long parseLongAscii(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 >= 19) { checkLongLimits(cs, index, length, i, hasSign); } long tally = 0; for (; i < endExclusive; i++) { tally = (tally * 10) + AsciiEncoding.getDigit(i, cs.charAt(i)); } if (hasSign) { tally = -tally; } return tally; } }
AsciiEncoding { public static long parseLongAscii(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 >= 19) { checkLongLimits(cs, index, length, i, hasSign); } long 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 long parseLongAscii(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 >= 19) { checkLongLimits(cs, index, length, i, hasSign); } long 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 long parseLongAscii(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 >= 19) { checkLongLimits(cs, index, length, i, hasSign); } long 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; }