3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #ifndef _STLP_NUM_GET_C
19 #define _STLP_NUM_GET_C
21 #ifndef _STLP_INTERNAL_NUM_GET_H
22 # include <stl/_num_get.h>
25 #ifndef _STLP_INTERNAL_LIMITS
26 # include <stl/_limits.h>
31 _STLP_MOVE_TO_PRIV_NAMESPACE
33 _STLP_DECLSPEC unsigned char _STLP_CALL __digit_val_table(unsigned);
34 _STLP_DECLSPEC const char* _STLP_CALL __narrow_atoms();
36 template < class _InputIter, class _Integer, class _CharT>
38 __do_get_integer(_InputIter&, _InputIter&, ios_base&, ios_base::iostate&, _Integer&, _CharT*);
40 // __do_get_integer and its helper functions.
42 inline bool _STLP_CALL __get_fdigit(char __c, const char*)
43 { return __c >= '0' && __c <= '9'; }
45 inline bool _STLP_CALL __get_fdigit_or_sep(char& __c, char __sep, const char *__digits) {
51 return __get_fdigit(__c, __digits);
55 __get_digit_from_table(unsigned __index)
56 { return (__index > 127 ? 0xFF : __digit_val_table(__index)); }
58 template <class _InputIter, class _CharT>
60 __get_base_or_zero(_InputIter& __in_ite, _InputIter& __end, ios_base& __str, _CharT*) {
62 const ctype<_CharT>& __c_type = *__STATIC_CAST(const ctype<_CharT>*, __str._M_ctype_facet());
64 __c_type.widen(__narrow_atoms(), __narrow_atoms() + 5, __atoms);
66 bool __negative = false;
67 _CharT __c = *__in_ite;
69 if (__c == __atoms[1] /* __xminus_char */ ) {
73 else if (__c == __atoms[0] /* __xplus_char */ )
79 ios_base::fmtflags __basefield = __str.flags() & ios_base::basefield;
81 switch (__basefield) {
90 if (__in_ite != __end && *__in_ite == __atoms[2] /* __zero_char */ ) {
92 if (__in_ite != __end &&
93 (*__in_ite == __atoms[3] /* __x_char */ || *__in_ite == __atoms[4] /* __X_char */ ))
96 __valid_zero = 1; // That zero is valid by itself.
100 if (__in_ite != __end && *__in_ite == __atoms[2] /* __zero_char */ ) {
102 if (__in_ite != __end &&
103 (*__in_ite == __atoms[3] /* __x_char */ || *__in_ite == __atoms[4] /* __X_char */ )) {
110 __valid_zero = 1; // That zero is still valid by itself.
117 return (__base << 2) | ((int)__negative << 1) | __valid_zero;
121 template <class _InputIter, class _Integer, class _CharT>
123 __get_integer(_InputIter& __first, _InputIter& __last,
124 int __base, _Integer& __val,
125 int __got, bool __is_negative, _CharT __separator, const string& __grouping, const __true_type& /*_IsSigned*/) {
126 bool __ovflow = false;
127 _Integer __result = 0;
128 bool __is_group = !__grouping.empty();
129 char __group_sizes[64];
130 char __current_group_size = 0;
131 char* __group_sizes_end = __group_sizes;
133 _Integer __over_base = (numeric_limits<_Integer>::min)() / __STATIC_CAST(_Integer, __base);
135 for ( ; __first != __last ; ++__first) {
137 const _CharT __c = *__first;
139 if (__is_group && __c == __separator) {
140 *__group_sizes_end++ = __current_group_size;
141 __current_group_size = 0;
145 int __n = __get_digit_from_table(__c);
151 ++__current_group_size;
153 if (__result < __over_base)
154 __ovflow = true; // don't need to keep accumulating
156 _Integer __next = __STATIC_CAST(_Integer, __base * __result - __n);
158 __ovflow = __ovflow || __next >= __result;
163 if (__is_group && __group_sizes_end != __group_sizes) {
164 *__group_sizes_end++ = __current_group_size;
167 // fbp : added to not modify value if nothing was read
169 __val = __ovflow ? __is_negative ? (numeric_limits<_Integer>::min)()
170 : (numeric_limits<_Integer>::max)()
171 : __is_negative ? __result
172 : __STATIC_CAST(_Integer, -__result);
174 // overflow is being treated as failure
175 return ((__got > 0) && !__ovflow) &&
177 __valid_grouping(__group_sizes, __group_sizes_end,
178 __grouping.data(), __grouping.data()+ __grouping.size()));
181 template <class _InputIter, class _Integer, class _CharT>
183 __get_integer(_InputIter& __first, _InputIter& __last,
184 int __base, _Integer& __val,
185 int __got, bool __is_negative, _CharT __separator, const string& __grouping, const __false_type& /*_IsSigned*/) {
186 bool __ovflow = false;
187 _Integer __result = 0;
188 bool __is_group = !__grouping.empty();
189 char __group_sizes[64];
190 char __current_group_size = 0;
191 char* __group_sizes_end = __group_sizes;
193 _Integer __over_base = (numeric_limits<_Integer>::max)() / __STATIC_CAST(_Integer, __base);
195 for ( ; __first != __last ; ++__first) {
197 const _CharT __c = *__first;
199 if (__is_group && __c == __separator) {
200 *__group_sizes_end++ = __current_group_size;
201 __current_group_size = 0;
205 int __n = __get_digit_from_table(__c);
211 ++__current_group_size;
213 if (__result > __over_base)
214 __ovflow = true; //don't need to keep accumulating
216 _Integer __next = __STATIC_CAST(_Integer, __base * __result + __n);
218 __ovflow = __ovflow || __next <= __result;
223 if (__is_group && __group_sizes_end != __group_sizes) {
224 *__group_sizes_end++ = __current_group_size;
227 // fbp : added to not modify value if nothing was read
229 __val = __ovflow ? (numeric_limits<_Integer>::max)()
230 : (__is_negative ? __STATIC_CAST(_Integer, -__result)
234 // overflow is being treated as failure
235 return ((__got > 0) && !__ovflow) &&
237 __valid_grouping(__group_sizes, __group_sizes_end,
238 __grouping.data(), __grouping.data()+ __grouping.size()));
242 template <class _InputIter, class _Integer, class _CharT>
244 __get_decimal_integer(_InputIter& __first, _InputIter& __last, _Integer& __val, _CharT* /*dummy*/) {
246 //Here there is no grouping so separator is not important, we just pass the default charater.
247 return __get_integer(__first, __last, 10, __val, 0, false, _CharT() /*separator*/, __grp, __false_type());
250 template <class _InputIter, class _Integer, class _CharT>
251 _InputIter _STLP_CALL
252 __do_get_integer(_InputIter& __in_ite, _InputIter& __end, ios_base& __str,
253 ios_base::iostate& __err, _Integer& __val, _CharT* __pc) {
254 #if defined (__HP_aCC) && (__HP_aCC == 1)
255 bool _IsSigned = !((_Integer)(-1) > 0);
257 typedef typename __bool2type<numeric_limits<_Integer>::is_signed>::_Ret _IsSigned;
260 const numpunct<_CharT>& __numpunct = *__STATIC_CAST(const numpunct<_CharT>*, __str._M_numpunct_facet());
261 const string& __grouping = __str._M_grouping(); // cached copy
263 const int __base_or_zero = __get_base_or_zero(__in_ite, __end, __str, __pc);
264 int __got = __base_or_zero & 1;
268 if (__in_ite == __end) { // We may have already read a 0. If so,
270 if (__got > 0) { // the result is 0 even if we're at eof.
278 const bool __negative = (__base_or_zero & 2) != 0;
279 const int __base = __base_or_zero >> 2;
281 #if defined (__HP_aCC) && (__HP_aCC == 1)
283 __result = __get_integer(__in_ite, __end, __base, __val, __got, __negative, __numpunct.thousands_sep(), __grouping, __true_type() );
285 __result = __get_integer(__in_ite, __end, __base, __val, __got, __negative, __numpunct.thousands_sep(), __grouping, __false_type() );
287 __result = __get_integer(__in_ite, __end, __base, __val, __got, __negative, __numpunct.thousands_sep(), __grouping, _IsSigned());
291 __err = __STATIC_CAST(ios_base::iostate, __result ? ios_base::goodbit : ios_base::failbit);
293 if (__in_ite == __end)
294 __err |= ios_base::eofbit;
298 // __read_float and its helper functions.
299 template <class _InputIter, class _CharT>
300 _InputIter _STLP_CALL
301 __copy_sign(_InputIter __first, _InputIter __last, __iostring& __v,
302 _CharT __xplus, _CharT __xminus) {
303 if (__first != __last) {
304 _CharT __c = *__first;
307 else if (__c == __xminus) {
316 template <class _InputIter, class _CharT>
318 __copy_digits(_InputIter& __first, _InputIter __last,
319 __iostring& __v, const _CharT* __digits) {
322 for ( ; __first != __last; ++__first) {
323 _CharT __c = *__first;
324 if (__get_fdigit(__c, __digits)) {
325 __v.push_back((char)__c);
334 template <class _InputIter, class _CharT>
336 __copy_grouped_digits(_InputIter& __first, _InputIter __last,
337 __iostring& __v, const _CharT * __digits,
338 _CharT __sep, const string& __grouping,
339 bool& __grouping_ok) {
341 char __group_sizes[64];
342 char*__group_sizes_end = __group_sizes;
343 char __current_group_size = 0;
345 for ( ; __first != __last; ++__first) {
346 _CharT __c = *__first;
347 bool __tmp = __get_fdigit_or_sep(__c, __sep, __digits);
350 *__group_sizes_end++ = __current_group_size;
351 __current_group_size = 0;
355 __v.push_back((char)__c);
356 ++__current_group_size;
363 if (__group_sizes_end != __group_sizes)
364 *__group_sizes_end++ = __current_group_size;
365 __grouping_ok = __valid_grouping(__group_sizes, __group_sizes_end, __grouping.data(), __grouping.data() + __grouping.size());
370 template <class _InputIter, class _CharT>
372 __read_float(__iostring& __buf, _InputIter& __in_ite, _InputIter& __end, ios_base& __s, _CharT*) {
373 // Create a string, copying characters of the form
374 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
376 bool __digits_before_dot /* = false */;
377 bool __digits_after_dot = false;
380 bool __grouping_ok = true;
382 const ctype<_CharT>& __ct = *__STATIC_CAST(const ctype<_CharT>*, __s._M_ctype_facet());
383 const numpunct<_CharT>& __numpunct = *__STATIC_CAST(const numpunct<_CharT>*, __s._M_numpunct_facet());
384 const string& __grouping = __s._M_grouping(); // cached copy
386 _CharT __dot = __numpunct.decimal_point();
387 _CharT __sep = __numpunct.thousands_sep();
396 _Initialize_get_float(__ct, __xplus, __xminus, __pow_e, __pow_E, __digits);
398 // Get an optional sign
399 __in_ite = __copy_sign(__in_ite, __end, __buf, __xplus, __xminus);
401 // Get an optional string of digits.
402 if (!__grouping.empty())
403 __digits_before_dot = __copy_grouped_digits(__in_ite, __end, __buf, __digits,
404 __sep, __grouping, __grouping_ok);
406 __digits_before_dot = __copy_digits(__in_ite, __end, __buf, __digits);
408 // Get an optional decimal point, and an optional string of digits.
409 if (__in_ite != __end && *__in_ite == __dot) {
410 __buf.push_back('.');
412 __digits_after_dot = __copy_digits(__in_ite, __end, __buf, __digits);
415 // There have to be some digits, somewhere.
416 __ok = __digits_before_dot || __digits_after_dot;
418 // Get an optional exponent.
419 if (__ok && __in_ite != __end && (*__in_ite == __pow_e || *__in_ite == __pow_E)) {
420 __buf.push_back('e');
422 __in_ite = __copy_sign(__in_ite, __end, __buf, __xplus, __xminus);
423 __ok = __copy_digits(__in_ite, __end, __buf, __digits);
424 // If we have an exponent then the sign
425 // is optional but the digits aren't.
431 _STLP_MOVE_TO_STD_NAMESPACE
434 // num_get<>, num_put<>
437 #if ( _STLP_STATIC_TEMPLATE_DATA > 0 )
438 # if !defined (__BORLANDC__)
439 template <class _CharT, class _InputIterator>
440 locale::id num_get<_CharT, _InputIterator>::id;
443 # if (defined (__CYGWIN__) || defined (__MINGW32__)) && \
444 defined (_STLP_USE_DYNAMIC_LIB) && !defined (__BUILDING_STLPORT)
446 * Under cygwin, when STLport is used as a shared library, the id needs
447 * to be specified as imported otherwise they will be duplicated in the
448 * calling executable.
451 _STLP_DECLSPEC locale::id num_get<char, istreambuf_iterator<char, char_traits<char> > >::id;
454 _STLP_DECLSPEC locale::id num_get<char, const char*>::id;
457 # if !defined (STLP_NO_WCHAR_T)
459 _STLP_DECLSPEC locale::id num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
462 _STLP_DECLSPEC locale::id num_get<wchar_t, const wchar_t*>::id;
466 # endif /* __CYGWIN__ && _STLP_USE_DYNAMIC_LIB */
468 #else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
470 //typedef num_get<char, const char*> num_get_char;
471 typedef num_get<char, istreambuf_iterator<char, char_traits<char> > > num_get_char_2;
473 //__DECLARE_INSTANCE(locale::id, num_get_char::id, );
474 __DECLARE_INSTANCE(locale::id, num_get_char_2::id, );
476 # if !defined (_STLP_NO_WCHAR_T)
478 //typedef num_get<wchar_t, const wchar_t*> num_get_wchar_t;
479 typedef num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > num_get_wchar_t_2;
481 //__DECLARE_INSTANCE(locale::id, num_get_wchar_t::id, );
482 __DECLARE_INSTANCE(locale::id, num_get_wchar_t_2::id, );
486 #endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
488 #if !defined (_STLP_NO_BOOL)
489 template <class _CharT, class _InputIter>
491 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end,
493 ios_base::iostate& __err, bool& __x) const {
494 if (__s.flags() & ios_base::boolalpha) {
495 locale __loc = __s.getloc();
496 const _Numpunct& __np = *__STATIC_CAST(const _Numpunct*, __s._M_numpunct_facet());
497 // const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc) ;
498 // const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc) ;
500 const basic_string<_CharT> __truename = __np.truename();
501 const basic_string<_CharT> __falsename = __np.falsename();
502 bool __true_ok = true;
503 bool __false_ok = true;
506 for ( ; __in_ite != __end; ++__in_ite) {
507 _CharT __c = *__in_ite;
508 __true_ok = __true_ok && (__c == __truename[__n]);
509 __false_ok = __false_ok && (__c == __falsename[__n]);
512 if ((!__true_ok && !__false_ok) ||
513 (__true_ok && __n >= __truename.size()) ||
514 (__false_ok && __n >= __falsename.size())) {
519 if (__true_ok && __n < __truename.size()) __true_ok = false;
520 if (__false_ok && __n < __falsename.size()) __false_ok = false;
522 if (__true_ok || __false_ok) {
523 __err = ios_base::goodbit;
527 __err = ios_base::failbit;
529 if (__in_ite == __end)
530 __err |= ios_base::eofbit;
537 _InputIter __tmp = this->do_get(__in_ite, __end, __s, __err, __lx);
538 if (!(__err & ios_base::failbit)) {
544 __err |= ios_base::failbit;
550 #endif /* _STLP_NO_BOOL */
552 #if defined (_STLP_FIX_LIBRARY_ISSUES)
553 template <class _CharT, class _InputIter>
555 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
556 ios_base::iostate& __err, short& __val) const
557 { return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 ); }
559 template <class _CharT, class _InputIter>
561 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
562 ios_base::iostate& __err, int& __val) const
563 { return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 ); }
567 template <class _CharT, class _InputIter>
569 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
570 ios_base::iostate& __err, long& __val) const
571 { return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 ); }
573 template <class _CharT, class _InputIter>
575 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
576 ios_base::iostate& __err,
577 unsigned short& __val) const
578 { return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 ); }
580 template <class _CharT, class _InputIter>
582 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
583 ios_base::iostate& __err,
584 unsigned int& __val) const
585 { return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 ); }
587 template <class _CharT, class _InputIter>
589 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
590 ios_base::iostate& __err,
591 unsigned long& __val) const
592 { return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 ); }
595 template <class _CharT, class _InputIter>
597 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
598 ios_base::iostate& __err,
599 float& __val) const {
600 _STLP_PRIV __iostring __buf ;
601 bool __ok = _STLP_PRIV __read_float(__buf, __in_ite, __end, __str, (_CharT*)0 );
602 _STLP_PRIV __string_to_float(__buf, __val);
603 __err = __STATIC_CAST(ios_base::iostate, __ok ? ios_base::goodbit : ios_base::failbit);
604 if (__in_ite == __end)
605 __err |= ios_base::eofbit;
609 template <class _CharT, class _InputIter>
611 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
612 ios_base::iostate& __err,
613 double& __val) const {
614 _STLP_PRIV __iostring __buf ;
615 bool __ok = _STLP_PRIV __read_float(__buf, __in_ite, __end, __str, (_CharT*)0 );
616 _STLP_PRIV __string_to_float(__buf, __val);
617 __err = __STATIC_CAST(ios_base::iostate, __ok ? ios_base::goodbit : ios_base::failbit);
618 if (__in_ite == __end)
619 __err |= ios_base::eofbit;
623 #if !defined (_STLP_NO_LONG_DOUBLE)
624 template <class _CharT, class _InputIter>
626 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
627 ios_base::iostate& __err,
628 long double& __val) const {
629 _STLP_PRIV __iostring __buf ;
630 bool __ok = _STLP_PRIV __read_float(__buf, __in_ite, __end, __str, (_CharT*)0 );
631 _STLP_PRIV __string_to_float(__buf, __val);
632 __err = __STATIC_CAST(ios_base::iostate, __ok ? ios_base::goodbit : ios_base::failbit);
633 if (__in_ite == __end)
634 __err |= ios_base::eofbit;
637 #endif /* _STLP_NO_LONG_DOUBLE */
639 template <class _CharT, class _InputIter>
641 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
642 ios_base::iostate& __err,
644 #if defined (_STLP_LONG_LONG) && !defined (__MRC__) //*ty 12/07/2001 - MrCpp can not cast from long long to void*
645 unsigned _STLP_LONG_LONG __val;
649 iter_type __tmp = _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 );
650 if (!(__err & ios_base::failbit))
651 __p = __REINTERPRET_CAST(void*,__val);
655 #if defined (_STLP_LONG_LONG)
656 template <class _CharT, class _InputIter>
658 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
659 ios_base::iostate& __err,
660 _STLP_LONG_LONG& __val) const {
661 return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 );
664 template <class _CharT, class _InputIter>
666 num_get<_CharT, _InputIter>::do_get(_InputIter __in_ite, _InputIter __end, ios_base& __str,
667 ios_base::iostate& __err,
668 unsigned _STLP_LONG_LONG& __val) const {
669 return _STLP_PRIV __do_get_integer(__in_ite, __end, __str, __err, __val, (_CharT*)0 );
671 #endif /* _STLP_LONG_LONG */
675 #endif /* _STLP_NUMERIC_FACETS_C */