1/*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2011 Jan Frederick Eick
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7==============================================================================*/
8#if !defined(BOOST_SPIRIT_X3_EXTRACT_INT_APRIL_17_2006_0830AM)
9#define BOOST_SPIRIT_X3_EXTRACT_INT_APRIL_17_2006_0830AM
10
11#include <boost/spirit/home/x3/support/traits/move_to.hpp>
12#include <boost/spirit/home/x3/support/numeric_utils/detail/extract_int.hpp>
13#include <boost/assert.hpp>
14
15namespace boost { namespace spirit { namespace x3
16{
17 ///////////////////////////////////////////////////////////////////////////
18 // Extract the prefix sign (- or +), return true if a '-' was found
19 ///////////////////////////////////////////////////////////////////////////
20 template <typename Iterator>
21 inline bool
22 extract_sign(Iterator& first, Iterator const& last)
23 {
24 (void)last; // silence unused warnings
25 BOOST_ASSERT(first != last); // precondition
26
27 // Extract the sign
28 bool neg = *first == '-';
29 if (neg || (*first == '+'))
30 {
31 ++first;
32 return neg;
33 }
34 return false;
35 }
36
37 ///////////////////////////////////////////////////////////////////////////
38 // Low level unsigned integer parser
39 ///////////////////////////////////////////////////////////////////////////
40 template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
41 , bool Accumulate = false>
42 struct extract_uint
43 {
44 // check template parameter 'Radix' for validity
45 static_assert(
46 (Radix >= 2 && Radix <= 36),
47 "Error Unsupported Radix");
48
49 template <typename Iterator>
50 inline static bool call(Iterator& first, Iterator const& last, T& attr)
51 {
52 if (first == last)
53 return false;
54
55 typedef detail::extract_int<
56 T
57 , Radix
58 , MinDigits
59 , MaxDigits
60 , detail::positive_accumulator<Radix>
61 , Accumulate>
62 extract_type;
63
64 Iterator save = first;
65 if (!extract_type::parse(first, last, attr))
66 {
67 first = save;
68 return false;
69 }
70 return true;
71 }
72
73 template <typename Iterator, typename Attribute>
74 inline static bool call(Iterator& first, Iterator const& last, Attribute& attr_)
75 {
76 // this case is called when Attribute is not T
77 T attr;
78 if (call(first, last, attr))
79 {
80 traits::move_to(attr, attr_);
81 return true;
82 }
83 return false;
84 }
85 };
86
87 ///////////////////////////////////////////////////////////////////////////
88 // Low level signed integer parser
89 ///////////////////////////////////////////////////////////////////////////
90 template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits>
91 struct extract_int
92 {
93 // check template parameter 'Radix' for validity
94 static_assert(
95 (Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16),
96 "Error Unsupported Radix");
97
98 template <typename Iterator>
99 inline static bool call(Iterator& first, Iterator const& last, T& attr)
100 {
101 if (first == last)
102 return false;
103
104 typedef detail::extract_int<
105 T, Radix, MinDigits, MaxDigits>
106 extract_pos_type;
107
108 typedef detail::extract_int<
109 T, Radix, MinDigits, MaxDigits, detail::negative_accumulator<Radix> >
110 extract_neg_type;
111
112 Iterator save = first;
113 bool hit = extract_sign(first, last);
114 if (hit)
115 hit = extract_neg_type::parse(first, last, attr);
116 else
117 hit = extract_pos_type::parse(first, last, attr);
118
119 if (!hit)
120 {
121 first = save;
122 return false;
123 }
124 return true;
125 }
126
127 template <typename Iterator, typename Attribute>
128 inline static bool call(Iterator& first, Iterator const& last, Attribute& attr_)
129 {
130 // this case is called when Attribute is not T
131 T attr;
132 if (call(first, last, attr))
133 {
134 traits::move_to(attr, attr_);
135 return true;
136 }
137 return false;
138 }
139 };
140}}}
141
142#endif
143

source code of boost/libs/spirit/include/boost/spirit/home/x3/support/numeric_utils/extract_int.hpp