commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1408524 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/math/NumberUtils.java test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Date Tue, 13 Nov 2012 00:59:23 GMT
Author: sebb
Date: Tue Nov 13 00:59:22 2012
New Revision: 1408524

URL: http://svn.apache.org/viewvc?rev=1408524&view=rev
Log:
LANG-855 NumberUtils#createBigInteger does not allow for hex and octal numbers

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1408524&r1=1408523&r2=1408524&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Tue Nov 13 00:59:22 2012
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.2" date="TBA" description="Next release">
+    <action issue="LANG-855" type="add">NumberUtils#createBigInteger does not allow
for hex and octal numbers</action>
     <action issue="LANG-853" type="add">StringUtils join APIs for primitives</action>
     <action issue="LANG-849" type="fix">FastDateFormat and FastDatePrinter generates
Date objects wastefully</action>
     <action issue="LANG-845" type="fix">Spelling fixes</action>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java?rev=1408524&r1=1408523&r2=1408524&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Tue Nov 13 00:59:22 2012
@@ -683,7 +683,8 @@ public class NumberUtils {
     }
 
     /**
-     * <p>Convert a <code>String</code> to a <code>BigInteger</code>.</p>
+     * <p>Convert a <code>String</code> to a <code>BigInteger</code>;
+     * since 3.2 it handles hex (0x or #) and octal (0) notations.</p>
      *
      * <p>Returns <code>null</code> if the string is <code>null</code>.</p>
      * 
@@ -695,7 +696,26 @@ public class NumberUtils {
         if (str == null) {
             return null;
         }
-        return new BigInteger(str);
+        int pos = 0; // offset within string
+        int radix = 10;
+        boolean negate = false; // need to negate later?
+        if (str.startsWith("-")) {
+            negate = true;
+            pos = 1;
+        }
+        if (str.startsWith("0x", pos) || str.startsWith("0x", pos)) { // hex
+            radix = 16;
+            pos += 2;
+        } else if (str.startsWith("#", pos)) { // alternative hex (allowed by Long/Integer)
+            radix = 16;
+            pos ++;
+        } else if (str.startsWith("0", pos) && str.length() > pos + 1) { // octal;
so long as there are additional digits
+            radix = 8;
+            pos ++;
+        } // default is to treat as decimal
+
+        final BigInteger value = new BigInteger(str.substring(pos), radix);
+        return negate ? value.negate() : value;
     }
 
     /**

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java?rev=1408524&r1=1408523&r2=1408524&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Tue Nov 13 00:59:22 2012
@@ -348,6 +348,18 @@ public class NumberUtilsTest {
         this.testCreateBigIntegerFailure("\b\t\n\f\r");
         // Funky whitespaces
         this.testCreateBigIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
+        assertEquals("createBigInteger(String) failed", new BigInteger("255"), NumberUtils.createBigInteger("0xff"));
+        assertEquals("createBigInteger(String) failed", new BigInteger("255"), NumberUtils.createBigInteger("#ff"));
+        assertEquals("createBigInteger(String) failed", new BigInteger("-255"), NumberUtils.createBigInteger("-0xff"));
+        assertEquals("createBigInteger(String) failed", new BigInteger("255"), NumberUtils.createBigInteger("0377"));
+        assertEquals("createBigInteger(String) failed", new BigInteger("-255"), NumberUtils.createBigInteger("-0377"));
+        assertEquals("createBigInteger(String) failed", new BigInteger("-255"), NumberUtils.createBigInteger("-0377"));
+        assertEquals("createBigInteger(String) failed", new BigInteger("-0"), NumberUtils.createBigInteger("-0"));
+        assertEquals("createBigInteger(String) failed", new BigInteger("0"), NumberUtils.createBigInteger("0"));
+        testCreateBigIntegerFailure("#");
+        testCreateBigIntegerFailure("-#");
+        testCreateBigIntegerFailure("0x");
+        testCreateBigIntegerFailure("-0x");
     }
 
     protected void testCreateBigIntegerFailure(String str) {



Mime
View raw message