You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/05/09 14:39:37 UTC
[GitHub] [commons-lang] garydgregory commented on a change in pull request #751: create API for searching for numbers within string - solving LANG-165…
garydgregory commented on a change in pull request #751:
URL: https://github.com/apache/commons-lang/pull/751#discussion_r628898328
##########
File path: src/test/java/org/apache/commons/lang3/StringUtilsTest.java
##########
@@ -3338,6 +3341,95 @@ public void testToRootUpperCase() {
}
}
+ @Test
+ void extractNumbersFromString() {
Review comment:
By convention test method have the "test" prefix.
You should break up each use case in their own method IMO.
There are no tests for edge cases: null and empty string. Also a non empty string with no numbers.
##########
File path: src/main/java/org/apache/commons/lang3/StringUtils.java
##########
@@ -9638,6 +9638,79 @@ public static String wrapIfMissing(final String str, final String wrapWith) {
return builder.toString();
}
+ /**
+ * Method that assembles all the numbers, form the passed string and returns them as list.
+ * It is important to note here, is that bu 'number' method assume any digit sequence, that
+ * can (but not necessary at all) contains dot within it (I mean just plain old floats,
+ * something like 51.82)
+ *
+ * For example, you may pass a string "21.2 days 3 minutes 22 seconds". For this particular string
+ * the result list of doubles will look like this : [21.2, 3.0, 22.0]
+ *
+ * if string contains invalid numbers (for example this string contains
+ * not valid number: "My height is 1234.23.13" This is invalid because it
+ * is not clear how to parse this part - 1234.23.13), {@link NumberFormatException}
+ * will be thrown. Though if string will contain number, where right
+ * after second dot resides not a number, or, any other char, then this
+ * case will be considered as valid. For example, this string contains
+ * only valid numbers: "My pulse is 90.123. and weight is 78.2"
+ * In this case sequence "90.123." will be considered as "90.123", as well as
+ * sequence "90." (imagine that there is no digit right after dot) will be
+ * considered as 90.0 double.
+ *
+ * @param stringThatContainsNumbers - string, that contains number or several numbers.
+ * Not necessary integers, may be numbers with float point.
+ * @return - list of numbers, that this particular string contains
+ *
+ * @throws NumberFormatException - see documentation clarification about cases when thrown above
+ */
+ public static List<Double> extractNumbersFromString(String stringThatContainsNumbers) {
+ boolean hasDigitAlreadyStarted = false;
+ boolean alreadyMetDotInThisNumber = false;
+
+ List<Double> resultList = new ArrayList<>();
+
+ StringBuilder currentNumberAsStringBuilder = new StringBuilder("");
+
+ for (int i = 0; i < stringThatContainsNumbers.length(); i++) {
+ char currentSymbol = stringThatContainsNumbers.charAt(i);
+ if (Character.isDigit(currentSymbol)) {
+ if (!hasDigitAlreadyStarted) {
+ hasDigitAlreadyStarted = true;
+ }
+ currentNumberAsStringBuilder.append(currentSymbol);
+ continue;
+ } else if (currentSymbol == '.') {
Review comment:
This will not work on locales that use a different character for the decimal marker.
##########
File path: src/main/java/org/apache/commons/lang3/StringUtils.java
##########
@@ -9638,6 +9638,79 @@ public static String wrapIfMissing(final String str, final String wrapWith) {
return builder.toString();
}
+ /**
+ * Method that assembles all the numbers, form the passed string and returns them as list.
+ * It is important to note here, is that bu 'number' method assume any digit sequence, that
+ * can (but not necessary at all) contains dot within it (I mean just plain old floats,
+ * something like 51.82)
+ *
+ * For example, you may pass a string "21.2 days 3 minutes 22 seconds". For this particular string
+ * the result list of doubles will look like this : [21.2, 3.0, 22.0]
+ *
+ * if string contains invalid numbers (for example this string contains
+ * not valid number: "My height is 1234.23.13" This is invalid because it
+ * is not clear how to parse this part - 1234.23.13), {@link NumberFormatException}
+ * will be thrown. Though if string will contain number, where right
+ * after second dot resides not a number, or, any other char, then this
+ * case will be considered as valid. For example, this string contains
+ * only valid numbers: "My pulse is 90.123. and weight is 78.2"
+ * In this case sequence "90.123." will be considered as "90.123", as well as
+ * sequence "90." (imagine that there is no digit right after dot) will be
+ * considered as 90.0 double.
+ *
+ * @param stringThatContainsNumbers - string, that contains number or several numbers.
+ * Not necessary integers, may be numbers with float point.
+ * @return - list of numbers, that this particular string contains
+ *
+ * @throws NumberFormatException - see documentation clarification about cases when thrown above
+ */
+ public static List<Double> extractNumbersFromString(String stringThatContainsNumbers) {
Review comment:
The API name says Number but the return type says Double. They should match.
##########
File path: src/test/java/org/apache/commons/lang3/StringUtilsTest.java
##########
@@ -3338,6 +3341,95 @@ public void testToRootUpperCase() {
}
}
+ @Test
+ void extractNumbersFromString() {
+ String input;
+ List<Double> expectedList = new ArrayList<>();
+
+ expectedList.add(123.112);
+ expectedList.add(42.0);
+ input = "Some number 123.112 another is 42.";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
+
+ expectedList.add(3.1);
+ expectedList.add(49.0);
+ input = "3.1 pinokio49";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
+
+ expectedList.add(1.11);
+ expectedList.add(0.42);
+ input = "Heartbeat : 1.11. pressure is is 0.42";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
+
+ expectedList.add(1.11);
+ expectedList.add(0.42);
+ input = "Heartbeat : 1.11. pressure is is 0.42";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
+
+ expectedList.add(737.0);
+ expectedList.add(42.23);
+ input = "737 another is 42.23.";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
+
+ expectedList.add(737.0);
+ expectedList.add(2193.0);
+ expectedList.add(12.1);
+ expectedList.add(0.11);
+ input = "737 2193, 12.1, 0.11.";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
+
+ expectedList.add(737.0);
+ expectedList.add(29.1);
+ expectedList.add(299.0);
+ input = "737. something29.1. 299";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
+
+ expectedList.add(737.0);
+ expectedList.add(29.1);
+ expectedList.add(299.0);
+ Assertions.assertThrows(NumberFormatException.class, () -> StringUtils.extractNumbersFromString("737 another is 42.23.32"));
+ expectedList.clear();
+
+ expectedList.add(737.0);
+ expectedList.add(29.1);
+ expectedList.add(299.0);
+ input = "737. something29.1. 299";
+ Assertions.assertEquals(expectedList, StringUtils.extractNumbersFromString(input));
+ expectedList.clear();
Review comment:
Why is this an error condition? Sentences end in a period.
How would you treat commas in a sentence when in a locale like French that uses the comma as the decimal separator?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org