use core::num;
use crate::error::Error;
use crate::format_flags as flags;
#[doc(hidden)]
pub type OptionU8 = Option<num::NonZeroU8>;
macro_rules! add_flag {
($format:ident, $bool:expr, $flag:ident) => {
if $bool {
$format |= flags::$flag;
}
};
}
macro_rules! add_flags {
($format:ident ; $($bool:expr, $flag:ident ;)*) => {{
$(add_flag!($format, $bool, $flag);)*
}};
}
macro_rules! has_flag {
($format:ident, $flag:ident) => {
$format & flags::$flag != 0
};
}
#[inline(always)]
const fn unwrap_or_zero(option: OptionU8) -> u8 {
match option {
Some(x) => x.get(),
None => 0,
}
}
#[cfg_attr(
feature = "power-of-two",
doc = "\n
[`exponent_base`]: Self::exponent_base
[`exponent_radix`]: Self::exponent_radix
[`mantissa_radix`]: Self::mantissa_radix
"
)]
#[cfg_attr(
not(feature = "power-of-two"),
doc = "\n
[`exponent_base`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L602\n
[`exponent_radix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L610\n
[`mantissa_radix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L594\n
"
)]
#[cfg_attr(
feature = "format",
doc = "\n
[`digit_separator`]: Self::digit_separator\n
[`required_integer_digits`]: Self::required_integer_digits\n
[`required_fraction_digits`]: Self::required_fraction_digits\n
[`required_exponent_digits`]: Self::required_exponent_digits\n
[`required_mantissa_digits`]: Self::required_mantissa_digits\n
[`no_positive_mantissa_sign`]: Self::no_positive_mantissa_sign\n
[`required_mantissa_sign`]: Self::required_mantissa_sign\n
[`no_exponent_notation`]: Self::no_exponent_notation\n
[`no_positive_exponent_sign`]: Self::no_positive_exponent_sign\n
[`required_exponent_sign`]: Self::required_exponent_sign\n
[`no_exponent_without_fraction`]: Self::no_exponent_without_fraction\n
[`no_special`]: Self::no_special\n
[`case_sensitive_special`]: Self::case_sensitive_special\n
[`no_integer_leading_zeros`]: Self::no_integer_leading_zeros\n
[`no_float_leading_zeros`]: Self::no_float_leading_zeros\n
[`required_exponent_notation`]: Self::required_exponent_notation\n
[`case_sensitive_exponent`]: Self::case_sensitive_exponent\n
[`integer_internal_digit_separator`]: Self::integer_internal_digit_separator\n
[`fraction_internal_digit_separator`]: Self::fraction_internal_digit_separator\n
[`exponent_internal_digit_separator`]: Self::exponent_internal_digit_separator\n
[`integer_leading_digit_separator`]: Self::integer_leading_digit_separator\n
[`fraction_leading_digit_separator`]: Self::fraction_leading_digit_separator\n
[`exponent_leading_digit_separator`]: Self::exponent_leading_digit_separator\n
[`integer_trailing_digit_separator`]: Self::integer_trailing_digit_separator\n
[`fraction_trailing_digit_separator`]: Self::fraction_trailing_digit_separator\n
[`exponent_trailing_digit_separator`]: Self::exponent_trailing_digit_separator\n
[`integer_consecutive_digit_separator`]: Self::integer_consecutive_digit_separator\n
[`fraction_consecutive_digit_separator`]: Self::fraction_consecutive_digit_separator\n
[`special_digit_separator`]: Self::special_digit_separator\n
"
)]
#[cfg_attr(
not(feature = "format"),
doc = "\n
[`digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L579\n
[`required_integer_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L634\n
[`required_fraction_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L642\n
[`required_exponent_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L650\n
[`required_mantissa_digits`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L658\n
[`no_positive_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L677\n
[`required_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L685\n
[`no_exponent_notation`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L693\n
[`no_positive_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L701\n
[`required_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L709\n
[`no_exponent_without_fraction`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L717\n
[`no_special`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L725\n
[`case_sensitive_special`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L733\n
[`no_integer_leading_zeros`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L741\n
[`no_float_leading_zeros`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L749\n
[`required_exponent_notation`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L757\n
[`case_sensitive_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L765\n
[`integer_internal_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L793\n
[`fraction_internal_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L805\n
[`exponent_internal_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L817\n
[`integer_leading_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L842\n
[`fraction_leading_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L853\n
[`exponent_leading_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L864\n
[`integer_trailing_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L888\n
[`fraction_trailing_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L899\n
[`exponent_trailing_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L910\n
[`integer_consecutive_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L931\n
[`fraction_consecutive_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L939\n
[`special_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L965\n
"
)]
#[cfg_attr(
all(feature = "format", feature = "power-of-two"),
doc = "\n
[`base_prefix`]: Self::base_prefix
[`base_suffix`]: Self::base_suffix
[`case_sensitive_base_prefix`]: Self::case_sensitive_base_prefix
[`case_sensitive_base_suffix`]: Self::case_sensitive_base_suffix
"
)]
#[cfg_attr(
not(all(feature = "format", feature = "power-of-two")),
doc = "\n
[`base_prefix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L618\n
[`base_suffix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L626\n
[`case_sensitive_base_prefix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L773\n
[`case_sensitive_base_suffix`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L781\n
"
)]
pub struct NumberFormatBuilder {
digit_separator: OptionU8,
base_prefix: OptionU8,
base_suffix: OptionU8,
mantissa_radix: u8,
exponent_base: OptionU8,
exponent_radix: OptionU8,
required_integer_digits: bool,
required_fraction_digits: bool,
required_exponent_digits: bool,
required_mantissa_digits: bool,
no_positive_mantissa_sign: bool,
required_mantissa_sign: bool,
no_exponent_notation: bool,
no_positive_exponent_sign: bool,
required_exponent_sign: bool,
no_exponent_without_fraction: bool,
no_special: bool,
case_sensitive_special: bool,
no_integer_leading_zeros: bool,
no_float_leading_zeros: bool,
required_exponent_notation: bool,
case_sensitive_exponent: bool,
case_sensitive_base_prefix: bool,
case_sensitive_base_suffix: bool,
integer_internal_digit_separator: bool,
fraction_internal_digit_separator: bool,
exponent_internal_digit_separator: bool,
integer_leading_digit_separator: bool,
fraction_leading_digit_separator: bool,
exponent_leading_digit_separator: bool,
integer_trailing_digit_separator: bool,
fraction_trailing_digit_separator: bool,
exponent_trailing_digit_separator: bool,
integer_consecutive_digit_separator: bool,
fraction_consecutive_digit_separator: bool,
exponent_consecutive_digit_separator: bool,
special_digit_separator: bool,
}
impl NumberFormatBuilder {
#[inline(always)]
pub const fn new() -> Self {
Self {
digit_separator: None,
base_prefix: None,
base_suffix: None,
mantissa_radix: 10,
exponent_base: None,
exponent_radix: None,
required_integer_digits: false,
required_fraction_digits: false,
required_exponent_digits: true,
required_mantissa_digits: true,
no_positive_mantissa_sign: false,
required_mantissa_sign: false,
no_exponent_notation: false,
no_positive_exponent_sign: false,
required_exponent_sign: false,
no_exponent_without_fraction: false,
no_special: false,
case_sensitive_special: false,
no_integer_leading_zeros: false,
no_float_leading_zeros: false,
required_exponent_notation: false,
case_sensitive_exponent: false,
case_sensitive_base_prefix: false,
case_sensitive_base_suffix: false,
integer_internal_digit_separator: false,
fraction_internal_digit_separator: false,
exponent_internal_digit_separator: false,
integer_leading_digit_separator: false,
fraction_leading_digit_separator: false,
exponent_leading_digit_separator: false,
integer_trailing_digit_separator: false,
fraction_trailing_digit_separator: false,
exponent_trailing_digit_separator: false,
integer_consecutive_digit_separator: false,
fraction_consecutive_digit_separator: false,
exponent_consecutive_digit_separator: false,
special_digit_separator: false,
}
}
#[cfg(feature = "power-of-two")]
pub const fn binary() -> u128 {
Self::from_radix(2)
}
#[cfg(feature = "power-of-two")]
pub const fn octal() -> u128 {
Self::from_radix(8)
}
pub const fn decimal() -> u128 {
let mut builder = Self::new();
builder.mantissa_radix = 10;
builder.exponent_base = num::NonZeroU8::new(10);
builder.exponent_radix = num::NonZeroU8::new(10);
builder.build_strict()
}
#[cfg(feature = "power-of-two")]
pub const fn hexadecimal() -> u128 {
Self::from_radix(16)
}
#[allow(deprecated)]
#[cfg(feature = "power-of-two")]
pub const fn from_radix(radix: u8) -> u128 {
Self::new()
.radix(radix)
.exponent_base(num::NonZeroU8::new(radix))
.exponent_radix(num::NonZeroU8::new(radix))
.build()
}
#[inline(always)]
pub const fn get_digit_separator(&self) -> OptionU8 {
self.digit_separator
}
#[inline(always)]
pub const fn get_mantissa_radix(&self) -> u8 {
self.mantissa_radix
}
#[inline(always)]
pub const fn get_exponent_base(&self) -> OptionU8 {
self.exponent_base
}
#[inline(always)]
pub const fn get_exponent_radix(&self) -> OptionU8 {
self.exponent_radix
}
#[inline(always)]
pub const fn get_base_prefix(&self) -> OptionU8 {
self.base_prefix
}
#[inline(always)]
pub const fn get_base_suffix(&self) -> OptionU8 {
self.base_suffix
}
#[inline(always)]
pub const fn get_required_integer_digits(&self) -> bool {
self.required_integer_digits
}
#[inline(always)]
pub const fn get_required_fraction_digits(&self) -> bool {
self.required_fraction_digits
}
#[inline(always)]
pub const fn get_required_exponent_digits(&self) -> bool {
self.required_exponent_digits
}
#[inline(always)]
pub const fn get_required_mantissa_digits(&self) -> bool {
self.required_mantissa_digits
}
#[inline(always)]
pub const fn get_no_positive_mantissa_sign(&self) -> bool {
self.no_positive_mantissa_sign
}
#[inline(always)]
pub const fn get_required_mantissa_sign(&self) -> bool {
self.required_mantissa_sign
}
#[inline(always)]
pub const fn get_no_exponent_notation(&self) -> bool {
self.no_exponent_notation
}
#[inline(always)]
pub const fn get_no_positive_exponent_sign(&self) -> bool {
self.no_positive_exponent_sign
}
#[inline(always)]
pub const fn get_required_exponent_sign(&self) -> bool {
self.required_exponent_sign
}
#[inline(always)]
pub const fn get_no_exponent_without_fraction(&self) -> bool {
self.no_exponent_without_fraction
}
#[inline(always)]
pub const fn get_no_special(&self) -> bool {
self.no_special
}
#[inline(always)]
pub const fn get_case_sensitive_special(&self) -> bool {
self.case_sensitive_special
}
#[inline(always)]
pub const fn get_no_integer_leading_zeros(&self) -> bool {
self.no_integer_leading_zeros
}
#[inline(always)]
pub const fn get_no_float_leading_zeros(&self) -> bool {
self.no_float_leading_zeros
}
#[inline(always)]
pub const fn get_required_exponent_notation(&self) -> bool {
self.required_exponent_notation
}
#[inline(always)]
pub const fn get_case_sensitive_exponent(&self) -> bool {
self.case_sensitive_exponent
}
#[inline(always)]
pub const fn get_case_sensitive_base_prefix(&self) -> bool {
self.case_sensitive_base_prefix
}
#[inline(always)]
pub const fn get_case_sensitive_base_suffix(&self) -> bool {
self.case_sensitive_base_suffix
}
#[inline(always)]
pub const fn get_integer_internal_digit_separator(&self) -> bool {
self.integer_internal_digit_separator
}
#[inline(always)]
pub const fn get_fraction_internal_digit_separator(&self) -> bool {
self.fraction_internal_digit_separator
}
#[inline(always)]
pub const fn get_exponent_internal_digit_separator(&self) -> bool {
self.exponent_internal_digit_separator
}
#[inline(always)]
pub const fn get_integer_leading_digit_separator(&self) -> bool {
self.integer_leading_digit_separator
}
#[inline(always)]
pub const fn get_fraction_leading_digit_separator(&self) -> bool {
self.fraction_leading_digit_separator
}
#[inline(always)]
pub const fn get_exponent_leading_digit_separator(&self) -> bool {
self.exponent_leading_digit_separator
}
#[inline(always)]
pub const fn get_integer_trailing_digit_separator(&self) -> bool {
self.integer_trailing_digit_separator
}
#[inline(always)]
pub const fn get_fraction_trailing_digit_separator(&self) -> bool {
self.fraction_trailing_digit_separator
}
#[inline(always)]
pub const fn get_exponent_trailing_digit_separator(&self) -> bool {
self.exponent_trailing_digit_separator
}
#[inline(always)]
pub const fn get_integer_consecutive_digit_separator(&self) -> bool {
self.integer_consecutive_digit_separator
}
#[inline(always)]
pub const fn get_fraction_consecutive_digit_separator(&self) -> bool {
self.fraction_consecutive_digit_separator
}
#[inline(always)]
pub const fn get_exponent_consecutive_digit_separator(&self) -> bool {
self.exponent_consecutive_digit_separator
}
#[inline(always)]
pub const fn get_special_digit_separator(&self) -> bool {
self.special_digit_separator
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn digit_separator(mut self, character: OptionU8) -> Self {
self.digit_separator = character;
self
}
#[inline(always)]
#[cfg(feature = "power-of-two")]
pub const fn radix(self, radix: u8) -> Self {
self.mantissa_radix(radix)
}
#[inline(always)]
#[cfg(feature = "power-of-two")]
pub const fn mantissa_radix(mut self, radix: u8) -> Self {
self.mantissa_radix = radix;
self
}
#[inline(always)]
#[cfg(feature = "power-of-two")]
pub const fn exponent_base(mut self, base: OptionU8) -> Self {
self.exponent_base = base;
self
}
#[inline(always)]
#[cfg(feature = "power-of-two")]
pub const fn exponent_radix(mut self, radix: OptionU8) -> Self {
self.exponent_radix = radix;
self
}
#[inline(always)]
#[cfg(all(feature = "power-of-two", feature = "format"))]
pub const fn base_prefix(mut self, base_prefix: OptionU8) -> Self {
self.base_prefix = base_prefix;
self
}
#[inline(always)]
#[cfg(all(feature = "power-of-two", feature = "format"))]
pub const fn base_suffix(mut self, base_suffix: OptionU8) -> Self {
self.base_suffix = base_suffix;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_integer_digits(mut self, flag: bool) -> Self {
self.required_integer_digits = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_fraction_digits(mut self, flag: bool) -> Self {
self.required_fraction_digits = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_exponent_digits(mut self, flag: bool) -> Self {
self.required_exponent_digits = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_mantissa_digits(mut self, flag: bool) -> Self {
self.required_mantissa_digits = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_digits(mut self, flag: bool) -> Self {
self = self.required_integer_digits(flag);
self = self.required_fraction_digits(flag);
self = self.required_exponent_digits(flag);
self = self.required_mantissa_digits(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn no_positive_mantissa_sign(mut self, flag: bool) -> Self {
self.no_positive_mantissa_sign = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_mantissa_sign(mut self, flag: bool) -> Self {
self.required_mantissa_sign = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn no_exponent_notation(mut self, flag: bool) -> Self {
self.no_exponent_notation = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn no_positive_exponent_sign(mut self, flag: bool) -> Self {
self.no_positive_exponent_sign = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_exponent_sign(mut self, flag: bool) -> Self {
self.required_exponent_sign = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn no_exponent_without_fraction(mut self, flag: bool) -> Self {
self.no_exponent_without_fraction = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn no_special(mut self, flag: bool) -> Self {
self.no_special = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn case_sensitive_special(mut self, flag: bool) -> Self {
self.case_sensitive_special = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn no_integer_leading_zeros(mut self, flag: bool) -> Self {
self.no_integer_leading_zeros = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn no_float_leading_zeros(mut self, flag: bool) -> Self {
self.no_float_leading_zeros = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn required_exponent_notation(mut self, flag: bool) -> Self {
self.required_exponent_notation = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn case_sensitive_exponent(mut self, flag: bool) -> Self {
self.case_sensitive_exponent = flag;
self
}
#[inline(always)]
#[cfg(all(feature = "power-of-two", feature = "format"))]
pub const fn case_sensitive_base_prefix(mut self, flag: bool) -> Self {
self.case_sensitive_base_prefix = flag;
self
}
#[inline(always)]
#[cfg(all(feature = "power-of-two", feature = "format"))]
pub const fn case_sensitive_base_suffix(mut self, flag: bool) -> Self {
self.case_sensitive_base_suffix = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn integer_internal_digit_separator(mut self, flag: bool) -> Self {
self.integer_internal_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn fraction_internal_digit_separator(mut self, flag: bool) -> Self {
self.fraction_internal_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn exponent_internal_digit_separator(mut self, flag: bool) -> Self {
self.exponent_internal_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn internal_digit_separator(mut self, flag: bool) -> Self {
self = self.integer_internal_digit_separator(flag);
self = self.fraction_internal_digit_separator(flag);
self = self.exponent_internal_digit_separator(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn integer_leading_digit_separator(mut self, flag: bool) -> Self {
self.integer_leading_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn fraction_leading_digit_separator(mut self, flag: bool) -> Self {
self.fraction_leading_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn exponent_leading_digit_separator(mut self, flag: bool) -> Self {
self.exponent_leading_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn leading_digit_separator(mut self, flag: bool) -> Self {
self = self.integer_leading_digit_separator(flag);
self = self.fraction_leading_digit_separator(flag);
self = self.exponent_leading_digit_separator(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn integer_trailing_digit_separator(mut self, flag: bool) -> Self {
self.integer_trailing_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn fraction_trailing_digit_separator(mut self, flag: bool) -> Self {
self.fraction_trailing_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn exponent_trailing_digit_separator(mut self, flag: bool) -> Self {
self.exponent_trailing_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn trailing_digit_separator(mut self, flag: bool) -> Self {
self = self.integer_trailing_digit_separator(flag);
self = self.fraction_trailing_digit_separator(flag);
self = self.exponent_trailing_digit_separator(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn integer_consecutive_digit_separator(mut self, flag: bool) -> Self {
self.integer_consecutive_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn fraction_consecutive_digit_separator(mut self, flag: bool) -> Self {
self.fraction_consecutive_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn exponent_consecutive_digit_separator(mut self, flag: bool) -> Self {
self.exponent_consecutive_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn consecutive_digit_separator(mut self, flag: bool) -> Self {
self = self.integer_consecutive_digit_separator(flag);
self = self.fraction_consecutive_digit_separator(flag);
self = self.exponent_consecutive_digit_separator(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn special_digit_separator(mut self, flag: bool) -> Self {
self.special_digit_separator = flag;
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn digit_separator_flags(mut self, flag: bool) -> Self {
self = self.integer_digit_separator_flags(flag);
self = self.fraction_digit_separator_flags(flag);
self = self.exponent_digit_separator_flags(flag);
self = self.special_digit_separator(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn integer_digit_separator_flags(mut self, flag: bool) -> Self {
self = self.integer_internal_digit_separator(flag);
self = self.integer_leading_digit_separator(flag);
self = self.integer_trailing_digit_separator(flag);
self = self.integer_consecutive_digit_separator(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn fraction_digit_separator_flags(mut self, flag: bool) -> Self {
self = self.fraction_internal_digit_separator(flag);
self = self.fraction_leading_digit_separator(flag);
self = self.fraction_trailing_digit_separator(flag);
self = self.fraction_consecutive_digit_separator(flag);
self
}
#[inline(always)]
#[cfg(feature = "format")]
pub const fn exponent_digit_separator_flags(mut self, flag: bool) -> Self {
self = self.exponent_internal_digit_separator(flag);
self = self.exponent_leading_digit_separator(flag);
self = self.exponent_trailing_digit_separator(flag);
self = self.exponent_consecutive_digit_separator(flag);
self
}
#[inline(always)]
pub const fn build_unchecked(&self) -> u128 {
let mut format: u128 = 0;
add_flags!(
format ;
self.required_integer_digits, REQUIRED_INTEGER_DIGITS ;
self.required_fraction_digits, REQUIRED_FRACTION_DIGITS ;
self.required_exponent_digits, REQUIRED_EXPONENT_DIGITS ;
self.required_mantissa_digits, REQUIRED_MANTISSA_DIGITS ;
self.no_positive_mantissa_sign, NO_POSITIVE_MANTISSA_SIGN ;
self.required_mantissa_sign, REQUIRED_MANTISSA_SIGN ;
self.no_exponent_notation, NO_EXPONENT_NOTATION ;
self.no_positive_exponent_sign, NO_POSITIVE_EXPONENT_SIGN ;
self.required_exponent_sign, REQUIRED_EXPONENT_SIGN ;
self.no_exponent_without_fraction, NO_EXPONENT_WITHOUT_FRACTION ;
self.no_special, NO_SPECIAL ;
self.case_sensitive_special, CASE_SENSITIVE_SPECIAL ;
self.no_integer_leading_zeros, NO_INTEGER_LEADING_ZEROS ;
self.no_float_leading_zeros, NO_FLOAT_LEADING_ZEROS ;
self.required_exponent_notation, REQUIRED_EXPONENT_NOTATION ;
self.case_sensitive_exponent, CASE_SENSITIVE_EXPONENT ;
self.case_sensitive_base_prefix, CASE_SENSITIVE_BASE_PREFIX ;
self.case_sensitive_base_suffix, CASE_SENSITIVE_BASE_SUFFIX ;
self.integer_internal_digit_separator, INTEGER_INTERNAL_DIGIT_SEPARATOR ;
self.fraction_internal_digit_separator, FRACTION_INTERNAL_DIGIT_SEPARATOR ;
self.exponent_internal_digit_separator, EXPONENT_INTERNAL_DIGIT_SEPARATOR ;
self.integer_leading_digit_separator, INTEGER_LEADING_DIGIT_SEPARATOR ;
self.fraction_leading_digit_separator, FRACTION_LEADING_DIGIT_SEPARATOR ;
self.exponent_leading_digit_separator, EXPONENT_LEADING_DIGIT_SEPARATOR ;
self.integer_trailing_digit_separator, INTEGER_TRAILING_DIGIT_SEPARATOR ;
self.fraction_trailing_digit_separator, FRACTION_TRAILING_DIGIT_SEPARATOR ;
self.exponent_trailing_digit_separator, EXPONENT_TRAILING_DIGIT_SEPARATOR ;
self.integer_consecutive_digit_separator, INTEGER_CONSECUTIVE_DIGIT_SEPARATOR ;
self.fraction_consecutive_digit_separator, FRACTION_CONSECUTIVE_DIGIT_SEPARATOR ;
self.exponent_consecutive_digit_separator, EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR ;
self.special_digit_separator, SPECIAL_DIGIT_SEPARATOR ;
);
if format & flags::DIGIT_SEPARATOR_FLAG_MASK != 0 {
format |=
(unwrap_or_zero(self.digit_separator) as u128) << flags::DIGIT_SEPARATOR_SHIFT;
}
format |= (unwrap_or_zero(self.base_prefix) as u128) << flags::BASE_PREFIX_SHIFT;
format |= (unwrap_or_zero(self.base_suffix) as u128) << flags::BASE_SUFFIX_SHIFT;
format |= (self.mantissa_radix as u128) << flags::MANTISSA_RADIX_SHIFT;
format |= (unwrap_or_zero(self.exponent_base) as u128) << flags::EXPONENT_BASE_SHIFT;
format |= (unwrap_or_zero(self.exponent_radix) as u128) << flags::EXPONENT_RADIX_SHIFT;
format
}
#[inline(always)]
pub const fn build_strict(&self) -> u128 {
use crate::format::format_error_impl;
let packed = self.build_unchecked();
match format_error_impl(packed) {
Error::Success => packed,
error => core::panic!("{}", error.description()),
}
}
#[inline(always)]
#[deprecated = "Use `build_strict` or `build_unchecked` instead."]
pub const fn build(&self) -> u128 {
self.build_unchecked()
}
#[inline(always)]
pub const fn rebuild(format: u128) -> Self {
NumberFormatBuilder {
digit_separator: num::NonZeroU8::new(flags::digit_separator(format)),
base_prefix: num::NonZeroU8::new(flags::base_prefix(format)),
base_suffix: num::NonZeroU8::new(flags::base_suffix(format)),
mantissa_radix: flags::mantissa_radix(format) as u8,
exponent_base: num::NonZeroU8::new(flags::exponent_base(format) as u8),
exponent_radix: num::NonZeroU8::new(flags::exponent_radix(format) as u8),
required_integer_digits: has_flag!(format, REQUIRED_INTEGER_DIGITS),
required_fraction_digits: has_flag!(format, REQUIRED_FRACTION_DIGITS),
required_exponent_digits: has_flag!(format, REQUIRED_EXPONENT_DIGITS),
required_mantissa_digits: has_flag!(format, REQUIRED_MANTISSA_DIGITS),
no_positive_mantissa_sign: has_flag!(format, NO_POSITIVE_MANTISSA_SIGN),
required_mantissa_sign: has_flag!(format, REQUIRED_MANTISSA_SIGN),
no_exponent_notation: has_flag!(format, NO_EXPONENT_NOTATION),
no_positive_exponent_sign: has_flag!(format, NO_POSITIVE_EXPONENT_SIGN),
required_exponent_sign: has_flag!(format, REQUIRED_EXPONENT_SIGN),
no_exponent_without_fraction: has_flag!(format, NO_EXPONENT_WITHOUT_FRACTION),
no_special: has_flag!(format, NO_SPECIAL),
case_sensitive_special: has_flag!(format, CASE_SENSITIVE_SPECIAL),
no_integer_leading_zeros: has_flag!(format, NO_INTEGER_LEADING_ZEROS),
no_float_leading_zeros: has_flag!(format, NO_FLOAT_LEADING_ZEROS),
required_exponent_notation: has_flag!(format, REQUIRED_EXPONENT_NOTATION),
case_sensitive_exponent: has_flag!(format, CASE_SENSITIVE_EXPONENT),
case_sensitive_base_prefix: has_flag!(format, CASE_SENSITIVE_BASE_PREFIX),
case_sensitive_base_suffix: has_flag!(format, CASE_SENSITIVE_BASE_SUFFIX),
integer_internal_digit_separator: has_flag!(format, INTEGER_INTERNAL_DIGIT_SEPARATOR),
fraction_internal_digit_separator: has_flag!(format, FRACTION_INTERNAL_DIGIT_SEPARATOR),
exponent_internal_digit_separator: has_flag!(format, EXPONENT_INTERNAL_DIGIT_SEPARATOR),
integer_leading_digit_separator: has_flag!(format, INTEGER_LEADING_DIGIT_SEPARATOR),
fraction_leading_digit_separator: has_flag!(format, FRACTION_LEADING_DIGIT_SEPARATOR),
exponent_leading_digit_separator: has_flag!(format, EXPONENT_LEADING_DIGIT_SEPARATOR),
integer_trailing_digit_separator: has_flag!(format, INTEGER_TRAILING_DIGIT_SEPARATOR),
fraction_trailing_digit_separator: has_flag!(format, FRACTION_TRAILING_DIGIT_SEPARATOR),
exponent_trailing_digit_separator: has_flag!(format, EXPONENT_TRAILING_DIGIT_SEPARATOR),
integer_consecutive_digit_separator: has_flag!(
format,
INTEGER_CONSECUTIVE_DIGIT_SEPARATOR
),
fraction_consecutive_digit_separator: has_flag!(
format,
FRACTION_CONSECUTIVE_DIGIT_SEPARATOR
),
exponent_consecutive_digit_separator: has_flag!(
format,
EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR
),
special_digit_separator: has_flag!(format, SPECIAL_DIGIT_SEPARATOR),
}
}
}
impl Default for NumberFormatBuilder {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}