简体   繁体   中英

How to parse complete f32 with nom?

There is a function float_s that parses floats in stream mod (can return Incomplete ). I want to use CompleteStr as input type instead. How I can achieve that?

Simple approach fails with complains about &str and CompleteStr mismatches:

named!(parse_float_complete(CompleteStr) -> f32,
    ws!(::num::float_s)
);

I'm using nom 4.0.0 .

nom v4.1.0 fixed this problem:

  • float and double now work on all of nom's input types ( &[u8] , &str , CompleteByteSlice , CompleteStr and any type that implements the required traits). float_s and double_s got the same modification, but are now deprecated

float_s expects a string, so you have to extract the string from the CompleteStr :

named!(parse_float_complete(CompleteStr) -> f32,
    ws!(call!(|input| ::num::float_s(input.0).map(|output, result| CompleteStr(output, result))))
);

My current workaround is to just copy-paste float_s implementation:

fn float_cs(input: CompleteStr) -> ::nom::IResult<CompleteStr, f32> {
  flat_map!(input, call!(::nom::recognize_float), parse_to!(f32))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM