Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def parseLong(input: CharSequence, start: Int, end: Int): Long = {
- var negative: Boolean = false
- var i: Int = start
- var result: Long = 0L
- if (input.charAt(start) == '-') {
- negative = true
- i += 1
- }
- val limit: Long = if (negative) Long.MinValue else -Long.MaxValue
- val limitMult: Long = limit / 10L
- while (i < end) {
- val digit: Int = input.charAt(i).toInt - 48
- if (i - start < 18) {
- result = result * 10L - digit
- } else {
- if (result < limitMult) {
- throw new NumberFormatException(s"For input string: $input")
- } else {
- result *= 10L
- if (digit > 0) {
- if (result < limit + digit) {
- throw new NumberFormatException(s"For input string: $input")
- } else {
- result -= digit
- }
- }
- }
- }
- i += 1
- }
- if (negative) result else -result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement