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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
#![doc(hidden)]
#[cfg(any(feature = "compact", feature = "radix"))]
use crate::bellerophon::bellerophon;
#[cfg(feature = "power-of-two")]
use crate::binary::{binary, slow_binary};
use crate::float::{extended_to_float, ExtendedFloat80, LemireFloat};
#[cfg(not(feature = "compact"))]
use crate::lemire::lemire;
use crate::number::Number;
use crate::options::Options;
use crate::shared;
use crate::slow::slow_radix;
#[cfg(not(feature = "compact"))]
use lexical_parse_integer::algorithm;
#[cfg(feature = "f16")]
use lexical_util::bf16::bf16;
use lexical_util::digit::{char_to_digit_const, char_to_valid_digit_const};
use lexical_util::error::Error;
#[cfg(feature = "f16")]
use lexical_util::f16::f16;
use lexical_util::format::NumberFormat;
use lexical_util::iterator::{AsBytes, Bytes, BytesIter};
use lexical_util::result::Result;
use lexical_util::step::u64_step;
#[cfg(feature = "power-of-two")]
macro_rules! is_power_two {
($radix:expr) => {
matches!($radix, 2 | 4 | 8 | 16 | 32)
};
}
macro_rules! check_radix {
($format:ident) => {{
let format = NumberFormat::<{ $format }> {};
#[cfg(feature = "power-of-two")]
{
if format.radix() != format.exponent_base() {
let valid_radix = matches!(
(format.radix(), format.exponent_base()),
(4, 2) | (8, 2) | (16, 2) | (32, 2) | (16, 4)
);
if !valid_radix {
return Err(Error::InvalidRadix);
}
}
}
#[cfg(not(feature = "power-of-two"))]
{
if format.radix() != format.exponent_base() {
return Err(Error::InvalidRadix);
}
}
}};
}
pub trait ParseFloat: LemireFloat {
#[cfg_attr(not(feature = "compact"), inline(always))]
fn parse_complete<const FORMAT: u128>(bytes: &[u8], options: &Options) -> Result<Self> {
check_radix!(FORMAT);
parse_complete::<Self, FORMAT>(bytes, options)
}
#[cfg_attr(not(feature = "compact"), inline(always))]
fn parse_partial<const FORMAT: u128>(bytes: &[u8], options: &Options) -> Result<(Self, usize)> {
check_radix!(FORMAT);
parse_partial::<Self, FORMAT>(bytes, options)
}
#[cfg_attr(not(feature = "compact"), inline(always))]
fn fast_path_complete<const FORMAT: u128>(bytes: &[u8], options: &Options) -> Result<Self> {
check_radix!(FORMAT);
fast_path_complete::<Self, FORMAT>(bytes, options)
}
#[cfg_attr(not(feature = "compact"), inline(always))]
fn fast_path_partial<const FORMAT: u128>(
bytes: &[u8],
options: &Options,
) -> Result<(Self, usize)> {
check_radix!(FORMAT);
fast_path_partial::<Self, FORMAT>(bytes, options)
}
}
macro_rules! parse_float_impl {
($($t:ty)*) => ($(
impl ParseFloat for $t {}
)*)
}
parse_float_impl! { f32 f64 }
#[cfg(feature = "f16")]
macro_rules! parse_float_as_f32 {
($($t:ty)*) => ($(
impl ParseFloat for $t {
#[cfg_attr(not(feature = "compact"), inline(always))]
fn parse_complete<const FORMAT: u128>(bytes: &[u8], options: &Options)
-> Result<Self>
{
Ok(Self::from_f32(parse_complete::<f32, FORMAT>(bytes, options)?))
}
#[cfg_attr(not(feature = "compact"), inline(always))]
fn parse_partial<const FORMAT: u128>(bytes: &[u8], options: &Options)
-> Result<(Self, usize)>
{
let (float, count) = parse_partial::<f32, FORMAT>(bytes, options)?;
Ok((Self::from_f32(float), count))
}
#[cfg_attr(not(feature = "compact"), inline(always))]
fn fast_path_complete<const FORMAT: u128>(bytes: &[u8], options: &Options)
-> Result<Self>
{
Ok(Self::from_f32(fast_path_complete::<f32, FORMAT>(bytes, options)?))
}
#[cfg_attr(not(feature = "compact"), inline(always))]
fn fast_path_partial<const FORMAT: u128>(bytes: &[u8], options: &Options)
-> Result<(Self, usize)>
{
let (float, count) = fast_path_partial::<f32, FORMAT>(bytes, options)?;
Ok((Self::from_f32(float), count))
}
}
)*)
}
#[cfg(feature = "f16")]
parse_float_as_f32! { bf16 f16 }
macro_rules! parse_mantissa_sign {
($byte:ident, $format:ident) => {{
match $byte.integer_iter().peek() {
Some(&b'+') if !$format.no_positive_mantissa_sign() => (false, 1),
Some(&b'+') if $format.no_positive_mantissa_sign() => {
return Err(Error::InvalidPositiveSign($byte.cursor()));
},
Some(&b'-') => (true, 1),
Some(_) if $format.required_mantissa_sign() => {
return Err(Error::MissingSign($byte.cursor()));
},
_ => (false, 0),
}
}};
}
macro_rules! parse_exponent_sign {
($byte:ident, $format:ident) => {{
match $byte.integer_iter().peek() {
Some(&b'+') if !$format.no_positive_exponent_sign() => (false, 1),
Some(&b'+') if $format.no_positive_exponent_sign() => {
return Err(Error::InvalidPositiveExponentSign($byte.cursor()));
},
Some(&b'-') => (true, 1),
Some(_) if $format.required_mantissa_sign() => {
return Err(Error::MissingExponentSign($byte.cursor()));
},
_ => (false, 0),
}
}};
}
macro_rules! parse_number {
(
$format:ident,
$byte:ident,
$is_negative:ident,
$options:ident,
$parse_normal:ident,
$parse_special:ident
) => {{
match $parse_normal::<$format>($byte.clone(), $is_negative, $options) {
Ok(n) => n,
Err(e) => {
if let Some(value) =
$parse_special::<_, $format>($byte.clone(), $is_negative, $options)
{
return Ok(value);
} else {
return Err(e);
}
},
}
}};
}
macro_rules! to_native {
($type:ident, $fp:ident, $is_negative:ident) => {{
let mut float = extended_to_float::<$type>($fp);
if $is_negative {
float = -float;
}
float
}};
}
pub fn parse_complete<F: LemireFloat, const FORMAT: u128>(
bytes: &[u8],
options: &Options,
) -> Result<F> {
let format = NumberFormat::<{ FORMAT }> {};
let mut byte = bytes.bytes::<{ FORMAT }>();
let (is_negative, shift) = parse_mantissa_sign!(byte, format);
unsafe { byte.step_by_unchecked(shift) };
if byte.integer_iter().is_consumed() {
return Err(Error::Empty(byte.cursor()));
}
let num = parse_number!(FORMAT, byte, is_negative, options, parse_number, parse_special);
if let Some(value) = num.try_fast_path::<_, FORMAT>() {
return Ok(value);
}
let mut fp = moderate_path::<F, FORMAT>(&num, options.lossy());
if fp.exp < 0 {
debug_assert!(!options.lossy());
fp.exp -= shared::INVALID_FP;
fp = slow_path::<F, FORMAT>(num, fp);
}
Ok(to_native!(F, fp, is_negative))
}
pub fn fast_path_complete<F: LemireFloat, const FORMAT: u128>(
bytes: &[u8],
options: &Options,
) -> Result<F> {
let format = NumberFormat::<{ FORMAT }> {};
let mut byte = bytes.bytes::<{ FORMAT }>();
let (is_negative, shift) = parse_mantissa_sign!(byte, format);
unsafe { byte.step_by_unchecked(shift) };
if byte.integer_iter().is_consumed() {
return Err(Error::Empty(byte.cursor()));
}
let num = parse_number!(FORMAT, byte, is_negative, options, parse_number, parse_special);
Ok(num.force_fast_path::<_, FORMAT>())
}
pub fn parse_partial<F: LemireFloat, const FORMAT: u128>(
bytes: &[u8],
options: &Options,
) -> Result<(F, usize)> {
let format = NumberFormat::<{ FORMAT }> {};
let mut byte = bytes.bytes::<{ FORMAT }>();
let (is_negative, shift) = parse_mantissa_sign!(byte, format);
unsafe { byte.step_by_unchecked(shift) };
if byte.integer_iter().is_consumed() {
return Err(Error::Empty(byte.cursor()));
}
let (num, count) = parse_number!(
FORMAT,
byte,
is_negative,
options,
parse_partial_number,
parse_partial_special
);
if let Some(value) = num.try_fast_path::<_, FORMAT>() {
return Ok((value, count));
}
let mut fp = moderate_path::<F, FORMAT>(&num, options.lossy());
if fp.exp < 0 {
debug_assert!(!options.lossy());
fp.exp -= shared::INVALID_FP;
fp = slow_path::<F, FORMAT>(num, fp);
}
Ok((to_native!(F, fp, is_negative), count))
}
pub fn fast_path_partial<F: LemireFloat, const FORMAT: u128>(
bytes: &[u8],
options: &Options,
) -> Result<(F, usize)> {
let format = NumberFormat::<{ FORMAT }> {};
let mut byte = bytes.bytes::<{ FORMAT }>();
let (is_negative, shift) = parse_mantissa_sign!(byte, format);
unsafe { byte.step_by_unchecked(shift) };
if byte.integer_iter().is_consumed() {
return Err(Error::Empty(byte.cursor()));
}
let (num, count) = parse_number!(
FORMAT,
byte,
is_negative,
options,
parse_partial_number,
parse_partial_special
);
Ok((num.force_fast_path::<_, FORMAT>(), count))
}
#[inline]
pub fn moderate_path<F: LemireFloat, const FORMAT: u128>(
num: &Number,
lossy: bool,
) -> ExtendedFloat80 {
#[cfg(feature = "compact")]
{
#[cfg(feature = "power-of-two")]
{
let format = NumberFormat::<{ FORMAT }> {};
if is_power_two!(format.mantissa_radix()) {
binary::<F, FORMAT>(num, lossy)
} else {
bellerophon::<F, FORMAT>(num, lossy)
}
}
#[cfg(not(feature = "power-of-two"))]
{
bellerophon::<F, FORMAT>(num, lossy)
}
}
#[cfg(not(feature = "compact"))]
{
#[cfg(feature = "radix")]
{
let format = NumberFormat::<{ FORMAT }> {};
let radix = format.mantissa_radix();
if radix == 10 {
lemire::<F>(num, lossy)
} else if is_power_two!(radix) {
binary::<F, FORMAT>(num, lossy)
} else {
bellerophon::<F, FORMAT>(num, lossy)
}
}
#[cfg(all(feature = "power-of-two", not(feature = "radix")))]
{
let format = NumberFormat::<{ FORMAT }> {};
let radix = format.mantissa_radix();
debug_assert!(matches!(radix, 2 | 4 | 8 | 10 | 16 | 32));
if radix == 10 {
lemire::<F>(num, lossy)
} else {
binary::<F, FORMAT>(num, lossy)
}
}
#[cfg(not(feature = "power-of-two"))]
{
lemire::<F>(num, lossy)
}
}
}
#[inline]
pub fn slow_path<F: LemireFloat, const FORMAT: u128>(
num: Number,
fp: ExtendedFloat80,
) -> ExtendedFloat80 {
#[cfg(not(feature = "power-of-two"))]
{
slow_radix::<F, FORMAT>(num, fp)
}
#[cfg(feature = "power-of-two")]
{
let format = NumberFormat::<{ FORMAT }> {};
if is_power_two!(format.mantissa_radix()) {
slow_binary::<F, FORMAT>(num)
} else {
slow_radix::<F, FORMAT>(num, fp)
}
}
}
#[inline]
#[allow(clippy::collapsible_if)]
pub fn parse_partial_number<'a, const FORMAT: u128>(
mut byte: Bytes<'a, FORMAT>,
is_negative: bool,
options: &Options,
) -> Result<(Number<'a>, usize)> {
let format = NumberFormat::<{ FORMAT }> {};
let decimal_point = options.decimal_point();
let exponent_character = options.exponent();
debug_assert!(format.is_valid());
debug_assert!(!byte.is_done());
let bits_per_digit = shared::log2(format.mantissa_radix()) as i64;
let bits_per_base = shared::log2(format.exponent_base()) as i64;
let base_prefix = format.base_prefix();
let mut is_prefix = false;
let mut iter = byte.integer_iter();
if cfg!(feature = "format") && base_prefix != 0 && iter.peek() == Some(&b'0') {
unsafe { iter.step_unchecked() };
if let Some(&c) = iter.peek() {
is_prefix = if format.case_sensitive_base_prefix() {
c == base_prefix
} else {
c.to_ascii_lowercase() == base_prefix.to_ascii_lowercase()
};
if is_prefix {
unsafe { iter.step_unchecked() };
if iter.is_done() {
return Err(Error::Empty(iter.cursor()));
}
}
}
}
let mut mantissa = 0_u64;
let start = byte.clone();
#[cfg(not(feature = "compact"))]
parse_8digits::<_, FORMAT>(byte.integer_iter(), &mut mantissa);
parse_digits::<_, _, FORMAT>(byte.integer_iter(), |digit| {
mantissa = mantissa.wrapping_mul(format.radix() as _).wrapping_add(digit as _);
});
let mut n_digits = byte.current_count() - start.current_count();
if cfg!(feature = "format") && format.required_integer_digits() && n_digits == 0 {
return Err(Error::EmptyInteger(byte.cursor()));
}
debug_assert!(n_digits <= start.as_slice().len());
let integer_digits = unsafe { start.as_slice().get_unchecked(..n_digits) };
if cfg!(feature = "format") && !is_prefix && format.no_float_leading_zeros() {
if integer_digits.len() > 1 && integer_digits.get(0) == Some(&b'0') {
return Err(Error::InvalidLeadingZeros(start.cursor()));
}
}
let mut n_after_dot = 0;
let mut exponent = 0_i64;
let mut implicit_exponent: i64;
let int_end = n_digits as i64;
let mut fraction_digits = None;
if byte.first_is(decimal_point) {
unsafe { byte.step_unchecked() };
let before = byte.clone();
#[cfg(not(feature = "compact"))]
parse_8digits::<_, FORMAT>(byte.fraction_iter(), &mut mantissa);
parse_digits::<_, _, FORMAT>(byte.fraction_iter(), |digit| {
mantissa = mantissa.wrapping_mul(format.radix() as _).wrapping_add(digit as _);
});
n_after_dot = byte.current_count() - before.current_count();
debug_assert!(n_after_dot <= before.as_slice().len());
fraction_digits = Some(unsafe { before.as_slice().get_unchecked(..n_after_dot) });
implicit_exponent = -(n_after_dot as i64);
if format.mantissa_radix() == format.exponent_base() {
exponent = implicit_exponent;
} else {
debug_assert!(bits_per_digit % bits_per_base == 0);
exponent = implicit_exponent * bits_per_digit / bits_per_base;
};
if cfg!(feature = "format") && format.required_fraction_digits() && n_after_dot == 0 {
return Err(Error::EmptyFraction(byte.cursor()));
}
}
n_digits += n_after_dot;
if format.required_mantissa_digits() && n_digits == 0 {
return Err(Error::EmptyMantissa(byte.cursor()));
}
let mut explicit_exponent = 0_i64;
let is_exponent = if cfg!(feature = "format") && format.case_sensitive_exponent() {
byte.first_is(exponent_character)
} else {
byte.case_insensitive_first_is(exponent_character)
};
if is_exponent {
if cfg!(feature = "format") {
if format.no_exponent_notation() {
return Err(Error::InvalidExponent(byte.cursor()));
}
if format.no_exponent_without_fraction() && fraction_digits.is_none() {
return Err(Error::ExponentWithoutFraction(byte.cursor()));
}
}
unsafe { byte.step_unchecked() };
let (is_negative, shift) = parse_exponent_sign!(byte, format);
unsafe { byte.step_by_unchecked(shift) };
if cfg!(feature = "format") && format.required_exponent_sign() && shift == 0 {
return Err(Error::MissingExponentSign(byte.cursor()));
}
let before = byte.current_count();
parse_digits::<_, _, FORMAT>(byte.exponent_iter(), |digit| {
if explicit_exponent < 0x10000000 {
explicit_exponent *= format.radix() as i64;
explicit_exponent += digit as i64;
}
});
if format.required_exponent_digits() && byte.current_count() - before == 0 {
return Err(Error::EmptyExponent(byte.cursor()));
}
explicit_exponent = if is_negative {
-explicit_exponent
} else {
explicit_exponent
};
exponent += explicit_exponent;
} else if cfg!(feature = "format") && format.required_exponent_notation() {
return Err(Error::MissingExponent(byte.cursor()));
}
let base_suffix = format.base_suffix();
if cfg!(feature = "format") && base_suffix != 0 {
let is_suffix = if cfg!(feature = "format") && format.case_sensitive_base_suffix() {
byte.first_is(base_suffix)
} else {
byte.case_insensitive_first_is(base_suffix)
};
if is_suffix {
unsafe { byte.step_unchecked() };
}
}
let end = byte.cursor();
let mut step = u64_step(format.radix());
let mut many_digits = false;
if cfg!(feature = "format") && !format.required_mantissa_digits() && n_digits == 0 {
exponent = 0;
}
if n_digits <= step {
return Ok((
Number {
exponent,
mantissa,
is_negative,
many_digits: false,
integer: integer_digits,
fraction: fraction_digits,
},
end,
));
}
n_digits -= step;
let mut zeros = start.clone();
let mut zeros_integer = zeros.integer_iter();
while zeros_integer.peek_is(b'0') {
n_digits = n_digits.saturating_sub(1);
unsafe { zeros_integer.step_unchecked() };
}
if zeros.first_is(decimal_point) {
unsafe { zeros.step_unchecked() };
}
let mut zeros_fraction = zeros.fraction_iter();
while zeros_fraction.peek_is(b'0') {
n_digits = n_digits.saturating_sub(1);
unsafe { zeros_fraction.step_unchecked() };
}
if n_digits > 0 {
many_digits = true;
mantissa = 0;
let mut integer = integer_digits.bytes::<{ FORMAT }>();
let mut integer_iter = integer.integer_iter();
integer_iter.skip_zeros();
parse_u64_digits::<_, FORMAT>(integer_iter, &mut mantissa, &mut step);
implicit_exponent = if step == 0 {
int_end - integer.current_count() as i64
} else {
let mut fraction = fraction_digits.unwrap().bytes::<{ FORMAT }>();
let mut fraction_iter = fraction.fraction_iter();
if mantissa == 0 {
fraction_iter.skip_zeros();
}
parse_u64_digits::<_, FORMAT>(fraction_iter, &mut mantissa, &mut step);
-(fraction.current_count() as i64)
};
if format.mantissa_radix() == format.exponent_base() {
exponent = implicit_exponent;
} else {
debug_assert!(bits_per_digit % bits_per_base == 0);
exponent = implicit_exponent * bits_per_digit / bits_per_base;
};
exponent += explicit_exponent;
}
Ok((
Number {
exponent,
mantissa,
is_negative,
many_digits,
integer: integer_digits,
fraction: fraction_digits,
},
end,
))
}
#[inline]
pub fn parse_number<'a, const FORMAT: u128>(
byte: Bytes<'a, FORMAT>,
is_negative: bool,
options: &Options,
) -> Result<Number<'a>> {
let length = byte.length();
let (float, count) = parse_partial_number::<FORMAT>(byte, is_negative, options)?;
if count == length {
Ok(float)
} else {
Err(Error::InvalidDigit(count))
}
}
#[inline]
pub fn parse_digits<'a, Iter, Cb, const FORMAT: u128>(mut iter: Iter, mut cb: Cb)
where
Iter: BytesIter<'a>,
Cb: FnMut(u32),
{
let format = NumberFormat::<{ FORMAT }> {};
let radix = format.radix();
while let Some(&c) = iter.peek() {
match char_to_digit_const(c, radix) {
Some(v) => cb(v),
None => break,
}
unsafe { iter.step_unchecked() };
}
}
#[inline]
#[cfg(not(feature = "compact"))]
pub fn parse_8digits<'a, Iter, const FORMAT: u128>(mut iter: Iter, mantissa: &mut u64)
where
Iter: BytesIter<'a>,
{
let format = NumberFormat::<{ FORMAT }> {};
let radix: u64 = format.radix() as u64;
if can_try_parse_8digits!(iter, radix) {
let radix2 = radix.wrapping_mul(radix);
let radix4 = radix2.wrapping_mul(radix2);
let radix8 = radix4.wrapping_mul(radix4);
while let Some(v) = algorithm::try_parse_8digits::<u64, _, FORMAT>(&mut iter) {
*mantissa = mantissa.wrapping_mul(radix8).wrapping_add(v);
}
}
}
#[inline]
pub fn parse_u64_digits<'a, Iter, const FORMAT: u128>(
mut iter: Iter,
mantissa: &mut u64,
step: &mut usize,
) where
Iter: BytesIter<'a>,
{
let format = NumberFormat::<{ FORMAT }> {};
let radix = format.radix() as u64;
#[cfg(not(feature = "compact"))]
if can_try_parse_8digits!(iter, radix) {
let radix2 = radix.wrapping_mul(radix);
let radix4 = radix2.wrapping_mul(radix2);
let radix8 = radix4.wrapping_mul(radix4);
while *step > 8 {
if let Some(v) = algorithm::try_parse_8digits::<u64, _, FORMAT>(&mut iter) {
*mantissa = mantissa.wrapping_mul(radix8).wrapping_add(v);
*step -= 8;
} else {
break;
}
}
}
while let Some(&c) = iter.peek() {
if *step > 0 {
let digit = char_to_valid_digit_const(c, radix as u32);
*mantissa = *mantissa * radix + digit as u64;
*step -= 1;
unsafe { iter.step_unchecked() };
} else {
break;
}
}
}
#[inline]
pub fn is_special_eq<const FORMAT: u128>(mut byte: Bytes<FORMAT>, string: &'static [u8]) -> usize {
let format = NumberFormat::<{ FORMAT }> {};
if cfg!(feature = "format") && format.case_sensitive_special() {
if shared::starts_with(byte.special_iter(), string.iter()) {
byte.special_iter().peek();
return byte.cursor();
}
} else if shared::case_insensitive_starts_with(byte.special_iter(), string.iter()) {
byte.special_iter().peek();
return byte.cursor();
}
0
}
#[inline]
pub fn parse_positive_special<F, const FORMAT: u128>(
byte: Bytes<FORMAT>,
options: &Options,
) -> Option<(F, usize)>
where
F: LemireFloat,
{
let format = NumberFormat::<{ FORMAT }> {};
if cfg!(feature = "format") && format.no_special() {
return None;
}
let cursor = byte.cursor();
let length = byte.length() - cursor;
if let Some(nan_string) = options.nan_string() {
if length >= nan_string.len() {
let count = is_special_eq::<FORMAT>(byte.clone(), nan_string);
if count != 0 {
return Some((F::NAN, count));
}
}
}
if let Some(infinity_string) = options.infinity_string() {
if length >= infinity_string.len() {
let count = is_special_eq::<FORMAT>(byte.clone(), infinity_string);
if count != 0 {
return Some((F::INFINITY, count));
}
}
}
if let Some(inf_string) = options.inf_string() {
if length >= inf_string.len() {
let count = is_special_eq::<FORMAT>(byte.clone(), inf_string);
if count != 0 {
return Some((F::INFINITY, count));
}
}
}
None
}
#[inline]
pub fn parse_partial_special<F, const FORMAT: u128>(
byte: Bytes<FORMAT>,
is_negative: bool,
options: &Options,
) -> Option<(F, usize)>
where
F: LemireFloat,
{
let (mut float, count) = parse_positive_special::<F, FORMAT>(byte, options)?;
if is_negative {
float = -float;
}
Some((float, count))
}
#[inline]
pub fn parse_special<F, const FORMAT: u128>(
byte: Bytes<FORMAT>,
is_negative: bool,
options: &Options,
) -> Option<F>
where
F: LemireFloat,
{
let length = byte.length();
if let Some((float, count)) = parse_partial_special::<F, FORMAT>(byte, is_negative, options) {
if count == length {
return Some(float);
}
}
None
}