From 2567d5a6c61c1573d115ed233dde5abf15784ea4 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 4 Nov 2024 14:55:33 -0500 Subject: [PATCH] Fix edge case of take_int and take_ident at end of string --- impl/src/fmt.rs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/impl/src/fmt.rs b/impl/src/fmt.rs index 4561e9f..1770621 100644 --- a/impl/src/fmt.rs +++ b/impl/src/fmt.rs @@ -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, @@ -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 }