-
Notifications
You must be signed in to change notification settings - Fork 17
/
FastDoubleParser.java
542 lines (513 loc) · 18.9 KB
/
FastDoubleParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* @(#)FastDoubleParser.java
* Copyright © 2021. Werner Randelshofer, Switzerland. MIT License.
*/
package ch.randelshofer.fastdoubleparser;
/**
* This is a C++ to Java port of Daniel Lemire's fast_double_parser.
* <p>
* The code has been changed, so that it parses the same syntax as
* {@link Double#parseDouble(String)}.
* <p>
* References:
* <dl>
* <dt>Daniel Lemire, fast_double_parser, 4x faster than strtod.
* Apache License 2.0 or Boost Software License.</dt>
* <dd><a href="https://github.com/lemire/fast_double_parser">github.com</a></dd>
*
* <dt>Daniel Lemire, fast_float number parsing library: 4x faster than strtod.
* Apache License 2.0.</dt>
* <dd><a href="https://github.com/fastfloat/fast_float">github.com</a></dd>
*
* <dt>Daniel Lemire, Number Parsing at a Gigabyte per Second.
* arXiv.2101.11408v3 [cs.DS] 24 Feb 2021</dt>
* <dd><a href="https://arxiv.org/pdf/2101.11408.pdf">arxiv.org</a></dd>
* </dl>
*/
public class FastDoubleParser {
private final static long MINIMAL_NINETEEN_DIGIT_INTEGER = 1000_00000_00000_00000L;
private final static int MINIMAL_EIGHT_DIGIT_INTEGER = 10_000_000;
/**
* Special value in {@link #CHAR_TO_HEX_MAP} for
* the decimal point character.
*/
private static final byte DECIMAL_POINT_CLASS = -4;
/**
* Special value in {@link #CHAR_TO_HEX_MAP} for
* characters that are neither a hex digit nor
* a decimal point character..
*/
private static final byte OTHER_CLASS = -1;
/**
* A table of 128 entries or of entries up to including
* character 'p' would suffice.
* <p>
* However for some reason, performance is best,
* if this table has exactly 256 entries.
*/
private static final byte[] CHAR_TO_HEX_MAP = new byte[256];
static {
for (char ch = 0; ch < CHAR_TO_HEX_MAP.length; ch++) {
CHAR_TO_HEX_MAP[ch] = OTHER_CLASS;
}
for (char ch = '0'; ch <= '9'; ch++) {
CHAR_TO_HEX_MAP[ch] = (byte) (ch - '0');
}
for (char ch = 'A'; ch <= 'F'; ch++) {
CHAR_TO_HEX_MAP[ch] = (byte) (ch - 'A' + 10);
}
for (char ch = 'a'; ch <= 'f'; ch++) {
CHAR_TO_HEX_MAP[ch] = (byte) (ch - 'a' + 10);
}
for (char ch = '.'; ch <= '.'; ch++) {
CHAR_TO_HEX_MAP[ch] = DECIMAL_POINT_CLASS;
}
}
/**
* Prevents instantiation.
*/
private FastDoubleParser() {
}
private static boolean isInteger(char c) {
return '0' <= c && c <= '9';
}
private static NumberFormatException newNumberFormatException(CharSequence str) {
if (str.length() > 1024) {
// str can be up to Integer.MAX_VALUE characters long
return new NumberFormatException("For input string of length " + str.length());
} else {
return new NumberFormatException("For input string: \"" + str.toString().trim() + "\"");
}
}
/**
* Returns a Double object holding the double value represented by the
* argument string {@code str}.
* <p>
* This method can be used as a drop in for method
* {@link Double#valueOf(String)}. (Assuming that the API of this method
* has not changed since Java SE 16).
* <p>
* Leading and trailing whitespace characters in {@code str} are ignored.
* Whitespace is removed as if by the {@link String#trim()} method;
* that is, characters in the range [U+0000,U+0020].
* <p>
* The rest of {@code str} should constitute a FloatValue as described by the
* lexical syntax rules shown below:
* <blockquote>
* <dl>
* <dt><i>FloatValue:</i>
* <dd><i>[Sign]</i> {@code NaN}
* <dd><i>[Sign]</i> {@code Infinity}
* <dd><i>[Sign] DecimalFloatingPointLiteral</i>
* <dd><i>[Sign] HexFloatingPointLiteral</i>
* <dd><i>SignedInteger</i>
* </dl>
*
* <dl>
* <dt><i>HexFloatingPointLiteral</i>:
* <dd><i>HexSignificand BinaryExponent</i>
* </dl>
*
* <dl>
* <dt><i>HexSignificand:</i>
* <dd><i>HexNumeral</i>
* <dd><i>HexNumeral</i> {@code .}
* <dd>{@code 0x} <i>[HexDigits]</i> {@code .} <i>HexDigits</i>
* <dd>{@code 0X} <i>[HexDigits]</i> {@code .} <i>HexDigits</i>
* </dl>
*
* <dl>
* <dt><i>HexSignificand:</i>
* <dd><i>HexNumeral</i>
* <dd><i>HexNumeral</i> {@code .}
* <dd>{@code 0x} <i>[HexDigits]</i> {@code .} <i>HexDigits</i>
* <dd>{@code 0X} <i>[HexDigits]</i> {@code .} <i>HexDigits</i>
* </dl>
*
* <dl>
* <dt><i>BinaryExponent:</i>
* <dd><i>BinaryExponentIndicator SignedInteger</i>
* </dl>
*
* <dl>
* <dt><i>BinaryExponentIndicator:</i>
* <dd>{@code p}
* <dd>{@code P}
* </dl>
*
* <dl>
* <dt><i>DecimalFloatingPointLiteral:</i>
* <dd><i>Digits {@code .} [Digits] [ExponentPart]</i>
* <dd><i>{@code .} Digits [ExponentPart]</i>
* <dd><i>Digits ExponentPart</i>
* </dl>
*
* <dl>
* <dt><i>ExponentPart:</i>
* <dd><i>ExponentIndicator SignedInteger</i>
* </dl>
*
* <dl>
* <dt><i>ExponentIndicator:</i>
* <dd><i>(one of)</i>
* <dd><i>e E</i>
* </dl>
*
* <dl>
* <dt><i>SignedInteger:</i>
* <dd><i>[Sign] Digits</i>
* </dl>
*
* <dl>
* <dt><i>Sign:</i>
* <dd><i>(one of)</i>
* <dd><i>+ -</i>
* </dl>
*
* <dl>
* <dt><i>Digits:</i>
* <dd><i>Digit {Digit}</i>
* </dl>
*
* <dl>
* <dt><i>HexNumeral:</i>
* <dd>{@code 0} {@code x} <i>HexDigits</i>
* <dd>{@code 0} {@code X} <i>HexDigits</i>
* </dl>
*
* <dl>
* <dt><i>HexDigits:</i>
* <dd><i>HexDigit {HexDigit}</i>
* </dl>
*
* <dl>
* <dt><i>HexDigit:</i>
* <dd><i>(one of)</i>
* <dd>{@code 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F}
* </dl>
* </blockquote>
*
* @param str the string to be parsed
* @return the parsed double value
* @throws NumberFormatException if the string can not be parsed
*/
public static double parseDouble(CharSequence str) throws NumberFormatException {
final int endIndex = str.length();
// Skip leading whitespace
// -------------------
int index = skipWhitespace(str, 0, endIndex);
if (index == endIndex) {
throw new NumberFormatException("empty String");
}
char ch = str.charAt(index);
// Parse optional sign
// -------------------
final boolean isNegative = ch == '-';
if (isNegative || ch == '+') {
ch = ++index < endIndex ? str.charAt(index) : 0;
if (ch == 0) {
throw newNumberFormatException(str);
}
}
// Parse NaN or Infinity
// ---------------------
if (ch == 'N') {
return parseNaN(str, index, endIndex);
} else if (ch == 'I') {
return parseInfinity(str, index, endIndex, isNegative);
}
// Parse optional leading zero
// ---------------------------
final boolean hasLeadingZero = ch == '0';
if (hasLeadingZero) {
ch = ++index < endIndex ? str.charAt(index) : 0;
if (ch == 'x' || ch == 'X') {
return parseRestOfHexFloatingPointLiteral(str, index + 1, endIndex, isNegative);
}
}
return parseRestOfDecimalFloatLiteral(str, endIndex, index, isNegative, hasLeadingZero);
}
private static double parseInfinity(CharSequence str, int index, int endIndex, boolean negative) {
if (index + 7 < endIndex
// && str.charAt(index) == 'I'
&& str.charAt(index + 1) == 'n'
&& str.charAt(index + 2) == 'f'
&& str.charAt(index + 3) == 'i'
&& str.charAt(index + 4) == 'n'
&& str.charAt(index + 5) == 'i'
&& str.charAt(index + 6) == 't'
&& str.charAt(index + 7) == 'y'
) {
index = skipWhitespace(str, index + 8, endIndex);
if (index < endIndex) {
throw newNumberFormatException(str);
}
return negative ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
} else {
throw newNumberFormatException(str);
}
}
private static double parseNaN(CharSequence str, int index, int endIndex) {
if (index + 2 < endIndex
// && str.charAt(index) == 'N'
&& str.charAt(index + 1) == 'a'
&& str.charAt(index + 2) == 'N') {
index = skipWhitespace(str, index + 3, endIndex);
if (index < endIndex) {
throw newNumberFormatException(str);
}
return Double.NaN;
} else {
throw newNumberFormatException(str);
}
}
/**
* Parses the following rules
* (more rules are defined in {@link #parseDouble(CharSequence)}):
* <dl>
* <dt><i>RestOfDecimalFloatingPointLiteral</i>:
* <dd><i>[Digits] {@code .} [Digits] [ExponentPart]</i>
* <dd><i>{@code .} Digits [ExponentPart]</i>
* <dd><i>[Digits] ExponentPart</i>
* </dl>
*
* @param str the input string
* @param endIndex the length of the string
* @param index index to the first character of RestOfHexFloatingPointLiteral
* @param isNegative if the resulting number is negative
* @param hasLeadingZero if the digit '0' has been consumed
* @return a double representation
*/
private static double parseRestOfDecimalFloatLiteral(CharSequence str, int endIndex, int index, boolean isNegative, boolean hasLeadingZero) {
// Parse digits
// ------------
// Note: a multiplication by a constant is cheaper than an
// arbitrary integer multiplication.
long digits = 0;// digits is treated as an unsigned long
int exponent = 0;
final int indexOfFirstDigit = index;
int virtualIndexOfPoint = -1;
final int digitCount;
char ch = 0;
for (; index < endIndex; index++) {
ch = str.charAt(index);
if (isInteger(ch)) {
// This might overflow, we deal with it later.
digits = 10 * digits + ch - '0';
} else if (ch == '.') {
if (virtualIndexOfPoint != -1) {
throw newNumberFormatException(str);
}
virtualIndexOfPoint = index;
} else {
break;
}
}
final int indexAfterDigits = index;
if (virtualIndexOfPoint == -1) {
digitCount = indexAfterDigits - indexOfFirstDigit;
virtualIndexOfPoint = indexAfterDigits;
} else {
digitCount = indexAfterDigits - indexOfFirstDigit - 1;
exponent = virtualIndexOfPoint - index + 1;
}
// Parse exponent number
// ---------------------
long exp_number = 0;
final boolean hasExponent = (ch == 'e') || (ch == 'E');
if (hasExponent) {
ch = ++index < endIndex ? str.charAt(index) : 0;
boolean neg_exp = ch == '-';
if (neg_exp || ch == '+') {
ch = ++index < endIndex ? str.charAt(index) : 0;
}
if (!isInteger(ch)) {
throw newNumberFormatException(str);
}
do {
// Guard against overflow of exp_number
if (exp_number < MINIMAL_EIGHT_DIGIT_INTEGER) {
exp_number = 10 * exp_number + ch - '0';
}
ch = ++index < endIndex ? str.charAt(index) : 0;
} while (isInteger(ch));
if (neg_exp) {
exp_number = -exp_number;
}
exponent += exp_number;
}
// Skip trailing whitespace
// ------------------------
index = skipWhitespace(str, index, endIndex);
if (index < endIndex
|| !hasLeadingZero && digitCount == 0 && str.charAt(virtualIndexOfPoint) != '.') {
throw newNumberFormatException(str);
}
// Re-parse digits in case of a potential overflow
// -----------------------------------------------
final boolean isDigitsTruncated;
int skipCountInTruncatedDigits = 0;//counts +1 if we skipped over the decimal point
if (digitCount > 19) {
digits = 0;
for (index = indexOfFirstDigit; index < indexAfterDigits; index++) {
ch = str.charAt(index);
if (ch == '.') {
skipCountInTruncatedDigits++;
} else {
if (Long.compareUnsigned(digits, MINIMAL_NINETEEN_DIGIT_INTEGER) < 0) {
digits = 10 * digits + ch - '0';
} else {
break;
}
}
}
isDigitsTruncated = (index < indexAfterDigits);
} else {
isDigitsTruncated = false;
}
Double result = FastDoubleMath.decFloatLiteralToDouble(index, isNegative, digits, exponent, virtualIndexOfPoint, exp_number, isDigitsTruncated, skipCountInTruncatedDigits);
if (result == null) {
return parseRestOfDecimalFloatLiteralTheHardWay(str);
}
return result;
}
/**
* Parses the following rules
* (more rules are defined in {@link #parseDouble(CharSequence)}):
* <dl>
* <dt><i>RestOfDecimalFloatingPointLiteral</i>:
* <dd><i>[Digits] {@code .} [Digits] [ExponentPart]</i>
* <dd><i>{@code .} Digits [ExponentPart]</i>
* <dd><i>[Digits] ExponentPart</i>
* </dl>
* @param str the input string
*/
private static double parseRestOfDecimalFloatLiteralTheHardWay(CharSequence str) {
return Double.parseDouble(str.toString());
}
/**
* Parses the following rules
* (more rules are defined in {@link #parseDouble(CharSequence)}):
* <dl>
* <dt><i>RestOfHexFloatingPointLiteral</i>:
* <dd><i>RestOfHexSignificand BinaryExponent</i>
* </dl>
*
* <dl>
* <dt><i>RestOfHexSignificand:</i>
* <dd><i>HexDigits</i>
* <dd><i>HexDigits</i> {@code .}
* <dd><i>[HexDigits]</i> {@code .} <i>HexDigits</i>
* </dl>
*
* @param str the input string
* @param index index to the first character of RestOfHexFloatingPointLiteral
* @param endIndex the end index of the string
* @param isNegative if the resulting number is negative
* @return a double representation
*/
private static double parseRestOfHexFloatingPointLiteral(
CharSequence str, int index, int endIndex, boolean isNegative) {
if (index >= endIndex) {
throw newNumberFormatException(str);
}
// Parse digits
// ------------
long digits = 0;// digits is treated as an unsigned long
int exponent = 0;
final int indexOfFirstDigit = index;
int virtualIndexOfPoint = -1;
final int digitCount;
char ch = 0;
for (; index < endIndex; index++) {
ch = str.charAt(index);
// Table look up is faster than a sequence of if-else-branches.
int hexValue = ch > 255 ? OTHER_CLASS : CHAR_TO_HEX_MAP[ch];
if (hexValue >= 0) {
digits = (digits << 4) | hexValue;// This might overflow, we deal with it later.
} else if (hexValue == DECIMAL_POINT_CLASS) {
if (virtualIndexOfPoint != -1) {
throw newNumberFormatException(str);
}
virtualIndexOfPoint = index;
} else {
break;
}
}
final int indexAfterDigits = index;
if (virtualIndexOfPoint == -1) {
digitCount = indexAfterDigits - indexOfFirstDigit;
virtualIndexOfPoint = indexAfterDigits;
} else {
digitCount = indexAfterDigits - indexOfFirstDigit - 1;
exponent = Math.min(virtualIndexOfPoint - index + 1, MINIMAL_EIGHT_DIGIT_INTEGER) * 4;
}
// Parse exponent number
// ---------------------
long exp_number = 0;
final boolean hasExponent = (ch == 'p') || (ch == 'P');
if (hasExponent) {
ch = ++index < endIndex ? str.charAt(index) : 0;
boolean neg_exp = ch == '-';
if (neg_exp || ch == '+') {
ch = ++index < endIndex ? str.charAt(index) : 0;
}
if (!isInteger(ch)) {
throw newNumberFormatException(str);
}
do {
// Guard against overflow of exp_number
if (exp_number < MINIMAL_EIGHT_DIGIT_INTEGER) {
exp_number = 10 * exp_number + ch - '0';
}
ch = ++index < endIndex ? str.charAt(index) : 0;
} while (isInteger(ch));
if (neg_exp) {
exp_number = -exp_number;
}
exponent += exp_number;
}
// Skip trailing whitespace
// ------------------------
index = skipWhitespace(str, index, endIndex);
if (index < endIndex
|| digitCount == 0 && str.charAt(virtualIndexOfPoint) != '.'
|| !hasExponent) {
throw newNumberFormatException(str);
}
// Re-parse digits in case of a potential overflow
// -----------------------------------------------
final boolean isDigitsTruncated;
int skipCountInTruncatedDigits = 0;//counts +1 if we skipped over the decimal point
if (digitCount > 16) {
digits = 0;
for (index = indexOfFirstDigit; index < indexAfterDigits; index++) {
ch = str.charAt(index);
// Table look up is faster than a sequence of if-else-branches.
int hexValue = ch > 127 ? OTHER_CLASS : CHAR_TO_HEX_MAP[ch];
if (hexValue >= 0) {
if (Long.compareUnsigned(digits, MINIMAL_NINETEEN_DIGIT_INTEGER) < 0) {
digits = (digits << 4) | hexValue;
} else {
break;
}
} else {
skipCountInTruncatedDigits++;
}
}
isDigitsTruncated = (index < indexAfterDigits);
} else {
isDigitsTruncated = false;
}
Double d = FastDoubleMath.hexFloatLiteralToDouble(index, isNegative, digits, exponent, virtualIndexOfPoint, exp_number, isDigitsTruncated, skipCountInTruncatedDigits);
return d == null ? Double.parseDouble(str.toString()) : d;
}
private static int skipWhitespace(CharSequence str, int index, int endIndex) {
for (; index < endIndex; index++) {
if (str.charAt(index) > 0x20) {
break;
}
}
return index;
}
}