2016-02-26 45 views
2

我想解析一些整数提升精神,我必须检查数据的溢出和精神doc说所有整数解析器检查溢出,但它只适用于无符号类型,如果我尝试解析带符号整数,精神将不再检查溢出。提升精神不检查int溢出

char const* b = "6600452345243524352340"; 
char const* e = b + strlen("6600452345243524352340"); 
int32_t res = 0; 
bool valid = boost::spirit::qi::parse(b, e, boost::spirit::qi::int_, res); 
std::cout << valid << " " << res << std::endl; 

有没有办法对有符号溢出进行精神检查?我使用boost 1.55,gcc 4.9。

+0

什么版本提升的是什么?你得到的输出是什么,你期望的输出是什么? – sehe

+0

像sehe在他的回答显示升压1.60没有这个问题,你应该考虑更新。 – llonesmiz

+0

@sehe正如我所提到的那样,它是1.55,看起来像一个助推精神中的错误,我刚刚检查1.56,它的工作原理。我必须坚持1.55,并且必须以某种方式解决问题。 – Vasaka

回答

2

两个检查溢出就好:

Live On Coliru

#include <boost/spirit/include/qi.hpp> 
#include <boost/multiprecision/cpp_int.hpp> 

using boost::multiprecision::int128_t; 
using boost::multiprecision::int256_t; 
using boost::multiprecision::uint128_t; 
using boost::multiprecision::uint256_t; 

template <typename T> 
bool try_parse(char const* s) { 
    auto b = s, e = b + strlen(s); 
    T res = 0; 
    namespace qi = boost::spirit::qi; 
    bool valid = qi::parse(b, e, qi::int_parser<T, 10>(), res); 
    if (!valid) 
     std::cout << "Unparsed (" << __PRETTY_FUNCTION__ << ")\n"; 
    else 
     std::cout << "Valid: " << res << "(" << __PRETTY_FUNCTION__ << ")\n";; 

    if (b!=e) 
     std::cout << " --> remaining: '" << std::string(b,e) << "'\n"; 

    return valid; 
} 

int main() { 
    try_parse<int8_t> ("6600452345243524352340"); 
    try_parse<uint8_t> ("6600452345243524352340"); 

    try_parse<int16_t> ("6600452345243524352340"); 
    try_parse<uint16_t> ("6600452345243524352340"); 

    try_parse<int32_t> ("6600452345243524352340"); 
    try_parse<uint32_t> ("6600452345243524352340"); 

    try_parse<int64_t> ("6600452345243524352340"); 
    try_parse<uint64_t> ("6600452345243524352340"); 

    try_parse<int128_t> ("6600452345243524352340"); 
    try_parse<uint128_t> ("6600452345243524352340"); 
} 

打印

Unparsed (bool try_parse(const char *) [T = signed char]) 
--> remaining: '6600452345243524352340' 
Unparsed (bool try_parse(const char *) [T = unsigned char]) 
--> remaining: '6600452345243524352340' 
Unparsed (bool try_parse(const char *) [T = short]) 
--> remaining: '6600452345243524352340' 
Unparsed (bool try_parse(const char *) [T = unsigned short]) 
--> remaining: '6600452345243524352340' 
Unparsed (bool try_parse(const char *) [T = int]) 
--> remaining: '6600452345243524352340' 
Unparsed (bool try_parse(const char *) [T = unsigned int]) 
--> remaining: '6600452345243524352340' 
Unparsed (bool try_parse(const char *) [T = long]) 
--> remaining: '6600452345243524352340' 
Unparsed (bool try_parse(const char *) [T = unsigned long]) 
--> remaining: '6600452345243524352340' 
Valid: 6600452345243524352340 (bool try_parse(const char *) [T = boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<128, 128, boost::multiprecision::cpp_integer_type::signed_magnitude, boost::multiprecision::cpp_int_check_type::unchecked, void>, boost::multiprecision::expression_template_option::et_off>]) 
Valid: 6600452345243524352340 (bool try_parse(const char *) [T = boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<128, 128, boost::multiprecision::cpp_integer_type::unsigned_magnitude, boost::multiprecision::cpp_int_check_type::unchecked, void>, boost::multiprecision::expression_template_option::et_off>]) 
+0

http://melpon.org/wandbox/permlink/OY9JOYtOHnp557wG – llonesmiz

+1

@cv_and_he似乎相关:https://svn.boost.org/trac/boost/ticket/9007和邮件列表讨论http://boost.2283326.n4 .nabble.com /溢出最整数解析器-td4650743.html – sehe