Skip to content

Commit

Permalink
Fix edge case of take_int and take_ident at end of string
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 4, 2024
1 parent 3814afe commit 2567d5a
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Display<'_> {
member
}
'a'..='z' | 'A'..='Z' | '_' => {
let ident = Ident::new(&take_ident(&mut read), span);
let ident = Ident::new(take_ident(&mut read), span);
Member::Named(ident)
}
_ => continue,
Expand Down Expand Up @@ -213,31 +213,29 @@ fn is_syn_full() -> bool {
}
}

fn take_int(read: &mut &str) -> String {
let mut int = String::new();
for (i, ch) in read.char_indices() {
fn take_int<'a>(read: &mut &'a str) -> &'a str {
let mut int_len = 0;
for ch in read.chars() {
match ch {
'0'..='9' => int.push(ch),
_ => {
*read = &read[i..];
break;
}
'0'..='9' => int_len += 1,
_ => break,
}
}
let (int, rest) = read.split_at(int_len);
*read = rest;
int
}

fn take_ident(read: &mut &str) -> String {
let mut ident = String::new();
for (i, ch) in read.char_indices() {
fn take_ident<'a>(read: &mut &'a str) -> &'a str {
let mut ident_len = 0;
for ch in read.chars() {
match ch {
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => ident.push(ch),
_ => {
*read = &read[i..];
break;
}
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => ident_len += 1,
_ => break,
}
}
let (ident, rest) = read.split_at(ident_len);
*read = rest;
ident
}

Expand Down

0 comments on commit 2567d5a

Please sign in to comment.