Skip to content

Commit

Permalink
Support negative int and float literals in FromStr
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 26, 2021
1 parent 25d7c75 commit 3f57d82
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,20 @@ impl Literal {
impl FromStr for Literal {
type Err = LexError;

fn from_str(repr: &str) -> Result<Self, Self::Err> {
fn from_str(mut repr: &str) -> Result<Self, Self::Err> {
let negative = repr.starts_with('-');
if negative {
repr = &repr[1..];
if !repr.starts_with(|ch: char| ch.is_ascii_digit()) {
return Err(LexError::call_site());
}
}
let cursor = get_cursor(repr);
if let Ok((_rest, literal)) = parse::literal(cursor) {
if let Ok((_rest, mut literal)) = parse::literal(cursor) {
if literal.text.len() == repr.len() {
if negative {
literal.text.insert(0, '-');
}
return Ok(literal);
}
}
Expand Down

0 comments on commit 3f57d82

Please sign in to comment.