Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 79404DBC7 for ; Tue, 13 Nov 2012 00:59:45 +0000 (UTC) Received: (qmail 85599 invoked by uid 500); 13 Nov 2012 00:59:45 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 85550 invoked by uid 500); 13 Nov 2012 00:59:45 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 85543 invoked by uid 99); 13 Nov 2012 00:59:44 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Nov 2012 00:59:44 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Nov 2012 00:59:43 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id BA13C23888E7 for ; Tue, 13 Nov 2012 00:59:23 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121113005923.BA13C23888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ + NumberUtils#createBigInteger does not allow for hex and octal numbers StringUtils join APIs for primitives FastDateFormat and FastDatePrinter generates Date objects wastefully Spelling fixes 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 { } /** - *

Convert a String to a BigInteger.

+ *

Convert a String to a BigInteger; + * since 3.2 it handles hex (0x or #) and octal (0) notations.

* *

Returns null if the string is null.

* @@ -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) {