Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. def parseLong(input: CharSequence, start: Int, end: Int): Long = {
  2. var negative: Boolean = false
  3. var i: Int = start
  4. var result: Long = 0L
  5.  
  6. if (input.charAt(start) == '-') {
  7. negative = true
  8. i += 1
  9. }
  10.  
  11. val limit: Long = if (negative) Long.MinValue else -Long.MaxValue
  12. val limitMult: Long = limit / 10L
  13.  
  14. while (i < end) {
  15. val digit: Int = input.charAt(i).toInt - 48
  16.  
  17. if (i - start < 18) {
  18. result = result * 10L - digit
  19. } else {
  20. if (result < limitMult) {
  21. throw new NumberFormatException(s"For input string: $input")
  22. } else {
  23. result *= 10L
  24. if (digit > 0) {
  25. if (result < limit + digit) {
  26. throw new NumberFormatException(s"For input string: $input")
  27. } else {
  28. result -= digit
  29. }
  30. }
  31. }
  32. }
  33.  
  34. i += 1
  35. }
  36.  
  37. if (negative) result else -result
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement