📄 numeric_utils.hpp
字号:
{ return true; } template <typename Char, typename T> static bool call(Char ch, std::size_t count, T& n) { return call(ch, count, n , mpl::bool_< ( (MaxDigits < 0) || (MaxDigits > radix_traits<Radix>::template digits<T>::value) ) && std::numeric_limits<T>::is_modulo >() ); } }; /////////////////////////////////////////////////////////////////////////// // End of loop checking: check if the number of digits // being parsed exceeds MaxDigits. Note: if MaxDigits == -1 // we don't do any checking. /////////////////////////////////////////////////////////////////////////// template <int MaxDigits> struct check_max_digits { static bool call(std::size_t count) { return count < MaxDigits; // bounded } }; template <> struct check_max_digits<-1> { static bool call(std::size_t /*count*/) { return true; // unbounded } }; /////////////////////////////////////////////////////////////////////////// // extract_int: main code for extracting integers ///////////////////////////////////////////////////////////////////////////#define SPIRIT_NUMERIC_INNER_LOOP(z, x, data) \ if (!check_max_digits<MaxDigits>::call(count + leading_zeros) \ || it == last) \ break; \ ch = *it; \ if (!radix_check::is_valid(ch) || !extractor::call(ch, count, val)) \ break; \ ++it; \ ++count; \ /**/ template < typename T, unsigned Radix, unsigned MinDigits, int MaxDigits , typename Accumulator = positive_accumulator<Radix> , bool Accumulate = false > struct extract_int {#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) # pragma warning(push) # pragma warning(disable: 4127) // conditional expression is constant#endif template <typename Iterator, typename Attribute> static bool parse_main( Iterator& first , Iterator const& last , Attribute& attr) { typedef radix_traits<Radix> radix_check; typedef int_extractor<Radix, Accumulator, MaxDigits> extractor; typedef typename boost::detail::iterator_traits<Iterator>::value_type char_type; Iterator it = first; std::size_t leading_zeros = 0; if (!Accumulate) { // skip leading zeros while (it != last && *it == '0') { ++it; ++leading_zeros; } } Attribute val = Accumulate ? attr : 0; std::size_t count = 0; char_type ch; while (true) { BOOST_PP_REPEAT( SPIRIT_NUMERICS_LOOP_UNROLL , SPIRIT_NUMERIC_INNER_LOOP, _) } if (count + leading_zeros >= MinDigits) { attr = val; first = it; return true; } return false; }#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) # pragma warning(pop) #endif template <typename Iterator> static bool parse( Iterator& first , Iterator const& last , unused_type) { T n = 0; // must calculate value to detect over/underflow return parse_main(first, last, n); } template <typename Iterator, typename Attribute> static bool parse( Iterator& first , Iterator const& last , Attribute& attr) { return parse_main(first, last, attr); } };#undef SPIRIT_NUMERIC_INNER_LOOP /////////////////////////////////////////////////////////////////////////// // extract_int: main code for extracting integers // common case where MinDigits == 1 and MaxDigits = -1 ///////////////////////////////////////////////////////////////////////////#define SPIRIT_NUMERIC_INNER_LOOP(z, x, data) \ if (it == last) \ break; \ ch = *it; \ if (!radix_check::is_valid(ch) || !extractor::call(ch, count, val)) \ break; \ ++it; \ ++count; \ /**/ template <typename T, unsigned Radix, typename Accumulator, bool Accumulate> struct extract_int<T, Radix, 1, -1, Accumulator, Accumulate> {#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) # pragma warning(push) # pragma warning(disable: 4127) // conditional expression is constant#endif template <typename Iterator, typename Attribute> static bool parse_main( Iterator& first , Iterator const& last , Attribute& attr) { typedef radix_traits<Radix> radix_check; typedef int_extractor<Radix, Accumulator, -1> extractor; typedef typename boost::detail::iterator_traits<Iterator>::value_type char_type; Iterator it = first; std::size_t count = 0; if (!Accumulate) { // skip leading zeros while (it != last && *it == '0') { ++it; ++count; } if (it == last) { if (count == 0) // must have at least one digit return false; attr = 0; first = it; return true; } } Attribute val = Accumulate ? attr : 0; char_type ch = *it; if (!radix_check::is_valid(ch) || !extractor::call(ch, 0, val)) { if (count == 0) // must have at least one digit return false; attr = val; first = it; return true; } count = 0; ++it; while (true) { BOOST_PP_REPEAT( SPIRIT_NUMERICS_LOOP_UNROLL , SPIRIT_NUMERIC_INNER_LOOP, _) } attr = val; first = it; return true; }#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) # pragma warning(pop) #endif template <typename Iterator> static bool parse( Iterator& first , Iterator const& last , unused_type) { T n = 0; // must calculate value to detect over/underflow return parse_main(first, last, n); } template <typename Iterator, typename Attribute> static bool parse( Iterator& first , Iterator const& last , Attribute& attr) { return parse_main(first, last, attr); } };#undef SPIRIT_NUMERIC_INNER_LOOP}}}}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -