lexical_parse_integer/
options.rs

1//! Configuration options for parsing integers.
2
3use lexical_util::options::ParseOptions;
4use lexical_util::result::Result;
5use static_assertions::const_assert;
6
7/// Builder for `Options`.
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
9pub struct OptionsBuilder {
10    /// Disable multi-digit optimizations.
11    ///
12    /// Using multi-digit optimizations allows parsing many digits
13    /// from longer input strings at once which can dramatically
14    /// improve performance (>70%) for long strings, but the
15    /// increased branching can decrease performance for simple
16    /// strings by 5-20%. Choose based on your inputs.
17    no_multi_digit: bool,
18}
19
20impl OptionsBuilder {
21    /// Create new options builder with default options.
22    #[inline(always)]
23    pub const fn new() -> Self {
24        Self {
25            no_multi_digit: true,
26        }
27    }
28
29    // GETTERS
30
31    /// Get if we disable the use of multi-digit optimizations.
32    #[inline(always)]
33    pub const fn get_no_multi_digit(&self) -> bool {
34        self.no_multi_digit
35    }
36
37    // SETTERS
38
39    /// Set if we disable the use of multi-digit optimizations.
40    #[inline(always)]
41    pub const fn no_multi_digit(mut self, no_multi_digit: bool) -> Self {
42        self.no_multi_digit = no_multi_digit;
43        self
44    }
45
46    // BUILDERS
47
48    /// Check if the builder state is valid.
49    #[inline(always)]
50    pub const fn is_valid(&self) -> bool {
51        true
52    }
53
54    /// Build the Options struct with bounds validation.
55    #[inline(always)]
56    pub const fn build_unchecked(&self) -> Options {
57        Options {
58            no_multi_digit: self.no_multi_digit,
59        }
60    }
61
62    /// Build the Options struct.
63    #[inline(always)]
64    pub const fn build(&self) -> Result<Options> {
65        Ok(self.build_unchecked())
66    }
67}
68
69impl Default for OptionsBuilder {
70    #[inline(always)]
71    fn default() -> Self {
72        Self::new()
73    }
74}
75
76/// Immutable options to customize writing integers.
77///
78/// # Examples
79///
80/// ```rust
81/// use lexical_parse_integer::options::Options;
82///
83/// # pub fn main() {
84/// let options = Options::builder()
85///     .build()
86///     .unwrap();
87/// # }
88/// ```
89#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
90pub struct Options {
91    /// Disable multi-digit optimizations.
92    ///
93    /// Using multi-digit optimizations allows parsing many digits
94    /// from longer input strings at once which can dramatically
95    /// improve performance (>70%) for long strings, but the
96    /// increased branching can decrease performance for simple
97    /// strings by 5-20%. Choose based on your inputs.
98    no_multi_digit: bool,
99}
100
101impl Options {
102    /// Create options with default values.
103    #[must_use]
104    #[inline(always)]
105    pub const fn new() -> Self {
106        Self::builder().build_unchecked()
107    }
108
109    // GETTERS
110
111    /// Check if the options state is valid.
112    #[inline(always)]
113    pub const fn is_valid(&self) -> bool {
114        self.rebuild().is_valid()
115    }
116
117    /// Get if we disable the use of multi-digit optimizations.
118    #[inline(always)]
119    pub const fn get_no_multi_digit(&self) -> bool {
120        self.no_multi_digit
121    }
122
123    // SETTERS
124
125    /// Set if we disable the use of multi-digit optimizations.
126    #[inline(always)]
127    pub fn no_multi_digit(&mut self, no_multi_digit: bool) {
128        self.no_multi_digit = no_multi_digit;
129    }
130
131    // BUILDERS
132
133    /// Get `OptionsBuilder` as a static function.
134    #[inline(always)]
135    pub const fn builder() -> OptionsBuilder {
136        OptionsBuilder::new()
137    }
138
139    /// Create `OptionsBuilder` using existing values.
140    #[inline(always)]
141    pub const fn rebuild(&self) -> OptionsBuilder {
142        OptionsBuilder {
143            no_multi_digit: self.no_multi_digit,
144        }
145    }
146}
147
148impl Default for Options {
149    #[inline(always)]
150    fn default() -> Self {
151        Self::new()
152    }
153}
154
155impl ParseOptions for Options {
156    #[inline(always)]
157    fn is_valid(&self) -> bool {
158        Self::is_valid(self)
159    }
160}
161
162// PRE-DEFINED CONSTANTS
163// ---------------------
164
165/// Standard number format.
166#[rustfmt::skip]
167pub const STANDARD: Options = Options::new();
168const_assert!(STANDARD.is_valid());
169
170/// Options optimized for small numbers.
171#[rustfmt::skip]
172pub const SMALL_NUMBERS: Options = Options::builder()
173        .no_multi_digit(true)
174        .build_unchecked();
175const_assert!(SMALL_NUMBERS.is_valid());
176
177/// Options optimized for large numbers and long strings.
178#[rustfmt::skip]
179pub const LARGE_NUMBERS: Options = Options::builder()
180        .no_multi_digit(false)
181        .build_unchecked();
182const_assert!(LARGE_NUMBERS.is_valid());