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
//! Fast lexical string-to-float conversion routines.
//!
//! This contains high-performance methods to parse floats from bytes.
//! Using [`from_lexical`] is analogous to [`parse`][`core-parse`],
//! while enabling parsing from bytes as well as [`str`].
//!
//!
//! [`from_lexical`]: FromLexical::from_lexical
//! [`core-parse`]: core::str::FromStr
//!
//! # Getting Started
//!
//! To parse a number from bytes, use [`from_lexical`]:
//!
//! ```rust
//! # #[no_std]
//! # use core::str;
//! use lexical_parse_float::{Error, FromLexical};
//!
//! let value = f64::from_lexical("1234.5".as_bytes());
//! assert_eq!(value, Ok(1234.5));
//!
//! let value = f64::from_lexical("1.2345e325".as_bytes());
//! assert_eq!(value, Ok(f64::INFINITY));
//!
//! let value = f64::from_lexical("1234.5 }, {\"Key\", \"Value\"}}".as_bytes());
//! assert_eq!(value, Err(Error::InvalidDigit(6)));
//! ```
//!
//! If wishing to incrementally parse a string from bytes, that is, parse as
//! many characters until an invalid digit is found, you can use the partial
//! parsers. This is useful in parsing data where the type is known, such as
//! JSON, but where the end of the number is not yet known.
//!
//! ```rust
//! # #[no_std]
//! # use core::str;
//! use lexical_parse_float::{Error, FromLexical};
//!
//! let value = f64::from_lexical_partial("1234.5 }, {\"Key\", \"Value\"}}".as_bytes());
//! assert_eq!(value, Ok((1234.5, 6)));
//!
//! let value = f64::from_lexical_partial("1.2345e325".as_bytes());
//! assert_eq!(value, Ok((f64::INFINITY, 10)));
//! ```
//!
//! # Options/Formatting API
//!
//! Each float parser contains extensive formatting control through
//! [`mod@options`] and [`mod@format`], including digit [`separator`]
//! support (that is, floats such as `1_2__3.4_5`), if integral,
//! fractional, or any significant digits are required, if to disable
//! parsing non-finite values, if `+` signs are invalid or required,
//! and much more. For more comprehensive examples, see the
//! [`format`](#format) and [Comprehensive Configuration] sections
//! below.
//!
//! [`separator`]: NumberFormat::digit_separator
//! [Comprehensive Configuration]: #comprehensive-configuration
//!
//! ```rust
//! # #[cfg(feature = "radix")] {
//! # use core::str;
//! use lexical_parse_float::{Error, FromLexicalWithOptions, NumberFormatBuilder, Options};
//!
//! const FORMAT: u128 = NumberFormatBuilder::new()
//! // require a `+` or `-` sign before the number
//! .required_mantissa_sign(true)
//! // require a `+` or `-` sign before the exponent digits
//! .required_exponent_sign(true)
//! // build the format, panicking if the format is invalid
//! .build_strict();
//! const OPTIONS: Options = Options::new();
//!
//! let value = "+1.234e+300";
//! let result = f64::from_lexical_with_options::<FORMAT>(value.as_bytes(), &OPTIONS);
//! assert_eq!(result, Ok(1.234e+300));
//!
//! let value = "1.234e+300";
//! let result = f64::from_lexical_with_options::<FORMAT>(value.as_bytes(), &OPTIONS);
//! assert_eq!(result, Err(Error::MissingSign(0)));
//! # }
//! ```
//!
//! # Features
//!
//! * `format` - Add support for parsing custom integer formats.
//! * `power-of-two` - Add support for parsing power-of-two integer strings.
//! * `radix` - Add support for strings of any radix.
//! * `compact` - Reduce code size at the cost of performance.
//! * `f16` - Enable support for half-precision [`f16`][`ieee-f16`] and
//! [`bf16`][`brain-float`] floats.
//! * `std` (Default) - Disable to allow use in a [`no_std`] environment.
//!
//! [`no_std`]: https://docs.rust-embedded.org/book/intro/no-std.html
//! [`ieee-f16`]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
//! [`brain-float`]: https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
//!
//! A complete description of supported features includes:
//!
//! #### format
//!
//! Add support custom float parsing specifications. This should be used in
//! conjunction with [`Options`] for extensible float parsing.
//!
//! ##### JSON
//!
//! For example, in JSON, the following floats are valid or invalid:
//!
//! ```text
//! -1 // valid
//! +1 // invalid
//! 1 // valid
//! 1. // invalid
//! .1 // invalid
//! 0.1 // valid
//! nan // invalid
//! inf // invalid
//! Infinity // invalid
//! ```
//!
//! All of the finite numbers are valid in Rust, and Rust provides constants
//! for non-finite floats. In order to parse standard-conforming JSON floats
//! using lexical, you may use the following approach:
//!
//! ```rust
//! # #[cfg(feature = "format")] {
//! use lexical_parse_float::{format, options, Error, FromLexicalWithOptions, Result};
//!
//! fn parse_json_float(bytes: &[u8]) -> Result<f64> {
//! f64::from_lexical_with_options::<{ format::JSON }>(bytes, &options::JSON)
//! }
//!
//! assert_eq!(parse_json_float(b"-1"), Ok(-1.0));
//! assert_eq!(parse_json_float(b"+1"), Err(Error::InvalidPositiveSign(0)));
//! assert_eq!(parse_json_float(b"1"), Ok(1.0));
//! assert_eq!(parse_json_float(b"1."), Err(Error::EmptyFraction(2)));
//! assert_eq!(parse_json_float(b"0.1"), Ok(0.1));
//! assert_eq!(parse_json_float(b"nan"), Err(Error::EmptyInteger(0)));
//! assert_eq!(parse_json_float(b"inf"), Err(Error::EmptyInteger(0)));
//! assert_eq!(parse_json_float(b"Infinity"), Err(Error::EmptyInteger(0)));
//! # }
//! ```
//!
//! ##### Custom Format
//!
//! An example building and using a custom format, with many of the available
//! options is:
//!
//! ```rust
//! # #[cfg(feature = "format")] {
//! # use core::{num, str};
//! use lexical_parse_float::{Error, NumberFormatBuilder, Options, FromLexicalWithOptions};
//!
//! const FORMAT: u128 = NumberFormatBuilder::new()
//! // enable the use of digit separators with `_`
//! .digit_separator(num::NonZeroU8::new(b'_'))
//! // require digits before and after the decimal point,
//! // if the decimal point is present.
//! .required_integer_digits(true)
//! .required_fraction_digits(true)
//! // do not allow a leading `+` sign, so `+123` is invalid
//! .no_positive_mantissa_sign(true)
//! // do not allow `0` before an integer, so `01.1` is invalid.
//! // however, `0.1` is valid.
//! .no_integer_leading_zeros(true)
//! // allow digit separators anywhere, including consecutive ones
//! .leading_digit_separator(true)
//! .trailing_digit_separator(true)
//! .internal_digit_separator(true)
//! .consecutive_digit_separator(true)
//! // make it so the exponent character, `e`, is case-sensitive
//! // that is, `E` is not considered a valid exponent character
//! .case_sensitive_exponent(true)
//! .build_strict();
//! const OPTIONS: Options = Options::builder()
//! // change the string representation of NaN from `NaN` to `nan`
//! .nan_string(Some(b"nan"))
//! // disable a short infinity: long infinity is still allowed
//! .inf_string(None)
//! .build_strict();
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"1_2.3_4", &OPTIONS);
//! assert_eq!(value, Ok(12.34));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"-inf", &OPTIONS);
//! assert_eq!(value, Err(Error::EmptyInteger(1)));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"Infinity", &OPTIONS);
//! assert_eq!(value, Ok(f64::INFINITY));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"nan", &OPTIONS);
//! assert_eq!(value.map(|x| x.is_nan()), Ok(true));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"+1_2.3_4", &OPTIONS);
//! assert_eq!(value, Err(Error::InvalidPositiveSign(0)));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"0.3_4", &OPTIONS);
//! assert_eq!(value, Ok(0.34));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"12", &OPTIONS);
//! assert_eq!(value, Ok(12.0));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"12.", &OPTIONS);
//! assert_eq!(value, Err(Error::EmptyFraction(3)));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"1.234e5", &OPTIONS);
//! assert_eq!(value, Ok(1.234e5));
//!
//! let value = f64::from_lexical_with_options::<FORMAT>(b"1.234E5", &OPTIONS);
//! assert_eq!(value, Err(Error::InvalidDigit(5)));
//! # }
//! ```
//!
//! Enabling the [`format`](crate#format) API significantly increases compile
//! times, however, it enables a large amount of customization in how floats are
//! written.
//!
//! #### power-of-two
//!
//! Enable parsing numbers with radixes that are powers of two, that is, `2`,
//! `4`, `8`, `16`, and `32`.
//!
//! ```rust
//! # #[cfg(feature = "power-of-two")] {
//! use lexical_parse_float::{NumberFormatBuilder, Options, FromLexicalWithOptions};
//!
//! const BINARY: u128 = NumberFormatBuilder::binary();
//! const OPTIONS: Options = Options::new();
//! let value = "1.0011101111100111011011001000101101000011100101011";
//! let result = f64::from_lexical_with_options::<BINARY>(value.as_bytes(), &OPTIONS);
//! assert_eq!(result, Ok(1.234f64));
//! # }
//! ```
//!
//! #### radix
//!
//! Enable parsing numbers using all radixes from `2` to `36`. This requires
//! more static storage than [`power-of-two`][crate#power-of-two], and increases
//! compile times, but can be quite useful for esoteric programming languages
//! which use duodecimal floats, for example.
//!
//! ```rust
//! # #[cfg(feature = "radix")] {
//! use lexical_parse_float::{NumberFormatBuilder, Options, FromLexicalWithOptions};
//!
//! const FORMAT: u128 = NumberFormatBuilder::from_radix(12);
//! const OPTIONS: Options = Options::new();
//! let value = "1.29842830A44BAA2";
//! let result = f64::from_lexical_with_options::<FORMAT>(value.as_bytes(), &OPTIONS);
//! assert_eq!(result, Ok(1.234f64));
//! # }
//! ```
//!
//! #### compact
//!
//! Reduce the generated code size at the cost of performance. This minimizes
//! the number of static tables, inlining, and generics used, drastically
//! reducing the size of the generated binaries. However, this resulting
//! performance of the generated code is much lower.
//!
//! #### f16
//!
//! This enables the use of the half-precision floats [`f16`][`ieee-f16`] and
//! [`bf16`][`brain-float`]. However, since these have limited hardware support
//! and are primarily used for vectorized operations, they are parsed as if
//! they were an [`f32`]. Due to the low precision of 16-bit floats, the results
//! may appear to have significant rounding error.
//!
//! ```rust
//! # #[cfg(feature = "f16")] {
//! # use core::str;
//! use lexical_parse_float::{f16, FromLexical};
//!
//! let value = "1.234375";
//! let result = f16::from_lexical(value.as_bytes());
//! assert_eq!(result, Ok(f16::from_f64_const(1.234f64)));
//! # }
//! ```
//!
//! #### std
//!
//! Enable use of the standard library. Currently, the standard library
//! is not used, and may be disabled without any change in functionality
//! on stable.
//!
//! # Comprehensive Configuration
//!
//! `lexical-parse-float` provides two main levels of configuration:
//! - The [`NumberFormatBuilder`], creating a packed struct with custom
//! formatting options.
//! - The [`Options`] API.
//!
//! ## Number Format
//!
//! The number format class provides numerous flags to specify number writing.
//! When the [`power-of-two`](#power-of-two) feature is enabled, additional
//! flags are added:
//! - The radix for the significant digits (default `10`).
//! - The radix for the exponent base (default `10`).
//! - The radix for the exponent digits (default `10`).
//!
//! When the [`format`](#format) feature is enabled, numerous other syntax and
//! digit separator flags are enabled, including:
//! - A digit separator character, to group digits for increased legibility.
//! - Whether leading, trailing, internal, and consecutive digit separators are
//! allowed.
//! - Toggling required float components, such as digits before the decimal
//! point.
//! - Toggling whether special floats are allowed or are case-sensitive.
//!
//! Many pre-defined constants therefore exist to simplify common use-cases,
//! including:
//! - [`JSON`], [`XML`], [`TOML`], [`YAML`], [`SQLite`], and many more.
//! - [`Rust`], [`Python`], [`C#`], [`FORTRAN`], [`COBOL`] literals and strings,
//! and many more.
//!
//! For a list of all supported fields, see [Parse
//! Float Fields][NumberFormatBuilder#parse-float-fields].
//!
//! <!-- Spacer for rustfmt -->
//!
//! ## Options API
//!
//! The Options API provides high-level options to specify number parsing
//! or writing, options not intrinsically tied to a number format.
//! For example, the Options API provides:
//! - The [`exponent`][OptionsBuilder::exponent] character (defaults to `b'e'`
//! or `b'^'`, depending on the radix).
//! - The [`decimal point`][OptionsBuilder::decimal_point] character (defaults
//! to `b'.'`).
//! - Custom [`NaN`][f64::NAN] and [`Infinity`][f64::INFINITY] string
//! [`representations`][Options::nan_string].
//!
//!
//! In addition, pre-defined constants for each category of options may
//! be found in their respective modules, for example, [`JSON`][`JSON-OPTS`].
//!
//! [`JSON-OPTS`]: options::JSON
//!
//! ## Examples
//!
//! An example of creating your own options to parse European-style
//! numbers (which use commas as decimal points, and periods as digit
//! separators) is as follows:
//!
//! ```
//! # #[cfg(feature = "format")] {
//! # use core::num;
//! use lexical_parse_float::{format, FromLexicalWithOptions, NumberFormatBuilder, Options};
//!
//! // This creates a format to parse a European-style float number.
//! // The decimal point is a comma, and the digit separators (optional)
//! // are periods.
//! const EUROPEAN: u128 = NumberFormatBuilder::new()
//! .digit_separator(num::NonZeroU8::new(b'.'))
//! .build_strict();
//! const COMMA_OPTIONS: Options = Options::builder()
//! .decimal_point(b',')
//! .build_strict();
//! assert_eq!(
//! f32::from_lexical_with_options::<EUROPEAN>(b"300,10", &COMMA_OPTIONS),
//! Ok(300.10)
//! );
//!
//! // Another example, using a pre-defined constant for JSON.
//! const JSON: u128 = format::JSON;
//! const JSON_OPTIONS: Options = Options::new();
//! assert_eq!(
//! f32::from_lexical_with_options::<JSON>(b"0e1", &JSON_OPTIONS),
//! Ok(0.0)
//! );
//! assert_eq!(
//! f32::from_lexical_with_options::<JSON>(b"1E+2", &JSON_OPTIONS),
//! Ok(100.0)
//! );
//! # }
//! ```
//!
//! # Higher-Level APIs
//!
//! If you would like an API that supports multiple numeric conversions rather
//! than just writing integers, use [`lexical`] or [`lexical-core`] instead.
//!
//! [`lexical`]: https://crates.io/crates/lexical
//! [`lexical-core`]: https://crates.io/crates/lexical-core
//!
//! # Version Support
//!
//! The minimum, standard, required version is [`1.63.0`][`rust-1.63.0`], for
//! const generic support. Older versions of lexical support older Rust
//! versions.
//!
//! # Algorithm
//!
//! The default implementations are highly optimized both for simple
//! strings, as well as input with large numbers of digits. In order to
//! keep performance optimal for simple strings, we avoid overly branching
//! to minimize the number of branches (and therefore optimization checks).
//! Most of the branches in the code are resolved at compile-time, and
//! the resulting ASM as well as comprehensive benchmarks are monitored
//! to ensure there are no regressions.
//!
//! For simple floats, we use an optimized digit parser with multiple-digit
//! optimizations (parsing 8 digits in 3 multiplication instructions),
//! and then use machine floats to create an exact representation with
//! high throughput. In more complex cases, we use the Eisel-Lemire
//! algorithm, described in "Number Parsing at a Gigabyte per Second",
//! available online [here](https://arxiv.org/abs/2101.11408). The
//! Eisel-Lemire algorithm creates an extended representation using a
//! 128-bit (or a fallback 192-bit representation) of the significant
//! digits of the float, scaled to the proper exponent using pre-computed
//! powers-of-5.
//!
//! If the Eisel-Lemire algorithm is unable to unambiguously round the float,
//! we fallback to using optimized, big-integer algorithms, which are
//! described in [Algorithm Approach](#algorithm-approach) below.
//!
//! ## Machine Float-Only Algorithm
//!
//! We also support an algorithm that uses only machine floats for the
//! fast-path algorithm, however, this may be slower for floats with large
//! exponents since it uses an iterative algorithm. A code sample
//! using this is:
//!
//! ```rust
//! use lexical_parse_float::Options;
//! use lexical_parse_float::format::STANDARD;
//! use lexical_parse_float::parse::ParseFloat;
//!
//! const OPTIONS: Options = Options::new();
//! let result = f64::fast_path_complete::<{ STANDARD }>(b"1.34000", &OPTIONS);
//! assert_eq!(result, Ok(1.34000));
//! ```
//!
//! # Design
//!
//! - [Algorithm Approach](https://github.com/Alexhuszagh/rust-lexical/blob/main/lexical-parse-float/docs/Algorithm.md)
//! - [Benchmarks](https://github.com/Alexhuszagh/rust-lexical/blob/main/lexical-parse-float/docs/Benchmarks.md)
//! - [Comprehensive Benchmarks](https://github.com/Alexhuszagh/lexical-benchmarks)
//! - [Big Integer Implementation](https://github.com/Alexhuszagh/rust-lexical/blob/main/lexical-parse-float/docs/BigInteger.md)
//!
//! # Safety Guarantees
//!
//! <div class="warning info-warning">
//! <style>
//! .info-warning::before {
//! color: #87CEFAb0 !important;
//! }
//! .info-warning {
//! border-left: 2px solid #87CEFAb0 !important;
//! }
//! </style>
//!
//! This module uses some unsafe code to achieve accept acceptable performance.
//! The safety guarantees and logic are described below.
//!
//! </div>
//!
//! The primary use of unsafe code is in the big integer implementation, which
//! for performance reasons requires unchecked indexing at certain points, where
//! rust cannot elide the index check. The use of unsafe code can be found in
//! the calculation of the [hi] bits, however, every invocation requires the
//! buffer to be of sufficient [length][longbits]. The other major source is the
//! implementation of methods such as [push_unchecked], however, the safety
//! invariants for each caller to create a safe API are documented and has
//! similar safety guarantees to a regular vector. All other invocations of
//! unsafe code are indexing a buffer where the index is proven to be within
//! bounds within a few lines of code of the unsafe index.
//!
//! [hi]: <https://github.com/Alexhuszagh/rust-lexical/blob/15d4c8c92d70b1fb9bd6d33f582ffe27e0e74f99/lexical-parse-float/src/bigint.rs#L266>
//! [longbits]: <https://github.com/Alexhuszagh/rust-lexical/blob/15d4c8c92d70b1fb9bd6d33f582ffe27e0e74f99/lexical-parse-float/src/bigint.rs#L550-L557>
//! [push_unchecked]: <https://github.com/Alexhuszagh/rust-lexical/blob/15d4c8c92d70b1fb9bd6d33f582ffe27e0e74f99/lexical-parse-float/src/bigint.rs#L377-L386>
//! [`rust-1.63.0`]: https://blog.rust-lang.org/2022/08/11/Rust-1.63.0.html
// FIXME: Implement clippy/allow reasons once we drop support for 1.80.0 and below
// Clippy reasons were stabilized in 1.81.0.
// We want to have the same safety guarantees as Rust core,
// so we allow unused unsafe to clearly document safety guarantees.
extern crate lexical_parse_integer;
// Re-exports
pub use bf16;
pub use Error;
pub use f16;
pub use ;
pub use ParseOptions;
pub use Result;
pub use ;
pub use ;