blob: 6be4ab55062162aa32180d02b9f779ac30b9bebe [file] [log] [blame]
Ivan Lozano57e660e2021-08-20 09:58:42 -04001#![cfg_attr(not(feature = "std"), no_std)]
2
3#[cfg(not(feature = "std"))]
4use core::str;
5
6#[cfg(feature = "std")]
7use std::str;
8
9use 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
21fn 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}