From 493667a6cc83e75cb92159b691ae1859aa5513aa Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Thu, 23 Aug 2007 20:35:27 -0600 Subject: [PATCH] handle negative numbers in Long.parseLong() and improve error detection --- classpath/java/lang/Long.java | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/classpath/java/lang/Long.java b/classpath/java/lang/Long.java index feb8c417ee..4439bfaf60 100644 --- a/classpath/java/lang/Long.java +++ b/classpath/java/lang/Long.java @@ -98,19 +98,30 @@ public final class Long extends Number { return parseLong(s, 10); } - public static long parseLong(String s, int radix) { + public static long parseLong(String s, int radix) { + int i = 0; long number = 0; + boolean negative = s.startsWith("-"); + if (negative) { + i = 1; + } - for (int i = 0; i < s.length(); ++i) { + for (; i < s.length(); ++i) { char c = s.charAt(i); if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'z'))) { long digit = ((c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10)); - number += digit * pow(radix, (s.length() - i - 1)); - } else { - throw new NumberFormatException("invalid character " + c + " code " + - (int) c); + if (digit < radix) { + number += digit * pow(radix, (s.length() - i - 1)); + continue; + } } + throw new NumberFormatException("invalid character " + c + " code " + + (int) c); + } + + if (negative) { + number = -number; } return number;