Skip to content

Commit 7f1cb8b

Browse files
committed
refactor: decompose condition of digit checks by using extra method 'isNumericChar(...)' in NumberConversionUtil.
1 parent 75419e3 commit 7f1cb8b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/main/java/org/json/NumberConversionUtil.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static Number stringToNumber(final String input) throws NumberFormatException {
2424
val = "-0."+val.substring(2);
2525
}
2626
char initial = val.charAt(0);
27-
if ((initial >= '0' && initial <= '9') || initial == '-' ) {
27+
if ( isNumericChar(initial) || initial == '-' ) {
2828
// decimal representation
2929
if (isDecimalNotation(val)) {
3030
// Use a BigDecimal all the time so we keep the original
@@ -53,13 +53,13 @@ static Number stringToNumber(final String input) throws NumberFormatException {
5353
initial = val.charAt(0);
5454
if(initial == '0' && val.length() > 1) {
5555
char at1 = val.charAt(1);
56-
if(at1 >= '0' && at1 <= '9') {
56+
if(isNumericChar(at1)) {
5757
throw new NumberFormatException("val ["+input+"] is not a valid number.");
5858
}
5959
} else if (initial == '-' && val.length() > 2) {
6060
char at1 = val.charAt(1);
6161
char at2 = val.charAt(2);
62-
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
62+
if(at1 == '0' && isNumericChar(at2)) {
6363
throw new NumberFormatException("val ["+input+"] is not a valid number.");
6464
}
6565
}
@@ -83,6 +83,16 @@ static Number stringToNumber(final String input) throws NumberFormatException {
8383
throw new NumberFormatException("val ["+input+"] is not a valid number.");
8484
}
8585

86+
/**
87+
* Checks if the character is a numeric digit ('0' to '9').
88+
*
89+
* @param c The character to be checked.
90+
* @return true if the character is a numeric digit, false otherwise.
91+
*/
92+
private static boolean isNumericChar(char c) {
93+
return (c >= '0' && c <= '9');
94+
}
95+
8696
/**
8797
* Checks if the value could be considered a number in decimal number system.
8898
* @param value

0 commit comments

Comments
 (0)