| 1 | //! Configuration options for parsing and formatting numbers. |
| 2 | //! |
| 3 | //! This comprises 2 parts: a low-level API for generating packed structs |
| 4 | //! containing enumerating for number formats (both syntax and lexer). |
| 5 | //! |
| 6 | //! # Syntax Format |
| 7 | //! |
| 8 | //! The syntax format defines **which** numeric string are valid. |
| 9 | //! For example, if exponent notation is required or not |
| 10 | //! allowed. |
| 11 | //! |
| 12 | //! # Control Format |
| 13 | //! |
| 14 | //! The control format defines what characters are valid, that is, which |
| 15 | //! characters should be consider valid to continue tokenization. |
| 16 | |
| 17 | #![cfg(feature = "format")] |
| 18 | |
| 19 | use crate::error::Error; |
| 20 | use crate::format_builder::NumberFormatBuilder; |
| 21 | use crate::format_flags as flags; |
| 22 | |
| 23 | /// Add multiple flags to `SyntaxFormat`. |
| 24 | macro_rules! from_flag { |
| 25 | ($format:ident, $flag:ident) => {{ |
| 26 | $format & flags::$flag != 0 |
| 27 | }}; |
| 28 | } |
| 29 | |
| 30 | /// Helper to access features from the packed format struct. |
| 31 | /// |
| 32 | /// This contains accessory methods to read the formatting settings |
| 33 | /// without using bitmasks directly on the underlying packed struct. |
| 34 | /// |
| 35 | /// Some of the core functionality includes support for: |
| 36 | /// - Digit separators: ignored characters used to make numbers more readable, |
| 37 | /// such as `100,000`. |
| 38 | /// - Non-decimal radixes: writing or parsing numbers written in binary, |
| 39 | /// hexadecimal, or other bases. |
| 40 | /// - Special numbers: disabling support for special floating-point, such as |
| 41 | /// [`NaN`][f64::NAN]. |
| 42 | /// - Number components: require signs, significant digits, and more. |
| 43 | /// |
| 44 | /// This should always be constructed via [`NumberFormatBuilder`]. |
| 45 | /// See [`NumberFormatBuilder`] for the fields for the packed struct. |
| 46 | /// |
| 47 | /// # Examples |
| 48 | /// |
| 49 | /// ```rust |
| 50 | /// # #[cfg(feature = "format")] { |
| 51 | /// use lexical_util::format::{RUST_LITERAL, NumberFormat}; |
| 52 | /// |
| 53 | /// let format = NumberFormat::<{ RUST_LITERAL }> {}; |
| 54 | /// assert!(format.no_positive_mantissa_sign()); |
| 55 | /// assert!(format.no_special()); |
| 56 | /// assert!(format.internal_digit_separator()); |
| 57 | /// assert!(format.trailing_digit_separator()); |
| 58 | /// assert!(format.consecutive_digit_separator()); |
| 59 | /// assert!(!format.no_exponent_notation()); |
| 60 | /// # } |
| 61 | /// ``` |
| 62 | pub struct NumberFormat<const FORMAT: u128>; |
| 63 | |
| 64 | #[rustfmt::skip] |
| 65 | impl<const FORMAT: u128> NumberFormat<FORMAT> { |
| 66 | // CONSTRUCTORS |
| 67 | |
| 68 | /// Create new instance (for methods and validation). |
| 69 | /// |
| 70 | /// This uses the same settings as in the `FORMAT` packed struct. |
| 71 | #[inline(always)] |
| 72 | pub const fn new() -> Self { |
| 73 | Self {} |
| 74 | } |
| 75 | |
| 76 | // VALIDATION |
| 77 | |
| 78 | /// Determine if the number format is valid. |
| 79 | #[inline(always)] |
| 80 | pub const fn is_valid(&self) -> bool { |
| 81 | self.error().is_success() |
| 82 | } |
| 83 | |
| 84 | /// Get the error type from the format. |
| 85 | /// |
| 86 | /// If [`Error::Success`] is returned, then no error occurred. |
| 87 | #[inline(always)] |
| 88 | pub const fn error(&self) -> Error { |
| 89 | format_error_impl(FORMAT) |
| 90 | } |
| 91 | |
| 92 | /// Determine if the radixes in the number format are valid. |
| 93 | #[inline(always)] |
| 94 | pub const fn is_valid_radix(&self) -> bool { |
| 95 | self.error_radix().is_success() |
| 96 | } |
| 97 | |
| 98 | /// Get the error type from the radix-only for the format. |
| 99 | /// |
| 100 | /// If [`Error::Success`] is returned, then no error occurred. |
| 101 | #[inline(always)] |
| 102 | pub const fn error_radix(&self) -> Error { |
| 103 | radix_error_impl(FORMAT) |
| 104 | } |
| 105 | |
| 106 | // NON-DIGIT SEPARATOR FLAGS & MASKS |
| 107 | |
| 108 | /// If digits are required before the decimal point. |
| 109 | /// |
| 110 | /// See [`required_integer_digits`][Self::required_integer_digits]. |
| 111 | pub const REQUIRED_INTEGER_DIGITS: bool = from_flag!(FORMAT, REQUIRED_INTEGER_DIGITS); |
| 112 | |
| 113 | /// Get if digits are required before the decimal point. |
| 114 | /// |
| 115 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 116 | /// to [`false`]. |
| 117 | /// |
| 118 | /// # Examples |
| 119 | /// |
| 120 | /// | Input | Valid? | |
| 121 | /// |:-:|:-:| |
| 122 | /// | `1.1` | ✔️ | |
| 123 | /// | `0.1` | ✔️ | |
| 124 | /// | `1` | ✔️ | |
| 125 | /// | `.1` | ❌ | |
| 126 | /// |
| 127 | /// # Used For |
| 128 | /// |
| 129 | /// - Parse Float |
| 130 | #[inline(always)] |
| 131 | pub const fn required_integer_digits(&self) -> bool { |
| 132 | Self::REQUIRED_INTEGER_DIGITS |
| 133 | } |
| 134 | |
| 135 | /// If digits are required after the decimal point. |
| 136 | /// |
| 137 | /// See [`required_fraction_digits`][Self::required_fraction_digits]. |
| 138 | pub const REQUIRED_FRACTION_DIGITS: bool = from_flag!(FORMAT, REQUIRED_FRACTION_DIGITS); |
| 139 | |
| 140 | /// Get if digits are required after the decimal point. |
| 141 | /// |
| 142 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 143 | /// to [`false`]. |
| 144 | /// |
| 145 | /// # Examples |
| 146 | /// |
| 147 | /// | Input | Valid? | |
| 148 | /// |:-:|:-:| |
| 149 | /// | `1.1` | ✔️ | |
| 150 | /// | `1` | ✔️ | |
| 151 | /// | `1.` | ❌ | |
| 152 | /// |
| 153 | /// # Used For |
| 154 | /// |
| 155 | /// - Parse Float |
| 156 | #[inline(always)] |
| 157 | pub const fn required_fraction_digits(&self) -> bool { |
| 158 | Self::REQUIRED_FRACTION_DIGITS |
| 159 | } |
| 160 | |
| 161 | /// If digits are required after the exponent character. |
| 162 | /// |
| 163 | /// See [`required_exponent_digits`][Self::required_exponent_digits]. |
| 164 | pub const REQUIRED_EXPONENT_DIGITS: bool = from_flag!(FORMAT, REQUIRED_EXPONENT_DIGITS); |
| 165 | |
| 166 | /// Get if digits are required after the exponent character. |
| 167 | /// |
| 168 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 169 | /// to [`true`]. |
| 170 | /// |
| 171 | /// # Examples |
| 172 | /// |
| 173 | /// | Input | Valid? | |
| 174 | /// |:-:|:-:| |
| 175 | /// | `1.1e+3` | ✔️ | |
| 176 | /// | `1.1e3` | ✔️ | |
| 177 | /// | `1.1e+` | ❌ | |
| 178 | /// | `1.1e` | ❌ | |
| 179 | /// |
| 180 | /// # Used For |
| 181 | /// |
| 182 | /// - Parse Float |
| 183 | #[inline(always)] |
| 184 | pub const fn required_exponent_digits(&self) -> bool { |
| 185 | Self::REQUIRED_EXPONENT_DIGITS |
| 186 | } |
| 187 | |
| 188 | /// If significant digits are required. |
| 189 | /// |
| 190 | /// See [`required_mantissa_digits`][Self::required_mantissa_digits]. |
| 191 | pub const REQUIRED_MANTISSA_DIGITS: bool = from_flag!(FORMAT, REQUIRED_MANTISSA_DIGITS); |
| 192 | |
| 193 | /// Get if at least 1 significant digit is required. |
| 194 | /// |
| 195 | /// If not required, then values like `.` (`0`) are valid, but empty strings |
| 196 | /// are still invalid. Can only be modified with [`feature`][crate#features] |
| 197 | /// `format`. Defaults to [`true`]. |
| 198 | /// |
| 199 | /// # Examples |
| 200 | /// |
| 201 | /// | Input | Valid? | |
| 202 | /// |:-:|:-:| |
| 203 | /// | `1.1` | ✔️ | |
| 204 | /// | `.` | ✔️ | |
| 205 | /// | `e10` | ✔️ | |
| 206 | /// | `.e10` | ✔️ | |
| 207 | /// | `` | ❌ | |
| 208 | /// |
| 209 | /// # Used For |
| 210 | /// |
| 211 | /// - Parse Float |
| 212 | #[inline(always)] |
| 213 | pub const fn required_mantissa_digits(&self) -> bool { |
| 214 | Self::REQUIRED_MANTISSA_DIGITS |
| 215 | } |
| 216 | |
| 217 | /// If at least 1 digit in the number is required. |
| 218 | /// |
| 219 | /// See [`required_digits`][Self::required_digits]. |
| 220 | pub const REQUIRED_DIGITS: bool = from_flag!(FORMAT, REQUIRED_DIGITS); |
| 221 | |
| 222 | /// Get if at least 1 digit in the number is required. |
| 223 | /// |
| 224 | /// This requires either [`mantissa`] or [`exponent`] digits. |
| 225 | /// |
| 226 | /// [`mantissa`]: Self::required_mantissa_digits |
| 227 | /// [`exponent`]: Self::required_exponent_digits |
| 228 | #[inline(always)] |
| 229 | pub const fn required_digits(&self) -> bool { |
| 230 | Self::REQUIRED_DIGITS |
| 231 | } |
| 232 | |
| 233 | /// If a positive sign before the mantissa is not allowed. |
| 234 | /// |
| 235 | /// See [`no_positive_mantissa_sign`][Self::no_positive_mantissa_sign]. |
| 236 | pub const NO_POSITIVE_MANTISSA_SIGN: bool = from_flag!(FORMAT, NO_POSITIVE_MANTISSA_SIGN); |
| 237 | |
| 238 | /// Get if a positive sign before the mantissa is not allowed. |
| 239 | /// |
| 240 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 241 | /// to `false`. |
| 242 | /// |
| 243 | /// # Examples |
| 244 | /// |
| 245 | /// | Input | Valid? | |
| 246 | /// |:-:|:-:| |
| 247 | /// | `1.1` | ✔️ | |
| 248 | /// | `-1.1` | ✔️ | |
| 249 | /// | `+1.1` | ❌ | |
| 250 | /// |
| 251 | /// # Used For |
| 252 | /// |
| 253 | /// - Parse Float |
| 254 | /// - Parse Integer |
| 255 | /// - Write Float |
| 256 | #[inline(always)] |
| 257 | pub const fn no_positive_mantissa_sign(&self) -> bool { |
| 258 | Self::NO_POSITIVE_MANTISSA_SIGN |
| 259 | } |
| 260 | |
| 261 | /// If a sign symbol before the mantissa is required. |
| 262 | /// |
| 263 | /// See [`required_mantissa_sign`][Self::required_mantissa_sign]. |
| 264 | pub const REQUIRED_MANTISSA_SIGN: bool = from_flag!(FORMAT, REQUIRED_MANTISSA_SIGN); |
| 265 | |
| 266 | /// Get if a sign symbol before the mantissa is required. |
| 267 | /// |
| 268 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 269 | /// to `false`. |
| 270 | /// |
| 271 | /// # Examples |
| 272 | /// |
| 273 | /// | Input | Valid? | |
| 274 | /// |:-:|:-:| |
| 275 | /// | `1.1` | ❌ | |
| 276 | /// | `-1.1` | ✔️ | |
| 277 | /// | `+1.1` | ✔️ | |
| 278 | /// |
| 279 | /// # Used For |
| 280 | /// |
| 281 | /// - Parse Float |
| 282 | /// - Parse Integer |
| 283 | /// - Write Float |
| 284 | #[inline(always)] |
| 285 | pub const fn required_mantissa_sign(&self) -> bool { |
| 286 | Self::REQUIRED_MANTISSA_SIGN |
| 287 | } |
| 288 | |
| 289 | /// If exponent notation is not allowed. |
| 290 | /// |
| 291 | /// See [`no_exponent_notation`][Self::no_exponent_notation]. |
| 292 | pub const NO_EXPONENT_NOTATION: bool = from_flag!(FORMAT, NO_EXPONENT_NOTATION); |
| 293 | |
| 294 | /// Get if exponent notation is not allowed. |
| 295 | /// |
| 296 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 297 | /// to `false`. |
| 298 | /// |
| 299 | /// # Examples |
| 300 | /// |
| 301 | /// | Input | Valid? | |
| 302 | /// |:-:|:-:| |
| 303 | /// | `1` | ✔️ | |
| 304 | /// | `1.1` | ✔️ | |
| 305 | /// | `1.1e` | ❌ | |
| 306 | /// | `1.1e5` | ❌ | |
| 307 | /// |
| 308 | /// # Used For |
| 309 | /// |
| 310 | /// - Parse Float |
| 311 | /// - Write Float |
| 312 | #[inline(always)] |
| 313 | pub const fn no_exponent_notation(&self) -> bool { |
| 314 | Self::NO_EXPONENT_NOTATION |
| 315 | } |
| 316 | |
| 317 | /// If a positive sign before the exponent is not allowed. |
| 318 | /// |
| 319 | /// See [`no_positive_exponent_sign`][Self::no_positive_exponent_sign]. |
| 320 | pub const NO_POSITIVE_EXPONENT_SIGN: bool = from_flag!(FORMAT, NO_POSITIVE_EXPONENT_SIGN); |
| 321 | |
| 322 | /// Get if a positive sign before the exponent is not allowed. |
| 323 | /// |
| 324 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 325 | /// to `false`. |
| 326 | /// |
| 327 | /// # Examples |
| 328 | /// |
| 329 | /// | Input | Valid? | |
| 330 | /// |:-:|:-:| |
| 331 | /// | `1.1e3` | ✔️ | |
| 332 | /// | `1.1e-3` | ✔️ | |
| 333 | /// | `1.1e+3` | ❌ | |
| 334 | /// |
| 335 | /// # Used For |
| 336 | /// |
| 337 | /// - Parse Float |
| 338 | /// - Write Float |
| 339 | #[inline(always)] |
| 340 | pub const fn no_positive_exponent_sign(&self) -> bool { |
| 341 | Self::NO_POSITIVE_EXPONENT_SIGN |
| 342 | } |
| 343 | |
| 344 | /// If a sign symbol before the exponent is required. |
| 345 | /// |
| 346 | /// See [`required_exponent_sign`][Self::required_exponent_sign]. |
| 347 | pub const REQUIRED_EXPONENT_SIGN: bool = from_flag!(FORMAT, REQUIRED_EXPONENT_SIGN); |
| 348 | |
| 349 | /// Get if a sign symbol before the exponent is required. |
| 350 | /// |
| 351 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 352 | /// to `false`. |
| 353 | /// |
| 354 | /// # Examples |
| 355 | /// |
| 356 | /// | Input | Valid? | |
| 357 | /// |:-:|:-:| |
| 358 | /// | `1.1e3` | ❌ | |
| 359 | /// | `1.1e-3` | ✔️ | |
| 360 | /// | `1.1e+3` | ✔️ | |
| 361 | /// |
| 362 | /// # Used For |
| 363 | /// |
| 364 | /// - Parse Float |
| 365 | /// - Write Float |
| 366 | #[inline(always)] |
| 367 | pub const fn required_exponent_sign(&self) -> bool { |
| 368 | Self::REQUIRED_EXPONENT_SIGN |
| 369 | } |
| 370 | |
| 371 | /// If an exponent without fraction is not allowed. |
| 372 | /// |
| 373 | /// See [`no_exponent_without_fraction`][Self::no_exponent_without_fraction]. |
| 374 | pub const NO_EXPONENT_WITHOUT_FRACTION: bool = from_flag!(FORMAT, NO_EXPONENT_WITHOUT_FRACTION); |
| 375 | |
| 376 | /// Get if an exponent without fraction is not allowed. |
| 377 | /// |
| 378 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 379 | /// to `false`. |
| 380 | /// |
| 381 | /// # Examples |
| 382 | /// |
| 383 | /// | Input | Valid? | |
| 384 | /// |:-:|:-:| |
| 385 | /// | `1e3` | ❌ | |
| 386 | /// | `1.e3` | ❌ | |
| 387 | /// | `1.1e` | ✔️ | |
| 388 | /// | `.1e3` | ✔️ | |
| 389 | /// |
| 390 | /// # Used For |
| 391 | /// |
| 392 | /// - Parse Float |
| 393 | #[inline(always)] |
| 394 | pub const fn no_exponent_without_fraction(&self) -> bool { |
| 395 | Self::NO_EXPONENT_WITHOUT_FRACTION |
| 396 | } |
| 397 | |
| 398 | /// If special (non-finite) values are not allowed. |
| 399 | /// |
| 400 | /// See [`no_special`][Self::no_special]. |
| 401 | pub const NO_SPECIAL: bool = from_flag!(FORMAT, NO_SPECIAL); |
| 402 | |
| 403 | /// Get if special (non-finite) values are not allowed. |
| 404 | /// |
| 405 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 406 | /// to `false`. |
| 407 | /// |
| 408 | /// # Examples |
| 409 | /// |
| 410 | /// | Input | Valid? | |
| 411 | /// |:-:|:-:| |
| 412 | /// | `NaN` | ❌ | |
| 413 | /// | `inf` | ❌ | |
| 414 | /// | `-Infinity` | ❌ | |
| 415 | /// | `1.1e` | ✔️ | |
| 416 | /// |
| 417 | /// # Used For |
| 418 | /// |
| 419 | /// - Parse Float |
| 420 | #[inline(always)] |
| 421 | pub const fn no_special(&self) -> bool { |
| 422 | Self::NO_SPECIAL |
| 423 | } |
| 424 | |
| 425 | /// If special (non-finite) values are case-sensitive. |
| 426 | /// |
| 427 | /// See [`case_sensitive_special`][Self::case_sensitive_special]. |
| 428 | pub const CASE_SENSITIVE_SPECIAL: bool = from_flag!(FORMAT, CASE_SENSITIVE_SPECIAL); |
| 429 | |
| 430 | /// Get if special (non-finite) values are case-sensitive. |
| 431 | /// |
| 432 | /// If set to [`true`], then `NaN` and `nan` are treated as the same value |
| 433 | /// ([Not a Number][f64::NAN]). Can only be modified with |
| 434 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 435 | /// |
| 436 | /// # Used For |
| 437 | /// |
| 438 | /// - Parse Float |
| 439 | #[inline(always)] |
| 440 | pub const fn case_sensitive_special(&self) -> bool { |
| 441 | Self::CASE_SENSITIVE_SPECIAL |
| 442 | } |
| 443 | |
| 444 | /// If leading zeros before an integer are not allowed. |
| 445 | /// |
| 446 | /// See [`no_integer_leading_zeros`][Self::no_integer_leading_zeros]. |
| 447 | pub const NO_INTEGER_LEADING_ZEROS: bool = from_flag!(FORMAT, NO_INTEGER_LEADING_ZEROS); |
| 448 | |
| 449 | /// Get if leading zeros before an integer are not allowed. |
| 450 | /// |
| 451 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 452 | /// to [`false`]. |
| 453 | /// |
| 454 | /// # Examples |
| 455 | /// |
| 456 | /// | Input | Valid? | |
| 457 | /// |:-:|:-:| |
| 458 | /// | `01` | ❌ | |
| 459 | /// | `0` | ✔️ | |
| 460 | /// | `10` | ✔️ | |
| 461 | /// |
| 462 | /// # Used For |
| 463 | /// |
| 464 | /// - Parse Integer |
| 465 | #[inline(always)] |
| 466 | pub const fn no_integer_leading_zeros(&self) -> bool { |
| 467 | Self::NO_INTEGER_LEADING_ZEROS |
| 468 | } |
| 469 | |
| 470 | /// If leading zeros before a float are not allowed. |
| 471 | /// |
| 472 | /// See [`no_float_leading_zeros`][Self::no_float_leading_zeros]. |
| 473 | pub const NO_FLOAT_LEADING_ZEROS: bool = from_flag!(FORMAT, NO_FLOAT_LEADING_ZEROS); |
| 474 | |
| 475 | /// Get if leading zeros before a float are not allowed. |
| 476 | /// |
| 477 | /// This is before the significant digits of the float, that is, if there is |
| 478 | /// 1 or more digits in the integral component and the leading digit is 0, |
| 479 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 480 | /// to [`false`]. |
| 481 | /// |
| 482 | /// # Examples |
| 483 | /// |
| 484 | /// | Input | Valid? | |
| 485 | /// |:-:|:-:| |
| 486 | /// | `01` | ❌ | |
| 487 | /// | `01.0` | ❌ | |
| 488 | /// | `0` | ✔️ | |
| 489 | /// | `10` | ✔️ | |
| 490 | /// | `0.1` | ✔️ | |
| 491 | /// |
| 492 | /// # Used For |
| 493 | /// |
| 494 | /// - Parse Float |
| 495 | #[inline(always)] |
| 496 | pub const fn no_float_leading_zeros(&self) -> bool { |
| 497 | Self::NO_FLOAT_LEADING_ZEROS |
| 498 | } |
| 499 | |
| 500 | /// If exponent notation is required. |
| 501 | /// |
| 502 | /// See [`required_exponent_notation`][Self::required_exponent_notation]. |
| 503 | pub const REQUIRED_EXPONENT_NOTATION: bool = from_flag!(FORMAT, REQUIRED_EXPONENT_NOTATION); |
| 504 | |
| 505 | /// Get if exponent notation is required. |
| 506 | /// |
| 507 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 508 | /// to [`false`]. |
| 509 | /// |
| 510 | /// # Examples |
| 511 | /// |
| 512 | /// | Input | Valid? | |
| 513 | /// |:-:|:-:| |
| 514 | /// | `1` | ❌ | |
| 515 | /// | `1.0` | ❌ | |
| 516 | /// | `1e3` | ✔️ | |
| 517 | /// | `1.1e3` | ✔️ | |
| 518 | /// |
| 519 | /// # Used For |
| 520 | /// |
| 521 | /// - Parse Float |
| 522 | /// - Write Float |
| 523 | #[inline(always)] |
| 524 | pub const fn required_exponent_notation(&self) -> bool { |
| 525 | Self::REQUIRED_EXPONENT_NOTATION |
| 526 | } |
| 527 | |
| 528 | /// If exponent characters are case-sensitive. |
| 529 | /// |
| 530 | /// See [`case_sensitive_exponent`][Self::case_sensitive_exponent]. |
| 531 | pub const CASE_SENSITIVE_EXPONENT: bool = from_flag!(FORMAT, CASE_SENSITIVE_EXPONENT); |
| 532 | |
| 533 | /// Get if exponent characters are case-sensitive. |
| 534 | /// |
| 535 | /// If set to [`true`], then the exponent character `e` would be considered |
| 536 | /// the different from `E`. Can only be modified with |
| 537 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 538 | /// |
| 539 | /// # Used For |
| 540 | /// |
| 541 | /// - Parse Float |
| 542 | #[inline(always)] |
| 543 | pub const fn case_sensitive_exponent(&self) -> bool { |
| 544 | Self::CASE_SENSITIVE_EXPONENT |
| 545 | } |
| 546 | |
| 547 | /// If base prefixes are case-sensitive. |
| 548 | /// |
| 549 | /// See [`case_sensitive_base_prefix`][Self::case_sensitive_base_prefix]. |
| 550 | pub const CASE_SENSITIVE_BASE_PREFIX: bool = from_flag!(FORMAT, CASE_SENSITIVE_BASE_PREFIX); |
| 551 | |
| 552 | /// Get if base prefixes are case-sensitive. |
| 553 | /// |
| 554 | /// If set to [`true`], then the base prefix `x` would be considered the |
| 555 | /// different from `X`. Can only be modified with |
| 556 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 557 | /// `format`. Defaults to [`false`]. |
| 558 | /// |
| 559 | /// # Used For |
| 560 | /// |
| 561 | /// - Parse Float |
| 562 | /// - Parse Integer |
| 563 | #[inline(always)] |
| 564 | pub const fn case_sensitive_base_prefix(&self) -> bool { |
| 565 | Self::CASE_SENSITIVE_BASE_PREFIX |
| 566 | } |
| 567 | |
| 568 | /// If base suffixes are case-sensitive. |
| 569 | /// |
| 570 | /// See [`case_sensitive_base_suffix`][Self::case_sensitive_base_suffix]. |
| 571 | pub const CASE_SENSITIVE_BASE_SUFFIX: bool = from_flag!(FORMAT, CASE_SENSITIVE_BASE_SUFFIX); |
| 572 | |
| 573 | /// Get if base suffixes are case-sensitive. |
| 574 | /// |
| 575 | /// If set to [`true`], then the base suffix `x` would be considered the |
| 576 | /// different from `X`. Can only be modified with |
| 577 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 578 | /// `format`. Defaults to [`false`]. |
| 579 | /// |
| 580 | /// # Used For |
| 581 | /// |
| 582 | /// - Parse Float |
| 583 | /// - Parse Integer |
| 584 | #[inline(always)] |
| 585 | pub const fn case_sensitive_base_suffix(&self) -> bool { |
| 586 | Self::CASE_SENSITIVE_BASE_SUFFIX |
| 587 | } |
| 588 | |
| 589 | // DIGIT SEPARATOR FLAGS & MASKS |
| 590 | |
| 591 | /// If digit separators are allowed between integer digits. |
| 592 | /// |
| 593 | /// This will not consider an input of only the digit separator |
| 594 | /// to be a valid separator: the digit separator must be surrounded by |
| 595 | /// digits. |
| 596 | /// |
| 597 | /// See [`integer_internal_digit_separator`][Self::integer_internal_digit_separator]. |
| 598 | pub const INTEGER_INTERNAL_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, INTEGER_INTERNAL_DIGIT_SEPARATOR); |
| 599 | |
| 600 | /// Get if digit separators are allowed between integer digits. |
| 601 | /// |
| 602 | /// This will not consider an input of only the digit separator |
| 603 | /// to be a valid separator: the digit separator must be surrounded by |
| 604 | /// digits. Can only be modified with [`feature`][crate#features] `format`. |
| 605 | /// Defaults to [`false`]. |
| 606 | /// |
| 607 | /// # Examples |
| 608 | /// |
| 609 | /// Using a digit separator of `_`. |
| 610 | /// |
| 611 | /// | Input | Valid? | |
| 612 | /// |:-:|:-:| |
| 613 | /// | `1` | ✔️ | |
| 614 | /// | `_` | ❌ | |
| 615 | /// | `1_1` | ✔️ | |
| 616 | /// | `1_` | ❌ | |
| 617 | /// | `_1` | ❌ | |
| 618 | /// |
| 619 | /// # Used For |
| 620 | /// |
| 621 | /// - Parse Float |
| 622 | /// - Parse Integer |
| 623 | #[inline(always)] |
| 624 | pub const fn integer_internal_digit_separator(&self) -> bool { |
| 625 | Self::INTEGER_INTERNAL_DIGIT_SEPARATOR |
| 626 | } |
| 627 | |
| 628 | /// If digit separators are allowed between fraction digits. |
| 629 | /// |
| 630 | /// This will not consider an input of only the digit separator |
| 631 | /// to be a valid separator: the digit separator must be surrounded by |
| 632 | /// digits. |
| 633 | /// |
| 634 | /// See [`fraction_internal_digit_separator`][Self::fraction_internal_digit_separator]. |
| 635 | pub const FRACTION_INTERNAL_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, FRACTION_INTERNAL_DIGIT_SEPARATOR); |
| 636 | |
| 637 | /// Get if digit separators are allowed between fraction digits. |
| 638 | /// |
| 639 | /// This will not consider an input of only the digit separator |
| 640 | /// to be a valid separator: the digit separator must be surrounded by |
| 641 | /// digits. Can only be modified with [`feature`][crate#features] `format`. |
| 642 | /// Defaults to [`false`]. |
| 643 | /// |
| 644 | /// # Examples |
| 645 | /// |
| 646 | /// Using a digit separator of `_`. |
| 647 | /// |
| 648 | /// | Input | Valid? | |
| 649 | /// |:-:|:-:| |
| 650 | /// | `1.1` | ✔️ | |
| 651 | /// | `1._` | ❌ | |
| 652 | /// | `1.1_1` | ✔️ | |
| 653 | /// | `1.1_` | ❌ | |
| 654 | /// | `1._1` | ❌ | |
| 655 | /// |
| 656 | /// # Used For |
| 657 | /// |
| 658 | /// - Parse Float |
| 659 | #[inline(always)] |
| 660 | pub const fn fraction_internal_digit_separator(&self) -> bool { |
| 661 | Self::FRACTION_INTERNAL_DIGIT_SEPARATOR |
| 662 | } |
| 663 | |
| 664 | /// If digit separators are allowed between exponent digits. |
| 665 | /// |
| 666 | /// This will not consider an input of only the digit separator |
| 667 | /// to be a valid separator: the digit separator must be surrounded by |
| 668 | /// digits. |
| 669 | /// |
| 670 | /// See [`exponent_internal_digit_separator`][Self::exponent_internal_digit_separator]. |
| 671 | pub const EXPONENT_INTERNAL_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, EXPONENT_INTERNAL_DIGIT_SEPARATOR); |
| 672 | |
| 673 | /// Get if digit separators are allowed between exponent digits. |
| 674 | /// |
| 675 | /// This will not consider an input of only the digit separator |
| 676 | /// to be a valid separator: the digit separator must be surrounded by |
| 677 | /// digits. Can only be modified with [`feature`][crate#features] `format`. |
| 678 | /// Defaults to [`false`]. |
| 679 | /// |
| 680 | /// # Examples |
| 681 | /// |
| 682 | /// Using a digit separator of `_`. |
| 683 | /// |
| 684 | /// | Input | Valid? | |
| 685 | /// |:-:|:-:| |
| 686 | /// | `1.1e1` | ✔️ | |
| 687 | /// | `1.1e_` | ❌ | |
| 688 | /// | `1.1e1_1` | ✔️ | |
| 689 | /// | `1.1e1_` | ❌ | |
| 690 | /// | `1.1e_1` | ❌ | |
| 691 | /// |
| 692 | /// # Used For |
| 693 | /// |
| 694 | /// - Parse Float |
| 695 | #[inline(always)] |
| 696 | pub const fn exponent_internal_digit_separator(&self) -> bool { |
| 697 | Self::EXPONENT_INTERNAL_DIGIT_SEPARATOR |
| 698 | } |
| 699 | |
| 700 | /// If digit separators are allowed between digits. |
| 701 | /// |
| 702 | /// This will not consider an input of only the digit separator |
| 703 | /// to be a valid separator: the digit separator must be surrounded by |
| 704 | /// digits. |
| 705 | /// |
| 706 | /// See [`internal_digit_separator`][Self::internal_digit_separator]. |
| 707 | pub const INTERNAL_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, INTERNAL_DIGIT_SEPARATOR); |
| 708 | |
| 709 | /// Get if digit separators are allowed between digits. |
| 710 | /// |
| 711 | /// This will not consider an input of only the digit separator |
| 712 | /// to be a valid separator: the digit separator must be surrounded by |
| 713 | /// digits. This is equivalent to any of [`integer_internal_digit_separator`], |
| 714 | /// [`fraction_internal_digit_separator`], or |
| 715 | /// [`exponent_internal_digit_separator`] being set. |
| 716 | /// |
| 717 | /// [`integer_internal_digit_separator`]: Self::integer_internal_digit_separator |
| 718 | /// [`fraction_internal_digit_separator`]: Self::fraction_internal_digit_separator |
| 719 | /// [`exponent_internal_digit_separator`]: Self::exponent_internal_digit_separator |
| 720 | #[inline(always)] |
| 721 | pub const fn internal_digit_separator(&self) -> bool { |
| 722 | Self::INTERNAL_DIGIT_SEPARATOR |
| 723 | } |
| 724 | |
| 725 | /// If a digit separator is allowed before any integer digits. |
| 726 | /// |
| 727 | /// This will consider an input of only the digit separator |
| 728 | /// to be a identical to empty input. |
| 729 | /// |
| 730 | /// See [`integer_leading_digit_separator`][Self::integer_leading_digit_separator]. |
| 731 | pub const INTEGER_LEADING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, INTEGER_LEADING_DIGIT_SEPARATOR); |
| 732 | |
| 733 | /// Get if a digit separator is allowed before any integer digits. |
| 734 | /// |
| 735 | /// This will consider an input of only the digit separator |
| 736 | /// to be a identical to empty input. Can only be modified with |
| 737 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 738 | /// |
| 739 | /// # Examples |
| 740 | /// |
| 741 | /// Using a digit separator of `_`. |
| 742 | /// |
| 743 | /// | Input | Valid? | |
| 744 | /// |:-:|:-:| |
| 745 | /// | `1` | ✔️ | |
| 746 | /// | `_` | ❌ | |
| 747 | /// | `1_1` | ❌ | |
| 748 | /// | `1_` | ❌ | |
| 749 | /// | `_1` | ✔️ | |
| 750 | /// |
| 751 | /// # Used For |
| 752 | /// |
| 753 | /// - Parse Float |
| 754 | /// - Parse Integer |
| 755 | #[inline(always)] |
| 756 | pub const fn integer_leading_digit_separator(&self) -> bool { |
| 757 | Self::INTEGER_LEADING_DIGIT_SEPARATOR |
| 758 | } |
| 759 | |
| 760 | /// If a digit separator is allowed before any integer digits. |
| 761 | /// |
| 762 | /// This will consider an input of only the digit separator |
| 763 | /// to be a identical to empty input. |
| 764 | /// |
| 765 | /// See [`fraction_leading_digit_separator`][Self::fraction_leading_digit_separator]. |
| 766 | pub const FRACTION_LEADING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, FRACTION_LEADING_DIGIT_SEPARATOR); |
| 767 | |
| 768 | /// Get if a digit separator is allowed before any fraction digits. |
| 769 | /// |
| 770 | /// This will consider an input of only the digit separator |
| 771 | /// to be a identical to empty input. Can only be modified with |
| 772 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 773 | /// |
| 774 | /// # Examples |
| 775 | /// |
| 776 | /// Using a digit separator of `_`. |
| 777 | /// |
| 778 | /// | Input | Valid? | |
| 779 | /// |:-:|:-:| |
| 780 | /// | `1.1` | ✔️ | |
| 781 | /// | `1._` | ❌ | |
| 782 | /// | `1.1_1` | ❌ | |
| 783 | /// | `1.1_` | ❌ | |
| 784 | /// | `1._1` | ✔️ | |
| 785 | /// |
| 786 | /// # Used For |
| 787 | /// |
| 788 | /// - Parse Float |
| 789 | #[inline(always)] |
| 790 | pub const fn fraction_leading_digit_separator(&self) -> bool { |
| 791 | Self::FRACTION_LEADING_DIGIT_SEPARATOR |
| 792 | } |
| 793 | |
| 794 | /// If a digit separator is allowed before any exponent digits. |
| 795 | /// |
| 796 | /// This will consider an input of only the digit separator |
| 797 | /// to be a identical to empty input. |
| 798 | /// |
| 799 | /// See [`exponent_leading_digit_separator`][Self::exponent_leading_digit_separator]. |
| 800 | pub const EXPONENT_LEADING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, EXPONENT_LEADING_DIGIT_SEPARATOR); |
| 801 | |
| 802 | /// Get if a digit separator is allowed before any exponent digits. |
| 803 | /// |
| 804 | /// This will consider an input of only the digit separator |
| 805 | /// to be a identical to empty input. Can only be modified with |
| 806 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 807 | /// |
| 808 | /// # Examples |
| 809 | /// |
| 810 | /// Using a digit separator of `_`. |
| 811 | /// |
| 812 | /// | Input | Valid? | |
| 813 | /// |:-:|:-:| |
| 814 | /// | `1.1e1` | ✔️ | |
| 815 | /// | `1.1e_` | ❌ | |
| 816 | /// | `1.1e1_1` | ❌ | |
| 817 | /// | `1.1e1_` | ❌ | |
| 818 | /// | `1.1e_1` | ✔️ | |
| 819 | /// |
| 820 | /// # Used For |
| 821 | /// |
| 822 | /// - Parse Float |
| 823 | #[inline(always)] |
| 824 | pub const fn exponent_leading_digit_separator(&self) -> bool { |
| 825 | Self::EXPONENT_LEADING_DIGIT_SEPARATOR |
| 826 | } |
| 827 | |
| 828 | /// If a digit separator is allowed before any digits. |
| 829 | /// |
| 830 | /// This will consider an input of only the digit separator |
| 831 | /// to be a identical to empty input. |
| 832 | /// |
| 833 | /// See [`leading_digit_separator`][Self::leading_digit_separator]. |
| 834 | pub const LEADING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, LEADING_DIGIT_SEPARATOR); |
| 835 | |
| 836 | /// Get if a digit separator is allowed before any digits. |
| 837 | /// |
| 838 | /// This will consider an input of only the digit separator |
| 839 | /// to be a identical to empty input. This is equivalent to |
| 840 | /// any of [`integer_leading_digit_separator`], |
| 841 | /// [`fraction_leading_digit_separator`], or |
| 842 | /// [`exponent_leading_digit_separator`] being set. |
| 843 | /// |
| 844 | /// [`integer_leading_digit_separator`]: Self::integer_leading_digit_separator |
| 845 | /// [`fraction_leading_digit_separator`]: Self::fraction_leading_digit_separator |
| 846 | /// [`exponent_leading_digit_separator`]: Self::exponent_leading_digit_separator |
| 847 | #[inline(always)] |
| 848 | pub const fn leading_digit_separator(&self) -> bool { |
| 849 | Self::LEADING_DIGIT_SEPARATOR |
| 850 | } |
| 851 | |
| 852 | /// If a digit separator is allowed after any integer digits. |
| 853 | /// |
| 854 | /// This will consider an input of only the digit separator |
| 855 | /// to be a identical to empty input. |
| 856 | /// |
| 857 | /// See [`integer_trailing_digit_separator`][Self::integer_trailing_digit_separator]. |
| 858 | pub const INTEGER_TRAILING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, INTEGER_TRAILING_DIGIT_SEPARATOR); |
| 859 | |
| 860 | /// Get if a digit separator is allowed after any integer digits. |
| 861 | /// |
| 862 | /// This will consider an input of only the digit separator |
| 863 | /// to be a identical to empty input. Can only be modified with |
| 864 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 865 | /// |
| 866 | /// # Examples |
| 867 | /// |
| 868 | /// Using a digit separator of `_`. |
| 869 | /// |
| 870 | /// | Input | Valid? | |
| 871 | /// |:-:|:-:| |
| 872 | /// | `1` | ✔️ | |
| 873 | /// | `_` | ❌ | |
| 874 | /// | `1_1` | ❌ | |
| 875 | /// | `1_` | ✔️ | |
| 876 | /// | `_1` | ❌ | |
| 877 | /// |
| 878 | /// # Used For |
| 879 | /// |
| 880 | /// - Parse Float |
| 881 | /// - Parse Integer |
| 882 | #[inline(always)] |
| 883 | pub const fn integer_trailing_digit_separator(&self) -> bool { |
| 884 | Self::INTEGER_TRAILING_DIGIT_SEPARATOR |
| 885 | } |
| 886 | |
| 887 | /// If a digit separator is allowed after any fraction digits. |
| 888 | /// |
| 889 | /// This will consider an input of only the digit separator |
| 890 | /// to be a identical to empty input. |
| 891 | /// |
| 892 | /// See [`fraction_trailing_digit_separator`][Self::fraction_trailing_digit_separator]. |
| 893 | pub const FRACTION_TRAILING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, FRACTION_TRAILING_DIGIT_SEPARATOR); |
| 894 | |
| 895 | /// Get if a digit separator is allowed after any fraction digits. |
| 896 | /// |
| 897 | /// This will consider an input of only the digit separator |
| 898 | /// to be a identical to empty input. Can only be modified with |
| 899 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. # Examples |
| 900 | /// |
| 901 | /// Using a digit separator of `_`. |
| 902 | /// |
| 903 | /// | Input | Valid? | |
| 904 | /// |:-:|:-:| |
| 905 | /// | `1.1` | ✔️ | |
| 906 | /// | `1._` | ❌ | |
| 907 | /// | `1.1_1` | ❌ | |
| 908 | /// | `1.1_` | ✔️ | |
| 909 | /// | `1._1` | ❌ | |
| 910 | /// |
| 911 | /// # Used For |
| 912 | /// |
| 913 | /// - Parse Float |
| 914 | #[inline(always)] |
| 915 | pub const fn fraction_trailing_digit_separator(&self) -> bool { |
| 916 | Self::FRACTION_TRAILING_DIGIT_SEPARATOR |
| 917 | } |
| 918 | |
| 919 | /// If a digit separator is allowed after any exponent digits. |
| 920 | /// |
| 921 | /// This will consider an input of only the digit separator |
| 922 | /// to be a identical to empty input. |
| 923 | /// |
| 924 | /// See [`exponent_trailing_digit_separator`][Self::exponent_trailing_digit_separator]. |
| 925 | pub const EXPONENT_TRAILING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, EXPONENT_TRAILING_DIGIT_SEPARATOR); |
| 926 | |
| 927 | /// Get if a digit separator is allowed after any exponent digits. |
| 928 | /// |
| 929 | /// This will consider an input of only the digit separator |
| 930 | /// to be a identical to empty input. Can only be modified with |
| 931 | /// [`feature`][crate#features] `format`. Defaults to [`false`]. |
| 932 | /// |
| 933 | /// # Examples |
| 934 | /// |
| 935 | /// Using a digit separator of `_`. |
| 936 | /// |
| 937 | /// | Input | Valid? | |
| 938 | /// |:-:|:-:| |
| 939 | /// | `1.1e1` | ✔️ | |
| 940 | /// | `1.1e_` | ❌ | |
| 941 | /// | `1.1e1_1` | ❌ | |
| 942 | /// | `1.1e1_` | ✔️ | |
| 943 | /// | `1.1e_1` | ❌ | |
| 944 | /// |
| 945 | /// # Used For |
| 946 | /// |
| 947 | /// - Parse Float |
| 948 | #[inline(always)] |
| 949 | pub const fn exponent_trailing_digit_separator(&self) -> bool { |
| 950 | Self::EXPONENT_TRAILING_DIGIT_SEPARATOR |
| 951 | } |
| 952 | |
| 953 | /// If a digit separator is allowed after any digits. |
| 954 | /// |
| 955 | /// This will consider an input of only the digit separator |
| 956 | /// to be a identical to empty input. |
| 957 | /// |
| 958 | /// See [`trailing_digit_separator`][Self::trailing_digit_separator]. |
| 959 | pub const TRAILING_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, TRAILING_DIGIT_SEPARATOR); |
| 960 | |
| 961 | /// Get if a digit separator is allowed after any digits. |
| 962 | /// |
| 963 | /// This will consider an input of only the digit separator |
| 964 | /// to be a identical to empty input. This is equivalent to |
| 965 | /// any of [`integer_trailing_digit_separator`], |
| 966 | /// [`fraction_trailing_digit_separator`], or |
| 967 | /// [`exponent_trailing_digit_separator`] being set. |
| 968 | /// |
| 969 | /// [`integer_trailing_digit_separator`]: Self::integer_trailing_digit_separator |
| 970 | /// [`fraction_trailing_digit_separator`]: Self::fraction_trailing_digit_separator |
| 971 | /// [`exponent_trailing_digit_separator`]: Self::exponent_trailing_digit_separator |
| 972 | #[inline(always)] |
| 973 | pub const fn trailing_digit_separator(&self) -> bool { |
| 974 | Self::TRAILING_DIGIT_SEPARATOR |
| 975 | } |
| 976 | |
| 977 | /// If multiple consecutive integer digit separators are allowed. |
| 978 | /// |
| 979 | /// See [`integer_consecutive_digit_separator`][Self::integer_consecutive_digit_separator]. |
| 980 | pub const INTEGER_CONSECUTIVE_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, INTEGER_CONSECUTIVE_DIGIT_SEPARATOR); |
| 981 | |
| 982 | /// Get if multiple consecutive integer digit separators are allowed. |
| 983 | /// |
| 984 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 985 | /// digit separators (leading, trailing, internal) are allowed in the |
| 986 | /// integer. Can only be modified with [`feature`][crate#features] `format`. |
| 987 | /// Defaults to [`false`]. |
| 988 | /// |
| 989 | /// # Used For |
| 990 | /// |
| 991 | /// - Parse Float |
| 992 | /// - Parse Integer |
| 993 | #[inline(always)] |
| 994 | pub const fn integer_consecutive_digit_separator(&self) -> bool { |
| 995 | Self::INTEGER_CONSECUTIVE_DIGIT_SEPARATOR |
| 996 | } |
| 997 | |
| 998 | /// If multiple consecutive fraction digit separators are allowed. |
| 999 | /// |
| 1000 | /// See [`fraction_consecutive_digit_separator`][Self::fraction_consecutive_digit_separator]. |
| 1001 | pub const FRACTION_CONSECUTIVE_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, FRACTION_CONSECUTIVE_DIGIT_SEPARATOR); |
| 1002 | |
| 1003 | /// Get if multiple consecutive fraction digit separators are allowed. |
| 1004 | /// |
| 1005 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 1006 | /// digit separators (leading, trailing, internal) are allowed in the |
| 1007 | /// fraction. Can only be modified with [`feature`][crate#features] |
| 1008 | /// `format`. Defaults to [`false`]. |
| 1009 | /// |
| 1010 | /// # Used For |
| 1011 | /// |
| 1012 | /// - Parse Float |
| 1013 | #[inline(always)] |
| 1014 | pub const fn fraction_consecutive_digit_separator(&self) -> bool { |
| 1015 | Self::FRACTION_CONSECUTIVE_DIGIT_SEPARATOR |
| 1016 | } |
| 1017 | |
| 1018 | /// If multiple consecutive exponent digit separators are allowed. |
| 1019 | /// |
| 1020 | /// See [`exponent_consecutive_digit_separator`][Self::exponent_consecutive_digit_separator]. |
| 1021 | pub const EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR); |
| 1022 | |
| 1023 | /// Get if multiple consecutive exponent digit separators are allowed. |
| 1024 | /// |
| 1025 | /// That is, using `_` as a digit separator `__` would be allowed where any |
| 1026 | /// digit separators (leading, trailing, internal) are allowed in the |
| 1027 | /// exponent. Can only be modified with [`feature`][crate#features] |
| 1028 | /// `format`. Defaults to [`false`]. |
| 1029 | /// |
| 1030 | /// # Used For |
| 1031 | /// |
| 1032 | /// - Parse Float |
| 1033 | #[inline(always)] |
| 1034 | pub const fn exponent_consecutive_digit_separator(&self) -> bool { |
| 1035 | Self::EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR |
| 1036 | } |
| 1037 | |
| 1038 | /// If multiple consecutive digit separators are allowed. |
| 1039 | /// |
| 1040 | /// See [`consecutive_digit_separator`][Self::consecutive_digit_separator]. |
| 1041 | pub const CONSECUTIVE_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, CONSECUTIVE_DIGIT_SEPARATOR); |
| 1042 | |
| 1043 | /// Get if multiple consecutive digit separators are allowed. |
| 1044 | /// |
| 1045 | /// This is equivalent to any of [`integer_consecutive_digit_separator`], |
| 1046 | /// [`fraction_consecutive_digit_separator`], or |
| 1047 | /// [`exponent_consecutive_digit_separator`] being set. |
| 1048 | /// |
| 1049 | /// [`integer_consecutive_digit_separator`]: Self::integer_consecutive_digit_separator |
| 1050 | /// [`fraction_consecutive_digit_separator`]: Self::fraction_consecutive_digit_separator |
| 1051 | /// [`exponent_consecutive_digit_separator`]: Self::exponent_consecutive_digit_separator |
| 1052 | #[inline(always)] |
| 1053 | pub const fn consecutive_digit_separator(&self) -> bool { |
| 1054 | Self::CONSECUTIVE_DIGIT_SEPARATOR |
| 1055 | } |
| 1056 | |
| 1057 | /// If any digit separators are allowed in special (non-finite) values. |
| 1058 | /// |
| 1059 | /// See [`special_digit_separator`][Self::special_digit_separator]. |
| 1060 | pub const SPECIAL_DIGIT_SEPARATOR: bool = from_flag!(FORMAT, SPECIAL_DIGIT_SEPARATOR); |
| 1061 | |
| 1062 | /// Get if any digit separators are allowed in special (non-finite) values. |
| 1063 | /// |
| 1064 | /// This enables leading, trailing, internal, and consecutive digit |
| 1065 | /// separators for any special floats: for example, `N__a_N_` is considered |
| 1066 | /// the same as `NaN`. Can only be modified with [`feature`][crate#features] |
| 1067 | /// `format`. Defaults to [`false`]. |
| 1068 | /// |
| 1069 | /// # Used For |
| 1070 | /// |
| 1071 | /// - Parse Float |
| 1072 | #[inline(always)] |
| 1073 | pub const fn special_digit_separator(&self) -> bool { |
| 1074 | Self::SPECIAL_DIGIT_SEPARATOR |
| 1075 | } |
| 1076 | |
| 1077 | // CHARACTERS |
| 1078 | |
| 1079 | /// The digit separator character in the packed struct. |
| 1080 | /// |
| 1081 | /// See [`digit_separator`][Self::digit_separator]. |
| 1082 | pub const DIGIT_SEPARATOR: u8 = flags::digit_separator(FORMAT); |
| 1083 | |
| 1084 | /// Get the digit separator for the number format. |
| 1085 | /// |
| 1086 | /// Digit separators are frequently used in number literals to group |
| 1087 | /// digits: `1,000,000` is a lot more readable than `1000000`, but |
| 1088 | /// the `,` characters should be ignored in the parsing of the number. |
| 1089 | /// |
| 1090 | /// Can only be modified with [`feature`][crate#features] `format`. Defaults |
| 1091 | /// to `0`, or no digit separators allowed. |
| 1092 | /// |
| 1093 | /// # Examples |
| 1094 | /// |
| 1095 | /// Using a digit separator of `_` (note that the validity |
| 1096 | /// oh where a digit separator can appear depends on the other digit |
| 1097 | /// separator flags). |
| 1098 | /// |
| 1099 | /// | Input | Valid? | |
| 1100 | /// |:-:|:-:| |
| 1101 | /// | `1` | ✔️ | |
| 1102 | /// | `1_4` | ✔️ | |
| 1103 | /// | `+_14` | ✔️ | |
| 1104 | /// | `+14e3_5` | ✔️ | |
| 1105 | /// | `1_d` | ❌ | |
| 1106 | /// |
| 1107 | /// # Used For |
| 1108 | /// |
| 1109 | /// - Parse Float |
| 1110 | /// - Parse Integer |
| 1111 | #[inline(always)] |
| 1112 | pub const fn digit_separator(&self) -> u8 { |
| 1113 | Self::DIGIT_SEPARATOR |
| 1114 | } |
| 1115 | |
| 1116 | /// Get if the format has a digit separator. |
| 1117 | #[inline(always)] |
| 1118 | pub const fn has_digit_separator(&self) -> bool { |
| 1119 | self.digit_separator() != 0 |
| 1120 | } |
| 1121 | |
| 1122 | /// The base prefix character in the packed struct. |
| 1123 | /// |
| 1124 | /// See [`base_prefix`][Self::base_prefix]. |
| 1125 | pub const BASE_PREFIX: u8 = flags::base_prefix(FORMAT); |
| 1126 | |
| 1127 | /// Get the optional character for the base prefix. |
| 1128 | /// |
| 1129 | /// This character will come after a leading zero, so for example |
| 1130 | /// setting the base prefix to `x` means that a leading `0x` will |
| 1131 | /// be ignore, if present. Can only be modified with |
| 1132 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 1133 | /// `format`. Defaults to `0`, or no base prefix allowed. |
| 1134 | /// |
| 1135 | /// # Examples |
| 1136 | /// |
| 1137 | /// Using a base prefix of `x`. |
| 1138 | /// |
| 1139 | /// | Input | Valid? | |
| 1140 | /// |:-:|:-:| |
| 1141 | /// | `0x1` | ✔️ | |
| 1142 | /// | `x1` | ❌ | |
| 1143 | /// | `1` | ✔️ | |
| 1144 | /// | `1x` | ❌ | |
| 1145 | /// | `1x1` | ❌ | |
| 1146 | /// |
| 1147 | /// # Used For |
| 1148 | /// |
| 1149 | /// - Parse Float |
| 1150 | /// - Parse Integer |
| 1151 | #[inline(always)] |
| 1152 | pub const fn base_prefix(&self) -> u8 { |
| 1153 | Self::BASE_PREFIX |
| 1154 | } |
| 1155 | |
| 1156 | /// Get if the format has a base suffix. |
| 1157 | #[inline(always)] |
| 1158 | pub const fn has_base_prefix(&self) -> bool { |
| 1159 | self.base_prefix() != 0 |
| 1160 | } |
| 1161 | |
| 1162 | /// The base suffix character in the packed struct. |
| 1163 | /// |
| 1164 | /// See [`base_suffix`][Self::base_suffix]. |
| 1165 | pub const BASE_SUFFIX: u8 = flags::base_suffix(FORMAT); |
| 1166 | |
| 1167 | /// Get the optional character for the base suffix. |
| 1168 | /// |
| 1169 | /// This character will at the end of the buffer, so for example |
| 1170 | /// setting the base prefix to `x` means that a trailing `x` will |
| 1171 | /// be ignored, if present. Can only be modified with |
| 1172 | /// [`feature`][crate#features] `power-of-two` or `radix` along with |
| 1173 | /// `format`. Defaults to `0`, or no base suffix allowed. |
| 1174 | /// |
| 1175 | /// # Examples |
| 1176 | /// |
| 1177 | /// Using a base suffix of `x`. |
| 1178 | /// |
| 1179 | /// | Input | Valid? | |
| 1180 | /// |:-:|:-:| |
| 1181 | /// | `1` | ✔️ | |
| 1182 | /// | `1x` | ✔️ | |
| 1183 | /// | `1d` | ❌ | |
| 1184 | /// |
| 1185 | /// # Used For |
| 1186 | /// |
| 1187 | /// - Parse Float |
| 1188 | /// - Parse Integer |
| 1189 | #[inline(always)] |
| 1190 | pub const fn base_suffix(&self) -> u8 { |
| 1191 | Self::BASE_SUFFIX |
| 1192 | } |
| 1193 | |
| 1194 | /// Get if the format has a base suffix. |
| 1195 | #[inline(always)] |
| 1196 | pub const fn has_base_suffix(&self) -> bool { |
| 1197 | self.base_suffix() != 0 |
| 1198 | } |
| 1199 | |
| 1200 | // RADIX |
| 1201 | |
| 1202 | /// The radix for the significant digits in the packed struct. |
| 1203 | /// |
| 1204 | /// See [`mantissa_radix`][Self::mantissa_radix]. |
| 1205 | pub const MANTISSA_RADIX: u32 = flags::mantissa_radix(FORMAT); |
| 1206 | |
| 1207 | /// Get the radix for mantissa digits. |
| 1208 | /// |
| 1209 | /// This is only used for the significant digits, that is, the integral and |
| 1210 | /// fractional components. Can only be modified with |
| 1211 | /// [`feature`][crate#features] `power-of-two` or `radix`. Defaults |
| 1212 | /// to `10`. |
| 1213 | /// |
| 1214 | /// | Radix | String | Number | |
| 1215 | /// |:-:|:-:|:-:| |
| 1216 | /// | 2 | "10011010010" | 1234 | |
| 1217 | /// | 3 | "1200201" | 1234 | |
| 1218 | /// | 8 | "2322" | 1234 | |
| 1219 | /// | 10 | "1234" | 1234 | |
| 1220 | /// | 16 | "4d2" | 1234 | |
| 1221 | /// | 31 | "18p" | 1234 | |
| 1222 | /// |
| 1223 | /// # Used For |
| 1224 | /// |
| 1225 | /// - Parse Float |
| 1226 | /// - Parse Integer |
| 1227 | /// - Write Float |
| 1228 | /// - Write Integer |
| 1229 | #[inline(always)] |
| 1230 | pub const fn mantissa_radix(&self) -> u32 { |
| 1231 | Self::MANTISSA_RADIX |
| 1232 | } |
| 1233 | |
| 1234 | /// The radix for the significant digits in the packed struct. |
| 1235 | /// |
| 1236 | /// Alias for [`MANTISSA_RADIX`][Self::MANTISSA_RADIX]. |
| 1237 | pub const RADIX: u32 = Self::MANTISSA_RADIX; |
| 1238 | |
| 1239 | /// Get the radix for the significant digits. |
| 1240 | /// |
| 1241 | /// This is an alias for [`mantissa_radix`][Self::mantissa_radix]. |
| 1242 | #[inline(always)] |
| 1243 | pub const fn radix(&self) -> u32 { |
| 1244 | Self::RADIX |
| 1245 | } |
| 1246 | |
| 1247 | /// Get the `radix^2` for the significant digits. |
| 1248 | #[inline(always)] |
| 1249 | pub const fn radix2(&self) -> u32 { |
| 1250 | self.radix().wrapping_mul(self.radix()) |
| 1251 | } |
| 1252 | |
| 1253 | /// Get the `radix^4` for the significant digits. |
| 1254 | #[inline(always)] |
| 1255 | pub const fn radix4(&self) -> u32 { |
| 1256 | self.radix2().wrapping_mul(self.radix2()) |
| 1257 | } |
| 1258 | |
| 1259 | /// Get the `radix^8` for the significant digits. |
| 1260 | #[inline(always)] |
| 1261 | pub const fn radix8(&self) -> u32 { |
| 1262 | // NOTE: radix >= 16 will overflow here but this has no security concerns |
| 1263 | self.radix4().wrapping_mul(self.radix4()) |
| 1264 | } |
| 1265 | |
| 1266 | /// The base for the exponent. |
| 1267 | /// |
| 1268 | /// See [`exponent_base`][Self::exponent_base]. |
| 1269 | pub const EXPONENT_BASE: u32 = flags::exponent_base(FORMAT); |
| 1270 | |
| 1271 | /// Get the radix for the exponent. |
| 1272 | /// |
| 1273 | /// For example, in `1.234e3`, it means `1.234 * 10^3`, and the exponent |
| 1274 | /// base here is 10. Some programming languages, like C, support hex floats |
| 1275 | /// with an exponent base of 2, for example `0x1.8p3`, or `1.5 * 2^3`. |
| 1276 | /// Defaults to `10`. Can only be modified with [`feature`][crate#features] |
| 1277 | /// `power-of-two` or `radix`. Defaults to `10`. |
| 1278 | /// |
| 1279 | /// # Used For |
| 1280 | /// |
| 1281 | /// - Parse Float |
| 1282 | /// - Parse Integer |
| 1283 | #[inline(always)] |
| 1284 | pub const fn exponent_base(&self) -> u32 { |
| 1285 | Self::EXPONENT_BASE |
| 1286 | } |
| 1287 | |
| 1288 | /// The radix for the exponent digits. |
| 1289 | /// |
| 1290 | /// See [`exponent_radix`][Self::exponent_radix]. |
| 1291 | pub const EXPONENT_RADIX: u32 = flags::exponent_radix(FORMAT); |
| 1292 | |
| 1293 | /// Get the radix for exponent digits. |
| 1294 | /// |
| 1295 | /// This is only used for the exponent digits. We assume the radix for the |
| 1296 | /// significant digits ([`mantissa_radix`][Self::mantissa_radix]) is |
| 1297 | /// 10 as is the exponent base. Defaults to `10`. Can only be modified with |
| 1298 | /// [`feature`][crate#features] `power-of-two` or `radix`. Defaults to `10`. |
| 1299 | /// |
| 1300 | /// | Radix | String | Number | |
| 1301 | /// |:-:|:-:|:-:| |
| 1302 | /// | 2 | "1.234^1100" | 1.234e9 | |
| 1303 | /// | 3 | "1.234^110" | 1.234e9 | |
| 1304 | /// | 8 | "1.234^14" | 1.234e9 | |
| 1305 | /// | 10 | "1.234^12" | 1.234e9 | |
| 1306 | /// | 16 | "1.234^c" | 1.234e9 | |
| 1307 | /// | 31 | "1.234^c" | 1.234e9 | |
| 1308 | /// |
| 1309 | /// # Used For |
| 1310 | /// |
| 1311 | /// - Parse Float |
| 1312 | /// - Parse Integer |
| 1313 | #[inline(always)] |
| 1314 | pub const fn exponent_radix(&self) -> u32 { |
| 1315 | Self::EXPONENT_RADIX |
| 1316 | } |
| 1317 | |
| 1318 | // FLAGS |
| 1319 | |
| 1320 | /// Get the flags from the number format. |
| 1321 | /// |
| 1322 | /// This contains all the non-character and non-radix values |
| 1323 | /// in the packed struct. |
| 1324 | #[inline(always)] |
| 1325 | pub const fn flags(&self) -> u128 { |
| 1326 | FORMAT & flags::FLAG_MASK |
| 1327 | } |
| 1328 | |
| 1329 | /// Get the interface flags from the number format. |
| 1330 | /// |
| 1331 | /// This contains all the flags that dictate code flows, and |
| 1332 | /// therefore excludes logic like case-sensitive characters. |
| 1333 | #[inline(always)] |
| 1334 | pub const fn interface_flags(&self) -> u128 { |
| 1335 | FORMAT & flags::INTERFACE_FLAG_MASK |
| 1336 | } |
| 1337 | |
| 1338 | /// Get the digit separator flags from the number format. |
| 1339 | #[inline(always)] |
| 1340 | pub const fn digit_separator_flags(&self) -> u128 { |
| 1341 | FORMAT & flags::DIGIT_SEPARATOR_FLAG_MASK |
| 1342 | } |
| 1343 | |
| 1344 | /// Get the exponent flags from the number format. |
| 1345 | /// |
| 1346 | /// This contains all the flags pertaining to exponent |
| 1347 | /// formats, including digit separators. |
| 1348 | #[inline(always)] |
| 1349 | pub const fn exponent_flags(&self) -> u128 { |
| 1350 | FORMAT & flags::EXPONENT_FLAG_MASK |
| 1351 | } |
| 1352 | |
| 1353 | /// Get the integer digit separator flags from the number format. |
| 1354 | #[inline(always)] |
| 1355 | pub const fn integer_digit_separator_flags(&self) -> u128 { |
| 1356 | FORMAT & flags::INTEGER_DIGIT_SEPARATOR_FLAG_MASK |
| 1357 | } |
| 1358 | |
| 1359 | /// Get the fraction digit separator flags from the number format. |
| 1360 | #[inline(always)] |
| 1361 | pub const fn fraction_digit_separator_flags(&self) -> u128 { |
| 1362 | FORMAT & flags::FRACTION_DIGIT_SEPARATOR_FLAG_MASK |
| 1363 | } |
| 1364 | |
| 1365 | /// Get the exponent digit separator flags from the number format. |
| 1366 | #[inline(always)] |
| 1367 | pub const fn exponent_digit_separator_flags(&self) -> u128 { |
| 1368 | FORMAT & flags::EXPONENT_DIGIT_SEPARATOR_FLAG_MASK |
| 1369 | } |
| 1370 | |
| 1371 | // BUILDER |
| 1372 | |
| 1373 | /// Get [`NumberFormatBuilder`] as a static function. |
| 1374 | #[inline(always)] |
| 1375 | pub const fn builder() -> NumberFormatBuilder { |
| 1376 | NumberFormatBuilder::new() |
| 1377 | } |
| 1378 | |
| 1379 | /// Create [`NumberFormatBuilder`] using existing values. |
| 1380 | #[inline(always)] |
| 1381 | pub const fn rebuild() -> NumberFormatBuilder { |
| 1382 | NumberFormatBuilder::rebuild(FORMAT) |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | impl<const FORMAT: u128> Default for NumberFormat<FORMAT> { |
| 1387 | fn default() -> Self { |
| 1388 | Self::new() |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | /// Get if the radix is valid. |
| 1393 | #[inline(always)] |
| 1394 | pub(crate) const fn radix_error_impl(format: u128) -> Error { |
| 1395 | if !flags::is_valid_radix(flags::mantissa_radix(format)) { |
| 1396 | Error::InvalidMantissaRadix |
| 1397 | } else if !flags::is_valid_radix(flags::exponent_base(format)) { |
| 1398 | Error::InvalidExponentBase |
| 1399 | } else if !flags::is_valid_radix(flags::exponent_radix(format)) { |
| 1400 | Error::InvalidExponentRadix |
| 1401 | } else { |
| 1402 | Error::Success |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /// Get the error type from the format. |
| 1407 | #[inline(always)] |
| 1408 | #[allow(clippy::if_same_then_else)] // reason="all are different logic conditions" |
| 1409 | pub(crate) const fn format_error_impl(format: u128) -> Error { |
| 1410 | if !flags::is_valid_radix(flags::mantissa_radix(format)) { |
| 1411 | Error::InvalidMantissaRadix |
| 1412 | } else if !flags::is_valid_radix(flags::exponent_base(format)) { |
| 1413 | Error::InvalidExponentBase |
| 1414 | } else if !flags::is_valid_radix(flags::exponent_radix(format)) { |
| 1415 | Error::InvalidExponentRadix |
| 1416 | } else if !flags::is_valid_digit_separator(format) { |
| 1417 | Error::InvalidDigitSeparator |
| 1418 | } else if !flags::is_valid_base_prefix(format) { |
| 1419 | Error::InvalidBasePrefix |
| 1420 | } else if !flags::is_valid_base_suffix(format) { |
| 1421 | Error::InvalidBaseSuffix |
| 1422 | } else if !flags::is_valid_punctuation(format) { |
| 1423 | Error::InvalidPunctuation |
| 1424 | } else if !flags::is_valid_exponent_flags(format) { |
| 1425 | Error::InvalidExponentFlags |
| 1426 | } else if from_flag!(format, NO_POSITIVE_MANTISSA_SIGN) |
| 1427 | && from_flag!(format, REQUIRED_MANTISSA_SIGN) |
| 1428 | { |
| 1429 | Error::InvalidMantissaSign |
| 1430 | } else if from_flag!(format, NO_POSITIVE_EXPONENT_SIGN) |
| 1431 | && from_flag!(format, REQUIRED_EXPONENT_SIGN) |
| 1432 | { |
| 1433 | Error::InvalidExponentSign |
| 1434 | } else if from_flag!(format, NO_SPECIAL) && from_flag!(format, CASE_SENSITIVE_SPECIAL) { |
| 1435 | Error::InvalidSpecial |
| 1436 | } else if from_flag!(format, NO_SPECIAL) && from_flag!(format, SPECIAL_DIGIT_SEPARATOR) { |
| 1437 | Error::InvalidSpecial |
| 1438 | } else if (format & flags::INTEGER_DIGIT_SEPARATOR_FLAG_MASK) |
| 1439 | == flags::INTEGER_CONSECUTIVE_DIGIT_SEPARATOR |
| 1440 | { |
| 1441 | Error::InvalidConsecutiveIntegerDigitSeparator |
| 1442 | } else if (format & flags::FRACTION_DIGIT_SEPARATOR_FLAG_MASK) |
| 1443 | == flags::FRACTION_CONSECUTIVE_DIGIT_SEPARATOR |
| 1444 | { |
| 1445 | Error::InvalidConsecutiveFractionDigitSeparator |
| 1446 | } else if (format & flags::EXPONENT_DIGIT_SEPARATOR_FLAG_MASK) |
| 1447 | == flags::EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR |
| 1448 | { |
| 1449 | Error::InvalidConsecutiveExponentDigitSeparator |
| 1450 | } else { |
| 1451 | Error::Success |
| 1452 | } |
| 1453 | } |