Skip to content

Commit 931493e

Browse files
committed
Accept more than just the standard rust literal suffixes in *Number::suffix
1 parent fc0354b commit 931493e

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

crates/syntax/src/ast/token_ext.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,6 @@ impl HasFormatSpecifier for ast::String {
534534
}
535535

536536
impl ast::IntNumber {
537-
const SUFFIXES: &'static [&'static str] = &[
538-
"u8", "u16", "u32", "u64", "u128", "usize", // Unsigned.
539-
"i8", "i16", "i32", "i64", "i128", "isize", // Signed.
540-
];
541-
542537
pub fn radix(&self) -> Radix {
543538
match self.text().get(..2).unwrap_or_default() {
544539
"0b" => Radix::Binary,
@@ -571,29 +566,30 @@ impl ast::IntNumber {
571566

572567
pub fn suffix(&self) -> Option<&str> {
573568
let text = self.text();
574-
// FIXME: don't check a fixed set of suffixes, `1_0_1_l_o_l` is valid
575-
// syntax, suffix is `l_o_l`.
576-
ast::IntNumber::SUFFIXES.iter().chain(ast::FloatNumber::SUFFIXES.iter()).find_map(
577-
|suffix| {
578-
if text.ends_with(suffix) {
579-
return Some(&text[text.len() - suffix.len()..]);
580-
}
581-
None
582-
},
583-
)
569+
let radix = self.radix();
570+
let mut indices = text.char_indices();
571+
if radix != Radix::Decimal {
572+
indices.next()?;
573+
indices.next()?;
574+
}
575+
let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
576+
Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
577+
_ => |(_, c)| c.is_ascii_alphabetic(),
578+
};
579+
let (suffix_start, _) = indices.find(is_suffix_start)?;
580+
Some(&text[suffix_start..])
584581
}
585582
}
586583

587584
impl ast::FloatNumber {
588-
const SUFFIXES: &'static [&'static str] = &["f32", "f64"];
589585
pub fn suffix(&self) -> Option<&str> {
590586
let text = self.text();
591-
ast::FloatNumber::SUFFIXES.iter().find_map(|suffix| {
592-
if text.ends_with(suffix) {
593-
return Some(&text[text.len() - suffix.len()..]);
594-
}
595-
None
596-
})
587+
let mut indices = text.char_indices();
588+
let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
589+
if c == 'e' || c == 'E' {
590+
suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
591+
}
592+
Some(&text[suffix_start..])
597593
}
598594
}
599595

0 commit comments

Comments
 (0)