command_parser: fix float parsing

master
Astro 2019-09-19 15:28:11 +02:00
parent 6f36c682cd
commit f3664f01be
2 changed files with 7 additions and 4 deletions

View File

@ -4,7 +4,7 @@ use nom::{
branch::alt, branch::alt,
bytes::complete::{is_a, tag, take_while1}, bytes::complete::{is_a, tag, take_while1},
character::{is_digit, complete::{char, one_of}}, character::{is_digit, complete::{char, one_of}},
combinator::{complete, map, value}, combinator::{complete, map, opt, value},
sequence::{preceded, separated_pair}, sequence::{preceded, separated_pair},
multi::{fold_many0, fold_many1}, multi::{fold_many0, fold_many1},
error::ErrorKind, error::ErrorKind,
@ -133,8 +133,8 @@ fn unsigned(input: &[u8]) -> IResult<&[u8], Result<u32, Error>> {
} }
fn float(input: &[u8]) -> IResult<&[u8], Result<f32, Error>> { fn float(input: &[u8]) -> IResult<&[u8], Result<f32, Error>> {
let (input, sign) = is_a("-")(input)?; let (input, sign) = opt(is_a("-"))(input)?;
let negative = sign.len() > 0; let negative = sign.is_some();
let (input, digits) = take_while1(|c| is_digit(c) || c == '.' as u8)(input)?; let (input, digits) = take_while1(|c| is_digit(c) || c == '.' as u8)(input)?;
let result = lexical::parse(digits) let result = lexical::parse(digits)
.map(|result: f32| if negative { -result } else { result }) .map(|result: f32| if negative { -result } else { result })

View File

@ -1,7 +1,10 @@
#![feature(const_fn, proc_macro_hygiene)] #![feature(const_fn, proc_macro_hygiene)]
#![no_std] #![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)] #![cfg_attr(not(test), no_main)]
#[cfg(not(test))]
extern crate std;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use core::fmt::{self, Write}; use core::fmt::{self, Write};
use smoltcp::time::Instant; use smoltcp::time::Instant;