1 use rustc_ast::ast::{Lit, LitFloatType, LitIntType, LitKind};
3 #[derive(Debug, PartialEq)]
12 /// Returns a reasonable digit group size for this radix.
14 fn suggest_grouping(&self) -> usize {
16 Self::Binary | Self::Hexadecimal => 4,
17 Self::Octal | Self::Decimal => 3,
22 /// A helper method to format numeric literals with digit grouping.
23 /// `lit` must be a valid numeric literal without suffix.
24 pub fn format(lit: &str, type_suffix: Option<&str>, float: bool) -> String {
25 NumericLiteral::new(lit, type_suffix, float).format()
29 pub struct NumericLiteral<'a> {
30 /// Which radix the literal was represented in.
32 /// The radix prefix, if present.
33 pub prefix: Option<&'a str>,
35 /// The integer part of the number.
37 /// The fraction part of the number.
38 pub fraction: Option<&'a str>,
39 /// The character used as exponent seperator (b'e' or b'E') and the exponent part.
40 pub exponent: Option<(char, &'a str)>,
42 /// The type suffix, including preceding underscore if present.
43 pub suffix: Option<&'a str>,
46 impl<'a> NumericLiteral<'a> {
47 pub fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
48 NumericLiteral::from_lit_kind(src, &lit.kind)
51 pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option<NumericLiteral<'a>> {
52 if lit_kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
53 let (unsuffixed, suffix) = split_suffix(&src, lit_kind);
54 let float = if let LitKind::Float(..) = lit_kind { true } else { false };
55 Some(NumericLiteral::new(unsuffixed, suffix, float))
62 pub fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
63 // Determine delimiter for radix prefix, if present, and radix.
64 let radix = if lit.starts_with("0x") {
66 } else if lit.starts_with("0b") {
68 } else if lit.starts_with("0o") {
74 // Grab part of the literal after prefix, if present.
75 let (prefix, mut sans_prefix) = if let Radix::Decimal = radix {
78 let (p, s) = lit.split_at(2);
82 if suffix.is_some() && sans_prefix.ends_with('_') {
83 // The '_' before the suffix isn't part of the digits
84 sans_prefix = &sans_prefix[..sans_prefix.len() - 1];
87 let (integer, fraction, exponent) = Self::split_digit_parts(sans_prefix, float);
99 pub fn is_decimal(&self) -> bool {
100 self.radix == Radix::Decimal
103 pub fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
104 let mut integer = digits;
105 let mut fraction = None;
106 let mut exponent = None;
109 for (i, c) in digits.char_indices() {
112 integer = &digits[..i];
113 fraction = Some(&digits[i + 1..]);
116 if integer.len() > i {
117 integer = &digits[..i];
119 fraction = Some(&digits[integer.len() + 1..i]);
121 exponent = Some((c, &digits[i + 1..]));
129 (integer, fraction, exponent)
132 /// Returns literal formatted in a sensible way.
133 pub fn format(&self) -> String {
134 let mut output = String::new();
136 if let Some(prefix) = self.prefix {
137 output.push_str(prefix);
140 let group_size = self.radix.suggest_grouping();
147 self.radix == Radix::Hexadecimal,
150 if let Some(fraction) = self.fraction {
152 Self::group_digits(&mut output, fraction, group_size, false, false);
155 if let Some((separator, exponent)) = self.exponent {
156 output.push(separator);
157 Self::group_digits(&mut output, exponent, group_size, true, false);
160 if let Some(suffix) = self.suffix {
162 output.push_str(suffix);
168 pub fn group_digits(output: &mut String, input: &str, group_size: usize, partial_group_first: bool, pad: bool) {
169 debug_assert!(group_size > 0);
171 let mut digits = input.chars().filter(|&c| c != '_');
173 let first_group_size;
175 if partial_group_first {
176 first_group_size = (digits.clone().count() - 1) % group_size + 1;
178 for _ in 0..group_size - first_group_size {
183 first_group_size = group_size;
186 for _ in 0..first_group_size {
187 if let Some(digit) = digits.next() {
192 for (c, i) in digits.zip((0..group_size).cycle()) {
201 fn split_suffix<'a>(src: &'a str, lit_kind: &LitKind) -> (&'a str, Option<&'a str>) {
202 debug_assert!(lit_kind.is_numeric());
203 if let Some(suffix_length) = lit_suffix_length(lit_kind) {
204 let (unsuffixed, suffix) = src.split_at(src.len() - suffix_length);
205 (unsuffixed, Some(suffix))
211 fn lit_suffix_length(lit_kind: &LitKind) -> Option<usize> {
212 debug_assert!(lit_kind.is_numeric());
213 let suffix = match lit_kind {
214 LitKind::Int(_, int_lit_kind) => match int_lit_kind {
215 LitIntType::Signed(int_ty) => Some(int_ty.name_str()),
216 LitIntType::Unsigned(uint_ty) => Some(uint_ty.name_str()),
217 LitIntType::Unsuffixed => None,
219 LitKind::Float(_, float_lit_kind) => match float_lit_kind {
220 LitFloatType::Suffixed(float_ty) => Some(float_ty.name_str()),
221 LitFloatType::Unsuffixed => None,