summaryrefslogtreecommitdiffstats
path: root/luni/src/main
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2011-01-26 14:36:51 -0800
committerElliott Hughes <enh@google.com>2011-01-26 15:21:11 -0800
commit8899157a607a6bfd4ca4f361c77db8726885e47b (patch)
treee7606a90d2a8e87a68f1630cacf9c016fa1910e5 /luni/src/main
parent30a7ff69872ad0b8de60550a740818b645ba0f29 (diff)
downloadlibcore-8899157a607a6bfd4ca4f361c77db8726885e47b.zip
libcore-8899157a607a6bfd4ca4f361c77db8726885e47b.tar.gz
libcore-8899157a607a6bfd4ca4f361c77db8726885e47b.tar.bz2
Minor float/double parsing improvements.
This removes some duplication and fixes an actual bug: we'd accept strings like ".NaN" or "Infinity." because the length check was done incorrectly. Also add explicit unit tests for some Double.toString duplicate bug reports, since that ought to be SOP. (I'm here to fix an unrelated bug, but want to keep this cleanup separate.) Change-Id: I197613afe52fae8da7a1598bb6b43514dcc03b98
Diffstat (limited to 'luni/src/main')
-rw-r--r--luni/src/main/java/org/apache/harmony/luni/util/FloatingPointParser.java65
1 files changed, 14 insertions, 51 deletions
diff --git a/luni/src/main/java/org/apache/harmony/luni/util/FloatingPointParser.java b/luni/src/main/java/org/apache/harmony/luni/util/FloatingPointParser.java
index f4774fd..038c1d3 100644
--- a/luni/src/main/java/org/apache/harmony/luni/util/FloatingPointParser.java
+++ b/luni/src/main/java/org/apache/harmony/luni/util/FloatingPointParser.java
@@ -196,66 +196,29 @@ public final class FloatingPointParser {
return new StringExponentPair(s, e, negative);
}
- /*
- * Assumes the string is trimmed.
- */
- private static double parseDblName(String namedDouble, int length) {
- // Valid strings are only +Nan, NaN, -Nan, +Infinity, Infinity,
- // -Infinity.
- if ((length != 3) && (length != 4) && (length != 8) && (length != 9)) {
- throw invalidReal(namedDouble, true);
- }
-
+ // Parses "+Nan", "NaN", "-Nan", "+Infinity", "Infinity", and "-Infinity", case-insensitively.
+ private static float parseName(String name, boolean isDouble) {
+ // Explicit sign?
boolean negative = false;
int i = 0;
- char firstChar = namedDouble.charAt(i);
- if (firstChar == '-') {
- negative = true;
- ++i;
- } else if (firstChar == '+') {
- ++i;
- }
-
- if (namedDouble.regionMatches(false, i, "Infinity", 0, 8)) {
- return negative ? Double.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
- }
-
- if (namedDouble.regionMatches(false, i, "NaN", 0, 3)) {
- return Double.NaN;
- }
-
- throw invalidReal(namedDouble, true);
- }
-
- /*
- * Assumes the string is trimmed.
- */
- private static float parseFltName(String namedFloat, int length) {
- // Valid strings are only +Nan, NaN, -Nan, +Infinity, Infinity,
- // -Infinity.
- if ((length != 3) && (length != 4) && (length != 8) && (length != 9)) {
- throw invalidReal(namedFloat, false);
- }
-
- boolean negative = false;
- int i = 0;
- char firstChar = namedFloat.charAt(i);
+ int length = name.length();
+ char firstChar = name.charAt(i);
if (firstChar == '-') {
negative = true;
++i;
+ --length;
} else if (firstChar == '+') {
++i;
+ --length;
}
- if (namedFloat.regionMatches(false, i, "Infinity", 0, 8)) {
+ if (length == 8 && name.regionMatches(false, i, "Infinity", 0, 8)) {
return negative ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
}
-
- if (namedFloat.regionMatches(false, i, "NaN", 0, 3)) {
+ if (length == 3 && name.regionMatches(false, i, "NaN", 0, 3)) {
return Float.NaN;
}
-
- throw invalidReal(namedFloat, false);
+ throw invalidReal(name, isDouble);
}
/**
@@ -278,8 +241,8 @@ public final class FloatingPointParser {
// See if this could be a named double
char last = s.charAt(length - 1);
- if ((last == 'y') || (last == 'N')) {
- return parseDblName(s, length);
+ if (last == 'y' || last == 'N') {
+ return parseName(s, true);
}
// See if it could be a hexadecimal representation.
@@ -320,8 +283,8 @@ public final class FloatingPointParser {
// See if this could be a named float
char last = s.charAt(length - 1);
- if ((last == 'y') || (last == 'N')) {
- return parseFltName(s, length);
+ if (last == 'y' || last == 'N') {
+ return parseName(s, false);
}
// See if it could be a hexadecimal representation