简体   繁体   English

如何使用nom解析完整的f32?

[英]How to parse complete f32 with nom?

There is a function float_s that parses floats in stream mod (can return Incomplete ). 有一个函数float_s ,用于解析流mod中的浮点数(可以返回Incomplete )。 I want to use CompleteStr as input type instead. 我想使用CompleteStr作为输入类型。 How I can achieve that? 我怎么能做到这一点?

Simple approach fails with complains about &str and CompleteStr mismatches: 关于&strCompleteStr不匹配的抱怨,简单的方法失败了:

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

I'm using nom 4.0.0 . 我正在使用nom 4.0.0

nom v4.1.0 fixed this problem: nom v4.1.0解决了这个问题:

  • float and double now work on all of nom's input types ( &[u8] , &str , CompleteByteSlice , CompleteStr and any type that implements the required traits). floatdouble现在可以处理所有nom的输入类型( &[u8]&strCompleteByteSliceCompleteStr以及任何实现所需特征的类型)。 float_s and double_s got the same modification, but are now deprecated float_sdouble_s得到了相同的修改,但现在已弃用

float_s expects a string, so you have to extract the string from the CompleteStr : float_s需要一个字符串,所以你必须从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: 我目前的解决方法是只复制粘贴float_s实现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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