hello all,

the attached patch fixes some problems in two of the BigInteger constructors 
highlighted by the new Mauve test gnu.testlet.java.math.BigInteger.ctor.

the expected ChangeLog entry will look like so:

2006-08-06  Raif S. Naffah  <[EMAIL PROTECTED]>

        * java/math/BigInteger.java (BigInteger(int, int, Random)): Ensure that
        constructed BigInteger has exactly bitLength bits.
        (valueOf): Do not allow minus signs in the middle of the string.
        Do not ignore underscores and whitespace at the beginning of the string,
        raise a NumberFormatException instead.
        Do not stop parsing if an invalid symbol is encountered in the string,
        raise a NumberFormatException instead.

the updated behavior now matches the RI's (both 1.4.2_12 and 1.5.0_07).

because BigInteger is an important class, i would appreciate it if another 
pair of eyes can go over the patch to ensure no glaring mistakes have been 
introduced.


TIA + cheers;
rsn
Index: BigInteger.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/math/BigInteger.java,v
retrieving revision 1.28
diff -u -r1.28 BigInteger.java
--- BigInteger.java	27 May 2006 02:09:30 -0000	1.28
+++ BigInteger.java	5 Aug 2006 15:59:21 -0000
@@ -225,8 +225,13 @@
     this(bitLength, rnd);

     // Keep going until we find a probable prime.
+    BigInteger result;
     while (true)
       {
+        // ...but first ensure that BI has bitLength bits
+        result = setBit(bitLength - 1);
+        this.ival = result.ival;
+        this.words = result.words;
 	if (isProbablePrime(certainty))
 	  return;

@@ -1589,24 +1594,31 @@
     // but slightly more expensive, for little practical gain.
     if (len <= 15 && radix <= 16)
       return valueOf(Long.parseLong(s, radix));
-
+
+    int i, digit;
+    boolean negative;
+    byte[] bytes;
+    char ch = s.charAt(0);
+    if (ch == '-')
+      {
+        negative = true;
+        i = 1;
+        bytes = new byte[len - 1];
+      }
+    else
+      {
+        negative = false;
+        i = 0;
+        bytes = new byte[len];
+      }
     int byte_len = 0;
-    byte[] bytes = new byte[len];
-    boolean negative = false;
-    for (int i = 0;  i < len;  i++)
+    for ( ; i < len;  i++)
       {
-	char ch = s.charAt(i);
-	if (ch == '-')
-	  negative = true;
-	else if (ch == '_' || (byte_len == 0 && (ch == ' ' || ch == '\t')))
-	  continue;
-	else
-	  {
-	    int digit = Character.digit(ch, radix);
-	    if (digit < 0)
-	      break;
-	    bytes[byte_len++] = (byte) digit;
-	  }
+        ch = s.charAt(i);
+        digit = Character.digit(ch, radix);
+        if (digit < 0)
+          throw new NumberFormatException();
+        bytes[byte_len++] = (byte) digit;
       }
     return valueOf(bytes, byte_len, negative, radix);
   }

Reply via email to