macros.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //! Macros for big integer literals.
  2. /// Create a [UBig](crate::UBig) value.
  3. ///
  4. /// Usually just pass use a numeric literal. This works for bases 2, 8, 10 or 16 using standard
  5. /// prefixes:
  6. /// ```
  7. /// # use ibig::ubig;
  8. /// let a = ubig!(100);
  9. /// let b = ubig!(0b101);
  10. /// let c = ubig!(0o202);
  11. /// let d = ubig!(0x2ff);
  12. /// ```
  13. ///
  14. /// For an arbitrary base, add `base N`:
  15. /// ```
  16. /// # use ibig::ubig;
  17. /// let e = ubig!(a3gp1 base 32);
  18. /// ```
  19. ///
  20. /// If the sequence of digits is not a valid Rust literal or identifier, put an underscore before
  21. /// the digits. This may be necessary when:
  22. /// * There are a lot of digits. Rust will fail to compile without an underscore if the number
  23. /// wouldn't fit in `u128`.
  24. /// * The first digit is decimal, but not all digits are decimal.
  25. /// ```
  26. /// # use ibig::ubig;
  27. /// let f = ubig!(_314159265358979323846264338327950288419716939937);
  28. /// let g = ubig!(_0b102 base 32);
  29. /// let h = ubig!(b102 base 32);
  30. /// assert_eq!(g, h);
  31. /// let i = ubig!(_100ef base 32);
  32. /// ```
  33. #[macro_export]
  34. macro_rules! ubig {
  35. ($val:ident) => {{
  36. let s = ::core::stringify!($val);
  37. let s = ::core::option::Option::unwrap_or(::core::primitive::str::strip_prefix(s, "_"), s);
  38. ::core::result::Result::expect(
  39. $crate::UBig::from_str_with_radix_prefix(s),
  40. "invalid number",
  41. )
  42. }};
  43. ($val:ident base $radix:literal) => {{
  44. let s = ::core::stringify!($val);
  45. let s = ::core::option::Option::unwrap_or(::core::primitive::str::strip_prefix(s, "_"), s);
  46. ::core::result::Result::expect($crate::UBig::from_str_radix(s, $radix), "invalid number")
  47. }};
  48. ($val:literal) => {{
  49. let val: ::core::primitive::u128 = $val;
  50. <$crate::UBig as ::core::convert::From<::core::primitive::u128>>::from(val)
  51. }};
  52. ($val:literal base $radix:literal) => {{
  53. let s = ::core::stringify!($val);
  54. let s = ::core::option::Option::unwrap_or(::core::primitive::str::strip_prefix(s, "_"), s);
  55. ::core::result::Result::expect($crate::UBig::from_str_radix(s, $radix), "invalid number")
  56. }};
  57. }
  58. /// Create an [IBig](crate::IBig) value.
  59. ///
  60. /// Usually just pass use a numeric literal. This works for bases 2, 8, 10 or 16 using standard
  61. /// prefixes:
  62. /// ```
  63. /// # use ibig::ibig;
  64. /// let a = ibig!(100);
  65. /// let b = ibig!(-0b101);
  66. /// let c = ibig!(0o202);
  67. /// let d = ibig!(-0x2ff);
  68. /// ```
  69. ///
  70. /// For an arbitrary base, add `base N`:
  71. /// ```
  72. /// # use ibig::ibig;
  73. /// let e = ibig!(-a3gp1 base 32);
  74. /// ```
  75. ///
  76. /// If the sequence of digits is not a valid Rust literal or identifier, put an underscore before
  77. /// the digits. This may be necessary when:
  78. /// * There are a lot of digits. Rust will fail to compile without an underscore if the number
  79. /// wouldn't fit in `u128`.
  80. /// * The first digit is decimal, but not all digits are decimal.
  81. /// ```
  82. /// # use ibig::ibig;
  83. /// let f = ibig!(-_314159265358979323846264338327950288419716939937);
  84. /// let g = ibig!(_0b102 base 32);
  85. /// let h = ibig!(b102 base 32);
  86. /// assert_eq!(g, h);
  87. /// let i = ibig!(-_100ef base 32);
  88. /// ```
  89. #[macro_export]
  90. macro_rules! ibig {
  91. (- $val:ident) => {
  92. - <$crate::IBig as ::core::convert::From<$crate::UBig>>::from($crate::ubig!($val))
  93. };
  94. (- $val:ident base $radix:literal) => {
  95. - <$crate::IBig as ::core::convert::From<$crate::UBig>>::from(
  96. $crate::ubig!($val base $radix)
  97. )
  98. };
  99. (- $val:literal) => {
  100. - <$crate::IBig as ::core::convert::From<$crate::UBig>>::from($crate::ubig!($val))
  101. };
  102. (- $val:literal base $radix:literal) => {
  103. - <$crate::IBig as ::core::convert::From<$crate::UBig>>::from(
  104. $crate::ubig!($val base $radix)
  105. )
  106. };
  107. ($val:ident) => {
  108. <$crate::IBig as ::core::convert::From<$crate::UBig>>::from($crate::ubig!($val))
  109. };
  110. ($val:ident base $radix:literal) => {
  111. <$crate::IBig as ::core::convert::From<$crate::UBig>>::from(
  112. $crate::ubig!($val base $radix)
  113. )
  114. };
  115. ($val:literal) => {
  116. <$crate::IBig as ::core::convert::From<$crate::UBig>>::from($crate::ubig!($val))
  117. };
  118. ($val:literal base $radix:literal) => {
  119. <$crate::IBig as ::core::convert::From<$crate::UBig>>::from(
  120. $crate::ubig!($val base $radix)
  121. )
  122. };
  123. }