LANG-747 NumberUtils does not handle Long Hex numbers

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1384126 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-12 21:10:18 +00:00
parent 2c6fa89aa4
commit c7adc7e86f
2 changed files with 15 additions and 6 deletions

View File

@ -39,6 +39,7 @@
<action issue="LANG-765" type="fix">EventListenerSupport.ProxyInvocationHandler no longer defines serialVersionUID</action>
<action issue="LANG-764" type="fix">StrBuilder is now serializable</action>
<action issue="LANG-761" type="fix">Fix Javadoc Ant warnings</action>
<action issue="LANG-747" type="fix">NumberUtils does not handle Long Hex numbers</action>
<action issue="LANG-743" type="fix">Javadoc bug in static inner class DateIterator</action>
<action issue="LANG-675" type="add">Add Triple class (ternary version of Pair)</action>
<action issue="LANG-462" type="add">FastDateFormat supports parse methods</action>

View File

@ -418,8 +418,13 @@ public static short toShort(String str, short defaultValue) {
/**
* <p>Turns a string value into a java.lang.Number.</p>
*
* <p>First, the value is examined for a type qualifier on the end
* (<code>'f','F','d','D','l','L'</code>). If it is found, it starts
* <p>If the string starts with <code>0x</code> or <code>-0x</code> (lower or upper case), it
* will be interpreted as a hexadecimal integer - or long, if the number of digits after the 0x
* prefix is more than 8.
* Values with leading <code>0</code>'s will not be interpreted as octal.</p>
*
* <p>Then, the value is examined for a type qualifier on the end, i.e. one of
* <code>'f','F','d','D','l','L'</code>. If it is found, it starts
* trying to create successively larger types from the type specified
* until one is found that can represent the value.</p>
*
@ -428,10 +433,6 @@ public static short toShort(String str, short defaultValue) {
* <code>BigInteger</code> and from <code>Float</code> to
* <code>BigDecimal</code>.</p>
*
* <p>If the string starts with <code>0x</code> or <code>-0x</code> (lower or upper case), it
* will be interpreted as a hexadecimal integer. Values with leading
* <code>0</code>'s will not be interpreted as octal.</p>
*
* <p>Returns <code>null</code> if the string is <code>null</code>.</p>
*
* <p>This method does not trim the input string, i.e., strings with leading
@ -456,6 +457,13 @@ public static Number createNumber(String str) throws NumberFormatException {
return null;
}
if (str.startsWith("0x") || str.startsWith("-0x") || str.startsWith("0X") || str.startsWith("-0X")) {
int hexDigits = str.length() - 2; // drop 0x
if (str.startsWith("-")) { // drop -
hexDigits--;
}
if (hexDigits > 8) { // too many for an int
return createLong(str);
}
return createInteger(str);
}
char lastChar = str.charAt(str.length() - 1);