Repository: commons-lang Updated Branches: refs/heads/master ec8bf5281 -> 28f7862ab
LANG-1151: Performance improvements for NumberUtils.isParsable (closes #99) ~2.5x performance increase wrt 3.4 version; added a couple checks more to unit tests Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/54ff33b2 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/54ff33b2 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/54ff33b2 Branch: refs/heads/master Commit: 54ff33b2ad26e3969cc59d7f2723563dee3e37dc Parents: ec8bf52 Author: Juan Pablo Santos RodrÃguez <juanpablo.san...@gmail.com> Authored: Mon Jun 22 13:53:06 2015 +0200 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Wed May 11 21:43:36 2016 +0200 ---------------------------------------------------------------------- .../apache/commons/lang3/math/NumberUtils.java | 32 +++++++++++++++++--- .../commons/lang3/math/NumberUtilsTest.java | 2 ++ 2 files changed, 30 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54ff33b2/src/main/java/org/apache/commons/lang3/math/NumberUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java index c640ceb..d602524 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -1504,13 +1504,37 @@ public class NumberUtils { * @since 3.4 */ public static boolean isParsable(final String str) { - if( StringUtils.endsWith( str, "." ) ) { + if (StringUtils.isEmpty(str)) { + return false; + } + if (str.charAt(str.length() - 1) == '.') { return false; } - if( StringUtils.startsWith( str, "-" ) ) { - return isDigits( StringUtils.replaceOnce( str.substring(1), ".", StringUtils.EMPTY ) ); + if (str.charAt(0) == '-') { + if (str.length() == 1) { + return false; + } + return withDecimalsParsing(str, 1); + } else { + return withDecimalsParsing(str, 0); + } + } + + private static boolean withDecimalsParsing(final String str, final int beginIdx) { + int decimalPoints = 0; + for (int i = beginIdx; i < str.length(); i++) { + final boolean isDecimalPoint = str.charAt(i) == '.'; + if (str.charAt(i) == '.') { + decimalPoints++; + } + if (decimalPoints > 1) { + return false; + } + if (!isDecimalPoint && !Character.isDigit(str.charAt(i))) { + return false; + } } - return isDigits( StringUtils.replaceOnce( str, ".", StringUtils.EMPTY ) ); + return true; } /** http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54ff33b2/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java index 92977d5..236084b 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -1304,6 +1304,8 @@ public class NumberUtilsTest { assertFalse( NumberUtils.isParsable("64.2.2") ); assertFalse( NumberUtils.isParsable("64.") ); assertFalse( NumberUtils.isParsable("64L") ); + assertFalse( NumberUtils.isParsable("-") ); + assertFalse( NumberUtils.isParsable("--2") ); assertTrue( NumberUtils.isParsable("64.2") ); assertTrue( NumberUtils.isParsable("64") ); assertTrue( NumberUtils.isParsable("018") );