]> git.sesse.net Git - casparcg/blob - dependencies64/boost/boost/lexical_cast.hpp
Unpacked dependencies64
[casparcg] / dependencies64 / boost / boost / lexical_cast.hpp
1 #ifndef BOOST_LEXICAL_CAST_INCLUDED
2 #define BOOST_LEXICAL_CAST_INCLUDED
3
4 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9
10 // Boost lexical_cast.hpp header  -------------------------------------------//
11 //
12 // See http://www.boost.org/libs/conversion for documentation.
13 // See end of this header for rights and permissions.
14 //
15 // what:  lexical_cast custom keyword cast
16 // who:   contributed by Kevlin Henney,
17 //        enhanced with contributions from Terje Slettebo,
18 //        with additional fixes and suggestions from Gennaro Prota,
19 //        Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
20 //        Alexander Nasonov, Antony Polukhin and other Boosters
21 // when:  November 2000, March 2003, June 2005, June 2006, March 2011
22
23 #include <climits>
24 #include <cstddef>
25 #include <istream>
26 #include <string>
27 #include <cstring>
28 #include <cstdio>
29 #include <typeinfo>
30 #include <exception>
31 #include <cmath>
32 #include <boost/config.hpp>
33 #include <boost/limits.hpp>
34 #include <boost/mpl/if.hpp>
35 #include <boost/throw_exception.hpp>
36 #include <boost/type_traits/is_pointer.hpp>
37 #include <boost/type_traits/is_integral.hpp>
38 #include <boost/type_traits/is_arithmetic.hpp>
39 #include <boost/type_traits/remove_pointer.hpp>
40 #include <boost/numeric/conversion/cast.hpp>
41 #include <boost/type_traits/ice.hpp>
42 #include <boost/type_traits/make_unsigned.hpp>
43 #include <boost/type_traits/is_signed.hpp>
44 #include <boost/math/special_functions/sign.hpp>
45 #include <boost/math/special_functions/fpclassify.hpp>
46 #include <boost/static_assert.hpp>
47 #include <boost/detail/lcast_precision.hpp>
48 #include <boost/detail/workaround.hpp>
49 #include <cwchar>
50
51
52 #ifndef BOOST_NO_STD_LOCALE
53 #   include <locale>
54 #else
55 #   ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
56 #       warning "Unable to use <locale> header. boost::lexical_cast will use the 'C' locale."
57 #       define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
58 #   endif
59 #endif
60
61 #ifdef BOOST_NO_STRINGSTREAM
62 #include <strstream>
63 #else
64 #include <sstream>
65 #endif
66
67 #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
68 #define BOOST_LCAST_NO_WCHAR_T
69 #endif
70
71 #ifdef BOOST_NO_TYPEID
72 #define BOOST_LCAST_THROW_BAD_CAST(S, T) throw_exception(bad_lexical_cast())
73 #else
74 #define BOOST_LCAST_THROW_BAD_CAST(Source, Target) \
75     throw_exception(bad_lexical_cast(typeid(Source), typeid(Target)))
76 #endif
77
78 namespace boost
79 {
80     // exception used to indicate runtime lexical_cast failure
81     class bad_lexical_cast :
82     // workaround MSVC bug with std::bad_cast when _HAS_EXCEPTIONS == 0 
83 #if defined(BOOST_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS 
84         public std::exception 
85 #else 
86         public std::bad_cast 
87 #endif 
88
89 #if defined(__BORLANDC__) && BOOST_WORKAROUND( __BORLANDC__, < 0x560 )
90         // under bcc32 5.5.1 bad_cast doesn't derive from exception
91         , public std::exception
92 #endif
93
94     {
95     public:
96         bad_lexical_cast() :
97 #ifndef BOOST_NO_TYPEID
98           source(&typeid(void)), target(&typeid(void))
99 #else
100           source(0), target(0) // this breaks getters
101 #endif
102         {
103         }
104
105         bad_lexical_cast(
106             const std::type_info &source_type_arg,
107             const std::type_info &target_type_arg) :
108             source(&source_type_arg), target(&target_type_arg)
109         {
110         }
111
112         const std::type_info &source_type() const
113         {
114             return *source;
115         }
116         const std::type_info &target_type() const
117         {
118             return *target;
119         }
120
121         virtual const char *what() const throw()
122         {
123             return "bad lexical cast: "
124                    "source type value could not be interpreted as target";
125         }
126         virtual ~bad_lexical_cast() throw()
127         {
128         }
129     private:
130         const std::type_info *source;
131         const std::type_info *target;
132     };
133
134     namespace detail // selectors for choosing stream character type
135     {
136     template<typename Type>
137     struct stream_char
138     {
139         typedef char type;
140     };
141
142 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
143     template<class CharT, class Traits, class Alloc>
144     struct stream_char< std::basic_string<CharT,Traits,Alloc> >
145     {
146         typedef CharT type;
147     };
148 #endif
149
150 #ifndef BOOST_LCAST_NO_WCHAR_T
151 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
152     template<>
153     struct stream_char<wchar_t>
154     {
155         typedef wchar_t type;
156     };
157 #endif
158
159     template<>
160     struct stream_char<wchar_t *>
161     {
162         typedef wchar_t type;
163     };
164
165     template<>
166     struct stream_char<const wchar_t *>
167     {
168         typedef wchar_t type;
169     };
170
171 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
172     template<>
173     struct stream_char<std::wstring>
174     {
175         typedef wchar_t type;
176     };
177 #endif
178 #endif
179
180
181 #ifndef BOOST_NO_CHAR16_T
182
183     template<>
184     struct stream_char<char16_t>
185     {
186         typedef char16_t type;
187     };
188
189     template<>
190     struct stream_char<char16_t *>
191     {
192         typedef char16_t type;
193     };
194
195     template<>
196     struct stream_char<const char16_t *>
197     {
198         typedef char16_t type;
199     };
200
201 #endif
202
203 #ifndef BOOST_NO_CHAR32_T
204
205     template<>
206     struct stream_char<char32_t>
207     {
208         typedef char32_t type;
209     };
210
211     template<>
212     struct stream_char<char32_t *>
213     {
214         typedef char32_t type;
215     };
216
217     template<>
218     struct stream_char<const char32_t *>
219     {
220         typedef char32_t type;
221     };
222
223 #endif
224
225         template<typename TargetChar, typename SourceChar>
226         struct widest_char
227         {
228             typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
229                 (sizeof(TargetChar) > sizeof(SourceChar))
230                 , TargetChar
231                 , SourceChar >::type type;
232         };
233     }
234
235     namespace detail // deduce_char_traits template
236     {
237 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
238         template<class CharT, class Target, class Source>
239         struct deduce_char_traits
240         {
241             typedef std::char_traits<CharT> type;
242         };
243
244         template<class CharT, class Traits, class Alloc, class Source>
245         struct deduce_char_traits< CharT
246                                  , std::basic_string<CharT,Traits,Alloc>
247                                  , Source
248                                  >
249         {
250             typedef Traits type;
251         };
252
253         template<class CharT, class Target, class Traits, class Alloc>
254         struct deduce_char_traits< CharT
255                                  , Target
256                                  , std::basic_string<CharT,Traits,Alloc>
257                                  >
258         {
259             typedef Traits type;
260         };
261
262         template<class CharT, class Traits, class Alloc1, class Alloc2>
263         struct deduce_char_traits< CharT
264                                  , std::basic_string<CharT,Traits,Alloc1>
265                                  , std::basic_string<CharT,Traits,Alloc2>
266                                  >
267         {
268             typedef Traits type;
269         };
270 #endif
271     }
272
273     namespace detail // lcast_src_length
274     {
275         // Return max. length of string representation of Source;
276         template< class Source // Source type of lexical_cast.
277                 >
278         struct lcast_src_length
279         {
280             BOOST_STATIC_CONSTANT(std::size_t, value = 1);
281             // To check coverage, build the test with
282             // bjam --v2 profile optimization=off
283             static void check_coverage() {}
284         };
285
286         // Helper for integral types.
287         // Notes on length calculation:
288         // Max length for 32bit int with grouping "\1" and thousands_sep ',':
289         // "-2,1,4,7,4,8,3,6,4,7"
290         //  ^                    - is_signed
291         //   ^                   - 1 digit not counted by digits10
292         //    ^^^^^^^^^^^^^^^^^^ - digits10 * 2
293         //
294         // Constant is_specialized is used instead of constant 1
295         // to prevent buffer overflow in a rare case when
296         // <boost/limits.hpp> doesn't add missing specialization for
297         // numeric_limits<T> for some integral type T.
298         // When is_specialized is false, the whole expression is 0.
299         template<class Source>
300         struct lcast_src_length_integral
301         {
302 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
303             BOOST_STATIC_CONSTANT(std::size_t, value =
304                   std::numeric_limits<Source>::is_signed +
305                   std::numeric_limits<Source>::is_specialized + /* == 1 */
306                   std::numeric_limits<Source>::digits10 * 2
307               );
308 #else
309             BOOST_STATIC_CONSTANT(std::size_t, value = 156);
310             BOOST_STATIC_ASSERT(sizeof(Source) * CHAR_BIT <= 256);
311 #endif
312         };
313 // TODO: FIX for char16_t, char32_t, we can ignore CharT
314 #define BOOST_LCAST_DEF(T)               \
315     template<> struct lcast_src_length<T> \
316         : lcast_src_length_integral<T>           \
317     { static void check_coverage() {} };
318
319         BOOST_LCAST_DEF(short)
320         BOOST_LCAST_DEF(unsigned short)
321         BOOST_LCAST_DEF(int)
322         BOOST_LCAST_DEF(unsigned int)
323         BOOST_LCAST_DEF(long)
324         BOOST_LCAST_DEF(unsigned long)
325 #if defined(BOOST_HAS_LONG_LONG)
326         BOOST_LCAST_DEF(boost::ulong_long_type)
327         BOOST_LCAST_DEF(boost::long_long_type )
328 #elif defined(BOOST_HAS_MS_INT64)
329         BOOST_LCAST_DEF(unsigned __int64)
330         BOOST_LCAST_DEF(         __int64)
331 #endif
332
333 #undef BOOST_LCAST_DEF
334
335 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
336         // Helper for floating point types.
337         // -1.23456789e-123456
338         // ^                   sign
339         //  ^                  leading digit
340         //   ^                 decimal point 
341         //    ^^^^^^^^         lcast_precision<Source>::value
342         //            ^        "e"
343         //             ^       exponent sign
344         //              ^^^^^^ exponent (assumed 6 or less digits)
345         // sign + leading digit + decimal point + "e" + exponent sign == 5
346         template<class Source>
347         struct lcast_src_length_floating
348         {
349             BOOST_STATIC_ASSERT(
350                     std::numeric_limits<Source>::max_exponent10 <=  999999L &&
351                     std::numeric_limits<Source>::min_exponent10 >= -999999L
352                 );
353             BOOST_STATIC_CONSTANT(std::size_t, value =
354                     5 + lcast_precision<Source>::value + 6
355                 );
356         };
357
358         template<>
359         struct lcast_src_length<float>
360           : lcast_src_length_floating<float>
361         {
362             static void check_coverage() {}
363         };
364
365         template<>
366         struct lcast_src_length<double>
367           : lcast_src_length_floating<double>
368         {
369             static void check_coverage() {}
370         };
371
372         template<>
373         struct lcast_src_length<long double>
374           : lcast_src_length_floating<long double>
375         {
376             static void check_coverage() {}
377         };
378
379 #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
380     }
381
382     namespace detail // '0', '+' and '-' constants
383     {
384         template<typename CharT> struct lcast_char_constants;
385
386         template<>
387         struct lcast_char_constants<char>
388         {
389             BOOST_STATIC_CONSTANT(char, zero  = '0');
390             BOOST_STATIC_CONSTANT(char, minus = '-');
391             BOOST_STATIC_CONSTANT(char, plus = '+');
392             BOOST_STATIC_CONSTANT(char, lowercase_e = 'e');
393             BOOST_STATIC_CONSTANT(char, capital_e = 'E');
394             BOOST_STATIC_CONSTANT(char, c_decimal_separator = '.');
395         };
396
397 #ifndef BOOST_LCAST_NO_WCHAR_T
398         template<>
399         struct lcast_char_constants<wchar_t>
400         {
401             BOOST_STATIC_CONSTANT(wchar_t, zero  = L'0');
402             BOOST_STATIC_CONSTANT(wchar_t, minus = L'-');
403             BOOST_STATIC_CONSTANT(wchar_t, plus = L'+');
404             BOOST_STATIC_CONSTANT(wchar_t, lowercase_e = L'e');
405             BOOST_STATIC_CONSTANT(wchar_t, capital_e = L'E');
406             BOOST_STATIC_CONSTANT(wchar_t, c_decimal_separator = L'.');
407         };
408 #endif
409
410 #ifndef BOOST_NO_CHAR16_T
411         template<>
412         struct lcast_char_constants<char16_t>
413         {
414             BOOST_STATIC_CONSTANT(char16_t, zero  = u'0');
415             BOOST_STATIC_CONSTANT(char16_t, minus = u'-');
416             BOOST_STATIC_CONSTANT(char16_t, plus = u'+');
417             BOOST_STATIC_CONSTANT(char16_t, lowercase_e = u'e');
418             BOOST_STATIC_CONSTANT(char16_t, capital_e = u'E');
419             BOOST_STATIC_CONSTANT(char16_t, c_decimal_separator = u'.');
420         };
421 #endif
422
423 #ifndef BOOST_NO_CHAR32_T
424         template<>
425         struct lcast_char_constants<char32_t>
426         {
427             BOOST_STATIC_CONSTANT(char32_t, zero  = U'0');
428             BOOST_STATIC_CONSTANT(char32_t, minus = U'-');
429             BOOST_STATIC_CONSTANT(char32_t, plus = U'+');
430             BOOST_STATIC_CONSTANT(char32_t, lowercase_e = U'e');
431             BOOST_STATIC_CONSTANT(char32_t, capital_e = U'E');
432             BOOST_STATIC_CONSTANT(char32_t, c_decimal_separator = U'.');
433         };
434 #endif
435     }
436
437     namespace detail // lcast_to_unsigned
438     {
439 #if (defined _MSC_VER)
440 # pragma warning( push )
441 // C4146: unary minus operator applied to unsigned type, result still unsigned
442 # pragma warning( disable : 4146 )
443 #elif defined( __BORLANDC__ )
444 # pragma option push -w-8041
445 #endif
446         template<class T>
447         inline
448         BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value)
449         {
450             typedef BOOST_DEDUCED_TYPENAME make_unsigned<T>::type result_type;
451             result_type uvalue = static_cast<result_type>(value);
452             return value < 0 ? -uvalue : uvalue;
453         }
454 #if (defined _MSC_VER)
455 # pragma warning( pop )
456 #elif defined( __BORLANDC__ )
457 # pragma option pop
458 #endif
459     }
460
461     namespace detail // lcast_put_unsigned
462     {
463         template<class Traits, class T, class CharT>
464         CharT* lcast_put_unsigned(const T n_param, CharT* finish)
465         {
466 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
467             BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
468 #endif
469
470             typedef typename Traits::int_type int_type;
471             CharT const czero = lcast_char_constants<CharT>::zero;
472             int_type const zero = Traits::to_int_type(czero);
473             BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
474                     (sizeof(int_type) > sizeof(T))
475                     , int_type
476                     , T
477             >::type n = n_param;
478
479 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
480             std::locale loc;
481             if (loc != std::locale::classic()) {
482                 typedef std::numpunct<CharT> numpunct;
483                 numpunct const& np = BOOST_USE_FACET(numpunct, loc);
484                 std::string const grouping = np.grouping();
485                 std::string::size_type const grouping_size = grouping.size();
486
487                 if ( grouping_size && grouping[0] > 0 )
488                 {
489
490 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
491                 // Check that ulimited group is unreachable:
492                 BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
493 #endif
494                     CharT thousands_sep = np.thousands_sep();
495                     std::string::size_type group = 0; // current group number
496                     char last_grp_size = grouping[0];
497                     char left = last_grp_size;
498
499                     do
500                     {
501                         if(left == 0)
502                         {
503                             ++group;
504                             if(group < grouping_size)
505                             {
506                                 char const grp_size = grouping[group];
507                                 last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
508                             }
509
510                             left = last_grp_size;
511                             --finish;
512                             Traits::assign(*finish, thousands_sep);
513                         }
514
515                         --left;
516
517                         --finish;
518                         int_type const digit = static_cast<int_type>(n % 10U);
519                         Traits::assign(*finish, Traits::to_char_type(zero + digit));
520                         n /= 10;
521                     } while(n);
522                     return finish;
523                 }
524             }
525 #endif
526             {
527                 do
528                 {
529                     --finish;
530                     int_type const digit = static_cast<int_type>(n % 10U);
531                     Traits::assign(*finish, Traits::to_char_type(zero + digit));
532                     n /= 10;
533                 } while(n);
534             }
535
536             return finish;
537         }
538     }
539
540     namespace detail // lcast_ret_unsigned
541     {
542         template<class Traits, class T, class CharT>
543         inline bool lcast_ret_unsigned(T& value, const CharT* const begin, const CharT* end)
544         {
545 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
546             BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
547 #endif
548             typedef typename Traits::int_type int_type;
549             CharT const czero = lcast_char_constants<CharT>::zero;
550             --end;
551             value = 0;
552
553             if ( *end < czero || *end >= czero + 10 || begin > end)
554                 return false;
555             value = *end - czero;
556             --end;
557             T multiplier = 1;
558
559 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
560             std::locale loc;
561             if (loc != std::locale::classic()) {
562                 typedef std::numpunct<CharT> numpunct;
563                 numpunct const& np = BOOST_USE_FACET(numpunct, loc);
564                 std::string const& grouping = np.grouping();
565                 std::string::size_type const grouping_size = grouping.size();
566
567                 /* According to Programming languages - C++
568                  * we MUST check for correct grouping
569                  */
570                 if (grouping_size && grouping[0] > 0)
571                 {
572                     unsigned char current_grouping = 0;
573                     CharT const thousands_sep = np.thousands_sep();
574                     char remained = grouping[current_grouping] - 1;
575                     bool shall_we_return = true;
576
577                     for(;end>=begin; --end)
578                     {
579                         if (remained) {
580                             T const new_sub_value = multiplier * 10 * (*end - czero);
581
582                             if (*end < czero || *end >= czero + 10
583                                     /* detecting overflow */
584                                     || new_sub_value/10 != multiplier * (*end - czero)
585                                     || static_cast<T>((std::numeric_limits<T>::max)()-new_sub_value) < value
586                                     )
587                                 return false;
588
589                             value += new_sub_value;
590                             multiplier *= 10;
591                             --remained;
592                         } else {
593                             if ( !Traits::eq(*end, thousands_sep) ) //|| begin == end ) return false;
594                             {
595                                 /*
596                                  * According to Programming languages - C++
597                                  * Digit grouping is checked. That is, the positions of discarded
598                                  * separators is examined for consistency with
599                                  * use_facet<numpunct<charT> >(loc ).grouping()
600                                  *
601                                  * BUT what if there is no separators at all and grouping()
602                                  * is not empty? Well, we have no extraced separators, so we
603                                  * won`t check them for consistency. This will allow us to
604                                  * work with "C" locale from other locales
605                                  */
606                                 shall_we_return = false;
607                                 break;
608                             } else {
609                                 if ( begin == end ) return false;
610                                 if (current_grouping < grouping_size-1 ) ++current_grouping;
611                                 remained = grouping[current_grouping];
612                             }
613                         }
614                     }
615
616                     if (shall_we_return) return true;
617                 }
618             }
619 #endif
620             {
621                 while ( begin <= end )
622                 {
623                     T const new_sub_value = multiplier * 10 * (*end - czero);
624
625                     if (*end < czero || *end >= czero + 10
626                             /* detecting overflow */
627                             || new_sub_value/10 != multiplier * (*end - czero)
628                             || static_cast<T>((std::numeric_limits<T>::max)()-new_sub_value) < value
629                             )
630                         return false;
631
632                     value += new_sub_value;
633                     multiplier *= 10;
634                     --end;
635                 }
636             }
637             return true;
638         }
639     }
640
641     namespace detail
642     {
643         /* Returns true and sets the correct value if found NaN or Inf. */
644         template <class CharT, class T>
645         inline bool parse_inf_nan_impl(const CharT* begin, const CharT* end, T& value
646             , const CharT* lc_NAN, const CharT* lc_nan
647             , const CharT* lc_INFINITY, const CharT* lc_infinity
648             , const CharT opening_brace, const CharT closing_brace)
649         {
650             using namespace std;
651             const wchar_t minus = lcast_char_constants<wchar_t>::minus;
652             const wchar_t plus = lcast_char_constants<wchar_t>::plus;
653             const int inifinity_size = 8;
654
655             bool has_minus = false;
656             /* Parsing +/- */
657             if( *begin == minus)
658             {
659                 ++ begin;
660                 has_minus = true;
661             }
662             else if( *begin == plus ) ++begin;
663
664             if( end-begin < 3 ) return false;
665             if( !memcmp(begin, lc_nan, 3*sizeof(CharT)) || !memcmp(begin, lc_NAN, 3*sizeof(CharT)) )
666             {
667                 begin += 3;
668                 if (end != begin) /* It is 'nan(...)' or some bad input*/
669                 {
670                     if(end-begin<2) return false; // bad input
671                     -- end;
672                     if( *begin != opening_brace || *end != closing_brace) return false; // bad input
673                 }
674
675                 if( !has_minus ) value = std::numeric_limits<T>::quiet_NaN();
676                 else value = (boost::math::changesign) (std::numeric_limits<T>::quiet_NaN());
677                 return true;
678             } else
679             if (( /* 'INF' or 'inf' */
680                   end-begin==3
681                   &&
682                   (!memcmp(begin, lc_infinity, 3*sizeof(CharT)) || !memcmp(begin, lc_INFINITY, 3*sizeof(CharT)))
683                 )
684                 ||
685                 ( /* 'INFINITY' or 'infinity' */
686                   end-begin==inifinity_size
687                   &&
688                   (!memcmp(begin, lc_infinity, inifinity_size)|| !memcmp(begin, lc_INFINITY, inifinity_size))
689                 )
690              )
691             {
692                 if( !has_minus ) value = std::numeric_limits<T>::infinity();
693                 else value = (boost::math::changesign) (std::numeric_limits<T>::infinity());
694                 return true;
695             }
696
697             return false;
698         }
699
700 #ifndef BOOST_LCAST_NO_WCHAR_T
701         template <class T>
702         bool parse_inf_nan(const wchar_t* begin, const wchar_t* end, T& value)
703         {
704             return parse_inf_nan_impl(begin, end, value
705                                , L"NAN", L"nan"
706                                , L"INFINITY", L"infinity"
707                                , L'(', L')');
708         }
709 #endif
710
711         template <class CharT, class T>
712         bool parse_inf_nan(const CharT* begin, const CharT* end, T& value)
713         {
714             return parse_inf_nan_impl(begin, end, value
715                                , "NAN", "nan"
716                                , "INFINITY", "infinity"
717                                , '(', ')');
718         }
719 #ifndef BOOST_LCAST_NO_WCHAR_T
720         template <class T>
721         bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value)
722         {
723             using namespace std;
724             if ( (boost::math::isnan)(value) )
725             {
726                 if ( (boost::math::signbit)(value) )
727                 {
728                     memcpy(begin,L"-nan", sizeof(L"-nan"));
729                     end = begin + 4;
730                 } else
731                 {
732                     memcpy(begin,L"nan", sizeof(L"nan"));
733                     end = begin + 3;
734                 }
735                 return true;
736             } else if ( (boost::math::isinf)(value) )
737             {
738                 if ( (boost::math::signbit)(value) )
739                 {
740                     memcpy(begin,L"-inf", sizeof(L"-inf"));
741                     end = begin + 4;
742                 } else
743                 {
744                     memcpy(begin,L"inf", sizeof(L"inf"));
745                     end = begin + 3;
746                 }
747                 return true;
748             }
749
750             return false;
751         }
752 #endif
753         template <class CharT, class T>
754         bool put_inf_nan(CharT* begin, CharT*& end, const T& value)
755         {
756             using namespace std;
757             if ( (boost::math::isnan)(value) )
758             {
759                 if ( (boost::math::signbit)(value) )
760                 {
761                     memcpy(begin,"-nan", sizeof("-nan"));
762                     end = begin + 4;
763                 } else
764                 {
765                     memcpy(begin,"nan", sizeof("nan"));
766                     end = begin + 3;
767                 }
768                 return true;
769             } else if ( (boost::math::isinf)(value) )
770             {
771                 if ( (boost::math::signbit)(value) )
772                 {
773                     memcpy(begin,"-inf", sizeof("-inf"));
774                     end = begin + 4;
775                 } else
776                 {
777                     memcpy(begin,"inf", sizeof("inf"));
778                     end = begin + 3;
779                 }
780                 return true;
781             }
782
783             return false;
784         }
785
786     }
787
788
789     namespace detail // lcast_ret_float
790     {
791         template <class T>
792         struct mantissa_holder_type
793         {
794             /* Can not be used with this type */
795         };
796
797         template <>
798         struct mantissa_holder_type<float>
799         {
800             typedef unsigned int type;
801         };
802
803         template <>
804         struct mantissa_holder_type<double>
805         {
806 #if defined(BOOST_HAS_LONG_LONG)
807             typedef boost::ulong_long_type type;
808 #elif defined(BOOST_HAS_MS_INT64)
809             typedef unsigned __int64 type;
810 #endif
811         };
812
813         template<class Traits, class T, class CharT>
814         inline bool lcast_ret_float(T& value, const CharT* begin, const CharT* end)
815         {
816
817 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
818             std::locale loc;
819             typedef std::numpunct<CharT> numpunct;
820             numpunct const& np = BOOST_USE_FACET(numpunct, loc);
821             std::string const grouping(
822                     (loc == std::locale::classic())
823                     ? std::string()
824                     : np.grouping()
825             );
826             std::string::size_type const grouping_size = grouping.size();
827             CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0;
828             CharT const decimal_point = np.decimal_point();
829             bool found_grouping = false;
830             unsigned int last_grouping_pos = grouping_size - 1;
831 #else
832             CharT const decimal_point = lcast_char_constants<CharT>::c_decimal_separator;
833 #endif
834
835             CharT const czero = lcast_char_constants<CharT>::zero;
836             CharT const minus = lcast_char_constants<CharT>::minus;
837             CharT const plus = lcast_char_constants<CharT>::plus;
838             CharT const capital_e = lcast_char_constants<CharT>::capital_e;
839             CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e;
840
841             value = 0.0;
842
843             if (parse_inf_nan(begin, end, value)) return true;
844
845             typedef typename Traits::int_type int_type;
846             typedef BOOST_DEDUCED_TYPENAME mantissa_holder_type<T>::type mantissa_type;
847             int_type const zero = Traits::to_int_type(czero);
848             if (begin == end) return false;
849
850             /* Getting the plus/minus sign */
851             bool has_minus = false;
852             if ( *begin == minus ) {
853                 ++ begin;
854                 has_minus = true;
855                 if (begin == end) return false;
856             } else if ( *begin == plus ) {
857                 ++begin;
858                 if (begin == end) return false;
859             }
860
861             bool found_decimal = false;
862             bool found_number_before_exp = false;
863             int pow_of_10 = 0;
864             mantissa_type mantissa=0;
865             bool is_mantissa_full = false;
866
867             char length_since_last_delim = 0;
868
869             while ( begin != end )
870             {
871                 if (found_decimal) {
872                     /* We allow no thousand_separators after decimal point */
873
874                     mantissa_type tmp_mantissa = mantissa * 10u;
875                     if ( *begin == lowercase_e || *begin == capital_e ) break;
876                     if ( *begin < czero || *begin >= czero + 10 ) return false;
877                     if (    is_mantissa_full
878                             || tmp_mantissa / 10u != mantissa
879                             || (std::numeric_limits<mantissa_type>::max)()-(*begin - zero) < tmp_mantissa
880                             ) {
881                         is_mantissa_full = true;
882                         ++ begin;
883                         continue;
884                     }
885
886                     -- pow_of_10;
887                     mantissa = tmp_mantissa;
888                     mantissa += *begin - zero;
889
890                     found_number_before_exp = true;
891                 } else {
892
893                     if (*begin >= czero && *begin < czero + 10) {
894
895                         /* Checking for mantissa overflow. If overflow will
896                          * occur, them we only increase multiplyer
897                          */
898                         mantissa_type tmp_mantissa = mantissa * 10u;
899                         if(     !is_mantissa_full
900                                 && tmp_mantissa / 10u == mantissa
901                                 && (std::numeric_limits<mantissa_type>::max)()-(*begin - zero) >= tmp_mantissa
902                             )
903                         {
904                             mantissa = tmp_mantissa;
905                             mantissa += *begin - zero;
906                         } else
907                         {
908                             is_mantissa_full = true;
909                             ++ pow_of_10;
910                         }
911
912                         found_number_before_exp = true;
913                         ++ length_since_last_delim;
914                     } else if ( *begin == decimal_point || *begin == lowercase_e || *begin == capital_e) {
915 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
916                         /* If ( we need to check grouping
917                          *      and (   grouping missmatches
918                          *              or grouping position is incorrect
919                          *              or we are using the grouping position 0 twice
920                          *           )
921                          *    ) then return error
922                          */
923                         if( grouping_size && found_grouping
924                             && (
925                                    length_since_last_delim != grouping[0]
926                                    || last_grouping_pos>1
927                                    || (last_grouping_pos==0 && grouping_size>1)
928                                 )
929                            ) return false;
930 #endif
931
932                         if(*begin == decimal_point){
933                             ++ begin;
934                             found_decimal = true;
935                             continue;
936                         }else {
937                             if (!found_number_before_exp) return false;
938                             break;
939                         }
940                     }
941 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
942                     else if (grouping_size && *begin == thousands_sep){
943                         if(found_grouping)
944                         {
945                             /* It is not he first time, when we find thousands separator,
946                              * so we need to chek, is the distance between two groupings
947                              * equal to grouping[last_grouping_pos] */
948
949                             if (length_since_last_delim != grouping[last_grouping_pos] )
950                             {
951                                 if (!last_grouping_pos) return false;
952                                 else
953                                 {
954                                     -- last_grouping_pos;
955                                     if (length_since_last_delim != grouping[last_grouping_pos]) return false;
956                                 }
957                             } else
958                                 /* We are calling the grouping[0] twice, when grouping size is more than 1 */
959                                 if (grouping_size>1u && last_grouping_pos+1<grouping_size) return false;
960
961                         } else {
962                             /* Delimiter at the begining ',000' */
963                             if (!length_since_last_delim) return false;
964
965                             found_grouping = true;
966                             if (length_since_last_delim > grouping[last_grouping_pos] ) return false;
967                         }
968
969                         length_since_last_delim = 0;
970                         ++ begin;
971
972                         /* Delimiter at the end '100,' */
973                         if (begin == end) return false;
974                         continue;
975                     }
976 #endif
977                     else return false;
978                 }
979
980                 ++begin;
981             }
982
983             // Exponent found
984             if ( begin != end && ( *begin == lowercase_e || *begin == capital_e ) ) {
985                 ++ begin;
986                 if ( begin == end ) return false;
987
988                 bool exp_has_minus = false;
989                 if( *begin == minus ) {
990                     exp_has_minus = true;
991                     ++ begin;
992                     if ( begin == end ) return false;
993                 } else if (*begin == plus ) {
994                     ++ begin;
995                     if ( begin == end ) return false;
996                 }
997
998                 int exp_pow_of_10 = 0;
999                 while ( begin != end )
1000                 {
1001                     if ( *begin < czero
1002                             || *begin >= czero + 10
1003                             || exp_pow_of_10 * 10 < exp_pow_of_10) /* Overflows are checked lower more precisely*/
1004                         return false;
1005
1006                     exp_pow_of_10 *= 10;
1007                     exp_pow_of_10 += *begin - zero;
1008                     ++ begin;
1009                 };
1010
1011                 if ( exp_pow_of_10 ) {
1012                     /* Overflows are checked lower */
1013                     if ( exp_has_minus ) {
1014                         pow_of_10 -= exp_pow_of_10;
1015                     } else {
1016                         pow_of_10 += exp_pow_of_10;
1017                     }
1018                 }
1019             }
1020
1021             /* We need a more accurate algorithm... We can not use current algorithm
1022              * with long doubles (and with doubles if sizeof(double)==sizeof(long double)).
1023              */
1024             long double result = std::pow(10.0L, pow_of_10) * mantissa;
1025             value = static_cast<T>( has_minus ? (boost::math::changesign)(result) : result);
1026
1027             if ( (boost::math::isinf)(value) || (boost::math::isnan)(value) ) return false;
1028
1029             return true;
1030         }
1031     }
1032
1033     namespace detail // stl_buf_unlocker
1034     {
1035         template< class BufferType, class CharT >
1036         class stl_buf_unlocker: public BufferType{
1037         public:
1038             typedef BufferType base_class;
1039 #ifndef BOOST_NO_USING_TEMPLATE
1040             using base_class::pptr;
1041             using base_class::pbase;
1042             using base_class::setg;
1043             using base_class::setp;
1044 #else
1045             CharT* pptr() const { return base_class::pptr(); }
1046             CharT* pbase() const { return base_class::pbase(); }
1047             void setg(CharT* gbeg, CharT* gnext, CharT* gend){ return base_class::setg(gbeg, gnext, gend); }
1048             void setp(CharT* pbeg, CharT* pend) { return setp(pbeg, pend); }
1049 #endif
1050         };
1051     }
1052
1053     namespace detail
1054     {
1055         struct do_not_construct_stringbuffer_t{};
1056     }
1057
1058     namespace detail // optimized stream wrapper
1059     {
1060         // String representation of Source has an upper limit.
1061         template< class CharT // a result of widest_char transformation
1062                 , class Traits // usually char_traits<CharT>
1063                 , bool RequiresStringbuffer
1064                 >
1065         class lexical_stream_limited_src
1066         {
1067             typedef stl_buf_unlocker<std::basic_streambuf<CharT, Traits>, CharT > local_streambuffer_t;
1068
1069 #if defined(BOOST_NO_STRINGSTREAM)
1070             typedef stl_buf_unlocker<std::strstream, CharT > local_stringbuffer_t;
1071 #elif defined(BOOST_NO_STD_LOCALE)
1072             typedef stl_buf_unlocker<std::stringstream, CharT > local_stringbuffer_t;
1073 #else
1074             typedef stl_buf_unlocker<std::basic_stringbuf<CharT, Traits>, CharT > local_stringbuffer_t;
1075 #endif
1076             typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1077                 RequiresStringbuffer,
1078                 local_stringbuffer_t,
1079                 do_not_construct_stringbuffer_t
1080             >::type deduced_stringbuffer_t;
1081
1082             // A string representation of Source is written to [start, finish).
1083             CharT* start;
1084             CharT* finish;
1085             deduced_stringbuffer_t stringbuffer;
1086
1087         public:
1088             lexical_stream_limited_src(CharT* sta, CharT* fin)
1089               : start(sta)
1090               , finish(fin)
1091             {}
1092
1093         private:
1094             // Undefined:
1095             lexical_stream_limited_src(lexical_stream_limited_src const&);
1096             void operator=(lexical_stream_limited_src const&);
1097
1098 /************************************ HELPER FUNCTIONS FOR OPERATORS << ( ... ) ********************************/
1099             bool shl_char(CharT ch)
1100             {
1101                 Traits::assign(*start, ch);
1102                 finish = start + 1;
1103                 return true;
1104             }
1105
1106 #ifndef BOOST_LCAST_NO_WCHAR_T
1107             template <class T>
1108             bool shl_char(T ch)
1109             {
1110                 BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)) ,
1111                     "boost::lexical_cast does not support conversions from whar_t to char types."
1112                     "Use boost::locale instead" );
1113 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
1114                 std::locale loc;
1115                 wchar_t w = BOOST_USE_FACET(std::ctype<wchar_t>, loc).widen(ch);
1116 #else
1117                 wchar_t w = ch;
1118 #endif
1119                 Traits::assign(*start, w);
1120                 finish = start + 1;
1121                 return true;
1122             }
1123 #endif
1124
1125             bool shl_char_array(CharT const* str)
1126             {
1127                 start = const_cast<CharT*>(str);
1128                 finish = start + Traits::length(str);
1129                 return true;
1130             }
1131
1132 #ifndef BOOST_LCAST_NO_WCHAR_T
1133             template <class T>
1134             bool shl_char_array(T const* str)
1135             {
1136                 BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)),
1137                     "boost::lexical_cast does not support conversions from wchar_t to char types."
1138                     "Use boost::locale instead" );
1139                 return shl_input_streamable(str);
1140             }
1141 #endif
1142
1143             template<typename InputStreamable>
1144             bool shl_input_streamable(InputStreamable& input)
1145             {
1146                 std::basic_ostream<CharT> stream(&stringbuffer);
1147                 bool const result = !(stream << input).fail();
1148                 start = stringbuffer.pbase();
1149                 finish = stringbuffer.pptr();
1150                 return result && (start != finish);
1151             }
1152
1153             template <class T>
1154             inline bool shl_signed(T n)
1155             {
1156                 start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
1157                 if(n < 0)
1158                 {
1159                     --start;
1160                     CharT const minus = lcast_char_constants<CharT>::minus;
1161                     Traits::assign(*start, minus);
1162                 }
1163                 return true;
1164             }
1165
1166 #if (defined _MSC_VER)
1167 # pragma warning( push )
1168 // C4996: This function or variable may be unsafe. Consider using sprintf_s instead
1169 # pragma warning( disable : 4996 )
1170 #endif
1171
1172             template <class T>
1173             bool shl_float(float val,T* out)
1174             {   using namespace std;
1175                 if (put_inf_nan(start,finish,val)) return true;
1176                 finish = start + sprintf(out,"%.*g", static_cast<int>(boost::detail::lcast_get_precision<float >()), val );
1177                 return finish > start;
1178             }
1179
1180             template <class T>
1181             bool shl_double(double val,T* out)
1182             {   using namespace std;
1183                 if (put_inf_nan(start,finish,val)) return true;
1184                 finish = start + sprintf(out,"%.*lg", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
1185                 return finish > start;
1186             }
1187 #ifndef __MINGW32__
1188             template <class T>
1189             bool shl_long_double(long double val,T* out)
1190             {   using namespace std;
1191                 if (put_inf_nan(start,finish,val)) return true;
1192                 finish = start + sprintf(out,"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
1193                 return finish > start;
1194             }
1195 #endif
1196
1197 #if (defined _MSC_VER)
1198 # pragma warning( pop )
1199 #endif
1200
1201
1202 #ifndef BOOST_LCAST_NO_WCHAR_T
1203             bool shl_float(float val,wchar_t* out)
1204             {   using namespace std;
1205                 if (put_inf_nan(start,finish,val)) return true;
1206                 finish = start + swprintf(out,
1207 #if !defined(__MINGW32__) && !defined(UNDER_CE)
1208                                           finish-start,
1209 #endif
1210                                           L"%.*g", static_cast<int>(boost::detail::lcast_get_precision<float >()), val );
1211
1212                 return finish > start;
1213             }
1214
1215
1216             bool shl_double(double val,wchar_t* out)
1217             {   using namespace std;
1218                 if (put_inf_nan(start,finish,val)) return true;
1219                 /* __MINGW32__ is defined for both mingw.org and for mingw-w64.
1220                  * For mingw-w64, __MINGW64__ is defined, too, when targetting
1221                  * 64 bits.
1222                  *
1223                  * swprintf realization in MinGW and under WinCE does not conform
1224                  * to the ISO C
1225                  * Standard.
1226                  */
1227                 finish = start + swprintf(out,
1228 #if !defined(__MINGW32__) && !defined(UNDER_CE)
1229                                           finish-start,
1230 #endif
1231                                           L"%.*lg", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
1232                 return finish > start;
1233             }
1234
1235 #ifndef __MINGW32__
1236             bool shl_long_double(long double val,wchar_t* out)
1237             {   using namespace std;
1238                 if (put_inf_nan(start,finish,val)) return true;
1239                 finish = start + swprintf(out,
1240 #if !defined(UNDER_CE)
1241                                           finish-start,
1242 #endif
1243                                           L"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
1244                 return finish > start;
1245             }
1246 #endif
1247
1248 #endif
1249
1250 /************************************ OPERATORS << ( ... ) ********************************/
1251         public:
1252             template<class Alloc>
1253             bool operator<<(std::basic_string<CharT,Traits,Alloc> const& str)
1254             {
1255                 start = const_cast<CharT*>(str.data());
1256                 finish = start + str.length();
1257                 return true;
1258             }
1259
1260             bool operator<<(bool value)
1261             {
1262                 CharT const czero = lcast_char_constants<CharT>::zero;
1263                 Traits::assign(*start, Traits::to_char_type(czero + value));
1264                 finish = start + 1;
1265                 return true;
1266             }
1267
1268             bool operator<<(char ch)                    { return shl_char(ch); }
1269             bool operator<<(unsigned char ch)           { return ((*this) << static_cast<char>(ch)); }
1270             bool operator<<(signed char ch)             { return ((*this) << static_cast<char>(ch)); }
1271 #if !defined(BOOST_LCAST_NO_WCHAR_T)
1272             bool operator<<(wchar_t const* str)         { return shl_char_array(str); }
1273             bool operator<<(wchar_t * str)              { return shl_char_array(str); }
1274 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
1275             bool operator<<(wchar_t ch)                 { return shl_char(ch); }
1276 #endif
1277 #endif
1278             bool operator<<(unsigned char const* ch)    { return ((*this) << reinterpret_cast<char const*>(ch)); }
1279             bool operator<<(unsigned char * ch)         { return ((*this) << reinterpret_cast<char *>(ch)); }
1280             bool operator<<(signed char const* ch)      { return ((*this) << reinterpret_cast<char const*>(ch)); }
1281             bool operator<<(signed char * ch)           { return ((*this) << reinterpret_cast<char *>(ch)); }
1282             bool operator<<(char const* str)            { return shl_char_array(str); }
1283             bool operator<<(char* str)                  { return shl_char_array(str); }
1284             bool operator<<(short n)                    { return shl_signed(n); }
1285             bool operator<<(int n)                      { return shl_signed(n); }
1286             bool operator<<(long n)                     { return shl_signed(n); }
1287             bool operator<<(unsigned short n)           { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1288             bool operator<<(unsigned int n)             { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1289             bool operator<<(unsigned long n)            { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1290
1291 #if defined(BOOST_HAS_LONG_LONG)
1292             bool operator<<(boost::ulong_long_type n)   { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1293             bool operator<<(boost::long_long_type n)    { return shl_signed(n); }
1294 #elif defined(BOOST_HAS_MS_INT64)
1295             bool operator<<(unsigned __int64 n)         { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1296             bool operator<<(         __int64 n)         { return shl_signed(n); }
1297 #endif
1298             bool operator<<(float val)                  { return shl_float(val,start); }
1299             bool operator<<(double val)                 { return shl_double(val,start); }
1300             bool operator<<(long double val)            {
1301 #ifndef __MINGW32__
1302                 return shl_long_double(val,start);
1303 #else
1304                 return shl_double(val,start);
1305 #endif
1306             }
1307
1308             template<class InStreamable>
1309             bool operator<<(const InStreamable& input)  { return shl_input_streamable(input); }
1310
1311 /************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/
1312         private:
1313             template <typename Type>
1314             bool shr_unsigned(Type& output)
1315             {
1316                 CharT const minus = lcast_char_constants<CharT>::minus;
1317                 CharT const plus = lcast_char_constants<CharT>::plus;
1318                 bool has_minus = false;
1319
1320                 /* We won`t use `start' any more, so no need in decrementing it after */
1321                 if ( Traits::eq(minus,*start) )
1322                 {
1323                     ++start;
1324                     has_minus = true;
1325                 } else if ( Traits::eq( plus, *start ) )
1326                 {
1327                     ++start;
1328                 }
1329
1330                 bool const succeed = lcast_ret_unsigned<Traits>(output, start, finish);
1331 #if (defined _MSC_VER)
1332 # pragma warning( push )
1333 // C4146: unary minus operator applied to unsigned type, result still unsigned
1334 # pragma warning( disable : 4146 )
1335 #elif defined( __BORLANDC__ )
1336 # pragma option push -w-8041
1337 #endif
1338                 if (has_minus) output = static_cast<Type>(-output);
1339 #if (defined _MSC_VER)
1340 # pragma warning( pop )
1341 #elif defined( __BORLANDC__ )
1342 # pragma option pop
1343 #endif
1344                 return succeed;
1345             }
1346
1347             template <typename Type>
1348             bool shr_signed(Type& output)
1349             {
1350                 CharT const minus = lcast_char_constants<CharT>::minus;
1351                 CharT const plus = lcast_char_constants<CharT>::plus;
1352                 typedef BOOST_DEDUCED_TYPENAME make_unsigned<Type>::type utype;
1353                 utype out_tmp =0;
1354                 bool has_minus = false;
1355
1356                 /* We won`t use `start' any more, so no need in decrementing it after */
1357                 if ( Traits::eq(minus,*start) )
1358                 {
1359                     ++start;
1360                     has_minus = true;
1361                 } else if ( Traits::eq(plus, *start) )
1362                 {
1363                     ++start;
1364                 }
1365
1366                 bool succeed = lcast_ret_unsigned<Traits>(out_tmp, start, finish);
1367                 if (has_minus) {
1368 #if (defined _MSC_VER)
1369 # pragma warning( push )
1370 // C4146: unary minus operator applied to unsigned type, result still unsigned
1371 # pragma warning( disable : 4146 )
1372 #elif defined( __BORLANDC__ )
1373 # pragma option push -w-8041
1374 #endif
1375                     utype const comp_val = static_cast<utype>(-(std::numeric_limits<Type>::min)());
1376                     succeed = succeed && out_tmp<=comp_val;
1377                     output = -out_tmp;
1378 #if (defined _MSC_VER)
1379 # pragma warning( pop )
1380 #elif defined( __BORLANDC__ )
1381 # pragma option pop
1382 #endif
1383                 } else {
1384                     utype const comp_val = static_cast<utype>((std::numeric_limits<Type>::max)());
1385                     succeed = succeed && out_tmp<=comp_val;
1386                     output = out_tmp;
1387                 }
1388                 return succeed;
1389             }
1390
1391             template<typename InputStreamable>
1392             bool shr_using_base_class(InputStreamable& output)
1393             {
1394 #if (defined _MSC_VER)
1395 # pragma warning( push )
1396   // conditional expression is constant
1397 # pragma warning( disable : 4127 )
1398 #endif
1399                 if(is_pointer<InputStreamable>::value)
1400                     return false;
1401
1402                 local_streambuffer_t bb;
1403                 bb.setg(start, start, finish);
1404                 std::basic_istream<CharT> stream(&bb);
1405                 stream.unsetf(std::ios::skipws);
1406                 lcast_set_precision(stream, static_cast<InputStreamable*>(0));
1407 #if (defined _MSC_VER)
1408 # pragma warning( pop )
1409 #endif
1410                 return stream >> output &&
1411                     stream.get() ==
1412 #if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
1413         // GCC 2.9x lacks std::char_traits<>::eof().
1414         // We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3
1415         // configurations, which do provide std::char_traits<>::eof().
1416
1417                     EOF;
1418 #else
1419                 Traits::eof();
1420 #endif
1421             }
1422
1423             template<class T>
1424             inline bool shr_xchar(T& output)
1425             {
1426                 BOOST_STATIC_ASSERT_MSG(( sizeof(CharT) == sizeof(T) ),
1427                     "boost::lexical_cast does not support conversions from whar_t to char types."
1428                     "Use boost::locale instead" );
1429                 bool const ok = (finish - start == 1);
1430                 if(ok) {
1431                     CharT out;
1432                     Traits::assign(out, *start);
1433                     output = static_cast<T>(out);
1434                 }
1435                 return ok;
1436             }
1437
1438 /************************************ OPERATORS >> ( ... ) ********************************/
1439         public:
1440             bool operator>>(unsigned short& output)             { return shr_unsigned(output); }
1441             bool operator>>(unsigned int& output)               { return shr_unsigned(output); }
1442             bool operator>>(unsigned long int& output)          { return shr_unsigned(output); }
1443             bool operator>>(short& output)                      { return shr_signed(output); }
1444             bool operator>>(int& output)                        { return shr_signed(output); }
1445             bool operator>>(long int& output)                   { return shr_signed(output); }
1446 #if defined(BOOST_HAS_LONG_LONG)
1447             bool operator>>(boost::ulong_long_type& output)     { return shr_unsigned(output); }
1448             bool operator>>(boost::long_long_type& output)      { return shr_signed(output); }
1449 #elif defined(BOOST_HAS_MS_INT64)
1450             bool operator>>(unsigned __int64& output)           { return shr_unsigned(output); }
1451             bool operator>>(__int64& output)                    { return shr_signed(output); }
1452
1453 #endif
1454             bool operator>>(CharT& output)                      { return shr_xchar(output); }
1455             bool operator>>(unsigned char& output)              { return shr_xchar(output); }
1456             bool operator>>(signed char& output)                { return shr_xchar(output); }
1457 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
1458             bool operator>>(std::string& str)                   { str.assign(start, finish); return true; }
1459 #   ifndef BOOST_LCAST_NO_WCHAR_T
1460             bool operator>>(std::wstring& str)                  { str.assign(start, finish); return true; }
1461 #   endif
1462 #else
1463             template<class Alloc>
1464             bool operator>>(std::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
1465 #endif
1466             /*
1467              * case "-0" || "0" || "+0" :   output = false; return true;
1468              * case "1" || "+1":            output = true;  return true;
1469              * default:                     return false;
1470              */
1471             bool operator>>(bool& output)
1472             {
1473                 CharT const zero = lcast_char_constants<CharT>::zero;
1474                 CharT const plus = lcast_char_constants<CharT>::plus;
1475                 CharT const minus = lcast_char_constants<CharT>::minus;
1476
1477                 switch(finish-start)
1478                 {
1479                     case 1:
1480                         output = Traits::eq(start[0],  zero+1);
1481                         return output || Traits::eq(start[0], zero );
1482                     case 2:
1483                         if ( Traits::eq( plus, *start) )
1484                         {
1485                             ++start;
1486                             output = Traits::eq(start[0], zero +1);
1487                             return output || Traits::eq(start[0], zero );
1488                         } else
1489                         {
1490                             output = false;
1491                             return Traits::eq( minus, *start)
1492                                 && Traits::eq( zero, start[1]);
1493                         }
1494                     default:
1495                         output = false; // Suppress warning about uninitalized variable
1496                         return false;
1497                 }
1498             }
1499
1500             bool operator>>(float& output) { return lcast_ret_float<Traits>(output,start,finish); }
1501
1502         private:
1503             // Not optimised converter
1504             template <class T>
1505             bool float_types_converter_internal(T& output, int /*tag*/) {
1506                 if (parse_inf_nan(start, finish, output)) return true;
1507                 bool return_value = shr_using_base_class(output);
1508
1509                 /* Some compilers and libraries successfully
1510                  * parse 'inf', 'INFINITY', '1.0E', '1.0E-'...
1511                  * We are trying to provide a unified behaviour,
1512                  * so we just forbid such conversions (as some
1513                  * of the most popular compilers/libraries do)
1514                  * */
1515                 CharT const minus = lcast_char_constants<CharT>::minus;
1516                 CharT const plus = lcast_char_constants<CharT>::plus;
1517                 CharT const capital_e = lcast_char_constants<CharT>::capital_e;
1518                 CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e;
1519                 if ( return_value &&
1520                      (
1521                         *(finish-1) == lowercase_e                   // 1.0e
1522                         || *(finish-1) == capital_e                  // 1.0E
1523                         || *(finish-1) == minus                      // 1.0e- or 1.0E-
1524                         || *(finish-1) == plus                       // 1.0e+ or 1.0E+
1525                      )
1526                 ) return false;
1527
1528                 return return_value;
1529             }
1530
1531             // Optimised converter
1532             bool float_types_converter_internal(double& output,char /*tag*/) {
1533                 return lcast_ret_float<Traits>(output,start,finish);
1534             }
1535         public:
1536
1537             bool operator>>(double& output)
1538             {
1539                 /*
1540                  * Some compilers implement long double as double. In that case these types have
1541                  * same size, same precision, same max and min values... And it means,
1542                  * that current implementation of lcast_ret_float cannot be used for type
1543                  * double, because it will give a big precision loss.
1544                  * */
1545                 boost::mpl::if_c<
1546 #if defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64)
1547                     ::boost::type_traits::ice_eq< sizeof(double), sizeof(long double) >::value,
1548 #else
1549                      0
1550 #endif
1551                     int,
1552                     char
1553                 >::type tag = 0;
1554
1555                 return float_types_converter_internal(output, tag);
1556             }
1557
1558             bool operator>>(long double& output)
1559             {
1560                 int tag = 0;
1561                 return float_types_converter_internal(output, tag);
1562             }
1563
1564             // Generic istream-based algorithm.
1565             // lcast_streambuf_for_target<InputStreamable>::value is true.
1566             template<typename InputStreamable>
1567             bool operator>>(InputStreamable& output) { return shr_using_base_class(output); }
1568         };
1569     }
1570
1571 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
1572
1573     // call-by-const reference version
1574
1575     namespace detail
1576     {
1577         template<class T>
1578         struct array_to_pointer_decay
1579         {
1580             typedef T type;
1581         };
1582
1583         template<class T, std::size_t N>
1584         struct array_to_pointer_decay<T[N]>
1585         {
1586             typedef const T * type;
1587         };
1588
1589         template<typename T>
1590         struct is_stdstring
1591         {
1592             BOOST_STATIC_CONSTANT(bool, value = false );
1593         };
1594
1595         template<typename CharT, typename Traits, typename Alloc>
1596         struct is_stdstring< std::basic_string<CharT, Traits, Alloc> >
1597         {
1598             BOOST_STATIC_CONSTANT(bool, value = true );
1599         };
1600
1601         template<typename T>
1602         struct is_char_or_wchar
1603         {
1604         private:
1605 #ifndef BOOST_LCAST_NO_WCHAR_T
1606             typedef wchar_t wchar_t_if_supported;
1607 #else
1608             typedef char wchar_t_if_supported;
1609 #endif
1610
1611 #ifndef BOOST_NO_CHAR16_T
1612             typedef char16_t char16_t_if_supported;
1613 #else
1614             typedef char char16_t_if_supported;
1615 #endif
1616
1617 #ifndef BOOST_NO_CHAR32_T
1618             typedef char32_t char32_t_if_supported;
1619 #else
1620             typedef char char32_t_if_supported;
1621 #endif
1622             public:
1623
1624             BOOST_STATIC_CONSTANT(bool, value =
1625                     (
1626                     ::boost::type_traits::ice_or<
1627                          is_same< T, char >::value,
1628                          is_same< T, wchar_t_if_supported >::value,
1629                          is_same< T, char16_t_if_supported >::value,
1630                          is_same< T, char32_t_if_supported >::value,
1631                          is_same< T, unsigned char >::value,
1632                          is_same< T, signed char >::value
1633                     >::value
1634                     )
1635             );
1636         };
1637
1638         template<typename Target, typename Source>
1639         struct is_arithmetic_and_not_xchars
1640         {
1641             BOOST_STATIC_CONSTANT(bool, value =
1642                (
1643                    ::boost::type_traits::ice_and<
1644                            is_arithmetic<Source>::value,
1645                            is_arithmetic<Target>::value,
1646                            ::boost::type_traits::ice_not<
1647                                 detail::is_char_or_wchar<Target>::value
1648                            >::value,
1649                            ::boost::type_traits::ice_not<
1650                                 detail::is_char_or_wchar<Source>::value
1651                            >::value
1652                    >::value
1653                )
1654             );
1655         };
1656
1657         /*
1658          * is_xchar_to_xchar<Target, Source>::value is true, when
1659          * Target and Souce are the same char types, or when
1660          * Target and Souce are char types of the same size.
1661          */
1662         template<typename Target, typename Source>
1663         struct is_xchar_to_xchar
1664         {
1665             BOOST_STATIC_CONSTANT(bool, value =
1666                 (
1667                     ::boost::type_traits::ice_or<
1668                         ::boost::type_traits::ice_and<
1669                              is_same<Source,Target>::value,
1670                              is_char_or_wchar<Target>::value
1671                         >::value,
1672                         ::boost::type_traits::ice_and<
1673                              ::boost::type_traits::ice_eq< sizeof(char),sizeof(Target)>::value,
1674                              ::boost::type_traits::ice_eq< sizeof(char),sizeof(Source)>::value,
1675                              is_char_or_wchar<Target>::value,
1676                              is_char_or_wchar<Source>::value
1677                         >::value
1678                     >::value
1679                 )
1680             );
1681         };
1682
1683         template<typename Target, typename Source>
1684         struct is_char_array_to_stdstring
1685         {
1686             BOOST_STATIC_CONSTANT(bool, value = false );
1687         };
1688
1689         template<typename CharT, typename Traits, typename Alloc>
1690         struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, CharT* >
1691         {
1692             BOOST_STATIC_CONSTANT(bool, value = true );
1693         };
1694
1695         template<typename CharT, typename Traits, typename Alloc>
1696         struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, const CharT* >
1697         {
1698             BOOST_STATIC_CONSTANT(bool, value = true );
1699         };
1700
1701 #if (defined _MSC_VER)
1702 # pragma warning( push )
1703 # pragma warning( disable : 4701 ) // possible use of ... before initialization
1704 # pragma warning( disable : 4702 ) // unreachable code
1705 # pragma warning( disable : 4267 ) // conversion from 'size_t' to 'unsigned int'
1706 #endif
1707         template<typename Target, typename Source>
1708         struct lexical_cast_do_cast
1709         {
1710             static inline Target lexical_cast_impl(const Source& arg)
1711             {
1712                 typedef BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
1713
1714                 typedef BOOST_DEDUCED_TYPENAME detail::widest_char<
1715                     BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
1716                     , BOOST_DEDUCED_TYPENAME detail::stream_char<src>::type
1717                 >::type char_type;
1718
1719                 typedef detail::lcast_src_length<src> lcast_src_length;
1720                 std::size_t const src_len = lcast_src_length::value;
1721                 char_type buf[src_len + 1];
1722                 lcast_src_length::check_coverage();
1723
1724                 typedef BOOST_DEDUCED_TYPENAME
1725                     deduce_char_traits<char_type,Target,Source>::type traits;
1726
1727                 typedef BOOST_DEDUCED_TYPENAME remove_pointer<src >::type removed_ptr_t;
1728                 const bool requires_stringbuf =
1729                         !(
1730                              ::boost::type_traits::ice_or<
1731                                  is_stdstring<src >::value,
1732                                  is_arithmetic<src >::value,
1733                                  ::boost::type_traits::ice_and<
1734                                      is_pointer<src >::value,
1735                                      is_char_or_wchar<removed_ptr_t >::value,
1736                                      ::boost::type_traits::ice_eq<
1737                                         sizeof(char_type),
1738                                         sizeof(removed_ptr_t)
1739                                      >::value
1740                                  >::value
1741                              >::value
1742                         );
1743
1744                 detail::lexical_stream_limited_src<char_type,traits, requires_stringbuf >
1745                         interpreter(buf, buf + src_len);
1746
1747                 Target result;
1748                 // Disabling ADL, by directly specifying operators.
1749                 if(!(interpreter.operator <<(arg) && interpreter.operator >>(result)))
1750                   BOOST_LCAST_THROW_BAD_CAST(Source, Target);
1751                 return result;
1752             }
1753         };
1754 #if (defined _MSC_VER)
1755 # pragma warning( pop )
1756 #endif
1757
1758         template<typename Source>
1759         struct lexical_cast_copy
1760         {
1761             static inline Source lexical_cast_impl(const Source &arg)
1762             {
1763                 return arg;
1764             }
1765         };
1766
1767         class precision_loss_error : public boost::numeric::bad_numeric_cast
1768         {
1769          public:
1770             virtual const char * what() const throw()
1771              {  return "bad numeric conversion: precision loss error"; }
1772         };
1773
1774         template<class S >
1775         struct throw_on_precision_loss
1776         {
1777          typedef boost::numeric::Trunc<S> Rounder;
1778          typedef S source_type ;
1779
1780          typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
1781
1782          static source_type nearbyint ( argument_type s )
1783          {
1784             source_type orig_div_round = s / Rounder::nearbyint(s);
1785
1786             if ( (orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > std::numeric_limits<source_type>::epsilon() )
1787                BOOST_THROW_EXCEPTION( precision_loss_error() );
1788             return s ;
1789          }
1790
1791          typedef typename Rounder::round_style round_style;
1792         } ;
1793
1794         template<typename Target, typename Source>
1795         struct lexical_cast_dynamic_num_not_ignoring_minus
1796         {
1797             static inline Target lexical_cast_impl(const Source &arg)
1798             {
1799                 try{
1800                     typedef boost::numeric::converter<
1801                             Target,
1802                             Source,
1803                             boost::numeric::conversion_traits<Target,Source>,
1804                             boost::numeric::def_overflow_handler,
1805                             throw_on_precision_loss<Source>
1806                     > Converter ;
1807
1808                     return Converter::convert(arg);
1809                 } catch( ::boost::numeric::bad_numeric_cast const& ) {
1810                     BOOST_LCAST_THROW_BAD_CAST(Source, Target);
1811                 }
1812                 BOOST_UNREACHABLE_RETURN(static_cast<Target>(0));
1813             }
1814         };
1815
1816         template<typename Target, typename Source>
1817         struct lexical_cast_dynamic_num_ignoring_minus
1818         {
1819             static inline Target lexical_cast_impl(const Source &arg)
1820             {
1821                 try{
1822                     typedef boost::numeric::converter<
1823                             Target,
1824                             Source,
1825                             boost::numeric::conversion_traits<Target,Source>,
1826                             boost::numeric::def_overflow_handler,
1827                             throw_on_precision_loss<Source>
1828                     > Converter ;
1829
1830                     bool has_minus = ( arg < 0);
1831                     if ( has_minus ) {
1832                         return static_cast<Target>(-Converter::convert(-arg));
1833                     } else {
1834                         return Converter::convert(arg);
1835                     }
1836                 } catch( ::boost::numeric::bad_numeric_cast const& ) {
1837                     BOOST_LCAST_THROW_BAD_CAST(Source, Target);
1838                 }
1839                 BOOST_UNREACHABLE_RETURN(static_cast<Target>(0));
1840             }
1841         };
1842
1843         /*
1844          * lexical_cast_dynamic_num follows the rules:
1845          * 1) If Source can be converted to Target without precision loss and
1846          * without overflows, then assign Source to Target and return
1847          *
1848          * 2) If Source is less than 0 and Target is an unsigned integer,
1849          * then negate Source, check the requirements of rule 1) and if
1850          * successful, assign static_casted Source to Target and return
1851          *
1852          * 3) Otherwise throw a bad_lexical_cast exception
1853          *
1854          *
1855          * Rule 2) required because boost::lexical_cast has the behavior of
1856          * stringstream, which uses the rules of scanf for conversions. And
1857          * in the C99 standard for unsigned input value minus sign is
1858          * optional, so if a negative number is read, no errors will arise
1859          * and the result will be the two's complement.
1860          */
1861         template<typename Target, typename Source>
1862         struct lexical_cast_dynamic_num
1863         {
1864             static inline Target lexical_cast_impl(const Source &arg)
1865             {
1866                 typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1867                     ::boost::type_traits::ice_and<
1868                         ::boost::type_traits::ice_or<
1869                             ::boost::is_signed<Source>::value,
1870                             ::boost::is_float<Source>::value
1871                         >::value,
1872                         ::boost::type_traits::ice_not<
1873                             is_same<Source, bool>::value
1874                         >::value,
1875                         ::boost::type_traits::ice_not<
1876                             is_same<Target, bool>::value
1877                         >::value,
1878                         ::boost::is_unsigned<Target>::value
1879                     >::value,
1880                     lexical_cast_dynamic_num_ignoring_minus<Target, Source>,
1881                     lexical_cast_dynamic_num_not_ignoring_minus<Target, Source>
1882                 >::type caster_type;
1883
1884                 return caster_type::lexical_cast_impl(arg);
1885             }
1886         };
1887     }
1888
1889     template<typename Target, typename Source>
1890     inline Target lexical_cast(const Source &arg)
1891     {
1892         typedef BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
1893
1894         typedef BOOST_DEDUCED_TYPENAME ::boost::type_traits::ice_or<
1895                 detail::is_xchar_to_xchar<Target, src>::value,
1896                 detail::is_char_array_to_stdstring<Target,src>::value,
1897                 ::boost::type_traits::ice_and<
1898                      is_same<Target, src>::value,
1899                      detail::is_stdstring<Target>::value
1900                 >::value
1901         > do_copy_type;
1902
1903         typedef BOOST_DEDUCED_TYPENAME
1904                 detail::is_arithmetic_and_not_xchars<Target, src> do_copy_with_dynamic_check_type;
1905
1906         typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1907             do_copy_type::value,
1908             detail::lexical_cast_copy<src>,
1909             BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1910                  do_copy_with_dynamic_check_type::value,
1911                  detail::lexical_cast_dynamic_num<Target, src>,
1912                  detail::lexical_cast_do_cast<Target, src>
1913             >::type
1914         >::type caster_type;
1915
1916         return caster_type::lexical_cast_impl(arg);
1917     }
1918
1919     #else
1920
1921     namespace detail // stream wrapper for handling lexical conversions
1922     {
1923         template<typename Target, typename Source, typename Traits>
1924         class lexical_stream
1925         {
1926         private:
1927             typedef typename widest_char<
1928                 typename stream_char<Target>::type,
1929                 typename stream_char<Source>::type>::type char_type;
1930
1931             typedef Traits traits_type;
1932
1933         public:
1934             lexical_stream(char_type* = 0, char_type* = 0)
1935             {
1936                 stream.unsetf(std::ios::skipws);
1937                 lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
1938             }
1939             ~lexical_stream()
1940             {
1941                 #if defined(BOOST_NO_STRINGSTREAM)
1942                 stream.freeze(false);
1943                 #endif
1944             }
1945             bool operator<<(const Source &input)
1946             {
1947                 return !(stream << input).fail();
1948             }
1949             template<typename InputStreamable>
1950             bool operator>>(InputStreamable &output)
1951             {
1952                 return !is_pointer<InputStreamable>::value &&
1953                        stream >> output &&
1954                        stream.get() ==
1955 #if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
1956 // GCC 2.9x lacks std::char_traits<>::eof().
1957 // We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3
1958 // configurations, which do provide std::char_traits<>::eof().
1959
1960                            EOF;
1961 #else
1962                            traits_type::eof();
1963 #endif
1964             }
1965
1966             bool operator>>(std::string &output)
1967             {
1968                 #if defined(BOOST_NO_STRINGSTREAM)
1969                 stream << '\0';
1970                 #endif
1971                 stream.str().swap(output);
1972                 return true;
1973             }
1974             #ifndef BOOST_LCAST_NO_WCHAR_T
1975             bool operator>>(std::wstring &output)
1976             {
1977                 stream.str().swap(output);
1978                 return true;
1979             }
1980             #endif
1981
1982         private:
1983             #if defined(BOOST_NO_STRINGSTREAM)
1984             std::strstream stream;
1985             #elif defined(BOOST_NO_STD_LOCALE)
1986             std::stringstream stream;
1987             #else
1988             std::basic_stringstream<char_type,traits_type> stream;
1989             #endif
1990         };
1991     }
1992
1993     // call-by-value fallback version (deprecated)
1994
1995     template<typename Target, typename Source>
1996     Target lexical_cast(Source arg)
1997     {
1998         typedef typename detail::widest_char< 
1999             BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type 
2000           , BOOST_DEDUCED_TYPENAME detail::stream_char<Source>::type 
2001         >::type char_type; 
2002
2003         typedef std::char_traits<char_type> traits;
2004         detail::lexical_stream<Target, Source, traits> interpreter;
2005         Target result;
2006
2007         if(!(interpreter << arg && interpreter >> result))
2008           BOOST_LCAST_THROW_BAD_CAST(Source, Target);
2009         return result;
2010     }
2011
2012     #endif
2013 }
2014
2015 // Copyright Kevlin Henney, 2000-2005.
2016 // Copyright Alexander Nasonov, 2006-2010.
2017 // Copyright Antony Polukhin, 2011.
2018 //
2019 // Distributed under the Boost Software License, Version 1.0. (See
2020 // accompanying file LICENSE_1_0.txt or copy at
2021 // http://www.boost.org/LICENSE_1_0.txt)
2022
2023 #undef BOOST_LCAST_NO_WCHAR_T
2024 #endif