From 7f1cb8bf62015016d4b02879b00cc7477b62c570 Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Sun, 19 Nov 2023 09:51:44 -0400 Subject: [PATCH] refactor: decompose condition of digit checks by using extra method 'isNumericChar(...)' in NumberConversionUtil. --- src/main/java/org/json/NumberConversionUtil.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java index d53c5e2..30ca74d 100644 --- a/src/main/java/org/json/NumberConversionUtil.java +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -24,7 +24,7 @@ class NumberConversionUtil { val = "-0."+val.substring(2); } char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-' ) { + if ( isNumericChar(initial) || initial == '-' ) { // decimal representation if (isDecimalNotation(val)) { // Use a BigDecimal all the time so we keep the original @@ -53,13 +53,13 @@ class NumberConversionUtil { initial = val.charAt(0); if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { + if(isNumericChar(at1)) { throw new NumberFormatException("val ["+input+"] is not a valid number."); } } else if (initial == '-' && val.length() > 2) { char at1 = val.charAt(1); char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { + if(at1 == '0' && isNumericChar(at2)) { throw new NumberFormatException("val ["+input+"] is not a valid number."); } } @@ -83,6 +83,16 @@ class NumberConversionUtil { throw new NumberFormatException("val ["+input+"] is not a valid number."); } + /** + * Checks if the character is a numeric digit ('0' to '9'). + * + * @param c The character to be checked. + * @return true if the character is a numeric digit, false otherwise. + */ + private static boolean isNumericChar(char c) { + return (c >= '0' && c <= '9'); + } + /** * Checks if the value could be considered a number in decimal number system. * @param value