StringUtils.isNumeric (String str) being given issue, if necessary, modify the source

Encountered in the project a bug, debugging result turned out to be StringUtils.isNumeric (String str) mischief (org.apache.commons.lang.StringUtils is used), the following code is to determine a parameter is not null and is integer:

if(StringUtils.isNumeric(str) && StringUtils.isNotBlank(str)){
            // do sth
}

But in simple code, but hidden bug!

Because if str = "-1";! StringUtils.isNumeric (str) returns false father is really willing to attractiveness ah.

Here is the test:

public static void main(String[] args)
{
        System.out.println(StringUtils.isNumeric("-1"));
}

Operating results: false

Ken Davis, right? As a regular expression implementation is not very simple? How could this be, I looked at the source:

Copy the code

public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
 }

Copy the code

Continue to jump in:

public static boolean isDigit(char ch) {
        return isDigit((int)ch);
}

carry on:

Copy the code

public static boolean isDigit(int codePoint) {
        boolean bDigit = false;

        if (codePoint >= MIN_CODE_POINT && codePoint <= FAST_PATH_MAX) {
            bDigit = CharacterDataLatin1.isDigit(codePoint);
        } else {
            int plane = getPlane(codePoint);
            switch(plane) {
            case(0):
                bDigit = CharacterData00.isDigit(codePoint);
                break;
            case(1):
                bDigit = CharacterData01.isDigit(codePoint);
                break;
            case(2):
                bDigit = CharacterData02.isDigit(codePoint);
                break;
            case(3): // Undefined
            case(4): // Undefined
            case(5): // Undefined
            case(6): // Undefined
            case(7): // Undefined
            case(8): // Undefined
            case(9): // Undefined
            case(10): // Undefined
            case(11): // Undefined
            case(12): // Undefined
            case(13): // Undefined
                bDigit = CharacterDataUndefined.isDigit(codePoint);
                break;
            case(14):
                bDigit = CharacterData0E.isDigit(codePoint);
                break;
            case(15): // Private Use
            case(16): // Private Use
                bDigit = CharacterDataPrivateUse.isDigit(codePoint);
                break;
            default:
                // the argument's plane is invalid, and thus is an invalid codepoint
                // bDigit remains false;
                break;                          
            }
        }
        return bDigit;
    }

Copy the code

In the following step fails:

 static boolean isDigit(int ch) {
        int type = getType(ch);
        return (type == Character.DECIMAL_DIGIT_NUMBER);
    }

That he did not take into account the implementation - + prefix of the problem, this is not a silly fork it?

The following results are false:

public static void main(String[] args)
{
        System.out.println(StringUtils.isNumeric("-1"));
        System.out.println(StringUtils.isNumeric("+1"));
}

This is his method Notes:

Copy the code

Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.

null will return false. An empty String ("") will return true.

 StringUtils.isNumeric(null)   = false
 StringUtils.isNumeric("")     = true
 StringUtils.isNumeric("  ")   = false
 StringUtils.isNumeric("123")  = true
 StringUtils.isNumeric("12 3") = false
 StringUtils.isNumeric("ab2c") = false
 StringUtils.isNumeric("12-3") = false
 StringUtils.isNumeric("12.3") = false
 
Parameters:
str the String to check, may be null
Returns:
true if only contains digits, and is non-null

Copy the code

Only contain numbers unicode, +, -, all of which are not counted as unicode number.

Published 57 original articles · won praise 33 · Views 140,000 +

Guess you like

Origin blog.csdn.net/u014156013/article/details/82380740