Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/ast/token_ext.rs')
-rw-r--r--crates/syntax/src/ast/token_ext.rs49
1 files changed, 23 insertions, 26 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index 1fb7f158f2..c32ab686cc 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -623,41 +623,38 @@ impl ast::IntNumber {
}
}
- pub fn value(&self) -> Option<u128> {
- let token = self.syntax();
-
- let mut text = token.text();
- if let Some(suffix) = self.suffix() {
- text = &text[..text.len() - suffix.len()];
- }
-
+ pub fn split_into_parts(&self) -> (&str, &str, &str) {
let radix = self.radix();
- text = &text[radix.prefix_len()..];
+ let (prefix, mut text) = self.text().split_at(radix.prefix_len());
+
+ let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
+ Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
+ _ => |(_, c)| c.is_ascii_alphabetic(),
+ };
- let buf;
- if text.contains('_') {
- buf = text.replace('_', "");
- text = buf.as_str();
+ let mut suffix = "";
+ if let Some((suffix_start, _)) = text.char_indices().find(is_suffix_start) {
+ let (text2, suffix2) = text.split_at(suffix_start);
+ text = text2;
+ suffix = suffix2;
};
- let value = u128::from_str_radix(text, radix as u32).ok()?;
+ (prefix, text, suffix)
+ }
+
+ pub fn value(&self) -> Option<u128> {
+ let (_, text, _) = self.split_into_parts();
+ let value = u128::from_str_radix(&text.replace("_", ""), self.radix() as u32).ok()?;
Some(value)
}
pub fn suffix(&self) -> Option<&str> {
- let text = self.text();
- let radix = self.radix();
- let mut indices = text.char_indices();
- if radix != Radix::Decimal {
- indices.next()?;
- indices.next()?;
+ let (_, _, suffix) = self.split_into_parts();
+ if suffix.is_empty() {
+ None
+ } else {
+ Some(suffix)
}
- let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
- Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
- _ => |(_, c)| c.is_ascii_alphabetic(),
- };
- let (suffix_start, _) = indices.find(is_suffix_start)?;
- Some(&text[suffix_start..])
}
}