| 1 | //! Builder for the number format. |
| 2 | |
| 3 | use core::num; |
| 4 | |
| 5 | use crate::error::Error; |
| 6 | use crate::format_flags as flags; |
| 7 | |
| 8 | // NOTE: The size of `Option<NonZero>` is guaranteed to be the same. |
| 9 | // https://doc.rust-lang.org/std/num/type.NonZeroUsize.html |
| 10 | /// Type with the exact same size as a `u8`. |
| 11 | #[doc(hidden)] |
| 12 | pub type OptionU8 = Option<num::NonZeroU8>; |
| 13 | |
| 14 | /// Add single flag to `SyntaxFormat`. |
| 15 | macro_rules! add_flag { |
| 16 | ($format:ident, $bool:expr, $flag:ident) => { |
| 17 | if $bool { |
| 18 | $format |= flags::$flag; |
| 19 | } |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | /// Add multiple flags to `SyntaxFormat`. |
| 24 | macro_rules! add_flags { |
| 25 | ($format:ident ; $($bool:expr, $flag:ident ;)*) => {{ |
| 26 | $(add_flag!($format, $bool, $flag);)* |
| 27 | }}; |
| 28 | } |
| 29 | |
| 30 | /// Determine if a flag is set in the format. |
| 31 | macro_rules! has_flag { |
| 32 | ($format:ident, $flag:ident) => { |
| 33 | $format & flags::$flag != 0 |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | /// Unwrap `Option` as a const fn. |
| 38 | #[inline(always)] |
| 39 | const fn unwrap_or_zero(option: OptionU8) -> u8 { |
| 40 | match option { |
| 41 | Some(x) => x.get(), |
| 42 | None => 0, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Validating builder for [`NumberFormat`] from the provided specifications. |
| 47 | /// |
| 48 | /// Some of the core functionality includes support for: |
| 49 | /// - Digit separators: ignored characters used to make numbers more readable, |
| 50 | /// such as `100,000`. |
| 51 | /// - Non-decimal radixes: writing or parsing numbers written in binary, |
| 52 | /// hexadecimal, or other bases. |
| 53 | /// - Special numbers: disabling support for special floating-point, such as |
| 54 | /// [`NaN`][f64::NAN]. |
| 55 | /// - Number components: require signs, significant digits, and more. |
| 56 | /// |
| 57 | /// Returns [`NumberFormat`] on calling [`build_strict`] if it was able to |
| 58 | /// compile the format, otherwise, returns [`None`]. |
| 59 | /// |
| 60 | /// [`NumberFormat`]: crate::NumberFormat |
| 61 | /// [`build_strict`]: Self::build_strict |
| 62 | /// |
| 63 | /// # Examples |
| 64 | /// |
| 65 | /// To create a format valid for Rust number literals, we can use the builder |
| 66 | /// API: |
| 67 | /// |
| 68 | /// ```rust |
| 69 | /// # #[cfg(feature = "format")] { |
| 70 | /// use core::num; |
| 71 | /// |
| 72 | /// use lexical_util::{NumberFormat, NumberFormatBuilder}; |
| 73 | /// |
| 74 | /// // create the format for literal Rust floats |
| 75 | /// const RUST: u128 = NumberFormatBuilder::new() |
| 76 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 77 | /// .required_digits(true) |
| 78 | /// .no_positive_mantissa_sign(true) |
| 79 | /// .no_special(true) |
| 80 | /// .internal_digit_separator(true) |
| 81 | /// .trailing_digit_separator(true) |
| 82 | /// .consecutive_digit_separator(true) |
| 83 | /// .build_strict(); |
| 84 | /// |
| 85 | /// // then, access the formats's properties |
| 86 | /// let format = NumberFormat::<{ RUST }> {}; |
| 87 | /// assert!(format.no_positive_mantissa_sign()); |
| 88 | /// assert!(format.no_special()); |
| 89 | /// assert!(format.internal_digit_separator()); |
| 90 | /// assert!(format.trailing_digit_separator()); |
| 91 | /// assert!(format.consecutive_digit_separator()); |
| 92 | /// assert!(!format.no_exponent_notation()); |
| 93 | /// # } |
| 94 | /// ``` |
| 95 | /// |
| 96 | /// # Fields |
| 97 | /// |
| 98 | /// - [`digit_separator`]: Character to separate digits. |
| 99 | /// - [`mantissa_radix`]: Radix for mantissa digits. |
| 100 | /// - [`exponent_base`]: Base for the exponent. |
| 101 | /// - [`exponent_radix`]: Radix for the exponent digits. |
| 102 | /// - [`base_prefix`]: Optional character for the base prefix. |
| 103 | /// - [`base_suffix`]: Optional character for the base suffix. |
| 104 | /// - [`required_integer_digits`]: If digits are required before the decimal |
| 105 | /// point. |
| 106 | /// - [`required_fraction_digits`]: If digits are required after the decimal |
| 107 | /// point. |
| 108 | /// - [`required_exponent_digits`]: If digits are required after the exponent |
| 109 | /// character. |
| 110 | /// - [`required_mantissa_digits`]: If at least 1 significant digit is required. |
| 111 | /// - [`no_positive_mantissa_sign`]: If positive sign before the mantissa is not |
| 112 | /// allowed. |
| 113 | /// - [`required_mantissa_sign`]: If positive sign before the mantissa is |
| 114 | /// required. |
| 115 | /// - [`no_exponent_notation`]: If exponent notation is not allowed. |
| 116 | /// - [`no_positive_exponent_sign`]: If positive sign before the exponent is not |
| 117 | /// allowed. |
| 118 | /// - [`required_exponent_sign`]: If sign before the exponent is required. |
| 119 | /// - [`no_exponent_without_fraction`]: If exponent without fraction is not |
| 120 | /// allowed. |
| 121 | /// - [`no_special`]: If special (non-finite) values are not allowed. |
| 122 | /// - [`case_sensitive_special`]: If special (non-finite) values are |
| 123 | /// case-sensitive. |
| 124 | /// - [`no_integer_leading_zeros`]: If leading zeros before an integer are not |
| 125 | /// allowed. |
| 126 | /// - [`no_float_leading_zeros`]: If leading zeros before a float are not |
| 127 | /// allowed. |
| 128 | /// - [`required_exponent_notation`]: If exponent notation is required. |
| 129 | /// - [`case_sensitive_exponent`]: If exponent characters are case-sensitive. |
| 130 | /// - [`case_sensitive_base_prefix`]: If base prefixes are case-sensitive. |
| 131 | /// - [`case_sensitive_base_suffix`]: If base suffixes are case-sensitive. |
| 132 | /// - [`integer_internal_digit_separator`]: If digit separators are allowed |
| 133 | /// between integer digits. |
| 134 | /// - [`fraction_internal_digit_separator`]: If digit separators are allowed |
| 135 | /// between fraction digits. |
| 136 | /// - [`exponent_internal_digit_separator`]: If digit separators are allowed |
| 137 | /// between exponent digits. |
| 138 | /// - [`integer_leading_digit_separator`]: If a digit separator is allowed |
| 139 | /// before any integer digits. |
| 140 | /// - [`fraction_leading_digit_separator`]: If a digit separator is allowed |
| 141 | /// before any fraction digits. |
| 142 | /// - [`exponent_leading_digit_separator`]: If a digit separator is allowed |
| 143 | /// before any exponent digits. |
| 144 | /// - [`integer_trailing_digit_separator`]: If a digit separator is allowed |
| 145 | /// after any integer digits. |
| 146 | /// - [`fraction_trailing_digit_separator`]: If a digit separator is allowed |
| 147 | /// after any fraction digits. |
| 148 | /// - [`exponent_trailing_digit_separator`]: If a digit separator is allowed |
| 149 | /// after any exponent digits. |
| 150 | /// - [`integer_consecutive_digit_separator`]: If multiple consecutive integer |
| 151 | /// digit separators are allowed. |
| 152 | /// - [`fraction_consecutive_digit_separator`]: If multiple consecutive fraction |
| 153 | /// digit separators are allowed. |
| 154 | /// - [`special_digit_separator`]: If any digit separators are allowed in |
| 155 | /// special (non-finite) values. |
| 156 | /// |
| 157 | /// # Write Integer Fields |
| 158 | /// |
| 159 | /// No fields are used for writing integers. |
| 160 | /// |
| 161 | /// # Parse Integer Fields |
| 162 | /// |
| 163 | /// These fields are used for parsing integers: |
| 164 | /// |
| 165 | /// - [`digit_separator`]: Character to separate digits. |
| 166 | /// - [`mantissa_radix`]: Radix for mantissa digits. |
| 167 | /// - [`base_prefix`]: Optional character for the base prefix. |
| 168 | /// - [`base_suffix`]: Optional character for the base suffix. |
| 169 | /// - [`no_positive_mantissa_sign`]: If positive sign before the mantissa is not |
| 170 | /// allowed. |
| 171 | /// - [`required_mantissa_sign`]: If positive sign before the mantissa is |
| 172 | /// required. |
| 173 | /// - [`no_integer_leading_zeros`]: If leading zeros before an integer are not |
| 174 | /// allowed. |
| 175 | /// - [`integer_internal_digit_separator`]: If digit separators are allowed |
| 176 | /// between integer digits. |
| 177 | /// - [`integer_leading_digit_separator`]: If a digit separator is allowed |
| 178 | /// before any integer digits. |
| 179 | /// - [`integer_trailing_digit_separator`]: If a digit separator is allowed |
| 180 | /// after any integer digits. |
| 181 | /// - [`integer_consecutive_digit_separator`]: If multiple consecutive integer |
| 182 | /// digit separators are allowed. |
| 183 | /// |
| 184 | /// # Write Float Fields |
| 185 | /// |
| 186 | /// These fields are used for writing floats: |
| 187 | /// |
| 188 | /// - [`mantissa_radix`]: Radix for mantissa digits. |
| 189 | /// - [`exponent_base`]: Base for the exponent. |
| 190 | /// - [`exponent_radix`]: Radix for the exponent digits. |
| 191 | /// - [`no_positive_mantissa_sign`]: If positive sign before the mantissa is not |
| 192 | /// allowed. |
| 193 | /// - [`required_mantissa_sign`]: If positive sign before the mantissa is |
| 194 | /// required. |
| 195 | /// - [`no_exponent_notation`]: If exponent notation is not allowed. |
| 196 | /// - [`no_positive_exponent_sign`]: If positive sign before the exponent is not |
| 197 | /// allowed. |
| 198 | /// - [`required_exponent_sign`]: If sign before the exponent is required. |
| 199 | /// - [`required_exponent_notation`]: If exponent notation is required. |
| 200 | /// |
| 201 | /// # Parse Float Fields |
| 202 | /// |
| 203 | /// These fields are used for parsing floats: |
| 204 | /// |
| 205 | /// - [`digit_separator`]: Character to separate digits. |
| 206 | /// - [`mantissa_radix`]: Radix for mantissa digits. |
| 207 | /// - [`exponent_base`]: Base for the exponent. |
| 208 | /// - [`exponent_radix`]: Radix for the exponent digits. |
| 209 | /// - [`base_prefix`]: Optional character for the base prefix. |
| 210 | /// - [`base_suffix`]: Optional character for the base suffix. |
| 211 | /// - [`required_mantissa_digits`]: If at least 1 significant digit is required. |
| 212 | /// - [`required_integer_digits`]: If digits are required before the decimal |
| 213 | /// point. |
| 214 | /// - [`required_fraction_digits`]: If digits are required after the decimal |
| 215 | /// point. |
| 216 | /// - [`required_exponent_digits`]: If digits are required after the exponent |
| 217 | /// character. |
| 218 | /// - [`no_positive_mantissa_sign`]: If positive sign before the mantissa is not |
| 219 | /// allowed. |
| 220 | /// - [`required_mantissa_sign`]: If positive sign before the mantissa is |
| 221 | /// required. |
| 222 | /// - [`no_exponent_notation`]: If exponent notation is not allowed. |
| 223 | /// - [`no_positive_exponent_sign`]: If positive sign before the exponent is not |
| 224 | /// allowed. |
| 225 | /// - [`required_exponent_sign`]: If sign before the exponent is required. |
| 226 | /// - [`no_exponent_without_fraction`]: If exponent without fraction is not |
| 227 | /// allowed. |
| 228 | /// - [`no_special`]: If special (non-finite) values are not allowed. |
| 229 | /// - [`case_sensitive_special`]: If special (non-finite) values are |
| 230 | /// case-sensitive. |
| 231 | /// - [`no_integer_leading_zeros`]: If leading zeros before an integer are not |
| 232 | /// allowed. |
| 233 | /// - [`no_float_leading_zeros`]: If leading zeros before a float are not |
| 234 | /// allowed. |
| 235 | /// - [`required_exponent_notation`]: If exponent notation is required. |
| 236 | /// - [`case_sensitive_exponent`]: If exponent characters are case-sensitive. |
| 237 | /// - [`case_sensitive_base_prefix`]: If base prefixes are case-sensitive. |
| 238 | /// - [`case_sensitive_base_suffix`]: If base suffixes are case-sensitive. |
| 239 | /// - [`integer_internal_digit_separator`]: If digit separators are allowed |
| 240 | /// between integer digits. |
| 241 | /// - [`fraction_internal_digit_separator`]: If digit separators are allowed |
| 242 | /// between fraction digits. |
| 243 | /// - [`exponent_internal_digit_separator`]: If digit separators are allowed |
| 244 | /// between exponent digits. |
| 245 | /// - [`integer_leading_digit_separator`]: If a digit separator is allowed |
| 246 | /// before any integer digits. |
| 247 | /// - [`fraction_leading_digit_separator`]: If a digit separator is allowed |
| 248 | /// before any fraction digits. |
| 249 | /// - [`exponent_leading_digit_separator`]: If a digit separator is allowed |
| 250 | /// before any exponent digits. |
| 251 | /// - [`integer_trailing_digit_separator`]: If a digit separator is allowed |
| 252 | /// after any integer digits. |
| 253 | /// - [`fraction_trailing_digit_separator`]: If a digit separator is allowed |
| 254 | /// after any fraction digits. |
| 255 | /// - [`exponent_trailing_digit_separator`]: If a digit separator is allowed |
| 256 | /// after any exponent digits. |
| 257 | /// - [`integer_consecutive_digit_separator`]: If multiple consecutive integer |
| 258 | /// digit separators are allowed. |
| 259 | /// - [`fraction_consecutive_digit_separator`]: If multiple consecutive fraction |
| 260 | /// digit separators are allowed. |
| 261 | /// - [`special_digit_separator`]: If any digit separators are allowed in |
| 262 | /// special (non-finite) values. |
| 263 | #[cfg_attr( |
| 264 | feature = "power-of-two", |
| 265 | doc = "\n |
| 266 | [`exponent_base`]: Self::exponent_base |
| 267 | [`exponent_radix`]: Self::exponent_radix |
| 268 | [`mantissa_radix`]: Self::mantissa_radix |
| 269 | " |
| 270 | )] |
| 271 | #[cfg_attr( |
| 272 | not(feature = "power-of-two"), |
| 273 | doc = "\n |
| 274 | [`exponent_base`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L602\n |
| 275 | [`exponent_radix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L610\n |
| 276 | [`mantissa_radix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L594\n |
| 277 | " |
| 278 | )] |
| 279 | #[cfg_attr( |
| 280 | feature = "format", |
| 281 | doc = "\n |
| 282 | [`digit_separator`]: Self::digit_separator\n |
| 283 | [`required_integer_digits`]: Self::required_integer_digits\n |
| 284 | [`required_fraction_digits`]: Self::required_fraction_digits\n |
| 285 | [`required_exponent_digits`]: Self::required_exponent_digits\n |
| 286 | [`required_mantissa_digits`]: Self::required_mantissa_digits\n |
| 287 | [`no_positive_mantissa_sign`]: Self::no_positive_mantissa_sign\n |
| 288 | [`required_mantissa_sign`]: Self::required_mantissa_sign\n |
| 289 | [`no_exponent_notation`]: Self::no_exponent_notation\n |
| 290 | [`no_positive_exponent_sign`]: Self::no_positive_exponent_sign\n |
| 291 | [`required_exponent_sign`]: Self::required_exponent_sign\n |
| 292 | [`no_exponent_without_fraction`]: Self::no_exponent_without_fraction\n |
| 293 | [`no_special`]: Self::no_special\n |
| 294 | [`case_sensitive_special`]: Self::case_sensitive_special\n |
| 295 | [`no_integer_leading_zeros`]: Self::no_integer_leading_zeros\n |
| 296 | [`no_float_leading_zeros`]: Self::no_float_leading_zeros\n |
| 297 | [`required_exponent_notation`]: Self::required_exponent_notation\n |
| 298 | [`case_sensitive_exponent`]: Self::case_sensitive_exponent\n |
| 299 | [`integer_internal_digit_separator`]: Self::integer_internal_digit_separator\n |
| 300 | [`fraction_internal_digit_separator`]: Self::fraction_internal_digit_separator\n |
| 301 | [`exponent_internal_digit_separator`]: Self::exponent_internal_digit_separator\n |
| 302 | [`integer_leading_digit_separator`]: Self::integer_leading_digit_separator\n |
| 303 | [`fraction_leading_digit_separator`]: Self::fraction_leading_digit_separator\n |
| 304 | [`exponent_leading_digit_separator`]: Self::exponent_leading_digit_separator\n |
| 305 | [`integer_trailing_digit_separator`]: Self::integer_trailing_digit_separator\n |
| 306 | [`fraction_trailing_digit_separator`]: Self::fraction_trailing_digit_separator\n |
| 307 | [`exponent_trailing_digit_separator`]: Self::exponent_trailing_digit_separator\n |
| 308 | [`integer_consecutive_digit_separator`]: Self::integer_consecutive_digit_separator\n |
| 309 | [`fraction_consecutive_digit_separator`]: Self::fraction_consecutive_digit_separator\n |
| 310 | [`special_digit_separator`]: Self::special_digit_separator\n |
| 311 | " |
| 312 | )] |
| 313 | #[cfg_attr( |
| 314 | not(feature = "format"), |
| 315 | doc = "\n |
| 316 | [`digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L579\n |
| 317 | [`required_integer_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L634\n |
| 318 | [`required_fraction_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L642\n |
| 319 | [`required_exponent_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L650\n |
| 320 | [`required_mantissa_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L658\n |
| 321 | [`no_positive_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L677\n |
| 322 | [`required_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L685\n |
| 323 | [`no_exponent_notation`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L693\n |
| 324 | [`no_positive_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L701\n |
| 325 | [`required_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L709\n |
| 326 | [`no_exponent_without_fraction`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L717\n |
| 327 | [`no_special`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L725\n |
| 328 | [`case_sensitive_special`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L733\n |
| 329 | [`no_integer_leading_zeros`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L741\n |
| 330 | [`no_float_leading_zeros`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L749\n |
| 331 | [`required_exponent_notation`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L757\n |
| 332 | [`case_sensitive_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L765\n |
| 333 | [`integer_internal_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L793\n |
| 334 | [`fraction_internal_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L805\n |
| 335 | [`exponent_internal_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L817\n |
| 336 | [`integer_leading_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L842\n |
| 337 | [`fraction_leading_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L853\n |
| 338 | [`exponent_leading_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L864\n |
| 339 | [`integer_trailing_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L888\n |
| 340 | [`fraction_trailing_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L899\n |
| 341 | [`exponent_trailing_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L910\n |
| 342 | [`integer_consecutive_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L931\n |
| 343 | [`fraction_consecutive_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L939\n |
| 344 | [`special_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L965\n |
| 345 | " |
| 346 | )] |
| 347 | #[cfg_attr( |
| 348 | all(feature = "format", feature = "power-of-two"), |
| 349 | doc = "\n |
| 350 | [`base_prefix`]: Self::base_prefix |
| 351 | [`base_suffix`]: Self::base_suffix |
| 352 | [`case_sensitive_base_prefix`]: Self::case_sensitive_base_prefix |
| 353 | [`case_sensitive_base_suffix`]: Self::case_sensitive_base_suffix |
| 354 | " |
| 355 | )] |
| 356 | #[cfg_attr( |
| 357 | not(all(feature = "format", feature = "power-of-two")), |
| 358 | doc = "\n |
| 359 | [`base_prefix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L618\n |
| 360 | [`base_suffix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L626\n |
| 361 | [`case_sensitive_base_prefix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L773\n |
| 362 | [`case_sensitive_base_suffix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L781\n |
| 363 | " |
| 364 | )] |
| 365 | pub struct NumberFormatBuilder { |
| 366 | digit_separator: OptionU8, |
| 367 | base_prefix: OptionU8, |
| 368 | base_suffix: OptionU8, |
| 369 | mantissa_radix: u8, |
| 370 | exponent_base: OptionU8, |
| 371 | exponent_radix: OptionU8, |
| 372 | required_integer_digits: bool, |
| 373 | required_fraction_digits: bool, |
| 374 | required_exponent_digits: bool, |
| 375 | required_mantissa_digits: bool, |
| 376 | no_positive_mantissa_sign: bool, |
| 377 | required_mantissa_sign: bool, |
| 378 | no_exponent_notation: bool, |
| 379 | no_positive_exponent_sign: bool, |
| 380 | required_exponent_sign: bool, |
| 381 | no_exponent_without_fraction: bool, |
| 382 | no_special: bool, |
| 383 | case_sensitive_special: bool, |
| 384 | no_integer_leading_zeros: bool, |
| 385 | no_float_leading_zeros: bool, |
| 386 | required_exponent_notation: bool, |
| 387 | case_sensitive_exponent: bool, |
| 388 | case_sensitive_base_prefix: bool, |
| 389 | case_sensitive_base_suffix: bool, |
| 390 | integer_internal_digit_separator: bool, |
| 391 | fraction_internal_digit_separator: bool, |
| 392 | exponent_internal_digit_separator: bool, |
| 393 | integer_leading_digit_separator: bool, |
| 394 | fraction_leading_digit_separator: bool, |
| 395 | exponent_leading_digit_separator: bool, |
| 396 | integer_trailing_digit_separator: bool, |
| 397 | fraction_trailing_digit_separator: bool, |
| 398 | exponent_trailing_digit_separator: bool, |
| 399 | integer_consecutive_digit_separator: bool, |
| 400 | fraction_consecutive_digit_separator: bool, |
| 401 | exponent_consecutive_digit_separator: bool, |
| 402 | special_digit_separator: bool, |
| 403 | } |
| 404 | |
| 405 | impl NumberFormatBuilder { |
| 406 | // CONSTRUCTORS |
| 407 | |
| 408 | /// Create new [`NumberFormatBuilder`] with default arguments. |
| 409 | /// |
| 410 | /// The default values are: |
| 411 | /// - [`digit_separator`][Self::get_digit_separator] - `None` |
| 412 | /// - [`base_prefix`][Self::get_base_prefix] - `None` |
| 413 | /// - [`base_suffix`][Self::get_base_suffix] - `None` |
| 414 | /// - [`mantissa_radix`][Self::get_mantissa_radix] - `10` |
| 415 | /// - [`exponent_base`][Self::get_exponent_base] - `None` |
| 416 | /// - [`exponent_radix`][Self::get_exponent_radix] - `None` |
| 417 | /// - [`required_integer_digits`][Self::get_required_integer_digits] - |
| 418 | /// `false` |
| 419 | /// - [`required_fraction_digits`][Self::get_required_fraction_digits] - |
| 420 | /// `false` |
| 421 | /// - [`required_exponent_digits`][Self::get_required_exponent_digits] - |
| 422 | /// `true` |
| 423 | /// - [`required_mantissa_digits`][Self::get_required_mantissa_digits] - |
| 424 | /// `true` |
| 425 | /// - [`no_positive_mantissa_sign`][Self::get_no_positive_mantissa_sign] - |
| 426 | /// `false` |
| 427 | /// - [`required_mantissa_sign`][Self::get_required_mantissa_sign] - `false` |
| 428 | /// - [`no_exponent_notation`][Self::get_no_exponent_notation] - `false` |
| 429 | /// - [`no_positive_exponent_sign`][Self::get_no_positive_exponent_sign] - |
| 430 | /// `false` |
| 431 | /// - [`required_exponent_sign`][Self::get_required_exponent_sign] - `false` |
| 432 | /// - [`no_exponent_without_fraction`][Self::get_no_exponent_without_fraction] - |
| 433 | /// `false` |
| 434 | /// - [`no_special`][Self::get_no_special] - `false` |
| 435 | /// - [`case_sensitive_special`][Self::get_case_sensitive_special] - `false` |
| 436 | /// - [`no_integer_leading_zeros`][Self::get_no_integer_leading_zeros] - |
| 437 | /// `false` |
| 438 | /// - [`no_float_leading_zeros`][Self::get_no_float_leading_zeros] - `false` |
| 439 | /// - [`required_exponent_notation`][Self::get_required_exponent_notation] - |
| 440 | /// `false` |
| 441 | /// - [`case_sensitive_exponent`][Self::get_case_sensitive_exponent] - |
| 442 | /// `false` |
| 443 | /// - [`case_sensitive_base_prefix`][Self::get_case_sensitive_base_prefix] - |
| 444 | /// `false` |
| 445 | /// - [`case_sensitive_base_suffix`][Self::get_case_sensitive_base_suffix] - |
| 446 | /// `false` |
| 447 | /// - [`integer_internal_digit_separator`][Self::get_integer_internal_digit_separator] - `false` |
| 448 | /// - [`fraction_internal_digit_separator`][Self::get_fraction_internal_digit_separator] - `false` |
| 449 | /// - [`exponent_internal_digit_separator`][Self::get_exponent_internal_digit_separator] - `false` |
| 450 | /// - [`integer_leading_digit_separator`][Self::get_integer_leading_digit_separator] - `false` |
| 451 | /// - [`fraction_leading_digit_separator`][Self::get_fraction_leading_digit_separator] - `false` |
| 452 | /// - [`exponent_leading_digit_separator`][Self::get_exponent_leading_digit_separator] - `false` |
| 453 | /// - [`integer_trailing_digit_separator`][Self::get_integer_trailing_digit_separator] - `false` |
| 454 | /// - [`fraction_trailing_digit_separator`][Self::get_fraction_trailing_digit_separator] - `false` |
| 455 | /// - [`exponent_trailing_digit_separator`][Self::get_exponent_trailing_digit_separator] - `false` |
| 456 | /// - [`integer_consecutive_digit_separator`][Self::get_integer_consecutive_digit_separator] - `false` |
| 457 | /// - [`fraction_consecutive_digit_separator`][Self::get_fraction_consecutive_digit_separator] - `false` |
| 458 | /// - [`exponent_consecutive_digit_separator`][Self::get_exponent_consecutive_digit_separator] - `false` |
| 459 | /// - [`special_digit_separator`][Self::get_special_digit_separator] - |
| 460 | /// `false` |
| 461 | #[inline(always)] |
| 462 | pub const fn new() -> Self { |
| 463 | Self { |
| 464 | digit_separator: None, |
| 465 | base_prefix: None, |
| 466 | base_suffix: None, |
| 467 | mantissa_radix: 10, |
| 468 | exponent_base: None, |
| 469 | exponent_radix: None, |
| 470 | required_integer_digits: false, |
| 471 | required_fraction_digits: false, |
| 472 | required_exponent_digits: true, |
| 473 | required_mantissa_digits: true, |
| 474 | no_positive_mantissa_sign: false, |
| 475 | required_mantissa_sign: false, |
| 476 | no_exponent_notation: false, |
| 477 | no_positive_exponent_sign: false, |
| 478 | required_exponent_sign: false, |
| 479 | no_exponent_without_fraction: false, |
| 480 | no_special: false, |
| 481 | case_sensitive_special: false, |
| 482 | no_integer_leading_zeros: false, |
| 483 | no_float_leading_zeros: false, |
| 484 | required_exponent_notation: false, |
| 485 | case_sensitive_exponent: false, |
| 486 | case_sensitive_base_prefix: false, |
| 487 | case_sensitive_base_suffix: false, |
| 488 | integer_internal_digit_separator: false, |
| 489 | fraction_internal_digit_separator: false, |
| 490 | exponent_internal_digit_separator: false, |
| 491 | integer_leading_digit_separator: false, |
| 492 | fraction_leading_digit_separator: false, |
| 493 | exponent_leading_digit_separator: false, |
| 494 | integer_trailing_digit_separator: false, |
| 495 | fraction_trailing_digit_separator: false, |
| 496 | exponent_trailing_digit_separator: false, |
| 497 | integer_consecutive_digit_separator: false, |
| 498 | fraction_consecutive_digit_separator: false, |
| 499 | exponent_consecutive_digit_separator: false, |
| 500 | special_digit_separator: false, |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /// Create number format for standard, binary number. |
| 505 | #[cfg(feature = "power-of-two")] |
| 506 | pub const fn binary() -> u128 { |
| 507 | Self::from_radix(2) |
| 508 | } |
| 509 | |
| 510 | /// Create number format for standard, octal number. |
| 511 | #[cfg(feature = "power-of-two")] |
| 512 | pub const fn octal() -> u128 { |
| 513 | Self::from_radix(8) |
| 514 | } |
| 515 | |
| 516 | /// Create number format for standard, decimal number. |
| 517 | pub const fn decimal() -> u128 { |
| 518 | let mut builder = Self::new(); |
| 519 | builder.mantissa_radix = 10; |
| 520 | builder.exponent_base = num::NonZeroU8::new(10); |
| 521 | builder.exponent_radix = num::NonZeroU8::new(10); |
| 522 | builder.build_strict() |
| 523 | } |
| 524 | |
| 525 | /// Create number format for standard, hexadecimal number. |
| 526 | #[cfg(feature = "power-of-two")] |
| 527 | pub const fn hexadecimal() -> u128 { |
| 528 | Self::from_radix(16) |
| 529 | } |
| 530 | |
| 531 | /// Create number format from radix. |
| 532 | /// |
| 533 | /// <div class="warning"> |
| 534 | /// |
| 535 | /// This function will never fail even if the radix is invalid. It is up to |
| 536 | /// the caller to ensure the format is valid using |
| 537 | /// [`NumberFormat::is_valid`]. Only radixes from `2` to `36` should be |
| 538 | /// used. |
| 539 | /// |
| 540 | /// </div> |
| 541 | /// |
| 542 | /// [`NumberFormat::is_valid`]: crate::NumberFormat::is_valid |
| 543 | // FIXME: Use `build_strict` when we can have a breaking change. |
| 544 | #[allow(deprecated)] |
| 545 | #[cfg(feature = "power-of-two")] |
| 546 | pub const fn from_radix(radix: u8) -> u128 { |
| 547 | Self::new() |
| 548 | .radix(radix) |
| 549 | .exponent_base(num::NonZeroU8::new(radix)) |
| 550 | .exponent_radix(num::NonZeroU8::new(radix)) |
| 551 | .build() |
| 552 | } |
| 553 | |
| 554 | // GETTERS |
| 555 | |
| 556 | // NOTE: This contains a lot of tests for our tables that would spam our |
| 557 | // documentation, so we hide them internally. See `scripts/docs.py` for |
| 558 | // how the tests are generated and run. This assumes the `format` and |
| 559 | // `radix` features are enabled. |
| 560 | |
| 561 | /// Get the digit separator for the number format. |
| 562 | /// |
| 563 | /// Digit separators are frequently used in number literals to group |
| 564 | /// digits: `1,000,000` is a lot more readable than `1000000`, but |
| 565 | /// the `,` characters should be ignored in the parsing of the number. |
| 566 | /// |
| 567 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 568 | /// to [`None`], or no digit separators allowed. |
| 569 | /// |
| 570 | /// # Examples |
| 571 | /// |
| 572 | /// Using a digit separator of `_` (note that the validity |
| 573 | /// oh where a digit separator can appear depends on the other digit |
| 574 | /// separator flags). |
| 575 | /// |
| 576 | /// | Input | Valid? | |
| 577 | /// |:-:|:-:| |
| 578 | /// | `1` | ✔️ | |
| 579 | /// | `1_4` | ✔️ | |
| 580 | /// | `+_14` | ✔️ | |
| 581 | /// | `+14e3_5` | ✔️ | |
| 582 | /// | `1_d` | ❌ | |
| 583 | /// |
| 584 | /// # Used For |
| 585 | /// |
| 586 | /// - Parse Float |
| 587 | /// - Parse Integer |
| 588 | #[inline(always)] |
| 589 | pub const fn get_digit_separator(&self) -> OptionU8 { |
| 590 | self.digit_separator |
| 591 | } |
| 592 | |
| 593 | /// Get the radix for mantissa digits. |
| 594 | /// |
| 595 | /// This is only used for the significant digits, that is, the integral and |
| 596 | /// fractional components. Can only be modified with |
| 597 | /// [`feature`][crate#features] `power-of-two` or `radix`. Defaults |
| 598 | /// to `10`. |
| 599 | /// |
| 600 | /// | Radix | String | Number | |
| 601 | /// |:-:|:-:|:-:| |
| 602 | /// | 2 | "10011010010" | 1234 | |
| 603 | /// | 3 | "1200201" | 1234 | |
| 604 | /// | 8 | "2322" | 1234 | |
| 605 | /// | 10 | "1234" | 1234 | |
| 606 | /// | 16 | "4d2" | 1234 | |
| 607 | /// | 31 | "18p" | 1234 | |
| 608 | /// |
| 609 | /// # Used For |
| 610 | /// |
| 611 | /// - Parse Float |
| 612 | /// - Parse Integer |
| 613 | /// - Write Float |
| 614 | /// - Write Integer |
| 615 | #[inline(always)] |
| 616 | pub const fn get_mantissa_radix(&self) -> u8 { |
| 617 | self.mantissa_radix |
| 618 | } |
| 619 | |
| 620 | /// Get the radix for the exponent. |
| 621 | /// |
| 622 | /// For example, in `1.234e3`, it means `1.234 * 10^3`, and the exponent |
| 623 | /// base here is 10. Some programming languages, like C, support hex floats |
| 624 | /// with an exponent base of 2, for example `0x1.8p3`, or `1.5 * 2^3`. |
| 625 | /// Defaults to `10`. Can only be modified with [`feature`][crate#features] |
| 626 | /// `power-of-two` or `radix`. Defaults to `10`. |
| 627 | /// |
| 628 | /// # Used For |
| 629 | /// |
| 630 | /// - Parse Float |
| 631 | /// - Parse Integer |
| 632 | #[inline(always)] |
| 633 | pub const fn get_exponent_base(&self) -> OptionU8 { |
| 634 | self.exponent_base |
| 635 | } |
| 636 | |
| 637 | /// Get the radix for exponent digits. |
| 638 | /// |
| 639 | /// This is only used for the exponent digits. We assume the radix for the |
| 640 | /// significant digits ([`get_mantissa_radix`][Self::get_mantissa_radix]) is |
| 641 | /// 10 as is the exponent base. Defaults to `10`. Can only be modified with |
| 642 | /// [`feature`][crate#features] `power-of-two` or `radix`. Defaults to `10`. |
| 643 | /// |
| 644 | /// | Radix | String | Number | |
| 645 | /// |:-:|:-:|:-:| |
| 646 | /// | 2 | "1.234^1100" | 1.234e9 | |
| 647 | /// | 3 | "1.234^110" | 1.234e9 | |
| 648 | /// | 8 | "1.234^14" | 1.234e9 | |
| 649 | /// | 10 | "1.234^12" | 1.234e9 | |
| 650 | /// | 16 | "1.234^c" | 1.234e9 | |
| 651 | /// | 31 | "1.234^c" | 1.234e9 | |
| 652 | /// |
| 653 | /// # Used For |
| 654 | /// |
| 655 | /// - Parse Float |
| 656 | /// - Parse Integer |
| 657 | #[inline(always)] |
| 658 | pub const fn get_exponent_radix(&self) -> OptionU8 { |
| 659 | self.exponent_radix |
| 660 | } |
| 661 | |
| 662 | /// Get the optional character for the base prefix. |
| 663 | /// |
| 664 | /// This character will come after a leading zero, so for example |
| 665 | /// setting the base prefix to `x` means that a leading `0x` will |
| 666 | /// be ignore, if present. Can only be modified with |
| 667 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 668 | /// `format`. Defaults to [`None`], or no base prefix allowed. |
| 669 | /// |
| 670 | /// # Examples |
| 671 | /// |
| 672 | /// Using a base prefix of `x`. |
| 673 | /// |
| 674 | /// | Input | Valid? | |
| 675 | /// |:-:|:-:| |
| 676 | /// | `0x1` | ✔️ | |
| 677 | /// | `x1` | ❌ | |
| 678 | /// | `1` | ✔️ | |
| 679 | /// | `1x` | ❌ | |
| 680 | /// | `1x1` | ❌ | |
| 681 | /// |
| 682 | /// # Used For |
| 683 | /// |
| 684 | /// - Parse Float |
| 685 | /// - Parse Integer |
| 686 | #[inline(always)] |
| 687 | pub const fn get_base_prefix(&self) -> OptionU8 { |
| 688 | self.base_prefix |
| 689 | } |
| 690 | |
| 691 | /// Get the optional character for the base suffix. |
| 692 | /// |
| 693 | /// This character will at the end of the buffer, so for example |
| 694 | /// setting the base prefix to `x` means that a trailing `x` will |
| 695 | /// be ignored, if present. Can only be modified with |
| 696 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 697 | /// `format`. Defaults to [`None`], or no base suffix allowed. |
| 698 | /// |
| 699 | /// # Examples |
| 700 | /// |
| 701 | /// Using a base suffix of `x`. |
| 702 | /// |
| 703 | /// | Input | Valid? | |
| 704 | /// |:-:|:-:| |
| 705 | /// | `1` | ✔️ | |
| 706 | /// | `1x` | ✔️ | |
| 707 | /// | `1d` | ❌ | |
| 708 | /// |
| 709 | /// # Used For |
| 710 | /// |
| 711 | /// - Parse Float |
| 712 | /// - Parse Integer |
| 713 | #[inline(always)] |
| 714 | pub const fn get_base_suffix(&self) -> OptionU8 { |
| 715 | self.base_suffix |
| 716 | } |
| 717 | |
| 718 | /// Get if digits are required before the decimal point. |
| 719 | /// |
| 720 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 721 | /// to [`false`]. |
| 722 | /// |
| 723 | /// # Examples |
| 724 | /// |
| 725 | /// | Input | Valid? | |
| 726 | /// |:-:|:-:| |
| 727 | /// | `1.1` | ✔️ | |
| 728 | /// | `0.1` | ✔️ | |
| 729 | /// | `1` | ✔️ | |
| 730 | /// | `.1` | ❌ | |
| 731 | /// |
| 732 | /// # Used For |
| 733 | /// |
| 734 | /// - Parse Float |
| 735 | #[inline(always)] |
| 736 | pub const fn get_required_integer_digits(&self) -> bool { |
| 737 | self.required_integer_digits |
| 738 | } |
| 739 | |
| 740 | /// Get if digits are required after the decimal point, if the decimal point |
| 741 | /// is present. |
| 742 | /// |
| 743 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 744 | /// to [`false`]. |
| 745 | /// |
| 746 | /// # Examples |
| 747 | /// |
| 748 | /// | Input | Valid? | |
| 749 | /// |:-:|:-:| |
| 750 | /// | `1.1` | ✔️ | |
| 751 | /// | `1` | ✔️ | |
| 752 | /// | `1.` | ❌ | |
| 753 | /// |
| 754 | /// # Used For |
| 755 | /// |
| 756 | /// - Parse Float |
| 757 | #[inline(always)] |
| 758 | pub const fn get_required_fraction_digits(&self) -> bool { |
| 759 | self.required_fraction_digits |
| 760 | } |
| 761 | |
| 762 | /// Get if digits are required after the exponent character, if the exponent |
| 763 | /// is present. |
| 764 | /// |
| 765 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 766 | /// to [`true`]. |
| 767 | /// |
| 768 | /// # Examples |
| 769 | /// |
| 770 | /// | Input | Valid? | |
| 771 | /// |:-:|:-:| |
| 772 | /// | `1.1e+3` | ✔️ | |
| 773 | /// | `1.1e3` | ✔️ | |
| 774 | /// | `1.1e+` | ❌ | |
| 775 | /// | `1.1e` | ❌ | |
| 776 | /// |
| 777 | /// # Used For |
| 778 | /// |
| 779 | /// - Parse Float |
| 780 | #[inline(always)] |
| 781 | pub const fn get_required_exponent_digits(&self) -> bool { |
| 782 | self.required_exponent_digits |
| 783 | } |
| 784 | |
| 785 | /// Get if at least 1 significant digit is required. |
| 786 | /// |
| 787 | /// If not required, then values like `.` (`0`) are valid, but empty strings |
| 788 | /// are still invalid. Can only be modified with [`feature`][crate#features] |
| 789 | /// `format`. Defaults to [`true`]. |
| 790 | /// |
| 791 | /// # Examples |
| 792 | /// |
| 793 | /// | Input | Valid? | |
| 794 | /// |:-:|:-:| |
| 795 | /// | `1.1` | ✔️ | |
| 796 | /// | `.` | ✔️ | |
| 797 | /// | `e10` | ✔️ | |
| 798 | /// | `.e10` | ✔️ | |
| 799 | /// | | ❌ | |
| 800 | /// |
| 801 | /// # Used For |
| 802 | /// |
| 803 | /// - Parse Float |
| 804 | #[inline(always)] |
| 805 | pub const fn get_required_mantissa_digits(&self) -> bool { |
| 806 | self.required_mantissa_digits |
| 807 | } |
| 808 | |
| 809 | /// Get if a positive sign before the mantissa is not allowed. |
| 810 | /// |
| 811 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 812 | /// to `false`. |
| 813 | /// |
| 814 | /// # Examples |
| 815 | /// |
| 816 | /// | Input | Valid? | |
| 817 | /// |:-:|:-:| |
| 818 | /// | `1.1` | ✔️ | |
| 819 | /// | `-1.1` | ✔️ | |
| 820 | /// | `+1.1` | ❌ | |
| 821 | /// |
| 822 | /// # Used For |
| 823 | /// |
| 824 | /// - Parse Float |
| 825 | /// - Parse Integer |
| 826 | /// - Write Float |
| 827 | #[inline(always)] |
| 828 | pub const fn get_no_positive_mantissa_sign(&self) -> bool { |
| 829 | self.no_positive_mantissa_sign |
| 830 | } |
| 831 | |
| 832 | /// Get if a sign symbol before the mantissa is required. |
| 833 | /// |
| 834 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 835 | /// to `false`. |
| 836 | /// |
| 837 | /// # Examples |
| 838 | /// |
| 839 | /// | Input | Valid? | |
| 840 | /// |:-:|:-:| |
| 841 | /// | `1.1` | ❌ | |
| 842 | /// | `-1.1` | ✔️ | |
| 843 | /// | `+1.1` | ✔️ | |
| 844 | /// |
| 845 | /// # Used For |
| 846 | /// |
| 847 | /// - Parse Float |
| 848 | /// - Parse Integer |
| 849 | /// - Write Float |
| 850 | #[inline(always)] |
| 851 | pub const fn get_required_mantissa_sign(&self) -> bool { |
| 852 | self.required_mantissa_sign |
| 853 | } |
| 854 | |
| 855 | /// Get if exponent notation is not allowed. |
| 856 | /// |
| 857 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 858 | /// to `false`. |
| 859 | /// |
| 860 | /// # Examples |
| 861 | /// |
| 862 | /// | Input | Valid? | |
| 863 | /// |:-:|:-:| |
| 864 | /// | `1` | ✔️ | |
| 865 | /// | `1.1` | ✔️ | |
| 866 | /// | `1.1e` | ❌ | |
| 867 | /// | `1.1e5` | ❌ | |
| 868 | /// |
| 869 | /// # Used For |
| 870 | /// |
| 871 | /// - Parse Float |
| 872 | /// - Write Float |
| 873 | #[inline(always)] |
| 874 | pub const fn get_no_exponent_notation(&self) -> bool { |
| 875 | self.no_exponent_notation |
| 876 | } |
| 877 | |
| 878 | /// Get if a positive sign before the exponent is not allowed. |
| 879 | /// |
| 880 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 881 | /// to `false`. |
| 882 | /// |
| 883 | /// # Examples |
| 884 | /// |
| 885 | /// | Input | Valid? | |
| 886 | /// |:-:|:-:| |
| 887 | /// | `1.1e3` | ✔️ | |
| 888 | /// | `1.1e-3` | ✔️ | |
| 889 | /// | `1.1e+3` | ❌ | |
| 890 | /// |
| 891 | /// # Used For |
| 892 | /// |
| 893 | /// - Parse Float |
| 894 | /// - Write Float |
| 895 | #[inline(always)] |
| 896 | pub const fn get_no_positive_exponent_sign(&self) -> bool { |
| 897 | self.no_positive_exponent_sign |
| 898 | } |
| 899 | |
| 900 | /// Get if a sign symbol before the exponent is required. |
| 901 | /// |
| 902 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 903 | /// to `false`. |
| 904 | /// |
| 905 | /// # Examples |
| 906 | /// |
| 907 | /// | Input | Valid? | |
| 908 | /// |:-:|:-:| |
| 909 | /// | `1.1e3` | ❌ | |
| 910 | /// | `1.1e-3` | ✔️ | |
| 911 | /// | `1.1e+3` | ✔️ | |
| 912 | /// |
| 913 | /// # Used For |
| 914 | /// |
| 915 | /// - Parse Float |
| 916 | /// - Write Float |
| 917 | #[inline(always)] |
| 918 | pub const fn get_required_exponent_sign(&self) -> bool { |
| 919 | self.required_exponent_sign |
| 920 | } |
| 921 | |
| 922 | /// Get if an exponent without fraction is not allowed. |
| 923 | /// |
| 924 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 925 | /// to `false`. |
| 926 | /// |
| 927 | /// # Examples |
| 928 | /// |
| 929 | /// | Input | Valid? | |
| 930 | /// |:-:|:-:| |
| 931 | /// | `1e3` | ❌ | |
| 932 | /// | `1.e3` | ❌ | |
| 933 | /// | `1.1e` | ✔️ | |
| 934 | /// | `.1e3` | ✔️ | |
| 935 | /// |
| 936 | /// # Used For |
| 937 | /// |
| 938 | /// - Parse Float |
| 939 | #[inline(always)] |
| 940 | pub const fn get_no_exponent_without_fraction(&self) -> bool { |
| 941 | self.no_exponent_without_fraction |
| 942 | } |
| 943 | |
| 944 | /// Get if special (non-finite) values are not allowed. |
| 945 | /// |
| 946 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 947 | /// to `false`. |
| 948 | /// |
| 949 | /// # Examples |
| 950 | /// |
| 951 | /// | Input | Valid? | |
| 952 | /// |:-:|:-:| |
| 953 | /// | `NaN` | ❌ | |
| 954 | /// | `inf` | ❌ | |
| 955 | /// | `-Infinity` | ❌ | |
| 956 | /// | `1.1e` | ✔️ | |
| 957 | /// |
| 958 | /// # Used For |
| 959 | /// |
| 960 | /// - Parse Float |
| 961 | #[inline(always)] |
| 962 | pub const fn get_no_special(&self) -> bool { |
| 963 | self.no_special |
| 964 | } |
| 965 | |
| 966 | /// Get if special (non-finite) values are case-sensitive. |
| 967 | /// |
| 968 | /// If set to [`true`], then `NaN` and `nan` are treated as the same value |
| 969 | /// ([Not a Number][f64::NAN]). Can only be modified with |
| 970 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 971 | /// |
| 972 | /// # Used For |
| 973 | /// |
| 974 | /// - Parse Float |
| 975 | #[inline(always)] |
| 976 | pub const fn get_case_sensitive_special(&self) -> bool { |
| 977 | self.case_sensitive_special |
| 978 | } |
| 979 | |
| 980 | /// Get if leading zeros before an integer are not allowed. |
| 981 | /// |
| 982 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 983 | /// to [`false`]. |
| 984 | /// |
| 985 | /// # Examples |
| 986 | /// |
| 987 | /// | Input | Valid? | |
| 988 | /// |:-:|:-:| |
| 989 | /// | `01` | ❌ | |
| 990 | /// | `0` | ✔️ | |
| 991 | /// | `10` | ✔️ | |
| 992 | /// |
| 993 | /// # Used For |
| 994 | /// |
| 995 | /// - Parse Integer |
| 996 | #[inline(always)] |
| 997 | pub const fn get_no_integer_leading_zeros(&self) -> bool { |
| 998 | self.no_integer_leading_zeros |
| 999 | } |
| 1000 | |
| 1001 | /// Get if leading zeros before a float are not allowed. |
| 1002 | /// |
| 1003 | /// This is before the significant digits of the float, that is, if there is |
| 1004 | /// 1 or more digits in the integral component and the leading digit is 0, |
| 1005 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 1006 | /// to [`false`]. |
| 1007 | /// |
| 1008 | /// # Examples |
| 1009 | /// |
| 1010 | /// | Input | Valid? | |
| 1011 | /// |:-:|:-:| |
| 1012 | /// | `01` | ❌ | |
| 1013 | /// | `01.0` | ❌ | |
| 1014 | /// | `0` | ✔️ | |
| 1015 | /// | `10` | ✔️ | |
| 1016 | /// | `0.1` | ✔️ | |
| 1017 | /// |
| 1018 | /// # Used For |
| 1019 | /// |
| 1020 | /// - Parse Float |
| 1021 | #[inline(always)] |
| 1022 | pub const fn get_no_float_leading_zeros(&self) -> bool { |
| 1023 | self.no_float_leading_zeros |
| 1024 | } |
| 1025 | |
| 1026 | /// Get if exponent notation is required. |
| 1027 | /// |
| 1028 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 1029 | /// to [`false`]. |
| 1030 | /// |
| 1031 | /// # Examples |
| 1032 | /// |
| 1033 | /// | Input | Valid? | |
| 1034 | /// |:-:|:-:| |
| 1035 | /// | `1` | ❌ | |
| 1036 | /// | `1.0` | ❌ | |
| 1037 | /// | `1e3` | ✔️ | |
| 1038 | /// | `1.1e3` | ✔️ | |
| 1039 | /// |
| 1040 | /// # Used For |
| 1041 | /// |
| 1042 | /// - Parse Float |
| 1043 | /// - Write Float |
| 1044 | #[inline(always)] |
| 1045 | pub const fn get_required_exponent_notation(&self) -> bool { |
| 1046 | self.required_exponent_notation |
| 1047 | } |
| 1048 | |
| 1049 | /// Get if exponent characters are case-sensitive. |
| 1050 | /// |
| 1051 | /// If set to [`true`], then the exponent character `e` would be considered |
| 1052 | /// the different from `E`. Can only be modified with |
| 1053 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 1054 | /// |
| 1055 | /// # Used For |
| 1056 | /// |
| 1057 | /// - Parse Float |
| 1058 | #[inline(always)] |
| 1059 | pub const fn get_case_sensitive_exponent(&self) -> bool { |
| 1060 | self.case_sensitive_exponent |
| 1061 | } |
| 1062 | |
| 1063 | /// Get if base prefixes are case-sensitive. |
| 1064 | /// |
| 1065 | /// If set to [`true`], then the base prefix `x` would be considered the |
| 1066 | /// different from `X`. Can only be modified with |
| 1067 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 1068 | /// `format`. Defaults to [`false`]. |
| 1069 | /// |
| 1070 | /// # Used For |
| 1071 | /// |
| 1072 | /// - Parse Float |
| 1073 | /// - Parse Integer |
| 1074 | #[inline(always)] |
| 1075 | pub const fn get_case_sensitive_base_prefix(&self) -> bool { |
| 1076 | self.case_sensitive_base_prefix |
| 1077 | } |
| 1078 | |
| 1079 | /// Get if base suffixes are case-sensitive. |
| 1080 | /// |
| 1081 | /// If set to [`true`], then the base suffix `x` would be considered the |
| 1082 | /// different from `X`. Can only be modified with |
| 1083 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 1084 | /// `format`. Defaults to [`false`]. |
| 1085 | /// |
| 1086 | /// # Used For |
| 1087 | /// |
| 1088 | /// - Parse Float |
| 1089 | /// - Parse Integer |
| 1090 | #[inline(always)] |
| 1091 | pub const fn get_case_sensitive_base_suffix(&self) -> bool { |
| 1092 | self.case_sensitive_base_suffix |
| 1093 | } |
| 1094 | |
| 1095 | /// Get if digit separators are allowed between integer digits. |
| 1096 | /// |
| 1097 | /// This will not consider an input of only the digit separator |
| 1098 | /// to be a valid separator: the digit separator must be surrounded by |
| 1099 | /// digits. Can only be modified with [`feature`][crate#features] `format`. |
| 1100 | /// Defaults to [`false`]. |
| 1101 | /// |
| 1102 | /// # Examples |
| 1103 | /// |
| 1104 | /// Using a digit separator of `_`. |
| 1105 | /// |
| 1106 | /// | Input | Valid? | |
| 1107 | /// |:-:|:-:| |
| 1108 | /// | `1` | ✔️ | |
| 1109 | /// | `_` | ❌ | |
| 1110 | /// | `1_1` | ✔️ | |
| 1111 | /// | `1_` | ❌ | |
| 1112 | /// | `_1` | ❌ | |
| 1113 | /// |
| 1114 | /// # Used For |
| 1115 | /// |
| 1116 | /// - Parse Float |
| 1117 | /// - Parse Integer |
| 1118 | #[inline(always)] |
| 1119 | pub const fn get_integer_internal_digit_separator(&self) -> bool { |
| 1120 | self.integer_internal_digit_separator |
| 1121 | } |
| 1122 | |
| 1123 | /// Get if digit separators are allowed between fraction digits. |
| 1124 | /// |
| 1125 | /// This will not consider an input of only the digit separator |
| 1126 | /// to be a valid separator: the digit separator must be surrounded by |
| 1127 | /// digits. Can only be modified with [`feature`][crate#features] `format`. |
| 1128 | /// Defaults to [`false`]. |
| 1129 | /// |
| 1130 | /// # Examples |
| 1131 | /// |
| 1132 | /// Using a digit separator of `_`. |
| 1133 | /// |
| 1134 | /// | Input | Valid? | |
| 1135 | /// |:-:|:-:| |
| 1136 | /// | `1.1` | ✔️ | |
| 1137 | /// | `1._` | ❌ | |
| 1138 | /// | `1.1_1` | ✔️ | |
| 1139 | /// | `1.1_` | ❌ | |
| 1140 | /// | `1._1` | ❌ | |
| 1141 | /// |
| 1142 | /// # Used For |
| 1143 | /// |
| 1144 | /// - Parse Float |
| 1145 | #[inline(always)] |
| 1146 | pub const fn get_fraction_internal_digit_separator(&self) -> bool { |
| 1147 | self.fraction_internal_digit_separator |
| 1148 | } |
| 1149 | |
| 1150 | /// Get if digit separators are allowed between exponent digits. |
| 1151 | /// |
| 1152 | /// This will not consider an input of only the digit separator |
| 1153 | /// to be a valid separator: the digit separator must be surrounded by |
| 1154 | /// digits. Can only be modified with [`feature`][crate#features] `format`. |
| 1155 | /// Defaults to [`false`]. |
| 1156 | /// |
| 1157 | /// # Examples |
| 1158 | /// |
| 1159 | /// Using a digit separator of `_`. |
| 1160 | /// |
| 1161 | /// | Input | Valid? | |
| 1162 | /// |:-:|:-:| |
| 1163 | /// | `1.1e1` | ✔️ | |
| 1164 | /// | `1.1e_` | ❌ | |
| 1165 | /// | `1.1e1_1` | ✔️ | |
| 1166 | /// | `1.1e1_` | ❌ | |
| 1167 | /// | `1.1e_1` | ❌ | |
| 1168 | /// |
| 1169 | /// # Used For |
| 1170 | /// |
| 1171 | /// - Parse Float |
| 1172 | #[inline(always)] |
| 1173 | pub const fn get_exponent_internal_digit_separator(&self) -> bool { |
| 1174 | self.exponent_internal_digit_separator |
| 1175 | } |
| 1176 | |
| 1177 | /// Get if a digit separator is allowed before any integer digits. |
| 1178 | /// |
| 1179 | /// This will consider an input of only the digit separator |
| 1180 | /// to be a identical to empty input. Can only be modified with |
| 1181 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 1182 | /// |
| 1183 | /// # Examples |
| 1184 | /// |
| 1185 | /// Using a digit separator of `_`. |
| 1186 | /// |
| 1187 | /// | Input | Valid? | |
| 1188 | /// |:-:|:-:| |
| 1189 | /// | `1` | ✔️ | |
| 1190 | /// | `_` | ❌ | |
| 1191 | /// | `1_1` | ❌ | |
| 1192 | /// | `1_` | ❌ | |
| 1193 | /// | `_1` | ✔️ | |
| 1194 | /// |
| 1195 | /// # Used For |
| 1196 | /// |
| 1197 | /// - Parse Float |
| 1198 | /// - Parse Integer |
| 1199 | #[inline(always)] |
| 1200 | pub const fn get_integer_leading_digit_separator(&self) -> bool { |
| 1201 | self.integer_leading_digit_separator |
| 1202 | } |
| 1203 | |
| 1204 | /// Get if a digit separator is allowed before any fraction digits. |
| 1205 | /// |
| 1206 | /// This will consider an input of only the digit separator |
| 1207 | /// to be a identical to empty input. Can only be modified with |
| 1208 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 1209 | /// |
| 1210 | /// # Examples |
| 1211 | /// |
| 1212 | /// Using a digit separator of `_`. |
| 1213 | /// |
| 1214 | /// | Input | Valid? | |
| 1215 | /// |:-:|:-:| |
| 1216 | /// | `1.1` | ✔️ | |
| 1217 | /// | `1._` | ❌ | |
| 1218 | /// | `1.1_1` | ❌ | |
| 1219 | /// | `1.1_` | ❌ | |
| 1220 | /// | `1._1` | ✔️ | |
| 1221 | /// |
| 1222 | /// # Used For |
| 1223 | /// |
| 1224 | /// - Parse Float |
| 1225 | #[inline(always)] |
| 1226 | pub const fn get_fraction_leading_digit_separator(&self) -> bool { |
| 1227 | self.fraction_leading_digit_separator |
| 1228 | } |
| 1229 | |
| 1230 | /// Get if a digit separator is allowed before any exponent digits. |
| 1231 | /// |
| 1232 | /// This will consider an input of only the digit separator |
| 1233 | /// to be a identical to empty input. Can only be modified with |
| 1234 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 1235 | /// |
| 1236 | /// # Examples |
| 1237 | /// |
| 1238 | /// Using a digit separator of `_`. |
| 1239 | /// |
| 1240 | /// | Input | Valid? | |
| 1241 | /// |:-:|:-:| |
| 1242 | /// | `1.1e1` | ✔️ | |
| 1243 | /// | `1.1e_` | ❌ | |
| 1244 | /// | `1.1e1_1` | ❌ | |
| 1245 | /// | `1.1e1_` | ❌ | |
| 1246 | /// | `1.1e_1` | ✔️ | |
| 1247 | /// |
| 1248 | /// # Used For |
| 1249 | /// |
| 1250 | /// - Parse Float |
| 1251 | #[inline(always)] |
| 1252 | pub const fn get_exponent_leading_digit_separator(&self) -> bool { |
| 1253 | self.exponent_leading_digit_separator |
| 1254 | } |
| 1255 | |
| 1256 | /// Get if a digit separator is allowed after any integer digits. |
| 1257 | /// |
| 1258 | /// This will consider an input of only the digit separator |
| 1259 | /// to be a identical to empty input. Can only be modified with |
| 1260 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 1261 | /// |
| 1262 | /// # Examples |
| 1263 | /// |
| 1264 | /// Using a digit separator of `_`. |
| 1265 | /// |
| 1266 | /// | Input | Valid? | |
| 1267 | /// |:-:|:-:| |
| 1268 | /// | `1` | ✔️ | |
| 1269 | /// | `_` | ❌ | |
| 1270 | /// | `1_1` | ❌ | |
| 1271 | /// | `1_` | ✔️ | |
| 1272 | /// | `_1` | ❌ | |
| 1273 | /// |
| 1274 | /// # Used For |
| 1275 | /// |
| 1276 | /// - Parse Float |
| 1277 | /// - Parse Integer |
| 1278 | #[inline(always)] |
| 1279 | pub const fn get_integer_trailing_digit_separator(&self) -> bool { |
| 1280 | self.integer_trailing_digit_separator |
| 1281 | } |
| 1282 | |
| 1283 | /// Get if a digit separator is allowed after any fraction digits. |
| 1284 | /// |
| 1285 | /// This will consider an input of only the digit separator |
| 1286 | /// to be a identical to empty input. Can only be modified with |
| 1287 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. # Examples |
| 1288 | /// |
| 1289 | /// Using a digit separator of `_`. |
| 1290 | /// |
| 1291 | /// | Input | Valid? | |
| 1292 | /// |:-:|:-:| |
| 1293 | /// | `1.1` | ✔️ | |
| 1294 | /// | `1._` | ❌ | |
| 1295 | /// | `1.1_1` | ❌ | |
| 1296 | /// | `1.1_` | ✔️ | |
| 1297 | /// | `1._1` | ❌ | |
| 1298 | /// |
| 1299 | /// # Used For |
| 1300 | /// |
| 1301 | /// - Parse Float |
| 1302 | #[inline(always)] |
| 1303 | pub const fn get_fraction_trailing_digit_separator(&self) -> bool { |
| 1304 | self.fraction_trailing_digit_separator |
| 1305 | } |
| 1306 | |
| 1307 | /// Get if a digit separator is allowed after any exponent digits. |
| 1308 | /// |
| 1309 | /// This will consider an input of only the digit separator |
| 1310 | /// to be a identical to empty input. Can only be modified with |
| 1311 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 1312 | /// |
| 1313 | /// # Examples |
| 1314 | /// |
| 1315 | /// Using a digit separator of `_`. |
| 1316 | /// |
| 1317 | /// | Input | Valid? | |
| 1318 | /// |:-:|:-:| |
| 1319 | /// | `1.1e1` | ✔️ | |
| 1320 | /// | `1.1e_` | ❌ | |
| 1321 | /// | `1.1e1_1` | ❌ | |
| 1322 | /// | `1.1e1_` | ✔️ | |
| 1323 | /// | `1.1e_1` | ❌ | |
| 1324 | /// |
| 1325 | /// # Used For |
| 1326 | /// |
| 1327 | /// - Parse Float |
| 1328 | #[inline(always)] |
| 1329 | pub const fn get_exponent_trailing_digit_separator(&self) -> bool { |
| 1330 | self.exponent_trailing_digit_separator |
| 1331 | } |
| 1332 | |
| 1333 | /// Get if multiple consecutive integer digit separators are allowed. |
| 1334 | /// |
| 1335 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 1336 | /// digit separators (leading, trailing, internal) are allowed in the |
| 1337 | /// integer. Can only be modified with [`feature`][crate#features] `format`. |
| 1338 | /// Defaults to [`false`]. |
| 1339 | /// |
| 1340 | /// # Used For |
| 1341 | /// |
| 1342 | /// - Parse Float |
| 1343 | /// - Parse Integer |
| 1344 | #[inline(always)] |
| 1345 | pub const fn get_integer_consecutive_digit_separator(&self) -> bool { |
| 1346 | self.integer_consecutive_digit_separator |
| 1347 | } |
| 1348 | |
| 1349 | /// Get if multiple consecutive fraction digit separators are allowed. |
| 1350 | /// |
| 1351 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 1352 | /// digit separators (leading, trailing, internal) are allowed in the |
| 1353 | /// fraction. Can only be modified with [`feature`][crate#features] |
| 1354 | /// `format`. Defaults to [`false`]. |
| 1355 | /// |
| 1356 | /// # Used For |
| 1357 | /// |
| 1358 | /// - Parse Float |
| 1359 | #[inline(always)] |
| 1360 | pub const fn get_fraction_consecutive_digit_separator(&self) -> bool { |
| 1361 | self.fraction_consecutive_digit_separator |
| 1362 | } |
| 1363 | |
| 1364 | /// Get if multiple consecutive exponent digit separators are allowed. |
| 1365 | /// |
| 1366 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 1367 | /// digit separators (leading, trailing, internal) are allowed in the |
| 1368 | /// exponent. Can only be modified with [`feature`][crate#features] |
| 1369 | /// `format`. Defaults to [`false`]. |
| 1370 | /// |
| 1371 | /// # Used For |
| 1372 | /// |
| 1373 | /// - Parse Float |
| 1374 | #[inline(always)] |
| 1375 | pub const fn get_exponent_consecutive_digit_separator(&self) -> bool { |
| 1376 | self.exponent_consecutive_digit_separator |
| 1377 | } |
| 1378 | |
| 1379 | /// Get if any digit separators are allowed in special (non-finite) values. |
| 1380 | /// |
| 1381 | /// This enables leading, trailing, internal, and consecutive digit |
| 1382 | /// separators for any special floats: for example, `N__a_N_` is considered |
| 1383 | /// the same as `NaN`. Can only be modified with [`feature`][crate#features] |
| 1384 | /// `format`. Defaults to [`false`]. |
| 1385 | /// |
| 1386 | /// # Used For |
| 1387 | /// |
| 1388 | /// - Parse Float |
| 1389 | #[inline(always)] |
| 1390 | pub const fn get_special_digit_separator(&self) -> bool { |
| 1391 | self.special_digit_separator |
| 1392 | } |
| 1393 | |
| 1394 | // SETTERS |
| 1395 | |
| 1396 | /// Set the digit separator for the number format. |
| 1397 | /// |
| 1398 | /// Digit separators are frequently used in number literals to group |
| 1399 | /// digits: `1,000,000` is a lot more readable than `1000000`, but |
| 1400 | /// the `,` characters should be ignored in the parsing of the number. |
| 1401 | /// |
| 1402 | /// Defaults to [`None`], or no digit separators allowed. |
| 1403 | /// |
| 1404 | /// # Examples |
| 1405 | /// |
| 1406 | /// Using a digit separator of `_` (note that the validity |
| 1407 | /// oh where a digit separator can appear depends on the other digit |
| 1408 | /// separator flags). |
| 1409 | /// |
| 1410 | /// | Input | Valid? | |
| 1411 | /// |:-:|:-:| |
| 1412 | /// | `1` | ✔️ | |
| 1413 | /// | `1_4` | ✔️ | |
| 1414 | /// | `+_14` | ✔️ | |
| 1415 | /// | `+14e3_5` | ✔️ | |
| 1416 | /// | `1_d` | ❌ | |
| 1417 | /// |
| 1418 | /// # Used For |
| 1419 | /// |
| 1420 | /// - Parse Float |
| 1421 | /// - Parse Integer |
| 1422 | /// |
| 1423 | /// <!-- TEST |
| 1424 | /// ```rust |
| 1425 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1426 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 1427 | /// .leading_digit_separator(true) |
| 1428 | /// .internal_digit_separator(true) |
| 1429 | /// .trailing_digit_separator(true) |
| 1430 | /// .build_strict(); |
| 1431 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 1432 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_4", &PF_OPTS), Ok(14.0)); |
| 1433 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"+_14", &PF_OPTS), Ok(14.0)); |
| 1434 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"+14e3_5", &PF_OPTS), Ok(14e35)); |
| 1435 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_d", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 1436 | /// |
| 1437 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_4", &PI_OPTS), Ok(14)); |
| 1438 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"+_14", &PI_OPTS), Ok(14)); |
| 1439 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_d", &PI_OPTS), Err(Error::InvalidDigit(2))); |
| 1440 | /// ``` |
| 1441 | /// --> |
| 1442 | #[inline(always)] |
| 1443 | #[cfg(feature = "format")] |
| 1444 | pub const fn digit_separator(mut self, character: OptionU8) -> Self { |
| 1445 | self.digit_separator = character; |
| 1446 | self |
| 1447 | } |
| 1448 | |
| 1449 | /// Alias for [`mantissa radix`][Self::mantissa_radix]. |
| 1450 | /// |
| 1451 | /// # Used For |
| 1452 | /// |
| 1453 | /// - Parse Float |
| 1454 | /// - Parse Integer |
| 1455 | /// - Write Float |
| 1456 | /// - Write Integer |
| 1457 | #[inline(always)] |
| 1458 | #[cfg(feature = "power-of-two")] |
| 1459 | pub const fn radix(self, radix: u8) -> Self { |
| 1460 | self.mantissa_radix(radix) |
| 1461 | } |
| 1462 | |
| 1463 | /// Set the radix for mantissa digits. |
| 1464 | /// |
| 1465 | /// This is only used for the significant digits, that is, the integral and |
| 1466 | /// fractional components. Defaults to `10`. |
| 1467 | /// |
| 1468 | /// | Radix | String | Number | |
| 1469 | /// |:-:|:-:|:-:| |
| 1470 | /// | 2 | "10011010010" | 1234 | |
| 1471 | /// | 3 | "1200201" | 1234 | |
| 1472 | /// | 8 | "2322" | 1234 | |
| 1473 | /// | 10 | "1234" | 1234 | |
| 1474 | /// | 16 | "4d2" | 1234 | |
| 1475 | /// | 31 | "18p" | 1234 | |
| 1476 | /// |
| 1477 | /// # Used For |
| 1478 | /// |
| 1479 | /// - Parse Float |
| 1480 | /// - Parse Integer |
| 1481 | /// - Write Float |
| 1482 | /// - Write Integer |
| 1483 | /// |
| 1484 | /// <!-- TEST |
| 1485 | /// ```rust |
| 1486 | /// const BASE2: u128 = NumberFormatBuilder::from_radix(2); |
| 1487 | /// const BASE3: u128 = NumberFormatBuilder::from_radix(3); |
| 1488 | /// const BASE8: u128 = NumberFormatBuilder::from_radix(8); |
| 1489 | /// const BASE10: u128 = NumberFormatBuilder::from_radix(10); |
| 1490 | /// const BASE16: u128 = NumberFormatBuilder::from_radix(16); |
| 1491 | /// const BASE31: u128 = NumberFormatBuilder::from_radix(31); |
| 1492 | /// const PI_RDX: ParseIntegerOptions = ParseIntegerOptions::from_radix(16); |
| 1493 | /// const PF_RDX: ParseFloatOptions = ParseFloatOptions::from_radix(16); |
| 1494 | /// const WI_RDX: WriteIntegerOptions = WriteIntegerOptions::from_radix(16); |
| 1495 | /// const WF_RDX: WriteFloatOptions = WriteFloatOptions::from_radix(16); |
| 1496 | /// |
| 1497 | /// assert_eq!(parse_with_options::<f64, BASE2>(b"10011010010", &PF_RDX), Ok(1234.0)); |
| 1498 | /// assert_eq!(parse_with_options::<f64, BASE3>(b"1200201", &PF_RDX), Ok(1234.0)); |
| 1499 | /// assert_eq!(parse_with_options::<f64, BASE8>(b"2322", &PF_RDX), Ok(1234.0)); |
| 1500 | /// assert_eq!(parse_with_options::<f64, BASE10>(b"1234", &PF_RDX), Ok(1234.0)); |
| 1501 | /// assert_eq!(parse_with_options::<f64, BASE16>(b"4d2", &PF_RDX), Ok(1234.0)); |
| 1502 | /// assert_eq!(parse_with_options::<f64, BASE31>(b"18p", &PF_RDX), Ok(1234.0)); |
| 1503 | /// |
| 1504 | /// assert_eq!(parse_with_options::<i64, BASE2>(b"10011010010", &PI_RDX), Ok(1234)); |
| 1505 | /// assert_eq!(parse_with_options::<i64, BASE3>(b"1200201", &PI_RDX), Ok(1234)); |
| 1506 | /// assert_eq!(parse_with_options::<i64, BASE8>(b"2322", &PI_RDX), Ok(1234)); |
| 1507 | /// assert_eq!(parse_with_options::<i64, BASE10>(b"1234", &PI_RDX), Ok(1234)); |
| 1508 | /// assert_eq!(parse_with_options::<i64, BASE16>(b"4d2", &PI_RDX), Ok(1234)); |
| 1509 | /// assert_eq!(parse_with_options::<i64, BASE31>(b"18p", &PI_RDX), Ok(1234)); |
| 1510 | /// |
| 1511 | /// let mut buffer = [0u8; BUFFER_SIZE]; |
| 1512 | /// assert_eq!(write_with_options::<f64, BASE2>(1234.0, &mut buffer, &WF_RDX), b"1.001101001^1010"); |
| 1513 | /// assert_eq!(write_with_options::<f64, BASE3>(1234.0, &mut buffer, &WF_RDX), b"1200201.0"); |
| 1514 | /// assert_eq!(write_with_options::<f64, BASE8>(1234.0, &mut buffer, &WF_RDX), b"2.322^3"); |
| 1515 | /// assert_eq!(write_with_options::<f64, BASE10>(1234.0, &mut buffer, &WF_RDX), b"1234.0"); |
| 1516 | /// assert_eq!(write_with_options::<f64, BASE16>(1234.0, &mut buffer, &WF_RDX), b"4.D2^2"); |
| 1517 | /// assert_eq!(write_with_options::<f64, BASE31>(1234.0, &mut buffer, &WF_RDX), b"18P.0"); |
| 1518 | /// |
| 1519 | /// assert_eq!(write_with_options::<i64, BASE2>(1234, &mut buffer, &WI_RDX), b"10011010010"); |
| 1520 | /// assert_eq!(write_with_options::<i64, BASE3>(1234, &mut buffer, &WI_RDX), b"1200201"); |
| 1521 | /// assert_eq!(write_with_options::<i64, BASE8>(1234, &mut buffer, &WI_RDX), b"2322"); |
| 1522 | /// assert_eq!(write_with_options::<i64, BASE10>(1234, &mut buffer, &WI_RDX), b"1234"); |
| 1523 | /// assert_eq!(write_with_options::<i64, BASE16>(1234, &mut buffer, &WI_RDX), b"4D2"); |
| 1524 | /// assert_eq!(write_with_options::<i64, BASE31>(1234, &mut buffer, &WI_RDX), b"18P"); |
| 1525 | /// ``` |
| 1526 | /// --> |
| 1527 | #[inline(always)] |
| 1528 | #[cfg(feature = "power-of-two")] |
| 1529 | pub const fn mantissa_radix(mut self, radix: u8) -> Self { |
| 1530 | self.mantissa_radix = radix; |
| 1531 | self |
| 1532 | } |
| 1533 | |
| 1534 | /// Set the radix for the exponent. |
| 1535 | /// |
| 1536 | /// For example, in `1.234e3`, it means `1.234 * 10^3`, and the exponent |
| 1537 | /// base here is 10. Some programming languages, like C, support hex floats |
| 1538 | /// with an exponent base of 2, for example `0x1.8p3`, or `1.5 * 2^3`. |
| 1539 | /// Defaults to `10`. |
| 1540 | /// |
| 1541 | /// # Used For |
| 1542 | /// |
| 1543 | /// - Parse Float |
| 1544 | /// - Parse Integer |
| 1545 | #[inline(always)] |
| 1546 | #[cfg(feature = "power-of-two")] |
| 1547 | pub const fn exponent_base(mut self, base: OptionU8) -> Self { |
| 1548 | self.exponent_base = base; |
| 1549 | self |
| 1550 | } |
| 1551 | |
| 1552 | /// Set the radix for exponent digits. |
| 1553 | /// |
| 1554 | /// This is only used for the exponent digits. We assume the radix for the |
| 1555 | /// significant digits ([`mantissa_radix`][Self::mantissa_radix]) is 10 as |
| 1556 | /// is the exponent base. Defaults to `10`. |
| 1557 | /// |
| 1558 | /// | Radix | String | Number | |
| 1559 | /// |:-:|:-:|:-:| |
| 1560 | /// | 2 | "1.234^1100" | 1.234e9 | |
| 1561 | /// | 3 | "1.234^110" | 1.234e9 | |
| 1562 | /// | 8 | "1.234^14" | 1.234e9 | |
| 1563 | /// | 10 | "1.234^12" | 1.234e9 | |
| 1564 | /// | 16 | "1.234^c" | 1.234e9 | |
| 1565 | /// | 31 | "1.234^c" | 1.234e9 | |
| 1566 | /// |
| 1567 | /// # Used For |
| 1568 | /// |
| 1569 | /// - Parse Float |
| 1570 | /// - Parse Integer |
| 1571 | /// |
| 1572 | /// <!-- TEST |
| 1573 | /// ```rust |
| 1574 | /// macro_rules! exp_radix { |
| 1575 | /// ($exp:literal) => { |
| 1576 | /// NumberFormatBuilder::new() |
| 1577 | /// .mantissa_radix(10) |
| 1578 | /// .exponent_base(num::NonZeroU8::new(10)) |
| 1579 | /// .exponent_radix(num::NonZeroU8::new($exp)) |
| 1580 | /// .build_strict() |
| 1581 | /// }; |
| 1582 | /// } |
| 1583 | /// const BASE2: u128 = exp_radix!(2); |
| 1584 | /// const BASE3: u128 = exp_radix!(3); |
| 1585 | /// const BASE8: u128 = exp_radix!(8); |
| 1586 | /// const BASE10: u128 = exp_radix!(10); |
| 1587 | /// const BASE16: u128 = exp_radix!(16); |
| 1588 | /// const BASE31: u128 = exp_radix!(31); |
| 1589 | /// const PF_RDX: ParseFloatOptions = ParseFloatOptions::from_radix(16); |
| 1590 | /// const WF_RDX: WriteFloatOptions = WriteFloatOptions::from_radix(16); |
| 1591 | /// |
| 1592 | /// assert_eq!(parse_with_options::<f64, BASE2>(b"1.234^1100", &PF_RDX), Ok(1234e9)); |
| 1593 | /// assert_eq!(parse_with_options::<f64, BASE3>(b"1.234^110", &PF_RDX), Ok(1234e9)); |
| 1594 | /// assert_eq!(parse_with_options::<f64, BASE8>(b"1.234^14", &PF_RDX), Ok(1234e9)); |
| 1595 | /// assert_eq!(parse_with_options::<f64, BASE10>(b"1.234^12", &PF_RDX), Ok(1234e9)); |
| 1596 | /// assert_eq!(parse_with_options::<f64, BASE16>(b"1.234^c", &PF_RDX), Ok(1234e9)); |
| 1597 | /// assert_eq!(parse_with_options::<f64, BASE31>(b"1.234^c", &PF_RDX), Ok(1234e9)); |
| 1598 | /// |
| 1599 | /// let mut buffer = [0u8; BUFFER_SIZE]; |
| 1600 | /// assert_eq!(write_with_options::<f64, BASE2>(1234e9, &mut buffer, &WF_RDX), b"1.234^1100"); |
| 1601 | /// assert_eq!(write_with_options::<f64, BASE3>(1234e9, &mut buffer, &WF_RDX), b"1.234^110"); |
| 1602 | /// assert_eq!(write_with_options::<f64, BASE8>(1234e9, &mut buffer, &WF_RDX), b"1.234^14"); |
| 1603 | /// assert_eq!(write_with_options::<f64, BASE10>(1234e9, &mut buffer, &WF_RDX), b"1.234^12"); |
| 1604 | /// assert_eq!(write_with_options::<f64, BASE16>(1234e9, &mut buffer, &WF_RDX), b"1.234^C"); |
| 1605 | /// assert_eq!(write_with_options::<f64, BASE31>(1234e9, &mut buffer, &WF_RDX), b"1.234^C"); |
| 1606 | /// ``` |
| 1607 | /// --> |
| 1608 | #[inline(always)] |
| 1609 | #[cfg(feature = "power-of-two")] |
| 1610 | pub const fn exponent_radix(mut self, radix: OptionU8) -> Self { |
| 1611 | self.exponent_radix = radix; |
| 1612 | self |
| 1613 | } |
| 1614 | |
| 1615 | /// Set the optional character for the base prefix. |
| 1616 | /// |
| 1617 | /// This character will come after a leading zero, so for example |
| 1618 | /// setting the base prefix to `x` means that a leading `0x` will |
| 1619 | /// be ignore, if present. Defaults to [`None`], or no base prefix |
| 1620 | /// allowed. |
| 1621 | /// |
| 1622 | /// # Examples |
| 1623 | /// |
| 1624 | /// Using a base prefix of `x`. |
| 1625 | /// |
| 1626 | /// | Input | Valid? | |
| 1627 | /// |:-:|:-:| |
| 1628 | /// | `0x1` | ✔️ | |
| 1629 | /// | `x1` | ❌ | |
| 1630 | /// | `1` | ✔️ | |
| 1631 | /// | `1x` | ❌ | |
| 1632 | /// | `1x1` | ❌ | |
| 1633 | /// |
| 1634 | /// # Used For |
| 1635 | /// |
| 1636 | /// - Parse Float |
| 1637 | /// - Parse Integer |
| 1638 | /// |
| 1639 | /// <!-- TEST |
| 1640 | /// ```rust |
| 1641 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1642 | /// .base_prefix(num::NonZeroU8::new(b'x')) |
| 1643 | /// .build_strict(); |
| 1644 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0x1", &PF_OPTS), Ok(1.0)); |
| 1645 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"x1", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 1646 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 1647 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 1648 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x1", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 1649 | /// |
| 1650 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Ok(1)); |
| 1651 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"x1", &PI_OPTS), Err(Error::InvalidDigit(0))); |
| 1652 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 1653 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 1654 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x1", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 1655 | /// ``` |
| 1656 | /// --> |
| 1657 | #[inline(always)] |
| 1658 | #[cfg(all(feature = "power-of-two", feature = "format"))] |
| 1659 | pub const fn base_prefix(mut self, base_prefix: OptionU8) -> Self { |
| 1660 | self.base_prefix = base_prefix; |
| 1661 | self |
| 1662 | } |
| 1663 | |
| 1664 | /// Set the optional character for the base suffix. |
| 1665 | /// |
| 1666 | /// This character will at the end of the buffer, so for example |
| 1667 | /// setting the base prefix to `x` means that a trailing `x` will |
| 1668 | /// be ignored, if present. Defaults to [`None`], or no base suffix |
| 1669 | /// allowed. |
| 1670 | /// |
| 1671 | /// # Examples |
| 1672 | /// |
| 1673 | /// Using a base suffix of `x`. |
| 1674 | /// |
| 1675 | /// | Input | Valid? | |
| 1676 | /// |:-:|:-:| |
| 1677 | /// | `1` | ✔️ | |
| 1678 | /// | `1x` | ✔️ | |
| 1679 | /// | `1d` | ❌ | |
| 1680 | /// |
| 1681 | /// # Used For |
| 1682 | /// |
| 1683 | /// - Parse Float |
| 1684 | /// - Parse Integer |
| 1685 | /// |
| 1686 | /// <!-- TEST |
| 1687 | /// ```rust |
| 1688 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1689 | /// .base_suffix(num::NonZeroU8::new(b'x')) |
| 1690 | /// .build_strict(); |
| 1691 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0x1", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 1692 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"x1", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 1693 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 1694 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Ok(1.0)); |
| 1695 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x1", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 1696 | /// |
| 1697 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Err(Error::InvalidDigit(2))); |
| 1698 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"x1", &PI_OPTS), Err(Error::InvalidDigit(0))); |
| 1699 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 1700 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Ok(1)); |
| 1701 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x1", &PI_OPTS), Err(Error::InvalidDigit(2))); |
| 1702 | /// ``` |
| 1703 | /// --> |
| 1704 | #[inline(always)] |
| 1705 | #[cfg(all(feature = "power-of-two", feature = "format"))] |
| 1706 | pub const fn base_suffix(mut self, base_suffix: OptionU8) -> Self { |
| 1707 | self.base_suffix = base_suffix; |
| 1708 | self |
| 1709 | } |
| 1710 | |
| 1711 | /// Set if digits are required before the decimal point. |
| 1712 | /// |
| 1713 | /// Defaults to [`false`]. |
| 1714 | /// |
| 1715 | /// # Examples |
| 1716 | /// |
| 1717 | /// | Input | Valid? | |
| 1718 | /// |:-:|:-:| |
| 1719 | /// | `1.1` | ✔️ | |
| 1720 | /// | `0.1` | ✔️ | |
| 1721 | /// | `1` | ✔️ | |
| 1722 | /// | `.1` | ❌ | |
| 1723 | /// | `1.` | ❌ | |
| 1724 | /// | | ❌ | |
| 1725 | /// |
| 1726 | /// # Used For |
| 1727 | /// |
| 1728 | /// - Parse Float |
| 1729 | /// |
| 1730 | /// <!-- TEST |
| 1731 | /// ```rust |
| 1732 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1733 | /// .required_integer_digits(true) |
| 1734 | /// .build_strict(); |
| 1735 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 1736 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0.1", &PF_OPTS), Ok(0.1)); |
| 1737 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 1738 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.", &PF_OPTS), Ok(1.0)); |
| 1739 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b".1", &PF_OPTS), Err(Error::EmptyInteger(0))); |
| 1740 | /// ``` |
| 1741 | /// --> |
| 1742 | #[inline(always)] |
| 1743 | #[cfg(feature = "format")] |
| 1744 | pub const fn required_integer_digits(mut self, flag: bool) -> Self { |
| 1745 | self.required_integer_digits = flag; |
| 1746 | self |
| 1747 | } |
| 1748 | |
| 1749 | /// Set if digits are required after the decimal point, if the decimal point |
| 1750 | /// is present. |
| 1751 | /// |
| 1752 | /// Defaults to [`false`]. |
| 1753 | /// |
| 1754 | /// # Examples |
| 1755 | /// |
| 1756 | /// | Input | Valid? | |
| 1757 | /// |:-:|:-:| |
| 1758 | /// | `1.1` | ✔️ | |
| 1759 | /// | `0.1` | ✔️ | |
| 1760 | /// | `1` | ✔️ | |
| 1761 | /// | `.1` | ✔️ | |
| 1762 | /// | `1.` | ❌ | |
| 1763 | /// | | ❌ | |
| 1764 | /// |
| 1765 | /// # Used For |
| 1766 | /// |
| 1767 | /// - Parse Float |
| 1768 | /// |
| 1769 | /// <!-- TEST |
| 1770 | /// ```rust |
| 1771 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1772 | /// .required_fraction_digits(true) |
| 1773 | /// .build_strict(); |
| 1774 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 1775 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0.1", &PF_OPTS), Ok(0.1)); |
| 1776 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 1777 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.", &PF_OPTS), Err(Error::EmptyFraction(2))); |
| 1778 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b".1", &PF_OPTS), Ok(0.1)); |
| 1779 | /// ``` |
| 1780 | /// --> |
| 1781 | #[inline(always)] |
| 1782 | #[cfg(feature = "format")] |
| 1783 | pub const fn required_fraction_digits(mut self, flag: bool) -> Self { |
| 1784 | self.required_fraction_digits = flag; |
| 1785 | self |
| 1786 | } |
| 1787 | |
| 1788 | /// Set if digits are required after the exponent character, if the exponent |
| 1789 | /// is present. |
| 1790 | /// |
| 1791 | /// Defaults to [`true`]. |
| 1792 | /// |
| 1793 | /// # Examples |
| 1794 | /// |
| 1795 | /// | Input | Valid? | |
| 1796 | /// |:-:|:-:| |
| 1797 | /// | `1.1e+3` | ✔️ | |
| 1798 | /// | `1.1e3` | ✔️ | |
| 1799 | /// | `1.1e+` | ❌ | |
| 1800 | /// | `1.1e` | ❌ | |
| 1801 | /// |
| 1802 | /// # Used For |
| 1803 | /// |
| 1804 | /// - Parse Float |
| 1805 | /// |
| 1806 | /// <!-- TEST |
| 1807 | /// ```rust |
| 1808 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1809 | /// .required_fraction_digits(true) |
| 1810 | /// .build_strict(); |
| 1811 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e+3", &PF_OPTS), Ok(1.1e3)); |
| 1812 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e3", &PF_OPTS), Ok(1.1e3)); |
| 1813 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e+", &PF_OPTS), Err(Error::EmptyExponent(5))); |
| 1814 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e", &PF_OPTS), Err(Error::EmptyExponent(4))); |
| 1815 | /// ``` |
| 1816 | /// --> |
| 1817 | #[inline(always)] |
| 1818 | #[cfg(feature = "format")] |
| 1819 | pub const fn required_exponent_digits(mut self, flag: bool) -> Self { |
| 1820 | self.required_exponent_digits = flag; |
| 1821 | self |
| 1822 | } |
| 1823 | |
| 1824 | /// Set if at least 1 significant digit is required. |
| 1825 | /// |
| 1826 | /// If not required, then values like `.` (`0`) are valid, but empty strings |
| 1827 | /// are still invalid. Defaults to [`true`]. |
| 1828 | /// |
| 1829 | /// # Examples |
| 1830 | /// |
| 1831 | /// | Input | Valid? | |
| 1832 | /// |:-:|:-:| |
| 1833 | /// | `1.1` | ✔️ | |
| 1834 | /// | `.` | ✔️ | |
| 1835 | /// | `e10` | ❌ | |
| 1836 | /// | `.e10` | ❌ | |
| 1837 | /// | | ❌ | |
| 1838 | /// |
| 1839 | /// # Used For |
| 1840 | /// |
| 1841 | /// - Parse Float |
| 1842 | /// |
| 1843 | /// <!-- TEST |
| 1844 | /// ```rust |
| 1845 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1846 | /// .required_mantissa_digits(true) |
| 1847 | /// .build_strict(); |
| 1848 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 1849 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b".", &PF_OPTS), Err(Error::EmptyMantissa(1))); |
| 1850 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"e10", &PF_OPTS), Err(Error::EmptyMantissa(0))); |
| 1851 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b".e10", &PF_OPTS), Err(Error::EmptyMantissa(1))); |
| 1852 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"", &PF_OPTS), Err(Error::Empty(0))); |
| 1853 | /// ``` |
| 1854 | /// --> |
| 1855 | #[inline(always)] |
| 1856 | #[cfg(feature = "format")] |
| 1857 | pub const fn required_mantissa_digits(mut self, flag: bool) -> Self { |
| 1858 | self.required_mantissa_digits = flag; |
| 1859 | self |
| 1860 | } |
| 1861 | |
| 1862 | /// Set if digits are required for all float components. |
| 1863 | /// |
| 1864 | /// Note that digits are **always** required for integers. Defaults |
| 1865 | /// to requiring digits only for the mantissa and exponent. |
| 1866 | /// |
| 1867 | /// # Examples |
| 1868 | /// |
| 1869 | /// | Input | Valid? | |
| 1870 | /// |:-:|:-:| |
| 1871 | /// | `1.1` | ✔️ | |
| 1872 | /// | `1.1e3` | ✔️ | |
| 1873 | /// | `1.1e` | ✔️ | |
| 1874 | /// | `0.1` | ✔️ | |
| 1875 | /// | `.1` | ❌ | |
| 1876 | /// | `1.` | ❌ | |
| 1877 | /// | `e10` | ❌ | |
| 1878 | /// | `.1e10` | ❌ | |
| 1879 | /// | | ❌ | |
| 1880 | /// |
| 1881 | /// # Used For |
| 1882 | /// |
| 1883 | /// - Parse Float |
| 1884 | /// |
| 1885 | /// <!-- TEST |
| 1886 | /// ```rust |
| 1887 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1888 | /// .required_digits(true) |
| 1889 | /// .build_strict(); |
| 1890 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 1891 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e3", &PF_OPTS), Ok(1.1e3)); |
| 1892 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e", &PF_OPTS), Err(Error::EmptyExponent(4))); |
| 1893 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0.1", &PF_OPTS), Ok(0.1)); |
| 1894 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b".", &PF_OPTS), Err(Error::EmptyInteger(0))); |
| 1895 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"e10", &PF_OPTS), Err(Error::EmptyInteger(0))); |
| 1896 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b".e10", &PF_OPTS), Err(Error::EmptyInteger(0))); |
| 1897 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"", &PF_OPTS), Err(Error::Empty(0))); |
| 1898 | /// ``` |
| 1899 | /// --> |
| 1900 | #[inline(always)] |
| 1901 | #[cfg(feature = "format")] |
| 1902 | pub const fn required_digits(mut self, flag: bool) -> Self { |
| 1903 | self = self.required_integer_digits(flag); |
| 1904 | self = self.required_fraction_digits(flag); |
| 1905 | self = self.required_exponent_digits(flag); |
| 1906 | self = self.required_mantissa_digits(flag); |
| 1907 | self |
| 1908 | } |
| 1909 | |
| 1910 | /// Set if a positive sign before the mantissa is not allowed. |
| 1911 | /// |
| 1912 | /// Defaults to `false`. |
| 1913 | /// |
| 1914 | /// # Examples |
| 1915 | /// |
| 1916 | /// | Input | Valid? | |
| 1917 | /// |:-:|:-:| |
| 1918 | /// | `1.1` | ✔️ | |
| 1919 | /// | `-1.1` | ✔️ | |
| 1920 | /// | `+1.1` | ❌ | |
| 1921 | /// |
| 1922 | /// # Used For |
| 1923 | /// |
| 1924 | /// - Parse Float |
| 1925 | /// - Parse Integer |
| 1926 | /// - Write Float |
| 1927 | /// |
| 1928 | /// <!-- TEST |
| 1929 | /// ```rust |
| 1930 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1931 | /// .no_positive_mantissa_sign(true) |
| 1932 | /// .build_strict(); |
| 1933 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 1934 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"-1.1", &PF_OPTS), Ok(-1.1)); |
| 1935 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"+1.1", &PF_OPTS), Err(Error::InvalidPositiveSign(0))); |
| 1936 | /// |
| 1937 | /// let mut buffer = [0u8; BUFFER_SIZE]; |
| 1938 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1, &mut buffer, &WF_OPTS), b"1.1"); |
| 1939 | /// assert_eq!(write_with_options::<f64, FORMAT>(-1.1, &mut buffer, &WF_OPTS), b"-1.1"); |
| 1940 | /// ``` |
| 1941 | /// --> |
| 1942 | #[inline(always)] |
| 1943 | #[cfg(feature = "format")] |
| 1944 | pub const fn no_positive_mantissa_sign(mut self, flag: bool) -> Self { |
| 1945 | self.no_positive_mantissa_sign = flag; |
| 1946 | self |
| 1947 | } |
| 1948 | |
| 1949 | /// Set if a sign symbol before the mantissa is required. |
| 1950 | /// |
| 1951 | /// Defaults to `false`. |
| 1952 | /// |
| 1953 | /// # Examples |
| 1954 | /// |
| 1955 | /// | Input | Valid? | |
| 1956 | /// |:-:|:-:| |
| 1957 | /// | `1.1` | ❌ | |
| 1958 | /// | `-1.1` | ✔️ | |
| 1959 | /// | `+1.1` | ✔️ | |
| 1960 | /// |
| 1961 | /// # Used For |
| 1962 | /// |
| 1963 | /// - Parse Float |
| 1964 | /// - Parse Integer |
| 1965 | /// - Write Float |
| 1966 | /// - Write Integer |
| 1967 | /// |
| 1968 | /// <!-- TEST |
| 1969 | /// ```rust |
| 1970 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 1971 | /// .required_mantissa_sign(true) |
| 1972 | /// .build_strict(); |
| 1973 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Err(Error::MissingSign(0))); |
| 1974 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"+1.1", &PF_OPTS), Ok(1.1)); |
| 1975 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"-1.1", &PF_OPTS), Ok(-1.1)); |
| 1976 | /// |
| 1977 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Err(Error::MissingSign(0))); |
| 1978 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"+1", &PI_OPTS), Ok(1)); |
| 1979 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"-1", &PI_OPTS), Ok(-1)); |
| 1980 | /// |
| 1981 | /// let mut buffer = [0u8; BUFFER_SIZE]; |
| 1982 | /// assert_eq!(write_with_options::<f64, FORMAT>(-1.0, &mut buffer, &WF_OPTS), b"-1.0"); |
| 1983 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.0, &mut buffer, &WF_OPTS), b"+1.0"); |
| 1984 | /// |
| 1985 | /// assert_eq!(write_with_options::<i64, FORMAT>(-1, &mut buffer, &WI_OPTS), b"-1"); |
| 1986 | /// assert_eq!(write_with_options::<i64, FORMAT>(1, &mut buffer, &WI_OPTS), b"+1"); |
| 1987 | /// ``` |
| 1988 | /// --> |
| 1989 | #[inline(always)] |
| 1990 | #[cfg(feature = "format")] |
| 1991 | pub const fn required_mantissa_sign(mut self, flag: bool) -> Self { |
| 1992 | self.required_mantissa_sign = flag; |
| 1993 | self |
| 1994 | } |
| 1995 | |
| 1996 | /// Set if exponent notation is not allowed. |
| 1997 | /// |
| 1998 | /// Defaults to `false`. |
| 1999 | /// |
| 2000 | /// # Examples |
| 2001 | /// |
| 2002 | /// | Input | Valid? | |
| 2003 | /// |:-:|:-:| |
| 2004 | /// | `1` | ✔️ | |
| 2005 | /// | `1.1` | ✔️ | |
| 2006 | /// | `1.1e` | ❌ | |
| 2007 | /// | `1.1e5` | ❌ | |
| 2008 | /// |
| 2009 | /// # Used For |
| 2010 | /// |
| 2011 | /// - Parse Float |
| 2012 | /// - Write Float |
| 2013 | /// |
| 2014 | /// <!-- TEST |
| 2015 | /// ```rust |
| 2016 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2017 | /// .no_exponent_notation(true) |
| 2018 | /// .build_strict(); |
| 2019 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2020 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 2021 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e", &PF_OPTS), Err(Error::InvalidExponent(3))); |
| 2022 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e5", &PF_OPTS), Err(Error::InvalidExponent(3))); |
| 2023 | /// |
| 2024 | /// const SIZE: usize = WF_OPTS.buffer_size_const::<f64, FORMAT>(); |
| 2025 | /// let mut buffer = [0u8; SIZE]; |
| 2026 | /// assert_eq!(write(1.0e10, &mut buffer), b"1.0e10"); |
| 2027 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.0, &mut buffer, &WF_OPTS), b"1.0"); |
| 2028 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1, &mut buffer, &WF_OPTS), b"1.1"); |
| 2029 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.0e10, &mut buffer, &WF_OPTS), b"10000000000.0"); |
| 2030 | /// ``` |
| 2031 | /// --> |
| 2032 | #[inline(always)] |
| 2033 | #[cfg(feature = "format")] |
| 2034 | pub const fn no_exponent_notation(mut self, flag: bool) -> Self { |
| 2035 | self.no_exponent_notation = flag; |
| 2036 | self |
| 2037 | } |
| 2038 | |
| 2039 | /// Set if a positive sign before the exponent is not allowed. |
| 2040 | /// |
| 2041 | /// Defaults to `false`. |
| 2042 | /// |
| 2043 | /// # Examples |
| 2044 | /// |
| 2045 | /// | Input | Valid? | |
| 2046 | /// |:-:|:-:| |
| 2047 | /// | `1.1e3` | ✔️ | |
| 2048 | /// | `1.1e-3` | ✔️ | |
| 2049 | /// | `1.1e+3` | ❌ | |
| 2050 | /// |
| 2051 | /// # Used For |
| 2052 | /// |
| 2053 | /// - Parse Float |
| 2054 | /// - Write Float |
| 2055 | /// |
| 2056 | /// <!-- TEST |
| 2057 | /// ```rust |
| 2058 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2059 | /// .no_positive_exponent_sign(true) |
| 2060 | /// .build_strict(); |
| 2061 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e15", &PF_OPTS), Ok(1.1e15)); |
| 2062 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e+15", &PF_OPTS), Err(Error::InvalidPositiveExponentSign(4))); |
| 2063 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e-15", &PF_OPTS), Ok(1.1e-15)); |
| 2064 | /// |
| 2065 | /// let mut buffer = [0u8; BUFFER_SIZE]; |
| 2066 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1e15, &mut buffer, &WF_OPTS), b"1.1e15"); |
| 2067 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1e-15, &mut buffer, &WF_OPTS), b"1.1e-15"); |
| 2068 | /// ``` |
| 2069 | /// --> |
| 2070 | #[inline(always)] |
| 2071 | #[cfg(feature = "format")] |
| 2072 | pub const fn no_positive_exponent_sign(mut self, flag: bool) -> Self { |
| 2073 | self.no_positive_exponent_sign = flag; |
| 2074 | self |
| 2075 | } |
| 2076 | |
| 2077 | /// Set if a sign symbol before the exponent is required. |
| 2078 | /// |
| 2079 | /// Defaults to `false`. |
| 2080 | /// |
| 2081 | /// # Examples |
| 2082 | /// |
| 2083 | /// | Input | Valid? | |
| 2084 | /// |:-:|:-:| |
| 2085 | /// | `1.1e3` | ❌ | |
| 2086 | /// | `1.1e-3` | ✔️ | |
| 2087 | /// | `1.1e+3` | ✔️ | |
| 2088 | /// |
| 2089 | /// # Used For |
| 2090 | /// |
| 2091 | /// - Parse Float |
| 2092 | /// - Write Float |
| 2093 | /// |
| 2094 | /// <!-- TEST |
| 2095 | /// ```rust |
| 2096 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2097 | /// .required_exponent_sign(true) |
| 2098 | /// .build_strict(); |
| 2099 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e15", &PF_OPTS), Err(Error::MissingExponentSign(4))); |
| 2100 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e+15", &PF_OPTS), Ok(1.1e15)); |
| 2101 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e-15", &PF_OPTS), Ok(1.1e-15)); |
| 2102 | /// |
| 2103 | /// let mut buffer = [0u8; BUFFER_SIZE]; |
| 2104 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1e15, &mut buffer, &WF_OPTS), b"1.1e+15"); |
| 2105 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1e-15, &mut buffer, &WF_OPTS), b"1.1e-15"); |
| 2106 | /// ``` |
| 2107 | /// --> |
| 2108 | #[inline(always)] |
| 2109 | #[cfg(feature = "format")] |
| 2110 | pub const fn required_exponent_sign(mut self, flag: bool) -> Self { |
| 2111 | self.required_exponent_sign = flag; |
| 2112 | self |
| 2113 | } |
| 2114 | |
| 2115 | /// Set if an exponent without fraction is not allowed. |
| 2116 | /// |
| 2117 | /// Defaults to `false`. |
| 2118 | /// |
| 2119 | /// # Examples |
| 2120 | /// |
| 2121 | /// | Input | Valid? | |
| 2122 | /// |:-:|:-:| |
| 2123 | /// | `1e3` | ❌ | |
| 2124 | /// | `1.e3` | ✔️ | |
| 2125 | /// | `1.1e` | ✔️ | |
| 2126 | /// | `.1e3` | ✔️ | |
| 2127 | /// |
| 2128 | /// # Used For |
| 2129 | /// |
| 2130 | /// - Parse Float |
| 2131 | /// |
| 2132 | /// <!-- TEST |
| 2133 | /// ```rust |
| 2134 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2135 | /// .no_exponent_without_fraction(true) |
| 2136 | /// .build_strict(); |
| 2137 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1e3", &PF_OPTS), Err(Error::ExponentWithoutFraction(1))); |
| 2138 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.e3", &PF_OPTS), Ok(1000.0)); |
| 2139 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2140 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e3", &PF_OPTS), Ok(1.1e3)); |
| 2141 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b".1e3", &PF_OPTS), Ok(1.0e2)); |
| 2142 | /// ``` |
| 2143 | /// --> |
| 2144 | #[inline(always)] |
| 2145 | #[cfg(feature = "format")] |
| 2146 | pub const fn no_exponent_without_fraction(mut self, flag: bool) -> Self { |
| 2147 | self.no_exponent_without_fraction = flag; |
| 2148 | self |
| 2149 | } |
| 2150 | |
| 2151 | /// Set if special (non-finite) values are not allowed. |
| 2152 | /// |
| 2153 | /// Defaults to `false`. |
| 2154 | /// |
| 2155 | /// # Examples |
| 2156 | /// |
| 2157 | /// | Input | Valid? | |
| 2158 | /// |:-:|:-:| |
| 2159 | /// | `NaN` | ❌ | |
| 2160 | /// | `inf` | ❌ | |
| 2161 | /// | `-Infinity` | ❌ | |
| 2162 | /// | `1.1e3` | ✔️ | |
| 2163 | /// |
| 2164 | /// # Used For |
| 2165 | /// |
| 2166 | /// - Parse Float |
| 2167 | /// |
| 2168 | /// <!-- TEST |
| 2169 | /// ```rust |
| 2170 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2171 | /// .no_special(true) |
| 2172 | /// .build_strict(); |
| 2173 | /// assert_eq!(parse::<f64>(b"NaN").map(|x| x.is_nan()), Ok(true)); |
| 2174 | /// assert_eq!(parse::<f64>(b"inf"), Ok(f64::INFINITY)); |
| 2175 | /// assert_eq!(parse::<f64>(b"infinity"), Ok(f64::INFINITY)); |
| 2176 | /// assert_eq!(parse::<f64>(b"1.1e3"), Ok(1.1e3)); |
| 2177 | /// |
| 2178 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"NaN", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2179 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"inf", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2180 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"infinity", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2181 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e3", &PF_OPTS), Ok(1.1e3)); |
| 2182 | /// ``` |
| 2183 | /// --> |
| 2184 | #[inline(always)] |
| 2185 | #[cfg(feature = "format")] |
| 2186 | pub const fn no_special(mut self, flag: bool) -> Self { |
| 2187 | self.no_special = flag; |
| 2188 | self |
| 2189 | } |
| 2190 | |
| 2191 | /// Set if special (non-finite) values are case-sensitive. |
| 2192 | /// |
| 2193 | /// If set to [`true`], then `NaN` and `nan` are treated as the same value |
| 2194 | /// ([Not a Number][f64::NAN]). Defaults to [`false`]. |
| 2195 | /// |
| 2196 | /// # Examples |
| 2197 | /// |
| 2198 | /// | Input | Valid? | |
| 2199 | /// |:-:|:-:| |
| 2200 | /// | `nan` | ❌ | |
| 2201 | /// | `NaN` | ✔️ | |
| 2202 | /// | `inf` | ✔️ | |
| 2203 | /// | `Inf` | ❌ | |
| 2204 | /// |
| 2205 | /// # Used For |
| 2206 | /// |
| 2207 | /// - Parse Float |
| 2208 | /// |
| 2209 | /// <!-- TEST |
| 2210 | /// ```rust |
| 2211 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2212 | /// .case_sensitive_special(true) |
| 2213 | /// .build_strict(); |
| 2214 | /// assert_eq!(parse::<f64>(b"nan").map(|x| x.is_nan()), Ok(true)); |
| 2215 | /// assert_eq!(parse::<f64>(b"NaN").map(|x| x.is_nan()), Ok(true)); |
| 2216 | /// assert_eq!(parse::<f64>(b"inf"), Ok(f64::INFINITY)); |
| 2217 | /// assert_eq!(parse::<f64>(b"Inf"), Ok(f64::INFINITY)); |
| 2218 | /// assert_eq!(parse::<f64>(b"1.1e3"), Ok(1.1e3)); |
| 2219 | /// |
| 2220 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"nan", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2221 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"NaN", &PF_OPTS).map(|x| x.is_nan()), Ok(true)); |
| 2222 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"inf", &PF_OPTS), Ok(f64::INFINITY)); |
| 2223 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"Inf", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2224 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e3", &PF_OPTS), Ok(1.1e3)); |
| 2225 | /// ``` |
| 2226 | /// --> |
| 2227 | #[inline(always)] |
| 2228 | #[cfg(feature = "format")] |
| 2229 | pub const fn case_sensitive_special(mut self, flag: bool) -> Self { |
| 2230 | self.case_sensitive_special = flag; |
| 2231 | self |
| 2232 | } |
| 2233 | |
| 2234 | /// Set if leading zeros before an integer are not allowed. |
| 2235 | /// |
| 2236 | /// Defaults to [`false`]. |
| 2237 | /// |
| 2238 | /// # Examples |
| 2239 | /// |
| 2240 | /// | Input | Valid? | |
| 2241 | /// |:-:|:-:| |
| 2242 | /// | `01` | ❌ | |
| 2243 | /// | `0` | ✔️ | |
| 2244 | /// | `10` | ✔️ | |
| 2245 | /// |
| 2246 | /// # Used For |
| 2247 | /// |
| 2248 | /// - Parse Integer |
| 2249 | /// |
| 2250 | /// <!-- TEST |
| 2251 | /// ```rust |
| 2252 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2253 | /// .no_integer_leading_zeros(true) |
| 2254 | /// .build_strict(); |
| 2255 | /// assert_eq!(parse::<i64>(b"01"), Ok(1)); |
| 2256 | /// assert_eq!(parse::<i64>(b"+01"), Ok(1)); |
| 2257 | /// assert_eq!(parse::<i64>(b"0"), Ok(0)); |
| 2258 | /// assert_eq!(parse::<i64>(b"10"), Ok(10)); |
| 2259 | /// |
| 2260 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"01", &PI_OPTS), Err(Error::InvalidLeadingZeros(0))); |
| 2261 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"+01", &PI_OPTS), Err(Error::InvalidLeadingZeros(1))); |
| 2262 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"0", &PI_OPTS), Ok(0)); |
| 2263 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"10", &PI_OPTS), Ok(10)); |
| 2264 | /// ``` |
| 2265 | /// --> |
| 2266 | #[inline(always)] |
| 2267 | #[cfg(feature = "format")] |
| 2268 | pub const fn no_integer_leading_zeros(mut self, flag: bool) -> Self { |
| 2269 | self.no_integer_leading_zeros = flag; |
| 2270 | self |
| 2271 | } |
| 2272 | |
| 2273 | /// Set if leading zeros before a float are not allowed. |
| 2274 | /// |
| 2275 | /// This is before the significant digits of the float, that is, if there is |
| 2276 | /// 1 or more digits in the integral component and the leading digit is 0, |
| 2277 | /// Defaults to [`false`]. |
| 2278 | /// |
| 2279 | /// # Examples |
| 2280 | /// |
| 2281 | /// | Input | Valid? | |
| 2282 | /// |:-:|:-:| |
| 2283 | /// | `01` | ❌ | |
| 2284 | /// | `01.0` | ❌ | |
| 2285 | /// | `0` | ✔️ | |
| 2286 | /// | `10` | ✔️ | |
| 2287 | /// | `0.1` | ✔️ | |
| 2288 | /// |
| 2289 | /// # Used For |
| 2290 | /// |
| 2291 | /// - Parse Float |
| 2292 | /// |
| 2293 | /// <!-- TEST |
| 2294 | /// ```rust |
| 2295 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2296 | /// .no_float_leading_zeros(true) |
| 2297 | /// .build_strict(); |
| 2298 | /// assert_eq!(parse::<f64>(b"01"), Ok(1.0)); |
| 2299 | /// assert_eq!(parse::<f64>(b"+01"), Ok(1.0)); |
| 2300 | /// assert_eq!(parse::<f64>(b"0"), Ok(0.0)); |
| 2301 | /// assert_eq!(parse::<f64>(b"10"), Ok(10.0)); |
| 2302 | /// assert_eq!(parse::<f64>(b"0.1"), Ok(0.1)); |
| 2303 | /// |
| 2304 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"01", &PF_OPTS), Err(Error::InvalidLeadingZeros(0))); |
| 2305 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"+01", &PF_OPTS), Err(Error::InvalidLeadingZeros(1))); |
| 2306 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0", &PF_OPTS), Ok(0.0)); |
| 2307 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"10", &PF_OPTS), Ok(10.0)); |
| 2308 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0.1", &PF_OPTS), Ok(0.1)); |
| 2309 | /// ``` |
| 2310 | /// --> |
| 2311 | #[inline(always)] |
| 2312 | #[cfg(feature = "format")] |
| 2313 | pub const fn no_float_leading_zeros(mut self, flag: bool) -> Self { |
| 2314 | self.no_float_leading_zeros = flag; |
| 2315 | self |
| 2316 | } |
| 2317 | |
| 2318 | /// Set if exponent notation is required. |
| 2319 | /// |
| 2320 | /// Defaults to [`false`]. |
| 2321 | /// |
| 2322 | /// # Examples |
| 2323 | /// |
| 2324 | /// | Input | Valid? | |
| 2325 | /// |:-:|:-:| |
| 2326 | /// | `1` | ❌ | |
| 2327 | /// | `1.0` | ❌ | |
| 2328 | /// | `1e3` | ✔️ | |
| 2329 | /// | `1.1e3` | ✔️ | |
| 2330 | /// |
| 2331 | /// # Used For |
| 2332 | /// |
| 2333 | /// - Parse Float |
| 2334 | /// - Write Float |
| 2335 | /// |
| 2336 | /// <!-- TEST |
| 2337 | /// ```rust |
| 2338 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2339 | /// .required_exponent_notation(true) |
| 2340 | /// .build_strict(); |
| 2341 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Err(Error::MissingExponent(1))); |
| 2342 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.0", &PF_OPTS), Err(Error::MissingExponent(3))); |
| 2343 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.0e3", &PF_OPTS), Ok(1.0e3)); |
| 2344 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e3", &PF_OPTS), Ok(1.1e3)); |
| 2345 | /// |
| 2346 | /// const SIZE: usize = WF_OPTS.buffer_size_const::<f64, FORMAT>(); |
| 2347 | /// let mut buffer = [0u8; SIZE]; |
| 2348 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.0, &mut buffer, &WF_OPTS), b"1.0e0"); |
| 2349 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1, &mut buffer, &WF_OPTS), b"1.1e0"); |
| 2350 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.0e3, &mut buffer, &WF_OPTS), b"1.0e3"); |
| 2351 | /// assert_eq!(write_with_options::<f64, FORMAT>(1.1e3, &mut buffer, &WF_OPTS), b"1.1e3"); |
| 2352 | /// ``` |
| 2353 | /// --> |
| 2354 | #[inline(always)] |
| 2355 | #[cfg(feature = "format")] |
| 2356 | pub const fn required_exponent_notation(mut self, flag: bool) -> Self { |
| 2357 | self.required_exponent_notation = flag; |
| 2358 | self |
| 2359 | } |
| 2360 | |
| 2361 | /// Set if exponent characters are case-sensitive. |
| 2362 | /// |
| 2363 | /// If set to [`true`], then the exponent character `e` would be considered |
| 2364 | /// the different from `E`. Defaults to [`false`]. |
| 2365 | /// |
| 2366 | /// # Examples |
| 2367 | /// |
| 2368 | /// | Input | Valid? | |
| 2369 | /// |:-:|:-:| |
| 2370 | /// | `1.1` | ✔️ | |
| 2371 | /// | `1.1e3` | ✔️ | |
| 2372 | /// | `1.1E3` | ❌ | |
| 2373 | /// |
| 2374 | /// # Used For |
| 2375 | /// |
| 2376 | /// - Parse Float |
| 2377 | /// |
| 2378 | /// <!-- TEST |
| 2379 | /// ```rust |
| 2380 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2381 | /// .case_sensitive_exponent(true) |
| 2382 | /// .build_strict(); |
| 2383 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2384 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.0e3", &PF_OPTS), Ok(1.0e3)); |
| 2385 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.0E3", &PF_OPTS), Err(Error::InvalidDigit(3))); |
| 2386 | /// ``` |
| 2387 | /// --> |
| 2388 | #[inline(always)] |
| 2389 | #[cfg(feature = "format")] |
| 2390 | pub const fn case_sensitive_exponent(mut self, flag: bool) -> Self { |
| 2391 | self.case_sensitive_exponent = flag; |
| 2392 | self |
| 2393 | } |
| 2394 | |
| 2395 | /// Set if base prefixes are case-sensitive. |
| 2396 | /// |
| 2397 | /// If set to [`true`], then the base prefix `x` would be considered the |
| 2398 | /// different from `X`. Defaults to [`false`]. |
| 2399 | /// |
| 2400 | /// # Examples |
| 2401 | /// |
| 2402 | /// Using a base prefix of `x`. |
| 2403 | /// |
| 2404 | /// | Input | Valid? | |
| 2405 | /// |:-:|:-:| |
| 2406 | /// | `0x1` | ✔️ | |
| 2407 | /// | `0X1` | ❌ | |
| 2408 | /// | `1` | ✔️ | |
| 2409 | /// | `1x` | ❌ | |
| 2410 | /// |
| 2411 | /// # Used For |
| 2412 | /// |
| 2413 | /// - Parse Float |
| 2414 | /// - Parse Integer |
| 2415 | /// |
| 2416 | /// <!-- TEST |
| 2417 | /// ```rust |
| 2418 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2419 | /// .base_prefix(num::NonZeroU8::new(b'x')) |
| 2420 | /// .case_sensitive_base_prefix(true) |
| 2421 | /// .build_strict(); |
| 2422 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0x1", &PF_OPTS), Ok(1.0)); |
| 2423 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0X1", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2424 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2425 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2426 | /// |
| 2427 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Ok(1)); |
| 2428 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"0X1", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2429 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 2430 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2431 | /// ``` |
| 2432 | /// --> |
| 2433 | #[inline(always)] |
| 2434 | #[cfg(all(feature = "power-of-two", feature = "format"))] |
| 2435 | pub const fn case_sensitive_base_prefix(mut self, flag: bool) -> Self { |
| 2436 | self.case_sensitive_base_prefix = flag; |
| 2437 | self |
| 2438 | } |
| 2439 | |
| 2440 | /// Set if base suffixes are case-sensitive. |
| 2441 | /// |
| 2442 | /// If set to [`true`], then the base suffix `x` would be considered the |
| 2443 | /// different from `X`. Defaults to [`false`]. |
| 2444 | /// |
| 2445 | /// # Examples |
| 2446 | /// |
| 2447 | /// Using a base prefix of `x`. |
| 2448 | /// |
| 2449 | /// | Input | Valid? | |
| 2450 | /// |:-:|:-:| |
| 2451 | /// | `1` | ✔️ | |
| 2452 | /// | `1x` | ✔️ | |
| 2453 | /// | `1X` | ❌ | |
| 2454 | /// | `1d` | ❌ | |
| 2455 | /// |
| 2456 | /// # Used For |
| 2457 | /// |
| 2458 | /// - Parse Float |
| 2459 | /// - Parse Integer |
| 2460 | /// |
| 2461 | /// <!-- TEST |
| 2462 | /// ```rust |
| 2463 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2464 | /// .base_suffix(num::NonZeroU8::new(b'x')) |
| 2465 | /// .case_sensitive_base_suffix(true) |
| 2466 | /// .build_strict(); |
| 2467 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"0x1", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 2468 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2469 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Ok(1.0)); |
| 2470 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1X", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2471 | /// |
| 2472 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Err(Error::InvalidDigit(2))); |
| 2473 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 2474 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Ok(1)); |
| 2475 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1X", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2476 | /// ``` |
| 2477 | /// --> |
| 2478 | #[inline(always)] |
| 2479 | #[cfg(all(feature = "power-of-two", feature = "format"))] |
| 2480 | pub const fn case_sensitive_base_suffix(mut self, flag: bool) -> Self { |
| 2481 | self.case_sensitive_base_suffix = flag; |
| 2482 | self |
| 2483 | } |
| 2484 | |
| 2485 | /// Set if digit separators are allowed between integer digits. |
| 2486 | /// |
| 2487 | /// This will not consider an input of only the digit separator |
| 2488 | /// to be a valid separator: the digit separator must be surrounded by |
| 2489 | /// digits. Defaults to [`false`]. |
| 2490 | /// |
| 2491 | /// # Examples |
| 2492 | /// |
| 2493 | /// Using a digit separator of `_`. |
| 2494 | /// |
| 2495 | /// | Input | Valid? | |
| 2496 | /// |:-:|:-:| |
| 2497 | /// | `1` | ✔️ | |
| 2498 | /// | `_` | ❌ | |
| 2499 | /// | `1_1` | ✔️ | |
| 2500 | /// | `1_` | ❌ | |
| 2501 | /// | `_1` | ❌ | |
| 2502 | /// |
| 2503 | /// # Used For |
| 2504 | /// |
| 2505 | /// - Parse Float |
| 2506 | /// - Parse Integer |
| 2507 | /// |
| 2508 | /// <!-- TEST |
| 2509 | /// ```rust |
| 2510 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2511 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2512 | /// .integer_internal_digit_separator(true) |
| 2513 | /// .build_strict(); |
| 2514 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2515 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2516 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_1", &PF_OPTS), Ok(11.0)); |
| 2517 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2518 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_1", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2519 | /// |
| 2520 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 2521 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_", &PI_OPTS), Err(Error::InvalidDigit(0))); |
| 2522 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_1", &PI_OPTS), Ok(11)); |
| 2523 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2524 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_1", &PI_OPTS), Err(Error::InvalidDigit(0))); |
| 2525 | /// ``` |
| 2526 | /// --> |
| 2527 | #[inline(always)] |
| 2528 | #[cfg(feature = "format")] |
| 2529 | pub const fn integer_internal_digit_separator(mut self, flag: bool) -> Self { |
| 2530 | self.integer_internal_digit_separator = flag; |
| 2531 | self |
| 2532 | } |
| 2533 | |
| 2534 | /// Set if digit separators are allowed between fraction digits. |
| 2535 | /// |
| 2536 | /// This will not consider an input of only the digit separator |
| 2537 | /// to be a valid separator: the digit separator must be surrounded by |
| 2538 | /// digits. Defaults to [`false`]. |
| 2539 | /// |
| 2540 | /// # Examples |
| 2541 | /// |
| 2542 | /// Using a digit separator of `_`. |
| 2543 | /// |
| 2544 | /// | Input | Valid? | |
| 2545 | /// |:-:|:-:| |
| 2546 | /// | `1.1` | ✔️ | |
| 2547 | /// | `1._` | ❌ | |
| 2548 | /// | `1.1_1` | ✔️ | |
| 2549 | /// | `1.1_` | ❌ | |
| 2550 | /// | `1._1` | ❌ | |
| 2551 | /// |
| 2552 | /// # Used For |
| 2553 | /// |
| 2554 | /// - Parse Float |
| 2555 | /// |
| 2556 | /// <!-- TEST |
| 2557 | /// ```rust |
| 2558 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2559 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2560 | /// .fraction_internal_digit_separator(true) |
| 2561 | /// .build_strict(); |
| 2562 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 2563 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 2564 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_1", &PF_OPTS), Ok(1.11)); |
| 2565 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_", &PF_OPTS), Err(Error::InvalidDigit(3))); |
| 2566 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._1", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 2567 | /// ``` |
| 2568 | /// --> |
| 2569 | #[inline(always)] |
| 2570 | #[cfg(feature = "format")] |
| 2571 | pub const fn fraction_internal_digit_separator(mut self, flag: bool) -> Self { |
| 2572 | self.fraction_internal_digit_separator = flag; |
| 2573 | self |
| 2574 | } |
| 2575 | |
| 2576 | /// Set if digit separators are allowed between exponent digits. |
| 2577 | /// |
| 2578 | /// This will not consider an input of only the digit separator |
| 2579 | /// to be a valid separator: the digit separator must be surrounded by |
| 2580 | /// digits. Defaults to [`false`]. |
| 2581 | /// |
| 2582 | /// # Examples |
| 2583 | /// |
| 2584 | /// Using a digit separator of `_`. |
| 2585 | /// |
| 2586 | /// | Input | Valid? | |
| 2587 | /// |:-:|:-:| |
| 2588 | /// | `1.1e1` | ✔️ | |
| 2589 | /// | `1.1e_` | ❌ | |
| 2590 | /// | `1.1e1_1` | ✔️ | |
| 2591 | /// | `1.1e1_` | ❌ | |
| 2592 | /// | `1.1e_1` | ❌ | |
| 2593 | /// |
| 2594 | /// # Used For |
| 2595 | /// |
| 2596 | /// - Parse Float |
| 2597 | /// |
| 2598 | /// <!-- TEST |
| 2599 | /// ```rust |
| 2600 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2601 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2602 | /// .exponent_internal_digit_separator(true) |
| 2603 | /// .build_strict(); |
| 2604 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1", &PF_OPTS), Ok(11.0)); |
| 2605 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_", &PF_OPTS), Err(Error::EmptyExponent(4))); |
| 2606 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_1", &PF_OPTS), Ok(1.1e11)); |
| 2607 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_", &PF_OPTS), Err(Error::InvalidDigit(5))); |
| 2608 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_1", &PF_OPTS), Err(Error::EmptyExponent(4))); |
| 2609 | /// ``` |
| 2610 | /// --> |
| 2611 | #[inline(always)] |
| 2612 | #[cfg(feature = "format")] |
| 2613 | pub const fn exponent_internal_digit_separator(mut self, flag: bool) -> Self { |
| 2614 | self.exponent_internal_digit_separator = flag; |
| 2615 | self |
| 2616 | } |
| 2617 | |
| 2618 | /// Set all internal digit separator flags. |
| 2619 | /// |
| 2620 | /// This will not consider an input of only the digit separator |
| 2621 | /// to be a valid separator: the digit separator must be surrounded by |
| 2622 | /// digits. Sets [`integer_internal_digit_separator`], |
| 2623 | /// [`fraction_internal_digit_separator`], and |
| 2624 | /// [`exponent_internal_digit_separator`]. |
| 2625 | /// |
| 2626 | /// [`integer_internal_digit_separator`]: Self::integer_internal_digit_separator |
| 2627 | /// [`fraction_internal_digit_separator`]: Self::fraction_internal_digit_separator |
| 2628 | /// [`exponent_internal_digit_separator`]: Self::exponent_internal_digit_separator |
| 2629 | #[inline(always)] |
| 2630 | #[cfg(feature = "format")] |
| 2631 | pub const fn internal_digit_separator(mut self, flag: bool) -> Self { |
| 2632 | self = self.integer_internal_digit_separator(flag); |
| 2633 | self = self.fraction_internal_digit_separator(flag); |
| 2634 | self = self.exponent_internal_digit_separator(flag); |
| 2635 | self |
| 2636 | } |
| 2637 | |
| 2638 | /// Set if a digit separator is allowed before any integer digits. |
| 2639 | /// |
| 2640 | /// This will consider an input of only the digit separator |
| 2641 | /// to be a identical to empty input. Defaults to [`false`]. |
| 2642 | /// |
| 2643 | /// # Examples |
| 2644 | /// |
| 2645 | /// Using a digit separator of `_`. |
| 2646 | /// |
| 2647 | /// | Input | Valid? | |
| 2648 | /// |:-:|:-:| |
| 2649 | /// | `1` | ✔️ | |
| 2650 | /// | `_` | ❌ | |
| 2651 | /// | `1_1` | ❌ | |
| 2652 | /// | `1_` | ❌ | |
| 2653 | /// | `_1` | ✔️ | |
| 2654 | /// |
| 2655 | /// # Used For |
| 2656 | /// |
| 2657 | /// - Parse Float |
| 2658 | /// - Parse Integer |
| 2659 | /// |
| 2660 | /// <!-- TEST |
| 2661 | /// ```rust |
| 2662 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2663 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2664 | /// .integer_leading_digit_separator(true) |
| 2665 | /// .build_strict(); |
| 2666 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2667 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_", &PF_OPTS), Err(Error::Empty(1))); |
| 2668 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_1", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2669 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2670 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_1", &PF_OPTS), Ok(1.0)); |
| 2671 | /// |
| 2672 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 2673 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_", &PI_OPTS), Err(Error::Empty(1))); |
| 2674 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_1", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2675 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2676 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_1", &PI_OPTS), Ok(1)); |
| 2677 | /// ``` |
| 2678 | /// --> |
| 2679 | #[inline(always)] |
| 2680 | #[cfg(feature = "format")] |
| 2681 | pub const fn integer_leading_digit_separator(mut self, flag: bool) -> Self { |
| 2682 | self.integer_leading_digit_separator = flag; |
| 2683 | self |
| 2684 | } |
| 2685 | |
| 2686 | /// Set if a digit separator is allowed before any fraction digits. |
| 2687 | /// |
| 2688 | /// This will consider an input of only the digit separator |
| 2689 | /// to be a identical to empty input. Defaults to [`false`]. |
| 2690 | /// |
| 2691 | /// # Examples |
| 2692 | /// |
| 2693 | /// Using a digit separator of `_`. |
| 2694 | /// |
| 2695 | /// | Input | Valid? | |
| 2696 | /// |:-:|:-:| |
| 2697 | /// | `1.1` | ✔️ | |
| 2698 | /// | `1._` | ✔️ | |
| 2699 | /// | `1.1_1` | ❌ | |
| 2700 | /// | `1.1_` | ❌ | |
| 2701 | /// | `1._1` | ✔️ | |
| 2702 | /// |
| 2703 | /// # Used For |
| 2704 | /// |
| 2705 | /// - Parse Float |
| 2706 | /// |
| 2707 | /// <!-- TEST |
| 2708 | /// ```rust |
| 2709 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2710 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2711 | /// .fraction_leading_digit_separator(true) |
| 2712 | /// .build_strict(); |
| 2713 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 2714 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._", &PF_OPTS), Ok(1.0)); |
| 2715 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_1", &PF_OPTS), Err(Error::InvalidDigit(3))); |
| 2716 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_", &PF_OPTS), Err(Error::InvalidDigit(3))); |
| 2717 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._1", &PF_OPTS), Ok(1.1)); |
| 2718 | /// ``` |
| 2719 | /// --> |
| 2720 | #[inline(always)] |
| 2721 | #[cfg(feature = "format")] |
| 2722 | pub const fn fraction_leading_digit_separator(mut self, flag: bool) -> Self { |
| 2723 | self.fraction_leading_digit_separator = flag; |
| 2724 | self |
| 2725 | } |
| 2726 | |
| 2727 | /// Set if a digit separator is allowed before any exponent digits. |
| 2728 | /// |
| 2729 | /// This will consider an input of only the digit separator |
| 2730 | /// to be a identical to empty input. Defaults to [`false`]. |
| 2731 | /// |
| 2732 | /// # Examples |
| 2733 | /// |
| 2734 | /// Using a digit separator of `_`. |
| 2735 | /// |
| 2736 | /// | Input | Valid? | |
| 2737 | /// |:-:|:-:| |
| 2738 | /// | `1.1e1` | ✔️ | |
| 2739 | /// | `1.1e_` | ❌ | |
| 2740 | /// | `1.1e1_1` | ❌ | |
| 2741 | /// | `1.1e1_` | ❌ | |
| 2742 | /// | `1.1e_1` | ✔️ | |
| 2743 | /// |
| 2744 | /// # Used For |
| 2745 | /// |
| 2746 | /// - Parse Float |
| 2747 | /// |
| 2748 | /// <!-- TEST |
| 2749 | /// ```rust |
| 2750 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2751 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2752 | /// .exponent_leading_digit_separator(true) |
| 2753 | /// .build_strict(); |
| 2754 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1", &PF_OPTS), Ok(11.0)); |
| 2755 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_", &PF_OPTS), Err(Error::EmptyExponent(5))); |
| 2756 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_1", &PF_OPTS), Err(Error::InvalidDigit(5))); |
| 2757 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_", &PF_OPTS), Err(Error::InvalidDigit(5))); |
| 2758 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_1", &PF_OPTS), Ok(11.0)); |
| 2759 | /// ``` |
| 2760 | /// --> |
| 2761 | #[inline(always)] |
| 2762 | #[cfg(feature = "format")] |
| 2763 | pub const fn exponent_leading_digit_separator(mut self, flag: bool) -> Self { |
| 2764 | self.exponent_leading_digit_separator = flag; |
| 2765 | self |
| 2766 | } |
| 2767 | |
| 2768 | /// Set all leading digit separator flags. |
| 2769 | /// |
| 2770 | /// This will consider an input of only the digit separator |
| 2771 | /// to be a identical to empty input. Sets |
| 2772 | /// [`integer_leading_digit_separator`], |
| 2773 | /// [`fraction_leading_digit_separator`], and |
| 2774 | /// [`exponent_leading_digit_separator`]. |
| 2775 | /// |
| 2776 | /// [`integer_leading_digit_separator`]: Self::integer_leading_digit_separator |
| 2777 | /// [`fraction_leading_digit_separator`]: Self::fraction_leading_digit_separator |
| 2778 | /// [`exponent_leading_digit_separator`]: Self::exponent_leading_digit_separator |
| 2779 | #[inline(always)] |
| 2780 | #[cfg(feature = "format")] |
| 2781 | pub const fn leading_digit_separator(mut self, flag: bool) -> Self { |
| 2782 | self = self.integer_leading_digit_separator(flag); |
| 2783 | self = self.fraction_leading_digit_separator(flag); |
| 2784 | self = self.exponent_leading_digit_separator(flag); |
| 2785 | self |
| 2786 | } |
| 2787 | |
| 2788 | /// Set if a digit separator is allowed after any integer digits. |
| 2789 | /// |
| 2790 | /// This will consider an input of only the digit separator |
| 2791 | /// to be a identical to empty input. Defaults to [`false`]. |
| 2792 | /// |
| 2793 | /// # Examples |
| 2794 | /// |
| 2795 | /// Using a digit separator of `_`. |
| 2796 | /// |
| 2797 | /// | Input | Valid? | |
| 2798 | /// |:-:|:-:| |
| 2799 | /// | `1` | ✔️ | |
| 2800 | /// | `_` | ❌ | |
| 2801 | /// | `1_1` | ❌ | |
| 2802 | /// | `1_` | ✔️ | |
| 2803 | /// | `_1` | ❌ | |
| 2804 | /// |
| 2805 | /// # Used For |
| 2806 | /// |
| 2807 | /// - Parse Float |
| 2808 | /// - Parse Integer |
| 2809 | /// |
| 2810 | /// <!-- TEST |
| 2811 | /// ```rust |
| 2812 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2813 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2814 | /// .integer_trailing_digit_separator(true) |
| 2815 | /// .build_strict(); |
| 2816 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2817 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_", &PF_OPTS), Err(Error::Empty(1))); |
| 2818 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_1", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2819 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_", &PF_OPTS), Ok(1.0)); |
| 2820 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_1", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2821 | /// |
| 2822 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 2823 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_", &PI_OPTS), Err(Error::Empty(1))); |
| 2824 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_1", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2825 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_", &PI_OPTS), Ok(1)); |
| 2826 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_1", &PI_OPTS), Err(Error::InvalidDigit(0))); |
| 2827 | /// ``` |
| 2828 | /// --> |
| 2829 | #[inline(always)] |
| 2830 | #[cfg(feature = "format")] |
| 2831 | pub const fn integer_trailing_digit_separator(mut self, flag: bool) -> Self { |
| 2832 | self.integer_trailing_digit_separator = flag; |
| 2833 | self |
| 2834 | } |
| 2835 | |
| 2836 | /// Set if a digit separator is allowed after any fraction digits. |
| 2837 | /// |
| 2838 | /// This will consider an input of only the digit separator |
| 2839 | /// to be a identical to empty input. Defaults to [`false`]. |
| 2840 | /// # Examples |
| 2841 | /// |
| 2842 | /// Using a digit separator of `_`. |
| 2843 | /// |
| 2844 | /// | Input | Valid? | |
| 2845 | /// |:-:|:-:| |
| 2846 | /// | `1.1` | ✔️ | |
| 2847 | /// | `1._` | ✔️ | |
| 2848 | /// | `1.1_1` | ❌ | |
| 2849 | /// | `1.1_` | ✔️ | |
| 2850 | /// | `1._1` | ❌ | |
| 2851 | /// |
| 2852 | /// # Used For |
| 2853 | /// |
| 2854 | /// - Parse Float |
| 2855 | /// |
| 2856 | /// <!-- TEST |
| 2857 | /// ```rust |
| 2858 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2859 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2860 | /// .fraction_trailing_digit_separator(true) |
| 2861 | /// .build_strict(); |
| 2862 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 2863 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._", &PF_OPTS), Ok(1.0)); |
| 2864 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_1", &PF_OPTS), Err(Error::InvalidDigit(3))); |
| 2865 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._1", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 2866 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_", &PF_OPTS), Ok(1.1)); |
| 2867 | /// ``` |
| 2868 | /// --> |
| 2869 | #[inline(always)] |
| 2870 | #[cfg(feature = "format")] |
| 2871 | pub const fn fraction_trailing_digit_separator(mut self, flag: bool) -> Self { |
| 2872 | self.fraction_trailing_digit_separator = flag; |
| 2873 | self |
| 2874 | } |
| 2875 | |
| 2876 | /// Set if a digit separator is allowed after any exponent digits. |
| 2877 | /// |
| 2878 | /// This will consider an input of only the digit separator |
| 2879 | /// to be a identical to empty input. Defaults to [`false`]. |
| 2880 | /// |
| 2881 | /// # Examples |
| 2882 | /// |
| 2883 | /// Using a digit separator of `_`. |
| 2884 | /// |
| 2885 | /// | Input | Valid? | |
| 2886 | /// |:-:|:-:| |
| 2887 | /// | `1.1e1` | ✔️ | |
| 2888 | /// | `1.1e_` | ❌ | |
| 2889 | /// | `1.1e1_1` | ❌ | |
| 2890 | /// | `1.1e1_` | ✔️ | |
| 2891 | /// | `1.1e_1` | ❌ | |
| 2892 | /// |
| 2893 | /// # Used For |
| 2894 | /// |
| 2895 | /// - Parse Float |
| 2896 | /// |
| 2897 | /// <!-- TEST |
| 2898 | /// ```rust |
| 2899 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2900 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2901 | /// .exponent_trailing_digit_separator(true) |
| 2902 | /// .build_strict(); |
| 2903 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1", &PF_OPTS), Ok(11.0)); |
| 2904 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_", &PF_OPTS), Err(Error::EmptyExponent(5))); |
| 2905 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_1", &PF_OPTS), Err(Error::InvalidDigit(5))); |
| 2906 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_", &PF_OPTS), Ok(11.0)); |
| 2907 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_1", &PF_OPTS), Err(Error::EmptyExponent(4))); |
| 2908 | /// ``` |
| 2909 | /// --> |
| 2910 | #[inline(always)] |
| 2911 | #[cfg(feature = "format")] |
| 2912 | pub const fn exponent_trailing_digit_separator(mut self, flag: bool) -> Self { |
| 2913 | self.exponent_trailing_digit_separator = flag; |
| 2914 | self |
| 2915 | } |
| 2916 | |
| 2917 | /// Set all trailing digit separator flags. |
| 2918 | /// |
| 2919 | /// This will consider an input of only the digit separator |
| 2920 | /// to be a identical to empty input. Sets |
| 2921 | /// [`integer_trailing_digit_separator`], |
| 2922 | /// [`fraction_trailing_digit_separator`], and |
| 2923 | /// [`exponent_trailing_digit_separator`]. |
| 2924 | /// |
| 2925 | /// [`integer_trailing_digit_separator`]: Self::integer_trailing_digit_separator |
| 2926 | /// [`fraction_trailing_digit_separator`]: Self::fraction_trailing_digit_separator |
| 2927 | /// [`exponent_trailing_digit_separator`]: Self::exponent_trailing_digit_separator |
| 2928 | #[inline(always)] |
| 2929 | #[cfg(feature = "format")] |
| 2930 | pub const fn trailing_digit_separator(mut self, flag: bool) -> Self { |
| 2931 | self = self.integer_trailing_digit_separator(flag); |
| 2932 | self = self.fraction_trailing_digit_separator(flag); |
| 2933 | self = self.exponent_trailing_digit_separator(flag); |
| 2934 | self |
| 2935 | } |
| 2936 | |
| 2937 | /// Set if multiple consecutive integer digit separators are allowed. |
| 2938 | /// |
| 2939 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 2940 | /// digit separators (leading, trailing, internal) are allowed in the |
| 2941 | /// integer. Defaults to [`false`]. |
| 2942 | /// |
| 2943 | /// # Examples |
| 2944 | /// |
| 2945 | /// Using a digit separator of `_` with only internal integer digit |
| 2946 | /// separators being valid. |
| 2947 | /// |
| 2948 | /// | Input | Valid? | |
| 2949 | /// |:-:|:-:| |
| 2950 | /// | `1` | ✔️ | |
| 2951 | /// | `_` | ❌ | |
| 2952 | /// | `1_1` | ✔️ | |
| 2953 | /// | `1__1` | ✔️ | |
| 2954 | /// | `1_` | ❌ | |
| 2955 | /// | `_1` | ❌ | |
| 2956 | /// |
| 2957 | /// # Used For |
| 2958 | /// |
| 2959 | /// - Parse Float |
| 2960 | /// - Parse Integer |
| 2961 | /// |
| 2962 | /// <!-- TEST |
| 2963 | /// ```rust |
| 2964 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 2965 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 2966 | /// .integer_internal_digit_separator(true) |
| 2967 | /// .integer_consecutive_digit_separator(true) |
| 2968 | /// .build_strict(); |
| 2969 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1", &PF_OPTS), Ok(1.0)); |
| 2970 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2971 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_1", &PF_OPTS), Ok(11.0)); |
| 2972 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1__1", &PF_OPTS), Ok(11.0)); |
| 2973 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1_", &PF_OPTS), Err(Error::InvalidDigit(1))); |
| 2974 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"_1", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 2975 | /// |
| 2976 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1)); |
| 2977 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_", &PI_OPTS), Err(Error::InvalidDigit(0))); |
| 2978 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_1", &PI_OPTS), Ok(11)); |
| 2979 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1__1", &PI_OPTS), Ok(11)); |
| 2980 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_", &PI_OPTS), Err(Error::InvalidDigit(1))); |
| 2981 | /// assert_eq!(parse_with_options::<i64, FORMAT>(b"_1", &PI_OPTS), Err(Error::InvalidDigit(0))); |
| 2982 | /// ``` |
| 2983 | /// --> |
| 2984 | #[inline(always)] |
| 2985 | #[cfg(feature = "format")] |
| 2986 | pub const fn integer_consecutive_digit_separator(mut self, flag: bool) -> Self { |
| 2987 | self.integer_consecutive_digit_separator = flag; |
| 2988 | self |
| 2989 | } |
| 2990 | |
| 2991 | /// Set if multiple consecutive fraction digit separators are allowed. |
| 2992 | /// |
| 2993 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 2994 | /// digit separators (leading, trailing, internal) are allowed in the |
| 2995 | /// fraction. Defaults to [`false`]. |
| 2996 | /// |
| 2997 | /// # Examples |
| 2998 | /// |
| 2999 | /// Using a digit separator of `_` with only internal fraction digit |
| 3000 | /// separators being valid. |
| 3001 | /// |
| 3002 | /// | Input | Valid? | |
| 3003 | /// |:-:|:-:| |
| 3004 | /// | `1.1` | ✔️ | |
| 3005 | /// | `1._` | ❌ | |
| 3006 | /// | `1.1_1` | ✔️ | |
| 3007 | /// | `1.1__1` | ✔️ | |
| 3008 | /// | `1.1_` | ❌ | |
| 3009 | /// | `1._1` | ❌ | |
| 3010 | /// |
| 3011 | /// # Used For |
| 3012 | /// |
| 3013 | /// - Parse Float |
| 3014 | /// |
| 3015 | /// <!-- TEST |
| 3016 | /// ```rust |
| 3017 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 3018 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 3019 | /// .fraction_internal_digit_separator(true) |
| 3020 | /// .fraction_consecutive_digit_separator(true) |
| 3021 | /// .build_strict(); |
| 3022 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1", &PF_OPTS), Ok(1.1)); |
| 3023 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 3024 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_1", &PF_OPTS), Ok(1.11)); |
| 3025 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1__1", &PF_OPTS), Ok(1.11)); |
| 3026 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1_", &PF_OPTS), Err(Error::InvalidDigit(3))); |
| 3027 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1._1", &PF_OPTS), Err(Error::InvalidDigit(2))); |
| 3028 | /// ``` |
| 3029 | /// --> |
| 3030 | #[inline(always)] |
| 3031 | #[cfg(feature = "format")] |
| 3032 | pub const fn fraction_consecutive_digit_separator(mut self, flag: bool) -> Self { |
| 3033 | self.fraction_consecutive_digit_separator = flag; |
| 3034 | self |
| 3035 | } |
| 3036 | |
| 3037 | /// Set if multiple consecutive exponent digit separators are allowed. |
| 3038 | /// |
| 3039 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 3040 | /// digit separators (leading, trailing, internal) are allowed in the |
| 3041 | /// exponent. Defaults to [`false`]. |
| 3042 | /// |
| 3043 | /// # Examples |
| 3044 | /// |
| 3045 | /// Using a digit separator of `_` with only internal exponent digit |
| 3046 | /// separators being valid. |
| 3047 | /// |
| 3048 | /// | Input | Valid? | |
| 3049 | /// |:-:|:-:| |
| 3050 | /// | `1.1e1` | ✔️ | |
| 3051 | /// | `1.1e_` | ❌ | |
| 3052 | /// | `1.1e1_1` | ✔️ | |
| 3053 | /// | `1.1e1__1` | ✔️ | |
| 3054 | /// | `1.1e1_` | ❌ | |
| 3055 | /// | `1.1e_1` | ❌ | |
| 3056 | /// |
| 3057 | /// # Used For |
| 3058 | /// |
| 3059 | /// - Parse Float |
| 3060 | /// |
| 3061 | /// <!-- TEST |
| 3062 | /// ```rust |
| 3063 | /// const FORMAT: u128 = NumberFormatBuilder::new() |
| 3064 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 3065 | /// .exponent_internal_digit_separator(true) |
| 3066 | /// .exponent_consecutive_digit_separator(true) |
| 3067 | /// .build_strict(); |
| 3068 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1", &PF_OPTS), Ok(11.0)); |
| 3069 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_", &PF_OPTS), Err(Error::EmptyExponent(4))); |
| 3070 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_1", &PF_OPTS), Ok(1.1e11)); |
| 3071 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1__1", &PF_OPTS), Ok(1.1e11)); |
| 3072 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e1_", &PF_OPTS), Err(Error::InvalidDigit(5))); |
| 3073 | /// assert_eq!(parse_with_options::<f64, FORMAT>(b"1.1e_1", &PF_OPTS), Err(Error::EmptyExponent(4))); |
| 3074 | /// ``` |
| 3075 | /// --> |
| 3076 | #[inline(always)] |
| 3077 | #[cfg(feature = "format")] |
| 3078 | pub const fn exponent_consecutive_digit_separator(mut self, flag: bool) -> Self { |
| 3079 | self.exponent_consecutive_digit_separator = flag; |
| 3080 | self |
| 3081 | } |
| 3082 | |
| 3083 | /// Set all consecutive digit separator flags. |
| 3084 | /// |
| 3085 | /// Sets [`integer_consecutive_digit_separator`], |
| 3086 | /// [`fraction_consecutive_digit_separator`], and |
| 3087 | /// [`exponent_consecutive_digit_separator`]. |
| 3088 | /// |
| 3089 | /// [`integer_consecutive_digit_separator`]: Self::integer_consecutive_digit_separator |
| 3090 | /// [`fraction_consecutive_digit_separator`]: Self::fraction_consecutive_digit_separator |
| 3091 | /// [`exponent_consecutive_digit_separator`]: Self::exponent_consecutive_digit_separator |
| 3092 | #[inline(always)] |
| 3093 | #[cfg(feature = "format")] |
| 3094 | pub const fn consecutive_digit_separator(mut self, flag: bool) -> Self { |
| 3095 | self = self.integer_consecutive_digit_separator(flag); |
| 3096 | self = self.fraction_consecutive_digit_separator(flag); |
| 3097 | self = self.exponent_consecutive_digit_separator(flag); |
| 3098 | self |
| 3099 | } |
| 3100 | |
| 3101 | /// Set if any digit separators are allowed in special (non-finite) values. |
| 3102 | /// |
| 3103 | /// This enables leading, trailing, internal, and consecutive digit |
| 3104 | /// separators for any special floats: for example, `N__a_N_` is considered |
| 3105 | /// the same as `NaN`. Defaults to [`false`]. |
| 3106 | /// |
| 3107 | /// Using a digit separator of `_`. |
| 3108 | /// |
| 3109 | /// | Input | Valid? | |
| 3110 | /// |:-:|:-:| |
| 3111 | /// | `nan` | ✔️ | |
| 3112 | /// | `na_n` | ✔️ | |
| 3113 | /// | `na_n_` | ✔️ | |
| 3114 | /// | `na_nx` | ❌ | |
| 3115 | /// |
| 3116 | /// # Used For |
| 3117 | /// |
| 3118 | /// - Parse Float |
| 3119 | /// |
| 3120 | /// <!-- TEST |
| 3121 | /// ```rust |
| 3122 | /// const FMT: u128 = NumberFormatBuilder::new() |
| 3123 | /// .digit_separator(num::NonZeroU8::new(b'_')) |
| 3124 | /// .special_digit_separator(true) |
| 3125 | /// .build_strict(); |
| 3126 | /// assert_eq!(parse_with_options::<f64, FMT>(b"nan", &PF_OPTS).map(|x| x.is_nan()), Ok(true)); |
| 3127 | /// assert_eq!(parse_with_options::<f64, FMT>(b"na_n", &PF_OPTS).map(|x| x.is_nan()), Ok(true)); |
| 3128 | /// assert_eq!(parse_with_options::<f64, FMT>(b"na_n_", &PF_OPTS).map(|x| x.is_nan()), Ok(true)); |
| 3129 | /// assert_eq!(parse_with_options::<f64, FMT>(b"na_nx", &PF_OPTS), Err(Error::InvalidDigit(0))); |
| 3130 | /// ``` |
| 3131 | /// --> |
| 3132 | #[inline(always)] |
| 3133 | #[cfg(feature = "format")] |
| 3134 | pub const fn special_digit_separator(mut self, flag: bool) -> Self { |
| 3135 | self.special_digit_separator = flag; |
| 3136 | self |
| 3137 | } |
| 3138 | |
| 3139 | /// Allow digit separators in all locations for all components. |
| 3140 | /// |
| 3141 | /// This enables leading, trailing, internal, and consecutive digit |
| 3142 | /// separators for the integer, fraction, and exponent components. Defaults |
| 3143 | /// to [`false`]. |
| 3144 | /// |
| 3145 | /// # Used For |
| 3146 | /// |
| 3147 | /// - Parse Float |
| 3148 | /// - Parse Integer |
| 3149 | #[inline(always)] |
| 3150 | #[cfg(feature = "format")] |
| 3151 | pub const fn digit_separator_flags(mut self, flag: bool) -> Self { |
| 3152 | self = self.integer_digit_separator_flags(flag); |
| 3153 | self = self.fraction_digit_separator_flags(flag); |
| 3154 | self = self.exponent_digit_separator_flags(flag); |
| 3155 | self = self.special_digit_separator(flag); |
| 3156 | self |
| 3157 | } |
| 3158 | |
| 3159 | /// Set all integer digit separator flag masks. |
| 3160 | /// |
| 3161 | /// This enables leading, trailing, internal, and consecutive digit |
| 3162 | /// separators for the integer component. Defaults to [`false`]. |
| 3163 | /// |
| 3164 | /// # Used For |
| 3165 | /// |
| 3166 | /// - Parse Float |
| 3167 | /// - Parse Integer |
| 3168 | #[inline(always)] |
| 3169 | #[cfg(feature = "format")] |
| 3170 | pub const fn integer_digit_separator_flags(mut self, flag: bool) -> Self { |
| 3171 | self = self.integer_internal_digit_separator(flag); |
| 3172 | self = self.integer_leading_digit_separator(flag); |
| 3173 | self = self.integer_trailing_digit_separator(flag); |
| 3174 | self = self.integer_consecutive_digit_separator(flag); |
| 3175 | self |
| 3176 | } |
| 3177 | |
| 3178 | /// Set all fraction digit separator flag masks. |
| 3179 | /// |
| 3180 | /// This enables leading, trailing, internal, and consecutive digit |
| 3181 | /// separators for the fraction component. Defaults to [`false`]. |
| 3182 | /// |
| 3183 | /// # Used For |
| 3184 | /// |
| 3185 | /// - Parse Float |
| 3186 | #[inline(always)] |
| 3187 | #[cfg(feature = "format")] |
| 3188 | pub const fn fraction_digit_separator_flags(mut self, flag: bool) -> Self { |
| 3189 | self = self.fraction_internal_digit_separator(flag); |
| 3190 | self = self.fraction_leading_digit_separator(flag); |
| 3191 | self = self.fraction_trailing_digit_separator(flag); |
| 3192 | self = self.fraction_consecutive_digit_separator(flag); |
| 3193 | self |
| 3194 | } |
| 3195 | |
| 3196 | /// Set all exponent digit separator flag masks. |
| 3197 | /// |
| 3198 | /// This enables leading, trailing, internal, and consecutive digit |
| 3199 | /// separators for the exponent component. Defaults to [`false`]. |
| 3200 | /// |
| 3201 | /// # Used For |
| 3202 | /// |
| 3203 | /// - Parse Float |
| 3204 | #[inline(always)] |
| 3205 | #[cfg(feature = "format")] |
| 3206 | pub const fn exponent_digit_separator_flags(mut self, flag: bool) -> Self { |
| 3207 | self = self.exponent_internal_digit_separator(flag); |
| 3208 | self = self.exponent_leading_digit_separator(flag); |
| 3209 | self = self.exponent_trailing_digit_separator(flag); |
| 3210 | self = self.exponent_consecutive_digit_separator(flag); |
| 3211 | self |
| 3212 | } |
| 3213 | |
| 3214 | // BUILDER |
| 3215 | |
| 3216 | /// Create 128-bit, packed number format struct from builder options. |
| 3217 | /// |
| 3218 | /// <div class="warning"> |
| 3219 | /// |
| 3220 | /// This function will never fail. It is up to the caller to ensure the |
| 3221 | /// format is valid using [`NumberFormat::is_valid`]. |
| 3222 | /// |
| 3223 | /// </div> |
| 3224 | /// |
| 3225 | /// [`NumberFormat::is_valid`]: crate::NumberFormat::is_valid |
| 3226 | #[inline(always)] |
| 3227 | pub const fn build_unchecked(&self) -> u128 { |
| 3228 | let mut format: u128 = 0; |
| 3229 | add_flags!( |
| 3230 | format ; |
| 3231 | self.required_integer_digits, REQUIRED_INTEGER_DIGITS ; |
| 3232 | self.required_fraction_digits, REQUIRED_FRACTION_DIGITS ; |
| 3233 | self.required_exponent_digits, REQUIRED_EXPONENT_DIGITS ; |
| 3234 | self.required_mantissa_digits, REQUIRED_MANTISSA_DIGITS ; |
| 3235 | self.no_positive_mantissa_sign, NO_POSITIVE_MANTISSA_SIGN ; |
| 3236 | self.required_mantissa_sign, REQUIRED_MANTISSA_SIGN ; |
| 3237 | self.no_exponent_notation, NO_EXPONENT_NOTATION ; |
| 3238 | self.no_positive_exponent_sign, NO_POSITIVE_EXPONENT_SIGN ; |
| 3239 | self.required_exponent_sign, REQUIRED_EXPONENT_SIGN ; |
| 3240 | self.no_exponent_without_fraction, NO_EXPONENT_WITHOUT_FRACTION ; |
| 3241 | self.no_special, NO_SPECIAL ; |
| 3242 | self.case_sensitive_special, CASE_SENSITIVE_SPECIAL ; |
| 3243 | self.no_integer_leading_zeros, NO_INTEGER_LEADING_ZEROS ; |
| 3244 | self.no_float_leading_zeros, NO_FLOAT_LEADING_ZEROS ; |
| 3245 | self.required_exponent_notation, REQUIRED_EXPONENT_NOTATION ; |
| 3246 | self.case_sensitive_exponent, CASE_SENSITIVE_EXPONENT ; |
| 3247 | self.case_sensitive_base_prefix, CASE_SENSITIVE_BASE_PREFIX ; |
| 3248 | self.case_sensitive_base_suffix, CASE_SENSITIVE_BASE_SUFFIX ; |
| 3249 | self.integer_internal_digit_separator, INTEGER_INTERNAL_DIGIT_SEPARATOR ; |
| 3250 | self.fraction_internal_digit_separator, FRACTION_INTERNAL_DIGIT_SEPARATOR ; |
| 3251 | self.exponent_internal_digit_separator, EXPONENT_INTERNAL_DIGIT_SEPARATOR ; |
| 3252 | self.integer_leading_digit_separator, INTEGER_LEADING_DIGIT_SEPARATOR ; |
| 3253 | self.fraction_leading_digit_separator, FRACTION_LEADING_DIGIT_SEPARATOR ; |
| 3254 | self.exponent_leading_digit_separator, EXPONENT_LEADING_DIGIT_SEPARATOR ; |
| 3255 | self.integer_trailing_digit_separator, INTEGER_TRAILING_DIGIT_SEPARATOR ; |
| 3256 | self.fraction_trailing_digit_separator, FRACTION_TRAILING_DIGIT_SEPARATOR ; |
| 3257 | self.exponent_trailing_digit_separator, EXPONENT_TRAILING_DIGIT_SEPARATOR ; |
| 3258 | self.integer_consecutive_digit_separator, INTEGER_CONSECUTIVE_DIGIT_SEPARATOR ; |
| 3259 | self.fraction_consecutive_digit_separator, FRACTION_CONSECUTIVE_DIGIT_SEPARATOR ; |
| 3260 | self.exponent_consecutive_digit_separator, EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR ; |
| 3261 | self.special_digit_separator, SPECIAL_DIGIT_SEPARATOR ; |
| 3262 | ); |
| 3263 | if format & flags::DIGIT_SEPARATOR_FLAG_MASK != 0 { |
| 3264 | format |= |
| 3265 | (unwrap_or_zero(self.digit_separator) as u128) << flags::DIGIT_SEPARATOR_SHIFT; |
| 3266 | } |
| 3267 | format |= (unwrap_or_zero(self.base_prefix) as u128) << flags::BASE_PREFIX_SHIFT; |
| 3268 | format |= (unwrap_or_zero(self.base_suffix) as u128) << flags::BASE_SUFFIX_SHIFT; |
| 3269 | format |= (self.mantissa_radix as u128) << flags::MANTISSA_RADIX_SHIFT; |
| 3270 | format |= (unwrap_or_zero(self.exponent_base) as u128) << flags::EXPONENT_BASE_SHIFT; |
| 3271 | format |= (unwrap_or_zero(self.exponent_radix) as u128) << flags::EXPONENT_RADIX_SHIFT; |
| 3272 | |
| 3273 | format |
| 3274 | } |
| 3275 | |
| 3276 | /// Build the packed number format, panicking if the builder is invalid. |
| 3277 | /// |
| 3278 | /// # Panics |
| 3279 | /// |
| 3280 | /// If the built format is not valid. This should always |
| 3281 | /// be used within a const context to avoid panics at runtime. |
| 3282 | #[inline(always)] |
| 3283 | pub const fn build_strict(&self) -> u128 { |
| 3284 | use crate::format::format_error_impl; |
| 3285 | |
| 3286 | let packed = self.build_unchecked(); |
| 3287 | match format_error_impl(packed) { |
| 3288 | Error::Success => packed, |
| 3289 | error => core::panic!("{}", error.description()), |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | /// Create 128-bit, packed number format struct from builder options. |
| 3294 | /// |
| 3295 | /// <div class="warning"> |
| 3296 | /// |
| 3297 | /// This function will never fail. It is up to the caller to ensure the |
| 3298 | /// format is valid using [`NumberFormat::is_valid`]. This function is |
| 3299 | /// soft-deprecated and you should prefer [`build_unchecked`] and handle |
| 3300 | /// if the result is invalid instead, or use [`build_strict`] to panic on |
| 3301 | /// any errors. This exists when compatibility with older Rust |
| 3302 | /// versions was required. |
| 3303 | /// |
| 3304 | /// </div> |
| 3305 | /// |
| 3306 | /// [`build_unchecked`]: Self::build_unchecked |
| 3307 | /// [`build_strict`]: Self::build_strict |
| 3308 | /// [`NumberFormat::is_valid`]: crate::NumberFormat::is_valid |
| 3309 | #[inline(always)] |
| 3310 | #[deprecated = "Use `build_strict` or `build_unchecked` instead."] |
| 3311 | pub const fn build(&self) -> u128 { |
| 3312 | self.build_unchecked() |
| 3313 | } |
| 3314 | |
| 3315 | /// Re-create builder from format. |
| 3316 | #[inline(always)] |
| 3317 | pub const fn rebuild(format: u128) -> Self { |
| 3318 | NumberFormatBuilder { |
| 3319 | digit_separator: num::NonZeroU8::new(flags::digit_separator(format)), |
| 3320 | base_prefix: num::NonZeroU8::new(flags::base_prefix(format)), |
| 3321 | base_suffix: num::NonZeroU8::new(flags::base_suffix(format)), |
| 3322 | mantissa_radix: flags::mantissa_radix(format) as u8, |
| 3323 | exponent_base: num::NonZeroU8::new(flags::exponent_base(format) as u8), |
| 3324 | exponent_radix: num::NonZeroU8::new(flags::exponent_radix(format) as u8), |
| 3325 | required_integer_digits: has_flag!(format, REQUIRED_INTEGER_DIGITS), |
| 3326 | required_fraction_digits: has_flag!(format, REQUIRED_FRACTION_DIGITS), |
| 3327 | required_exponent_digits: has_flag!(format, REQUIRED_EXPONENT_DIGITS), |
| 3328 | required_mantissa_digits: has_flag!(format, REQUIRED_MANTISSA_DIGITS), |
| 3329 | no_positive_mantissa_sign: has_flag!(format, NO_POSITIVE_MANTISSA_SIGN), |
| 3330 | required_mantissa_sign: has_flag!(format, REQUIRED_MANTISSA_SIGN), |
| 3331 | no_exponent_notation: has_flag!(format, NO_EXPONENT_NOTATION), |
| 3332 | no_positive_exponent_sign: has_flag!(format, NO_POSITIVE_EXPONENT_SIGN), |
| 3333 | required_exponent_sign: has_flag!(format, REQUIRED_EXPONENT_SIGN), |
| 3334 | no_exponent_without_fraction: has_flag!(format, NO_EXPONENT_WITHOUT_FRACTION), |
| 3335 | no_special: has_flag!(format, NO_SPECIAL), |
| 3336 | case_sensitive_special: has_flag!(format, CASE_SENSITIVE_SPECIAL), |
| 3337 | no_integer_leading_zeros: has_flag!(format, NO_INTEGER_LEADING_ZEROS), |
| 3338 | no_float_leading_zeros: has_flag!(format, NO_FLOAT_LEADING_ZEROS), |
| 3339 | required_exponent_notation: has_flag!(format, REQUIRED_EXPONENT_NOTATION), |
| 3340 | case_sensitive_exponent: has_flag!(format, CASE_SENSITIVE_EXPONENT), |
| 3341 | case_sensitive_base_prefix: has_flag!(format, CASE_SENSITIVE_BASE_PREFIX), |
| 3342 | case_sensitive_base_suffix: has_flag!(format, CASE_SENSITIVE_BASE_SUFFIX), |
| 3343 | integer_internal_digit_separator: has_flag!(format, INTEGER_INTERNAL_DIGIT_SEPARATOR), |
| 3344 | fraction_internal_digit_separator: has_flag!(format, FRACTION_INTERNAL_DIGIT_SEPARATOR), |
| 3345 | exponent_internal_digit_separator: has_flag!(format, EXPONENT_INTERNAL_DIGIT_SEPARATOR), |
| 3346 | integer_leading_digit_separator: has_flag!(format, INTEGER_LEADING_DIGIT_SEPARATOR), |
| 3347 | fraction_leading_digit_separator: has_flag!(format, FRACTION_LEADING_DIGIT_SEPARATOR), |
| 3348 | exponent_leading_digit_separator: has_flag!(format, EXPONENT_LEADING_DIGIT_SEPARATOR), |
| 3349 | integer_trailing_digit_separator: has_flag!(format, INTEGER_TRAILING_DIGIT_SEPARATOR), |
| 3350 | fraction_trailing_digit_separator: has_flag!(format, FRACTION_TRAILING_DIGIT_SEPARATOR), |
| 3351 | exponent_trailing_digit_separator: has_flag!(format, EXPONENT_TRAILING_DIGIT_SEPARATOR), |
| 3352 | integer_consecutive_digit_separator: has_flag!( |
| 3353 | format, |
| 3354 | INTEGER_CONSECUTIVE_DIGIT_SEPARATOR |
| 3355 | ), |
| 3356 | fraction_consecutive_digit_separator: has_flag!( |
| 3357 | format, |
| 3358 | FRACTION_CONSECUTIVE_DIGIT_SEPARATOR |
| 3359 | ), |
| 3360 | exponent_consecutive_digit_separator: has_flag!( |
| 3361 | format, |
| 3362 | EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR |
| 3363 | ), |
| 3364 | special_digit_separator: has_flag!(format, SPECIAL_DIGIT_SEPARATOR), |
| 3365 | } |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | impl Default for NumberFormatBuilder { |
| 3370 | #[inline(always)] |
| 3371 | fn default() -> Self { |
| 3372 | Self::new() |
| 3373 | } |
| 3374 | } |