OSDN Git Service

2004-11-18 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / locale_facets.tcc
index e5c8e0b..b503c95 100644 (file)
@@ -122,6 +122,7 @@ namespace std
       operator() (const locale& __loc) const;
     };
 
+  // Specializations.
   template<typename _CharT>
     struct __use_cache<__numpunct_cache<_CharT> >
     {
@@ -149,6 +150,111 @@ namespace std
       }
     };
 
+  template<typename _CharT, bool _Intl>
+    struct __use_cache<__moneypunct_cache<_CharT, _Intl> >
+    {
+      const __moneypunct_cache<_CharT, _Intl>*
+      operator() (const locale& __loc) const
+      {
+       const size_t __i = moneypunct<_CharT, _Intl>::id._M_id();
+       const locale::facet** __caches = __loc._M_impl->_M_caches;
+       if (!__caches[__i])
+         {
+           __moneypunct_cache<_CharT, _Intl>* __tmp = NULL;
+           try
+             {
+               __tmp = new __moneypunct_cache<_CharT, _Intl>;
+               __tmp->_M_cache(__loc);
+             }
+           catch(...)
+             {
+               delete __tmp;
+               __throw_exception_again;
+             }
+           __loc._M_impl->_M_install_cache(__tmp, __i);
+         }
+       return static_cast<
+         const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]);
+      }
+    };
+
+  template<typename _CharT>
+    void
+    __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
+    {
+      _M_allocated = true;
+
+      const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
+
+      _M_grouping_size = __np.grouping().size();
+      char* __grouping = new char[_M_grouping_size];
+      __np.grouping().copy(__grouping, _M_grouping_size);
+      _M_grouping = __grouping;
+      _M_use_grouping = _M_grouping_size && __np.grouping()[0] != 0;
+
+      _M_truename_size = __np.truename().size();
+      _CharT* __truename = new _CharT[_M_truename_size];
+      __np.truename().copy(__truename, _M_truename_size);
+      _M_truename = __truename;
+
+      _M_falsename_size = __np.falsename().size();
+      _CharT* __falsename = new _CharT[_M_falsename_size];
+      __np.falsename().copy(__falsename, _M_falsename_size);
+      _M_falsename = __falsename;
+
+      _M_decimal_point = __np.decimal_point();
+      _M_thousands_sep = __np.thousands_sep();
+
+      const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
+      __ct.widen(__num_base::_S_atoms_out,
+                __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
+      __ct.widen(__num_base::_S_atoms_in,
+                __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
+    }
+
+  template<typename _CharT, bool _Intl>
+    void
+    __moneypunct_cache<_CharT, _Intl>::_M_cache(const locale& __loc)
+    {
+      _M_allocated = true;
+
+      const moneypunct<_CharT, _Intl>& __mp =
+       use_facet<moneypunct<_CharT, _Intl> >(__loc);
+
+      _M_grouping_size = __mp.grouping().size();
+      char* __grouping = new char[_M_grouping_size];
+      __mp.grouping().copy(__grouping, _M_grouping_size);
+      _M_grouping = __grouping;
+      _M_use_grouping = _M_grouping_size && __mp.grouping()[0] != 0;
+      
+      _M_decimal_point = __mp.decimal_point();
+      _M_thousands_sep = __mp.thousands_sep();
+      _M_frac_digits = __mp.frac_digits();
+      
+      _M_curr_symbol_size = __mp.curr_symbol().size();
+      _CharT* __curr_symbol = new _CharT[_M_curr_symbol_size];
+      __mp.curr_symbol().copy(__curr_symbol, _M_curr_symbol_size);
+      _M_curr_symbol = __curr_symbol;
+      
+      _M_positive_sign_size = __mp.positive_sign().size();
+      _CharT* __positive_sign = new _CharT[_M_positive_sign_size];
+      __mp.positive_sign().copy(__positive_sign, _M_positive_sign_size);
+      _M_positive_sign = __positive_sign;
+
+      _M_negative_sign_size = __mp.negative_sign().size();
+      _CharT* __negative_sign = new _CharT[_M_negative_sign_size];
+      __mp.negative_sign().copy(__negative_sign, _M_negative_sign_size);
+      _M_negative_sign = __negative_sign;
+      
+      _M_pos_format = __mp.pos_format();
+      _M_neg_format = __mp.neg_format();
+
+      const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
+      __ct.widen(money_base::_S_atoms,
+                money_base::_S_atoms + money_base::_S_end, _M_atoms);
+    }
+
+
   // Used by both numeric and monetary facets.
   // Check to make sure that the __grouping_tmp string constructed in
   // money_get or num_get matches the canonical grouping for a given
@@ -173,39 +279,46 @@ namespace std
       const locale& __loc = __io._M_getloc();
       const __cache_type* __lc = __uc(__loc);
       const _CharT* __lit = __lc->_M_atoms_in;
+      char_type __c = char_type();
 
-      // True if a mantissa is found.
-      bool __found_mantissa = false;
+      // True if __beg becomes equal to __end.
+      bool __testeof = __beg == __end;
 
       // First check for sign.
-      if (__beg != __end)
+      if (!__testeof)
        {
-         const char_type __c = *__beg;
-         const bool __plus = __c == __lit[_S_iplus];
-         if ((__plus || __c == __lit[_S_iminus])
+         __c = *__beg;
+         const bool __plus = __c == __lit[__num_base::_S_iplus];
+         if ((__plus || __c == __lit[__num_base::_S_iminus])
              && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
              && !(__c == __lc->_M_decimal_point))
            {
              __xtrc += __plus ? '+' : '-';
-             ++__beg;
+             if (++__beg != __end)
+               __c = *__beg;
+             else
+               __testeof = true;
            }
        }
 
       // Next, look for leading zeros.
-      while (__beg != __end)
+      bool __found_mantissa = false;
+      while (!__testeof)
        {
-         const char_type __c = *__beg;
          if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep
              || __c == __lc->_M_decimal_point)
            break;
-         else if (__c == __lit[_S_izero])
+         else if (__c == __lit[__num_base::_S_izero])
            {
              if (!__found_mantissa)
                {
                  __xtrc += '0';
                  __found_mantissa = true;
                }
-             ++__beg;
+             if (++__beg != __end)
+               __c = *__beg;
+             else
+               __testeof = true;
            }
          else
            break;
@@ -218,13 +331,12 @@ namespace std
       if (__lc->_M_use_grouping)
        __found_grouping.reserve(32);
       int __sep_pos = 0;
-      const char_type* __lit_zero = __lit + _S_izero;
       const char_type* __q;
-      while (__beg != __end)
+      const char_type* __lit_zero = __lit + __num_base::_S_izero;
+      while (!__testeof)
         {
          // According to 22.2.2.1.2, p8-9, first look for thousands_sep
          // and decimal_point.
-         const char_type __c = *__beg;
           if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
            {
              if (!__found_dec && !__found_sci)
@@ -235,7 +347,6 @@ namespace std
                    {
                      __found_grouping += static_cast<char>(__sep_pos);
                      __sep_pos = 0;
-                     ++__beg;
                    }
                  else
                    {
@@ -257,19 +368,18 @@ namespace std
                    __found_grouping += static_cast<char>(__sep_pos);
                  __xtrc += '.';
                  __found_dec = true;
-                 ++__beg;
                }
              else
                break;
            }
-          else if (__q = __traits_type::find(__lit_zero, 10, __c))
+          else if ((__q = __traits_type::find(__lit_zero, 10, __c)))
            {
-             __xtrc += _S_atoms_in[__q - __lit];
+             __xtrc += __num_base::_S_atoms_in[__q - __lit];
              __found_mantissa = true;
              ++__sep_pos;
-             ++__beg;
            }
-         else if ((__c == __lit[_S_ie] || __c == __lit[_S_iE])
+         else if ((__c == __lit[__num_base::_S_ie] 
+                   || __c == __lit[__num_base::_S_iE])
                   && __found_mantissa && !__found_sci)
            {
              // Scientific notation.
@@ -281,20 +391,30 @@ namespace std
              // Remove optional plus or minus sign, if they exist.
              if (++__beg != __end)
                {
-                 const bool __plus = *__beg == __lit[_S_iplus];
-                 if ((__plus || *__beg == __lit[_S_iminus])
+                 __c = *__beg;
+                 const bool __plus = __c == __lit[__num_base::_S_iplus];
+                 if ((__plus || __c == __lit[__num_base::_S_iminus])
                      && !(__lc->_M_use_grouping
-                          && *__beg == __lc->_M_thousands_sep)
-                     && !(*__beg == __lc->_M_decimal_point))
-                   {
-                     __xtrc += __plus ? '+' : '-';
-                     ++__beg;
-                   }
+                          && __c == __lc->_M_thousands_sep)
+                     && !(__c == __lc->_M_decimal_point))
+                   __xtrc += __plus ? '+' : '-';
+                 else
+                   continue;
+               }
+             else
+               {
+                 __testeof = true;
+                 break;
                }
            }
          else
            // Not a valid input item.
            break;
+
+         if (++__beg != __end)
+           __c = *__beg;
+         else
+           __testeof = true;
         }
 
       // Digit grouping is checked. If grouping and found_grouping don't
@@ -305,13 +425,14 @@ namespace std
          if (!__found_dec && !__found_sci)
            __found_grouping += static_cast<char>(__sep_pos);
 
-          if (!std::__verify_grouping(__lc->_M_grouping, __lc->_M_grouping_size,
+          if (!std::__verify_grouping(__lc->_M_grouping, 
+                                     __lc->_M_grouping_size,
                                      __found_grouping))
            __err |= ios_base::failbit;
         }
 
       // Finish up.
-      if (__beg == __end)
+      if (__testeof)
         __err |= ios_base::eofbit;
       return __beg;
     }
@@ -329,6 +450,7 @@ namespace std
        const locale& __loc = __io._M_getloc();
        const __cache_type* __lc = __uc(__loc);
        const _CharT* __lit = __lc->_M_atoms_in;
+       char_type __c = char_type();
 
        // NB: Iff __basefield == 0, __base can change based on contents.
        const ios_base::fmtflags __basefield = __io.flags()
@@ -336,58 +458,74 @@ namespace std
        const bool __oct = __basefield == ios_base::oct;
        int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
 
-       // True if numeric digits are found.
-       bool __found_num = false;
+       // True if __beg becomes equal to __end.
+       bool __testeof = __beg == __end;
 
        // First check for sign.
        bool __negative = false;
-       if (__beg != __end)
+       if (!__testeof)
          {
-           const char_type __c = *__beg;
+           __c = *__beg;
            if (numeric_limits<_ValueT>::is_signed)
-             __negative = __c == __lit[_S_iminus];
-           if ((__negative || __c == __lit[_S_iplus])
+             __negative = __c == __lit[__num_base::_S_iminus];
+           if ((__negative || __c == __lit[__num_base::_S_iplus])
                && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
                && !(__c == __lc->_M_decimal_point))
-             ++__beg;
+             {
+               if (++__beg != __end)
+                 __c = *__beg;
+               else
+                 __testeof = true;
+             }
          }
 
        // Next, look for leading zeros and check required digits
        // for base formats.
-       while (__beg != __end)
+       bool __found_zero = false;
+       while (!__testeof)
          {
-           const char_type __c = *__beg;
            if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep
                || __c == __lc->_M_decimal_point)
              break;
-           else if (__c == __lit[_S_izero] && (!__found_num || __base == 10))
-             {
-               __found_num = true;
-               ++__beg;
-             }
-           else if (__found_num)
+           else if (__c == __lit[__num_base::_S_izero] 
+                    && (!__found_zero || __base == 10))
+             __found_zero = true;
+           else if (__found_zero)
              {
-               if (__c == __lit[_S_ix] || __c == __lit[_S_iX])
+               if (__c == __lit[__num_base::_S_ix] 
+                   || __c == __lit[__num_base::_S_iX])
                  {
                    if (__basefield == 0)
                      __base = 16;
                    if (__base == 16)
-                     {
-                       __found_num = false;
-                       ++__beg;
-                     }
+                     __found_zero = false;
+                   else
+                     break;
+                 }
+               else
+                 {
+                   if (__basefield == 0)
+                     __base = 8;
+                   break;
                  }
-               else if (__basefield == 0)
-                 __base = 8;
-               break;
              }
            else
              break;
-         }
 
+           if (++__beg != __end)
+             {
+               __c = *__beg;
+               if (!__found_zero)
+                 break;
+             }
+           else
+             __testeof = true;
+         }
+       
        // At this point, base is determined. If not hex, only allow
        // base digits as valid input.
-       const size_t __len = __base == 16 ? _S_iend - _S_izero : __base;
+       const size_t __len = (__base == 16 ? __num_base::_S_iend
+                             - __num_base::_S_izero : __base);
 
        // Extract.
        string __found_grouping;
@@ -396,16 +534,15 @@ namespace std
        int __sep_pos = 0;
        bool __overflow = false;
        _ValueT __result = 0;
-       const char_type* __lit_zero = __lit + _S_izero;
        const char_type* __q;
+       const char_type* __lit_zero = __lit + __num_base::_S_izero;
        if (__negative)
          {
            const _ValueT __min = numeric_limits<_ValueT>::min() / __base;
-           for (; __beg != __end; ++__beg)
+           while (!__testeof)
              {
                // According to 22.2.2.1.2, p8-9, first look for thousands_sep
                // and decimal_point.
-               const char_type __c = *__beg;
                if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
                  {
                    // NB: Thousands separator at the beginning of a string
@@ -423,7 +560,7 @@ namespace std
                  }
                else if (__c == __lc->_M_decimal_point)
                  break;
-               else if (__q = __traits_type::find(__lit_zero, __len, __c))
+               else if ((__q = __traits_type::find(__lit_zero, __len, __c)))
                  {
                    int __digit = __q - __lit_zero;
                    if (__digit > 15)
@@ -432,25 +569,28 @@ namespace std
                      __overflow = true;
                    else
                      {
-                       const _ValueT __new_result = __result * __base
-                                                    - __digit;
+                       const _ValueT __new_result = (__result * __base
+                                                     - __digit);
                        __overflow |= __new_result > __result;
                        __result = __new_result;
                        ++__sep_pos;
-                       __found_num = true;
                      }
                  }
                else
                  // Not a valid input item.
                  break;
+
+               if (++__beg != __end)
+                 __c = *__beg;
+               else
+                 __testeof = true;
              }
          }
        else
          {
            const _ValueT __max = numeric_limits<_ValueT>::max() / __base;
-           for (; __beg != __end; ++__beg)
+           while (!__testeof)
              {
-               const char_type __c = *__beg;
                if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
                  {
                    if (__sep_pos)
@@ -466,7 +606,7 @@ namespace std
                  }
                else if (__c == __lc->_M_decimal_point)
                  break;
-               else if (__q = __traits_type::find(__lit_zero, __len, __c))
+               else if ((__q = __traits_type::find(__lit_zero, __len, __c)))
                  {
                    int __digit = __q - __lit_zero;
                    if (__digit > 15)
@@ -475,16 +615,20 @@ namespace std
                      __overflow = true;
                    else
                      {
-                       const _ValueT __new_result = __result * __base
-                                                    + __digit;
+                       const _ValueT __new_result = (__result * __base
+                                                     + __digit);
                        __overflow |= __new_result < __result;
                        __result = __new_result;
                        ++__sep_pos;
-                       __found_num = true;
                      }
                  }
                else
                  break;
+
+               if (++__beg != __end)
+                 __c = *__beg;
+               else
+                 __testeof = true;
              }
          }
 
@@ -502,12 +646,12 @@ namespace std
          }
 
        if (!(__err & ios_base::failbit) && !__overflow
-           && __found_num)
+           && (__sep_pos || __found_zero || __found_grouping.size()))
          __v = __result;
        else
          __err |= ios_base::failbit;
 
-       if (__beg == __end)
+       if (__testeof)
          __err |= ios_base::eofbit;
        return __beg;
       }
@@ -535,7 +679,6 @@ namespace std
       else
         {
          // Parse bool values as alphanumeric.
-         typedef char_traits<_CharT>                     __traits_type;
          typedef typename numpunct<_CharT>::__cache_type __cache_type;
          __use_cache<__cache_type> __uc;
          const locale& __loc = __io._M_getloc();
@@ -544,22 +687,28 @@ namespace std
          bool __testf = true;
          bool __testt = true;
          size_t __n;
-          for (__n = 0; __beg != __end; ++__n, ++__beg)
+         bool __testeof = __beg == __end;
+          for (__n = 0; !__testeof; ++__n)
             {
+             const char_type __c = *__beg;
+
              if (__testf)
                if (__n < __lc->_M_falsename_size)
-                 __testf = *__beg == __lc->_M_falsename[__n];
+                 __testf = __c == __lc->_M_falsename[__n];
                else
                  break;
 
              if (__testt)
                if (__n < __lc->_M_truename_size)
-                 __testt = *__beg == __lc->_M_truename[__n];
+                 __testt = __c == __lc->_M_truename[__n];
                else
                  break;
 
              if (!__testf && !__testt)
                break;
+             
+             if (++__beg == __end)
+               __testeof = true;
             }
          if (__testf && __n == __lc->_M_falsename_size)
            __v = 0;
@@ -568,7 +717,7 @@ namespace std
          else
            __err |= ios_base::failbit;
 
-          if (__beg == __end)
+          if (__testeof)
             __err |= ios_base::eofbit;
         }
       return __beg;
@@ -676,8 +825,6 @@ namespace std
 
       if (!(__err & ios_base::failbit))
        __v = reinterpret_cast<void*>(__ul);
-      else
-       __err |= ios_base::failbit;
       return __beg;
     }
 
@@ -716,7 +863,11 @@ namespace std
     inline int
     __int_to_char(_CharT* __bufend, unsigned long __v, const _CharT* __lit,
                  ios_base::fmtflags __flags)
-    { return __int_to_char(__bufend, __v, __lit, __flags, false); }
+    {
+      // About showpos, see Table 60 and C99 7.19.6.1, p6 (+).
+      return __int_to_char(__bufend, __v, __lit,
+                          __flags & ~ios_base::showpos, false);
+    }
 
 #ifdef _GLIBCXX_USE_LONG_LONG
   template<typename _CharT>
@@ -736,9 +887,10 @@ namespace std
 
   template<typename _CharT>
     inline int
-    __int_to_char(_CharT* __bufend, unsigned long long __v, const _CharT* __lit,
-                 ios_base::fmtflags __flags)
-    { return __int_to_char(__bufend, __v, __lit, __flags, false); }
+    __int_to_char(_CharT* __bufend, unsigned long long __v, 
+                 const _CharT* __lit, ios_base::fmtflags __flags)
+    { return __int_to_char(__bufend, __v, __lit,
+                          __flags & ~ios_base::showpos, false); }
 #endif
 
   template<typename _CharT, typename _ValueT>
@@ -827,10 +979,9 @@ namespace std
            __new[0] = __cs[0];
            __new[1] = __cs[1];
          }
-      _CharT* __p;
-      __p = std::__add_grouping(__new + __off, __sep, __grouping,
-                               __grouping_size, __cs + __off,
-                               __cs + __len);
+      _CharT* __p = std::__add_grouping(__new + __off, __sep, __grouping,
+                                       __grouping_size, __cs + __off,
+                                       __cs + __len);
       __len = __p - __new;
     }
 
@@ -896,10 +1047,10 @@ namespace std
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 282. What types does numpunct grouping refer to?
       // Add grouping, if necessary.
-      _CharT* __p2;
       const int __declen = __p ? __p - __cs : __len;
-      __p2 = std::__add_grouping(__new, __sep, __grouping, __grouping_size,
-                                __cs, __cs + __declen);
+      _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
+                                        __grouping_size,
+                                        __cs, __cs + __declen);
 
       // Tack on decimal part.
       int __newlen = __p2 - __new;
@@ -933,34 +1084,25 @@ namespace std
        const locale& __loc = __io._M_getloc();
        const __cache_type* __lc = __uc(__loc);
 
-       // Note: digits10 is rounded down: add 1 to ensure the maximum
-       // available precision.  Then, in general, one more 1 needs to
-       // be added since, when the %{g,G} conversion specifiers are
-       // chosen inside _S_format_float, the precision field is "the
-       // maximum number of significant digits", *not* the "number of
-       // digits to appear after the decimal point", as happens for
-       // %{e,E,f,F} (C99, 7.19.6.1,4).
-       const int __max_digits = numeric_limits<_ValueT>::digits10 + 2;
-
        // Use default precision if out of range.
        streamsize __prec = __io.precision();
-       if (__prec > static_cast<streamsize>(__max_digits))
-         __prec = static_cast<streamsize>(__max_digits);
-       else if (__prec < static_cast<streamsize>(0))
+       if (__prec < static_cast<streamsize>(0))
          __prec = static_cast<streamsize>(6);
 
+       const int __max_digits = numeric_limits<_ValueT>::digits10;
+
        // [22.2.2.2.2] Stage 1, numeric conversion to character.
        int __len;
        // Long enough for the max format spec.
        char __fbuf[16];
 
 #ifdef _GLIBCXX_USE_C99
-       // First try a buffer perhaps big enough (for sure sufficient
+       // First try a buffer perhaps big enough (most probably sufficient
        // for non-ios_base::fixed outputs)
        int __cs_size = __max_digits * 3;
        char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
 
-       _S_format_float(__io, __fbuf, __mod);
+       __num_base::_S_format_float(__io, __fbuf, __mod);
        __len = std::__convert_from_v(__cs, __cs_size, __fbuf, __v,
                                      _S_get_c_locale(), __prec);
 
@@ -978,16 +1120,16 @@ namespace std
        const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
 
        // The size of the output string is computed as follows.
-       // ios_base::fixed outputs may need up to __max_exp+1 chars
-       // for the integer part + up to __max_digits chars for the
-       // fractional part + 3 chars for sign, decimal point, '\0'. On
-       // the other hand, for non-fixed outputs __max_digits*3 chars
-       // are largely sufficient.
-       const int __cs_size = __fixed ? __max_exp + __max_digits + 4
-                                     : __max_digits * 3;
+       // ios_base::fixed outputs may need up to __max_exp + 1 chars
+       // for the integer part + __prec chars for the fractional part
+       // + 3 chars for sign, decimal point, '\0'. On the other hand,
+       // for non-fixed outputs __max_digits * 2 + __prec chars are
+       // largely sufficient.
+       const int __cs_size = __fixed ? __max_exp + __prec + 4
+                                     : __max_digits * 2 + __prec;
        char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
 
-       _S_format_float(__io, __fbuf, __mod);
+       __num_base::_S_format_float(__io, __fbuf, __mod);
        __len = std::__convert_from_v(__cs, 0, __fbuf, __v,
                                      _S_get_c_locale(), __prec);
 #endif
@@ -1003,8 +1145,8 @@ namespace std
       // Replace decimal point.
       const _CharT __cdec = __ctype.widen('.');
       const _CharT __dec = __lc->_M_decimal_point;
-      const _CharT* __p;
-      if (__p = char_traits<_CharT>::find(__ws, __len, __cdec))
+      const _CharT* __p = char_traits<_CharT>::find(__ws, __len, __cdec);
+      if (__p)
        __ws[__p - __ws] = __dec;
 
       // Add grouping, if necessary.
@@ -1043,8 +1185,8 @@ namespace std
       const ios_base::fmtflags __flags = __io.flags();
       if ((__flags & ios_base::boolalpha) == 0)
         {
-          unsigned long __uv = __v;
-          __s = _M_insert_int(__s, __io, __fill, __uv);
+          const long __l = __v;
+          __s = _M_insert_int(__s, __io, __fill, __l);
         }
       else
         {
@@ -1121,8 +1263,7 @@ namespace std
            const void* __v) const
     {
       const ios_base::fmtflags __flags = __io.flags();
-      const ios_base::fmtflags __fmt = ~(ios_base::showpos
-                                        | ios_base::basefield
+      const ios_base::fmtflags __fmt = ~(ios_base::basefield
                                         | ios_base::uppercase
                                         | ios_base::internal);
       __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
@@ -1133,34 +1274,6 @@ namespace std
       return __s;
     }
 
-  template<typename _CharT, bool _Intl>
-    struct __use_cache<__moneypunct_cache<_CharT, _Intl> >
-    {
-      const __moneypunct_cache<_CharT, _Intl>*
-      operator() (const locale& __loc) const
-      {
-       const size_t __i = moneypunct<_CharT, _Intl>::id._M_id();
-       const locale::facet** __caches = __loc._M_impl->_M_caches;
-       if (!__caches[__i])
-         {
-           __moneypunct_cache<_CharT, _Intl>* __tmp = NULL;
-           try
-             {
-               __tmp = new __moneypunct_cache<_CharT, _Intl>;
-               __tmp->_M_cache(__loc);
-             }
-           catch(...)
-             {
-               delete __tmp;
-               __throw_exception_again;
-             }
-           __loc._M_impl->_M_install_cache(__tmp, __i);
-         }
-       return static_cast<
-         const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]);
-      }
-    };
-
   template<typename _CharT, typename _InIter>
     template<bool _Intl>
       _InIter
@@ -1205,9 +1318,8 @@ namespace std
        string __res;
        __res.reserve(32);
 
-       const char_type* __lit_zero = __lit + _S_zero;
-       const char_type* __q;
-       const money_base::pattern __p = __lc->_M_neg_format;    
+       const char_type* __lit_zero = __lit + money_base::_S_zero;
+       const money_base::pattern __p = __lc->_M_neg_format;
        for (int __i = 0; __i < 4 && __testvalid; ++__i)
          {
            const part __which = static_cast<part>(__p.field[__i]);
@@ -1222,9 +1334,9 @@ namespace std
                    || __i == 0
                    || (__i == 1 && (__mandatory_sign
                                     || (static_cast<part>(__p.field[0])
-                                        == sign)
+                                        == money_base::sign)
                                     || (static_cast<part>(__p.field[2])
-                                        == space)))
+                                        == money_base::space)))
                    || (__i == 2 && ((static_cast<part>(__p.field[3])
                                      == money_base::value)
                                     || __mandatory_sign
@@ -1268,35 +1380,41 @@ namespace std
                // Extract digits, remove and stash away the
                // grouping of found thousands separators.
                for (; __beg != __end; ++__beg)
-                 if (__q = __traits_type::find(__lit_zero, 10, *__beg))
-                   {
-                     __res += _S_atoms[__q - __lit];
-                     ++__n;
-                   }
-                 else if (*__beg == __lc->_M_decimal_point && !__testdecfound)
-                   {
-                     __last_pos = __n;
-                     __n = 0;
-                     __testdecfound = true;
-                   }
-                 else if (__lc->_M_use_grouping
-                          && *__beg == __lc->_M_thousands_sep
-                          && !__testdecfound)
-                   {
-                     if (__n)
-                       {
-                         // Mark position for later analysis.
-                         __grouping_tmp += static_cast<char>(__n);
-                         __n = 0;
-                       }
-                     else
-                       {
-                         __testvalid = false;
-                         break;
-                       }
-                   }
-                 else
-                   break;
+                 {
+                   const char_type __c = *__beg;
+                   const char_type* __q = __traits_type::find(__lit_zero, 
+                                                              10, __c);
+                   if (__q != 0)
+                     {
+                       __res += money_base::_S_atoms[__q - __lit];
+                       ++__n;
+                     }
+                   else if (__c == __lc->_M_decimal_point 
+                            && !__testdecfound)
+                     {
+                       __last_pos = __n;
+                       __n = 0;
+                       __testdecfound = true;
+                     }
+                   else if (__lc->_M_use_grouping
+                            && __c == __lc->_M_thousands_sep
+                            && !__testdecfound)
+                     {
+                       if (__n)
+                         {
+                           // Mark position for later analysis.
+                           __grouping_tmp += static_cast<char>(__n);
+                           __n = 0;
+                         }
+                       else
+                         {
+                           __testvalid = false;
+                           break;
+                         }
+                     }
+                   else
+                     break;
+                 }
                if (__res.empty())
                  __testvalid = false;
                break;
@@ -1361,16 +1479,15 @@ namespace std
              __testvalid = false;
          }
        
-       // Iff no more characters are available.
-       if (__beg == __end)
-         __err |= ios_base::eofbit;
-       
        // Iff valid sequence is not recognized.
        if (!__testvalid)
          __err |= ios_base::failbit;
        else
          __units.swap(__res);
        
+       // Iff no more characters are available.
+       if (__beg == __end)
+         __err |= ios_base::eofbit;
        return __beg;
       }
 
@@ -1443,7 +1560,7 @@ namespace std
        money_base::pattern __p;
        const char_type* __sign;
        size_type __sign_size;
-       if (*__beg != __lit[_S_minus])
+       if (*__beg != __lit[money_base::_S_minus])
          {
            __p = __lc->_M_pos_format;
            __sign = __lc->_M_positive_sign;
@@ -1501,13 +1618,14 @@ namespace std
                else
                  {
                    // Have to pad zeros in the decimal position.
-                   __value.append(-__paddec, __lit[_S_zero]);
+                   __value.append(-__paddec, __lit[money_base::_S_zero]);
                    __value.append(__beg, __len);
                  }
              }
   
            // Calculate length of resulting string.
-           const ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
+           const ios_base::fmtflags __f = __io.flags() 
+                                          & ios_base::adjustfield;
            __len = __value.size() + __sign_size;
            __len += ((__io.flags() & ios_base::showbase)
                      ? __lc->_M_curr_symbol_size : 0);
@@ -1515,7 +1633,7 @@ namespace std
            string_type __res;
            __res.reserve(2 * __len);
            
-           const size_type __width = static_cast<size_type>(__io.width());       
+           const size_type __width = static_cast<size_type>(__io.width());  
            const bool __testipad = (__f == ios_base::internal
                                     && __len < __width);
            // Fit formatted digits into the required pattern.
@@ -1593,22 +1711,22 @@ namespace std
       char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 328. Bad sprintf format modifier in money_put<>::do_put()
-      int __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units,
-                                       _S_get_c_locale());
+      int __len = std::__convert_from_v(__cs, __cs_size, "%.*Lf", __units,
+                                       _S_get_c_locale(), 0);
       // If the buffer was not large enough, try again with the correct size.
       if (__len >= __cs_size)
        {
          __cs_size = __len + 1;
          __cs = static_cast<char*>(__builtin_alloca(__cs_size));
-         __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units,
-                                       _S_get_c_locale());
+         __len = std::__convert_from_v(__cs, __cs_size, "%.*Lf", __units,
+                                       _S_get_c_locale(), 0);
        }
 #else
       // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
       const int __cs_size = numeric_limits<long double>::max_exponent10 + 3;
       char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
-      int __len = std::__convert_from_v(__cs, 0, "%.0Lf", __units,
-                                       _S_get_c_locale());
+      int __len = std::__convert_from_v(__cs, 0, "%.*Lf", __units,
+                                       _S_get_c_locale(), 0);
 #endif
       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
                                                           * __cs_size));
@@ -1626,6 +1744,7 @@ namespace std
     { return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
                    : _M_insert<false>(__s, __io, __fill, __digits); }
 
+
   // NB: Not especially useful. Without an ios_base object or some
   // kind of locale reference, we are left clawing at the air where
   // the side of the mountain used to be...
@@ -1634,18 +1753,16 @@ namespace std
     time_get<_CharT, _InIter>::do_date_order() const
     { return time_base::no_order; }
 
-  // Recursively expand a strftime format string and parse it.  Starts w/ %x
-  // and %X from do_get_time() and do_get_date(), which translate to a more
-  // specific string, which may contain yet more strings.  I.e. %x => %r =>
-  // %H:%M:%S => extracted characters.
+  // Expand a strftime format string and parse it.  E.g., do_get_date() may
+  // pass %m/%d/%Y => extracted characters.
   template<typename _CharT, typename _InIter>
-    void
+    _InIter
     time_get<_CharT, _InIter>::
-    _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
+    _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
                          ios_base::iostate& __err, tm* __tm,
                          const _CharT* __format) const
     {
-      const locale __loc = __io.getloc();
+      const locale& __loc = __io._M_getloc();
       const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
       const size_t __len = char_traits<_CharT>::length(__format);
@@ -1667,81 +1784,81 @@ namespace std
                  // Abbreviated weekday name [tm_wday]
                  const char_type*  __days1[7];
                  __tp._M_days_abbreviated(__days1);
-                 _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7,
-                                 __ctype, __err);
+                 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days1,
+                                         7, __io, __err);
                  break;
                case 'A':
                  // Weekday name [tm_wday].
                  const char_type*  __days2[7];
                  __tp._M_days(__days2);
-                 _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7,
-                                 __ctype, __err);
+                 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days2,
+                                         7, __io, __err);
                  break;
                case 'h':
                case 'b':
                  // Abbreviated month name [tm_mon]
                  const char_type*  __months1[12];
                  __tp._M_months_abbreviated(__months1);
-                 _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12,
-                                 __ctype, __err);
+                 __beg = _M_extract_name(__beg, __end, __tm->tm_mon, 
+                                         __months1, 12, __io, __err);
                  break;
                case 'B':
                  // Month name [tm_mon].
                  const char_type*  __months2[12];
                  __tp._M_months(__months2);
-                 _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12,
-                                 __ctype, __err);
+                 __beg = _M_extract_name(__beg, __end, __tm->tm_mon, 
+                                         __months2, 12, __io, __err);
                  break;
                case 'c':
                  // Default time and date representation.
                  const char_type*  __dt[2];
                  __tp._M_date_time_formats(__dt);
-                 _M_extract_via_format(__beg, __end, __io, __err, __tm,
-                                       __dt[0]);
+                 __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                               __tm, __dt[0]);
                  break;
                case 'd':
                  // Day [01, 31]. [tm_mday]
-                 _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
-                                __ctype, __err);
+                 __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
+                                        __io, __err);
                  break;
                case 'e':
                  // Day [1, 31], with single digits preceded by
                  // space. [tm_mday]
                  if (__ctype.is(ctype_base::space, *__beg))
-                   _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9, 1,
-                                  __ctype, __err);
+                   __beg = _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9,
+                                          1, __io, __err);
                  else
-                   _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31, 2,
-                                  __ctype, __err);
+                   __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31,
+                                          2, __io, __err);
                  break;
                case 'D':
                  // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
                  __cs = "%m/%d/%y";
                  __ctype.widen(__cs, __cs + 9, __wcs);
-                 _M_extract_via_format(__beg, __end, __io, __err, __tm,
-                                       __wcs);
+                 __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                               __tm, __wcs);
                  break;
                case 'H':
                  // Hour [00, 23]. [tm_hour]
-                 _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
-                                __ctype, __err);
+                 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
+                                        __io, __err);
                  break;
                case 'I':
                  // Hour [01, 12]. [tm_hour]
-                 _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
-                                __ctype, __err);
+                 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
+                                        __io, __err);
                  break;
                case 'm':
                  // Month [01, 12]. [tm_mon]
-                 _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype,
-                                __err);
+                 __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2, 
+                                        __io, __err);
                  if (!__err)
                    __tm->tm_mon = __mem - 1;
                  break;
                case 'M':
                  // Minute [00, 59]. [tm_min]
-                 _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
-                                __ctype, __err);
+                 __beg = _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
+                                        __io, __err);
                  break;
                case 'n':
                  if (__ctype.narrow(*__beg, 0) == '\n')
@@ -1753,13 +1870,18 @@ namespace std
                  // Equivalent to (%H:%M).
                  __cs = "%H:%M";
                  __ctype.widen(__cs, __cs + 6, __wcs);
-                 _M_extract_via_format(__beg, __end, __io, __err, __tm,
-                                       __wcs);
+                 __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                               __tm, __wcs);
                  break;
                case 'S':
-                 // Seconds.
-                 _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
-                                __ctype, __err);
+                 // Seconds. [tm_sec]
+                 // [00, 60] in C99 (one leap-second), [00, 61] in C89.
+#ifdef _GLIBCXX_USE_C99
+                 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 60, 2,
+#else
+                 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 61, 2,
+#endif
+                                        __io, __err);
                  break;
                case 't':
                  if (__ctype.narrow(*__beg, 0) == '\t')
@@ -1771,33 +1893,33 @@ namespace std
                  // Equivalent to (%H:%M:%S).
                  __cs = "%H:%M:%S";
                  __ctype.widen(__cs, __cs + 9, __wcs);
-                 _M_extract_via_format(__beg, __end, __io, __err, __tm,
-                                       __wcs);
+                 __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                               __tm, __wcs);
                  break;
                case 'x':
                  // Locale's date.
                  const char_type*  __dates[2];
                  __tp._M_date_formats(__dates);
-                 _M_extract_via_format(__beg, __end, __io, __err, __tm,
-                                       __dates[0]);
+                 __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                               __tm, __dates[0]);
                  break;
                case 'X':
                  // Locale's time.
                  const char_type*  __times[2];
                  __tp._M_time_formats(__times);
-                 _M_extract_via_format(__beg, __end, __io, __err, __tm,
-                                       __times[0]);
+                 __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                               __tm, __times[0]);
                  break;
                case 'y':
                case 'C': // C99
                  // Two digit year. [tm_year]
-                 _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
-                                __ctype, __err);
+                 __beg = _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
+                                        __io, __err);
                  break;
                case 'Y':
                  // Year [1900). [tm_year]
-                 _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
-                                __ctype, __err);
+                 __beg = _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
+                                        __io, __err);
                  if (!__err)
                    __tm->tm_year = __mem - 1900;
                  break;
@@ -1806,19 +1928,19 @@ namespace std
                  if (__ctype.is(ctype_base::upper, *__beg))
                    {
                      int __tmp;
-                     _M_extract_name(__beg, __end, __tmp,
-                                     __timepunct_cache<_CharT>::_S_timezones,
-                                     14, __ctype, __err);
+                     __beg = _M_extract_name(__beg, __end, __tmp,
+                                      __timepunct_cache<_CharT>::_S_timezones,
+                                             14, __io, __err);
 
                      // GMT requires special effort.
                      if (__beg != __end && !__err && __tmp == 0
                          && (*__beg == __ctype.widen('-')
                              || *__beg == __ctype.widen('+')))
                        {
-                         _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
-                                         __ctype, __err);
-                         _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
-                                         __ctype, __err);
+                         __beg = _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
+                                                __io, __err);
+                         __beg = _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
+                                                __io, __err);
                        }
                    }
                  else
@@ -1838,16 +1960,19 @@ namespace std
                __err |= ios_base::failbit;
            }
        }
+      return __beg;
     }
 
   template<typename _CharT, typename _InIter>
-    void
+    _InIter
     time_get<_CharT, _InIter>::
-    _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
+    _M_extract_num(iter_type __beg, iter_type __end, int& __member,
                   int __min, int __max, size_t __len,
-                  const ctype<_CharT>& __ctype,
-                  ios_base::iostate& __err) const
+                  ios_base& __io, ios_base::iostate& __err) const
     {
+      const locale& __loc = __io._M_getloc();
+      const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
+
       // As-is works for __len = 1, 2, 4, the values actually used.
       int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
 
@@ -1872,19 +1997,22 @@ namespace std
        __member = __value;
       else
        __err |= ios_base::failbit;
+      return __beg;
     }
 
   // Assumptions:
   // All elements in __names are unique.
   template<typename _CharT, typename _InIter>
-    void
+    _InIter
     time_get<_CharT, _InIter>::
-    _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
+    _M_extract_name(iter_type __beg, iter_type __end, int& __member,
                    const _CharT** __names, size_t __indexlen,
-                   const ctype<_CharT>& __ctype,
-                   ios_base::iostate& __err) const
+                   ios_base& __io, ios_base::iostate& __err) const
     {
       typedef char_traits<_CharT>              __traits_type;
+      const locale& __loc = __io._M_getloc();
+      const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
+
       int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
                                                          * __indexlen));
       size_t __nmatches = 0;
@@ -1908,35 +2036,28 @@ namespace std
       while (__nmatches > 1)
        {
          // Find smallest matching string.
-         size_t __minlen = 10;
-         for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
+         size_t __minlen = __traits_type::length(__names[__matches[0]]);
+         for (size_t __i2 = 1; __i2 < __nmatches; ++__i2)
            __minlen = std::min(__minlen,
-                               __traits_type::length(__names[__matches[__i2]]));
-         ++__beg;
+                             __traits_type::length(__names[__matches[__i2]]));
+         ++__beg, ++__pos;
          if (__pos < __minlen && __beg != __end)
-           {
-             ++__pos;
-             for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
-               {
-                 __name = __names[__matches[__i3]];
-                 if (__name[__pos] != *__beg)
-                   __matches[__i3] = __matches[--__nmatches];
-               }
-           }
+           for (size_t __i3 = 0; __i3 < __nmatches;)
+             {
+               __name = __names[__matches[__i3]];
+               if (__name[__pos] != *__beg)
+                 __matches[__i3] = __matches[--__nmatches];
+               else
+                 ++__i3;
+             }
          else
            break;
        }
 
       if (__nmatches == 1)
        {
-         // If there was only one match, the first compare is redundant.
-         if (__pos == 0)
-           {
-             ++__pos;
-             ++__beg;
-           }
-
          // Make sure found name is completely extracted.
+         ++__beg, ++__pos;
          __name = __names[__matches[0]];
          const size_t __len = __traits_type::length(__name);
          while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
@@ -1951,6 +2072,7 @@ namespace std
        __testvalid = false;
       if (!__testvalid)
        __err |= ios_base::failbit;
+      return __beg;
     }
 
   template<typename _CharT, typename _InIter>
@@ -1959,12 +2081,12 @@ namespace std
     do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
                ios_base::iostate& __err, tm* __tm) const
     {
-      _CharT __wcs[3];
-      const char* __cs = "%X";
-      const locale __loc = __io.getloc();
-      ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
-      __ctype.widen(__cs, __cs + 3, __wcs);
-      _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
+      const locale& __loc = __io._M_getloc();
+      const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
+      const char_type*  __times[2];
+      __tp._M_time_formats(__times);
+      __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                   __tm, __times[0]);
       if (__beg == __end)
        __err |= ios_base::eofbit;
       return __beg;
@@ -1976,12 +2098,12 @@ namespace std
     do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
                ios_base::iostate& __err, tm* __tm) const
     {
-      _CharT __wcs[3];
-      const char* __cs = "%x";
-      const locale __loc = __io.getloc();
-      ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
-      __ctype.widen(__cs, __cs + 3, __wcs);
-      _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
+      const locale& __loc = __io._M_getloc();
+      const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
+      const char_type*  __dates[2];
+      __tp._M_date_formats(__dates);
+      __beg = _M_extract_via_format(__beg, __end, __io, __err, 
+                                   __tm, __dates[0]);
       if (__beg == __end)
        __err |= ios_base::eofbit;
       return __beg;
@@ -1994,13 +2116,13 @@ namespace std
                   ios_base::iostate& __err, tm* __tm) const
     {
       typedef char_traits<_CharT>              __traits_type;
-      const locale __loc = __io.getloc();
+      const locale& __loc = __io._M_getloc();
       const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
       const char_type*  __days[7];
       __tp._M_days_abbreviated(__days);
       int __tmpwday;
-      _M_extract_name(__beg, __end, __tmpwday, __days, 7, __ctype, __err);
+      __beg = _M_extract_name(__beg, __end, __tmpwday, __days, 7, __io, __err);
 
       // Check to see if non-abbreviated name exists, and extract.
       // NB: Assumes both _M_days and _M_days_abbreviated organized in
@@ -2008,7 +2130,7 @@ namespace std
       // __days array with the same index points to a day, and that
       // day's abbreviated form.
       // NB: Also assumes that an abbreviated name is a subset of the name.
-      if (!__err)
+      if (!__err && __beg != __end)
        {
          size_t __pos = __traits_type::length(__days[__tmpwday]);
          __tp._M_days(__days);
@@ -2023,9 +2145,10 @@ namespace std
              if (__len != __pos)
                __err |= ios_base::failbit;
            }
-         if (!__err)
-           __tm->tm_wday = __tmpwday;
        }
+      if (!__err)
+       __tm->tm_wday = __tmpwday;
+      
       if (__beg == __end)
        __err |= ios_base::eofbit;
       return __beg;
@@ -2038,13 +2161,14 @@ namespace std
                      ios_base& __io, ios_base::iostate& __err, tm* __tm) const
     {
       typedef char_traits<_CharT>              __traits_type;
-      const locale __loc = __io.getloc();
+      const locale& __loc = __io._M_getloc();
       const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
       const char_type*  __months[12];
       __tp._M_months_abbreviated(__months);
       int __tmpmon;
-      _M_extract_name(__beg, __end, __tmpmon, __months, 12, __ctype, __err);
+      __beg = _M_extract_name(__beg, __end, __tmpmon, __months, 12, 
+                             __io, __err);
 
       // Check to see if non-abbreviated name exists, and extract.
       // NB: Assumes both _M_months and _M_months_abbreviated organized in
@@ -2052,7 +2176,7 @@ namespace std
       // __months array with the same index points to a month, and that
       // month's abbreviated form.
       // NB: Also assumes that an abbreviated name is a subset of the name.
-      if (!__err)
+      if (!__err && __beg != __end)
        {
          size_t __pos = __traits_type::length(__months[__tmpmon]);
          __tp._M_months(__months);
@@ -2067,9 +2191,9 @@ namespace std
              if (__len != __pos)
                __err |= ios_base::failbit;
            }
-         if (!__err)
-           __tm->tm_mon = __tmpmon;
        }
+      if (!__err)
+       __tm->tm_mon = __tmpmon;
 
       if (__beg == __end)
        __err |= ios_base::eofbit;
@@ -2082,7 +2206,7 @@ namespace std
     do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
                ios_base::iostate& __err, tm* __tm) const
     {
-      const locale __loc = __io.getloc();
+      const locale& __loc = __io._M_getloc();
       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
 
       size_t __i = 0;
@@ -2110,7 +2234,7 @@ namespace std
     put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
        const _CharT* __beg, const _CharT* __end) const
     {
-      const locale __loc = __io.getloc();
+      const locale& __loc = __io._M_getloc();
       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
       for (; __beg != __end; ++__beg)
        if (__ctype.narrow(*__beg, 0) != '%')
@@ -2132,8 +2256,7 @@ namespace std
              }
            else
              break;
-           __s = this->do_put(__s, __io, __fill, __tm,
-                              __format, __mod);
+           __s = this->do_put(__s, __io, __fill, __tm, __format, __mod);
          }
        else
          break;
@@ -2146,16 +2269,15 @@ namespace std
     do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
           char __format, char __mod) const
     {
-      const locale __loc = __io.getloc();
+      const locale& __loc = __io._M_getloc();
       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
 
       // NB: This size is arbitrary. Should this be a data member,
       // initialized at construction?
-      const size_t __maxlen = 64;
-      char_type* __res =
-       static_cast<char_type*>(__builtin_alloca(sizeof(char_type)
-                                                * __maxlen));
+      const size_t __maxlen = 128;
+      char_type* __res = 
+       static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
 
       // NB: In IEE 1003.1-200x, and perhaps other locale models, it
       // is possible that the format character will be longer than one
@@ -2182,7 +2304,6 @@ namespace std
       return std::__write(__s, __res, char_traits<char_type>::length(__res));
     }
 
-
   // Generic version does nothing.
   template<typename _CharT>
     int
@@ -2265,7 +2386,7 @@ namespace std
              __len = __res + 1;
              __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
                                                          * __len));
-             __res = _M_transform(__c, __p, __res + 1);
+             __res = _M_transform(__c, __p, __len);
            }
 
          __ret.append(__c, __res);
@@ -2359,7 +2480,7 @@ namespace std
                    const string& __grouping_tmp)
   {
     const size_t __n = __grouping_tmp.size() - 1;
-    const size_t __min = std::min(__n, __grouping_size - 1);
+    const size_t __min = std::min(__n, size_t(__grouping_size - 1));
     size_t __i = __n;
     bool __test = true;