⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numerics.ipp

📁 著名的Parser库Spirit在VC6上的Port
💻 IPP
📖 第 1 页 / 共 2 页
字号:
/*=============================================================================
    Numeric parsers

    Spirit V1.3.1
    Copyright (c) 2001, Joel de Guzman
    Copyright (c) 2001, Hartmut Kaiser

    This software is provided 'as-is', without any express or implied
    warranty. In no event will the copyright holder be held liable for
    any damages arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute
    it freely, subject to the following restrictions:

    1.  The origin of this software must not be misrepresented; you must
        not claim that you wrote the original software. If you use this
        software in a product, an acknowledgment in the product documentation
        would be appreciated but is not required.

    2.  Altered source versions must be plainly marked as such, and must
        not be misrepresented as being the original software.

    3.  This notice may not be removed or altered from any source
        distribution.

    Acknowledgements:

        Special thanks to Dan Nuffer, John (EBo) David, Chris Uzdavinis,
        and Doug Gregor. These people are most instrumental in steering
        Spirit in the right direction.

        Special thanks also to people who have contributed to the code Radix
        and sample code, ported Spirit to various platforms and compilers,
        reported and provided bug fixes. Alexander Hirner, Andy Elvey,
        Bogdan Kushnir, Brett Calcott, Bruce Florman, Changzhe Han, Colin
        McPhail, Hakki Dogusan, Jan Bares, Joseph Smith, Martijn W. van der
        Lee, Raghavendra Satish, Remi Delcos, Tom Spilman, Vladimir Prus,
        W. Scott Dillman.

        Finally special thanks also to people who gave feedback and
        valuable comments, particularly members of Spirit's Source Forge
        mailing list and boost.org.

    URL: http://spirit.sourceforge.net/

=============================================================================*/
#ifndef SPIRIT_NUMERICS_IPP
#define SPIRIT_NUMERICS_IPP

///////////////////////////////////////////////////////////////////////////////

#include <cmath>
#include <functional>
#include <iterator>

#include <assert.h>

#include "boost/spirit/MSVC/ps_helper.hpp"
#include "boost/spirit/MSVC/primitives.hpp"
#include "boost/spirit/MSVC/iterators.hpp"
#include "boost/config.hpp"
#include <limits>

///////////////////////////////////////////////////////////////////////////////
namespace spirit {

//////////////////////////////////
namespace impl {

    //////////////////////////////////
    template <typename IteratorT>
    bool
    extract_sign(IteratorT& iter, unsigned& count)
    {
        //  Extract the sign

        count = 0;
        bool neg = *iter == '-';
        if (neg || (*iter == '+'))
        {
            ++iter;
            ++count;
            return neg;
        }

        return false;
    }

///////////////////////////////////////////////////////////////////////////////
// Traits class for number conversion
    template<const int Radix>
    struct radix_traits
    {
    // Tests validity of a single character in the context of the given
    // radix (template parameter Radix)
        template<typename CharT> static bool isok(CharT ch);

    // Converts a digit from character representation to binary representation
    // in the context of the given radix (template parameter Radix)
        template<typename CharT> static int uint(CharT ch);
    };

    template<>
    struct radix_traits<2> {
        template<typename CharT>
        static bool isok(CharT ch) { return ('0' == ch || '1' == ch); }

        template<typename CharT>
        static int uint(CharT ch) { return ch - '0'; }
    };

    template<>
    struct radix_traits<8> {
        template<typename CharT>
        static bool isok(CharT ch) { return ('0' <= ch && ch <= '7'); }

        template<typename CharT>
        static int uint(CharT ch) { return ch - '0'; }
    };

    template<>
    struct radix_traits<10> {
        template<typename CharT>
        static bool isok(CharT ch) { return isdigit(ch) ? true : false; }

        template<typename CharT>
        static int uint(CharT ch) { return ch - '0'; }
    };

    template<>
    struct radix_traits<16> {
        template<typename CharT>
        static bool isok(CharT ch) { return isxdigit(ch) ? true : false; }

        template<typename CharT>
        static int uint(CharT ch)
        {
            if (isdigit(ch))
                return ch - '0';
            return tolower(ch) - 'a' + 10;
        }
    };

///////////////////////////////////////////////////////////////////////////////
// Helper templates for abstracting the compare methods for different 
// numeric_parser_traits classes
    template<typename IteratorT>
    struct trait_type_cmp
    {
    // trait is given as a parser (template parameter ParserT)
        template <typename ParserT>
        static match parse (parser<ParserT> p, IteratorT &it1, IteratorT const &it2) 
        {
            return p.derived().parse(it1, it2);
        }

        template <typename ParserT>
        static match parse2 (parser<ParserT> p, IteratorT &it1, IteratorT const &it2) 
        {
            return parse (p.derived(), it1, it2);
        }

    // trait is given as a single character
#if defined (BOOST_MSVC)    
        typedef typename spirit::iterator_traits<IteratorT>::value_type CharT;
#else
        typedef typename std::iterator_traits<IteratorT>::value_type CharT;
#endif
        static match parse (CharT ch, IteratorT &it1, IteratorT const &/*it2*/) 
        {
            if (ch == *it1) {
                ++it1;
                return match(1);
            }
            return match();
        }
        static match parse2 (CharT ch, IteratorT &it1, IteratorT const &it2) 
        {
            return nocase[ch].parse(it1, it2);
        }

    // trait is given as a string
#if defined (BOOST_MSVC)
        typedef typename spirit::iterator_traits<IteratorT>::pointer PointerT;
#else
        typedef typename std::iterator_traits<IteratorT>::pointer PointerT;
#endif
        static match parse (PointerT pch, IteratorT &it1, IteratorT const &it2) 
        {
#if defined (BOOST_MSVC)
            typedef typename spirit::iterator_traits<IteratorT>::value_type CharT;
#else
            typedef typename std::iterator_traits<IteratorT>::value_type CharT;
#endif
            return strlit<CharT>(pch).parse(it1, it2);
        }
        static match parse2 (PointerT pch, IteratorT &it1, 
                IteratorT const &it2) 
        {
            return parse (pch, it1, it2);
        }
    };

/*
    template<typename IteratorT, typename ParserT>
    struct trait_type_cmp
    {
    // trait is given as a parser (template parameter ParserT)
        static match parse (ParserT p, IteratorT &it1, IteratorT const &it2) 
        {
            return p.parse(it1, it2);
        }
        static match parse2 (ParserT p, IteratorT &it1, IteratorT const &it2) 
        {
            return parse (p, it1, it2);
        }
    };

    template<typename IteratorT>
    struct trait_type_cmp<IteratorT, const int> 
    {
    // trait is given as a single character
#if defined (BOOST_MSVC)   
        typedef typename spirit::iterator_traits<IteratorT>::value_type CharT;
#else
        typedef typename std::iterator_traits<IteratorT>::value_type CharT;
#endif
        static match parse (CharT ch, IteratorT &it1, IteratorT const &) 
        {
            if (ch == *it1) {
                ++it1;
                return match(1);
            }
            return match();
        }
        static match parse2 (CharT ch, IteratorT &it1, IteratorT const &it2) 
        {
            return nocase[ch].parse(it1, it2);
        }
    };

    template<typename IteratorT>
#if defined (BOOST_MSVC)
    struct trait_type_cmp<IteratorT, 
        const spirit::iterator_traits<IteratorT>::pointer> 
#else
    struct trait_type_cmp<IteratorT, 
        const std::iterator_traits<IteratorT>::pointer> 
#endif
    {
    // trait is given as a string
#if defined (BOOST_MSVC) 
        typedef typename spirit::iterator_traits<IteratorT>::pointer PointerT;
#else
        typedef typename std::iterator_traits<IteratorT>::pointer PointerT;
#endif
        static match parse (PointerT pch, IteratorT &it1, IteratorT const &it2) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -