This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch] Simplify (and speed-up) do_get_year


Hi,

noticed while working on 12791: we can spare the very slow __convert_to_v
call (in the "C" locale, anyway) for such a simple input: just narrow,
check that is a digit ('0' - '9') and convert to integer with a trivial
small loop.

Tested x86-linux. Ok with everyone?

Paolo.

////////////
2003-12-02  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/locale_facets.tcc (time_get::do_get_year):
	Avoid using a basic_string and calling a full blown strtol
	(via __convert_to_v) for simple 2 or 4 digits, base 10,
	positive integers; simplify.
diff -prN libstdc++-v3-orig/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
*** libstdc++-v3-orig/include/bits/locale_facets.tcc	Sun Nov 30 12:33:23 2003
--- libstdc++-v3/include/bits/locale_facets.tcc	Tue Dec  2 11:14:59 2003
*************** namespace std
*** 1964,1984 ****
        const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
  
        size_t __i = 0;
!       string __digits;
!       for (; __i < 4 && __beg != __end
! 	     && __ctype.is(ctype_base::digit, *__beg); ++__beg, ++__i)
! 	__digits += __ctype.narrow(*__beg, 0);
!       if (__i == 2 || __i == 4)
  	{
! 	  long __l;
! 	  std::__convert_to_v(__digits.c_str(), __l, __err,
! 			      _S_get_c_locale());
! 	  if (!(__err & ios_base::failbit) && __l <= INT_MAX)
! 	    {
! 	      __l = __i == 2 ? __l : __l - 1900; 
! 	      __tm->tm_year = static_cast<int>(__l);
! 	    }
  	}
        else
  	__err |= ios_base::failbit;
        if (__beg == __end)
--- 1964,1980 ----
        const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
  
        size_t __i = 0;
!       int __value = 0;
!       for (; __beg != __end && __i < 4; ++__beg, ++__i)
  	{
! 	  const char __c = __ctype.narrow(*__beg, '*');
! 	  if (__c >= '0' && __c <= '9')
! 	    __value = __value * 10 + (__c - '0');
! 	  else
! 	    break;
  	}
+       if (__i == 2 || __i == 4)
+ 	__tm->tm_year = __i == 2 ? __value : __value - 1900; 
        else
  	__err |= ios_base::failbit;
        if (__beg == __end)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]