Skip to content

Commit 4cab916

Browse files
optimize DoubleConverter
1 parent 47a0c78 commit 4cab916

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

quickfixj-core/src/main/java/quickfix/field/converter/DoubleConverter.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@
2525
import java.text.DecimalFormat;
2626
import java.text.DecimalFormatSymbols;
2727
import java.util.Locale;
28-
import java.util.regex.Matcher;
29-
import java.util.regex.Pattern;
3028

3129
/**
3230
* Converts between a double and a String.
3331
*/
3432
public class DoubleConverter {
35-
private static final Pattern DECIMAL_PATTERN = Pattern.compile("-?\\d*(\\.\\d*)?");
3633
private static final ThreadLocal<DecimalFormat[]> THREAD_DECIMAL_FORMATS = new ThreadLocal<>();
3734

3835
/**
@@ -92,13 +89,25 @@ public static String convert(double d, int padding) {
9289
*/
9390
public static double convert(String value) throws FieldConvertError {
9491
try {
95-
Matcher matcher = DECIMAL_PATTERN.matcher(value);
96-
if (!matcher.matches()) {
97-
throw new NumberFormatException();
98-
}
99-
return Double.parseDouble(value);
92+
return parseDouble(value);
10093
} catch (NumberFormatException e) {
10194
throw new FieldConvertError("invalid double value: " + value);
10295
}
10396
}
97+
98+
private static double parseDouble(String value) {
99+
if(value.length() == 0) throw new NumberFormatException(value);
100+
boolean dot = false; int i = 0;
101+
char c = value.charAt(i);
102+
switch (c) {
103+
case '-': i++; break;
104+
case '+': throw new NumberFormatException(value);
105+
}
106+
for (; i < value.length(); i++) {
107+
c = value.charAt(i);
108+
if (!dot && c == '.') dot = true;
109+
else if (!Character.isDigit(c)) throw new NumberFormatException(value);
110+
}
111+
return Double.parseDouble(value);
112+
}
104113
}

0 commit comments

Comments
 (0)