Ivan Lozano | 57e660e | 2021-08-20 09:58:42 -0400 | [diff] [blame] | 1 | #![cfg_attr(not(feature = "std"), no_std)] |
| 2 | |
| 3 | #[cfg(not(feature = "std"))] |
| 4 | use core::str; |
| 5 | |
| 6 | #[cfg(feature = "std")] |
| 7 | use std::str; |
| 8 | |
| 9 | use combine::{ |
| 10 | error::UnexpectedParse, |
| 11 | parser::{ |
| 12 | byte::digit, |
| 13 | choice::optional, |
| 14 | range::recognize, |
| 15 | repeat::{skip_many, skip_many1}, |
| 16 | token::token, |
| 17 | }, |
| 18 | Parser, |
| 19 | }; |
| 20 | |
| 21 | fn main() { |
| 22 | let mut parser = recognize(( |
| 23 | skip_many1(digit()), |
| 24 | optional((token(b'.'), skip_many(digit()))), |
| 25 | )) |
| 26 | .and_then(|bs: &[u8]| { |
| 27 | // `bs` only contains digits which are ascii and thus UTF-8 |
| 28 | let s = unsafe { str::from_utf8_unchecked(bs) }; |
| 29 | s.parse::<f64>().map_err(|_| UnexpectedParse::Unexpected) |
| 30 | }); |
| 31 | let result = parser.parse(&b"123.45"[..]); |
| 32 | assert_eq!(result, Ok((123.45, &b""[..]))); |
| 33 | } |