This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Integer.decode() patch for negative numbers


Hi,

The following fixes two problems I found with Mauve.
With this patch we throw a NullPointerException when the argument to
decode is null. And we can now correctly decode Strings like "-0x0".

2002-04-01  Mark Wielaard  <mark@klomp.org>

    * java/lang/Integer.java (decode): Throw NullPointerException when
    argument is null. A minus sign can precede other leading characters.

OK to commit to mainline/branch?

Cheers,

Mark
Index: java/lang/Integer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/Integer.java,v
retrieving revision 1.13
diff -u -r1.13 Integer.java
--- Integer.java	2002/01/22 22:40:16	1.13
+++ Integer.java	2002/04/01 12:31:28
@@ -433,8 +433,8 @@
    * octal numbers.
    *
    * The <code>String</code> argument is interpreted based on the leading
-   * characters.  Depending on what the String begins with, the base will be
-   * interpreted differently:
+   * characters.  Depending on what the String begins with (after an optional
+   * minus sign), the base will be interpreted differently:
    *
    * <table border=1>
    * <tr><th>Leading<br>Characters</th><th>Base</th></tr>
@@ -445,6 +445,8 @@
    * <tr><td>Anything<br>Else</td><td>10</td></tr>
    * </table>
    *
+   * If the String starts with a minus sign the result is negated.
+   *
    * @param str the <code>String</code> to interpret.
    * @return the value of the String as an <code>Integer</code>.
    * @exception NumberFormatException thrown if the <code>String</code> 
@@ -457,28 +459,34 @@
     int radix = 10;
     final int len;
 
-    if (str == null || (len = str.length()) == 0)
-      throw new NumberFormatException("string null or empty");
+    if ((len = str.length()) == 0)
+      throw new NumberFormatException("empty string");
 
-    // Negative numbers are always radix 10.
     if (str.charAt(index) == '-')
       {
-        radix = 10;
-        index++;
-        isNeg = true;
+        // The minus sign should be followed by at least one more char
+        if (len > 1)
+          {
+            isNeg = true;
+            index++;
+          }
+        else
+          throw new NumberFormatException();
       }
-    else if (str.charAt(index) == '#')
+
+    if (str.charAt(index) == '#')
       {
         radix = 16;
         index++;
       }
     else if (str.charAt(index) == '0')
       {
-        // Check if str is just "0"
-        if (len == 1)
+        index++;
+
+        // Check if str is just "0" or "-0"
+        if (len == index)
           return new Integer(0);
 
-        index++;
         if (str.charAt(index) == 'x' || str.charAt(index) == 'X')
           {
             radix = 16;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]