Skip to content

Commit 2d46836

Browse files
committed
support fast parser option
1 parent abb1a28 commit 2d46836

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ private final void _parseSlowFloatValue(boolean exactNumber)
12281228
_numTypesValid = NR_BIGDECIMAL;
12291229
} else {
12301230
// Otherwise double has to do
1231-
_numberDouble = _textBuffer.contentsAsDouble();
1231+
_numberDouble = _textBuffer.contentsAsDouble(_owner.isEnabled(StreamReadFeature.USE_FAST_DOUBLE_PARSER));
12321232
_numTypesValid = NR_DOUBLE;
12331233
}
12341234
} catch (NumberFormatException nex) {

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/NumberInput.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,24 @@ public final static boolean inLongRange(char[] digitChars, int offset, int len,
9494
return true;
9595
}
9696

97-
public final static double parseDouble(String numStr) throws NumberFormatException
98-
{
99-
return Double.parseDouble(numStr);
97+
/**
98+
* @param s a string representing a number to parse
99+
* @return closest matching double
100+
* @throws NumberFormatException if string cannot be represented by a double where useFastParser=false
101+
* @see #parseDouble(String, boolean)
102+
*/
103+
public static double parseDouble(final String s) throws NumberFormatException {
104+
return parseDouble(s, false);
105+
}
106+
107+
/**
108+
* @param s a string representing a number to parse
109+
* @param useFastParser whether to use {@link com.fasterxml.jackson.core.io.doubleparser}
110+
* @return closest matching double
111+
* @throws NumberFormatException if string cannot be represented by a double
112+
* @since v2.14
113+
*/
114+
public static double parseDouble(final String s, final boolean useFastParser) throws NumberFormatException {
115+
return com.fasterxml.jackson.core.io.NumberInput.parseDouble(s, useFastParser);
100116
}
101117
}

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/TextBuffer.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,24 @@ public BigDecimal contentsAsDecimal()
313313
* Convenience method for converting contents of the buffer
314314
* into a Double value.
315315
*/
316-
public double contentsAsDouble()
317-
throws NumberFormatException
318-
{
316+
public double contentsAsDouble() throws NumberFormatException {
319317
return NumberInput.parseDouble(contentsAsString());
320318
}
321319

320+
/**
321+
* Convenience method for converting contents of the buffer
322+
* into a Double value.
323+
*
324+
* @param useFastParser whether to use {@link com.fasterxml.jackson.core.io.doubleparser}
325+
* @return Buffered text value parsed as a {@link Double}, if possible
326+
*
327+
* @throws NumberFormatException if contents are not a valid Java number
328+
* @since 2.14
329+
*/
330+
public double contentsAsDouble(final boolean useFastParser) throws NumberFormatException {
331+
return NumberInput.parseDouble(contentsAsString(), useFastParser);
332+
}
333+
322334
public boolean looksLikeInt() {
323335
final char[] ch = contentsAsArray();
324336
final int len = ch.length;

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/FeaturesTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.fasterxml.jackson.dataformat.csv;
22

3+
import com.fasterxml.jackson.core.StreamReadFeature;
4+
import com.fasterxml.jackson.core.StreamWriteFeature;
5+
36
import java.io.StringWriter;
47

58
public class FeaturesTest extends ModuleTestBase
@@ -26,4 +29,13 @@ public void testFactoryFeatures() throws Exception
2629
assertTrue(g.canUseSchema(CsvSchema.emptySchema()));
2730
g.close();
2831
}
32+
33+
public void testFactoryFastFeatures() throws Exception
34+
{
35+
CsvFactory f = new CsvFactory();
36+
f.enable(StreamReadFeature.USE_FAST_DOUBLE_PARSER.mappedFeature());
37+
assertTrue(f.isEnabled(StreamReadFeature.USE_FAST_DOUBLE_PARSER.mappedFeature()));
38+
f.enable(StreamWriteFeature.USE_FAST_DOUBLE_WRITER.mappedFeature());
39+
assertTrue(f.isEnabled(StreamWriteFeature.USE_FAST_DOUBLE_WRITER.mappedFeature()));
40+
}
2941
}

0 commit comments

Comments
 (0)