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] Fix num_get::do_get(bool&) vs 22.2.2.1.2, p16


Hi,

this is pretty obvious on the basis of the standard.

I didn't really benchmark, but, besides the correctness issue,
probably the final result is faster since, as soon as the initial
part of truename or falsename stops to match we now stop comparing
its chars to *__beg and, typically, one of the two stops quite
early (e.g., 'true'/'false' compared to 'false'/'true', respectively).
I.e., from:

 if (__n <= __fn)
   __testf = __traits_type::eq(*__beg, __lc->_M_falsename[__n]);

to, now:

 if (__testf && __n <= __fn)
   __testf = __traits_type::eq(*__beg, __lc->_M_falsename[__n]);

Tested x86-linux. Will commit on monday.

Paolo.

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

	* include/bits/locale_facets.tcc (num_get::do_get(bool&)):
	Fail as soon as the begins of both truename and falsename
	stop to match; always leave __beg one position beyond the
	last char successfully matched.
	* testsuite/22_locale/num_get/get/char/8.cc: New.
	* testsuite/22_locale/num_get/get/wchar_t/8.cc: Likewise.
	
diff -urN libstdc++-v3-1/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
--- libstdc++-v3-1/include/bits/locale_facets.tcc	2003-12-10 11:06:19.000000000 +0100
+++ libstdc++-v3/include/bits/locale_facets.tcc	2003-12-13 23:27:18.000000000 +0100
@@ -476,25 +476,25 @@
           const size_t __tn = __traits_type::length(__lc->_M_truename) - 1;
           const size_t __fn = __traits_type::length(__lc->_M_falsename) - 1;
 
-	  bool __testf = false;
-	  bool __testt = false;
+	  bool __testf = true;
+	  bool __testt = true;
           for (size_t __n = 0; __beg != __end; ++__n)
             {
-              const char_type __c = *__beg;
-	      ++__beg;
-
-	      if (__n <= __fn)
-		__testf = __traits_type::eq(__c, __lc->_M_falsename[__n]);
+	      if (__testf && __n <= __fn)
+		__testf = __traits_type::eq(*__beg, __lc->_M_falsename[__n]);
 
-	      if (__n <= __tn)
-		__testt = __traits_type::eq(__c, __lc->_M_truename[__n]);
+	      if (__testt && __n <= __tn)
+		__testt = __traits_type::eq(*__beg, __lc->_M_truename[__n]);
 
               if (!(__testf || __testt))
                 {
                   __err |= ios_base::failbit;
                   break;
                 }
-              else if (__testf && __n == __fn)
+
+	      ++__beg;
+
+              if (__testf && __n == __fn)
                 {
                   __v = 0;
                   break;
diff -urN libstdc++-v3-1/testsuite/22_locale/num_get/get/char/8.cc libstdc++-v3/testsuite/22_locale/num_get/get/char/8.cc
--- libstdc++-v3-1/testsuite/22_locale/num_get/get/char/8.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/num_get/get/char/8.cc	2003-12-13 23:32:09.000000000 +0100
@@ -0,0 +1,70 @@
+// 2003-12-13  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2003 Free Software Foundation
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 22.2.2.1.1  num_get members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  using namespace std;
+  typedef istreambuf_iterator<char> iterator_type;
+
+  bool test __attribute__((unused)) = true;
+
+  bool b;
+
+  // cache the num_get facet
+  istringstream iss;
+  const num_get<char>& ng = use_facet<num_get<char> >(iss.getloc()); 
+  const ios_base::iostate goodbit = ios_base::goodbit;
+  const ios_base::iostate failbit = ios_base::failbit;
+  ios_base::iostate err;
+  iterator_type end;
+
+  iss.setf(ios_base::boolalpha);
+  iss.str("faLse");
+  err = goodbit;
+  end = ng.get(iss.rdbuf(), 0, iss, err, b);
+  VERIFY( *end == 'L' );
+  VERIFY( err == failbit );
+
+  iss.str("falsr");
+  iss.clear();  
+  err = goodbit;
+  end = ng.get(iss.rdbuf(), 0, iss, err, b);
+  VERIFY( *end == 'r' );
+  VERIFY( err == failbit );
+
+  iss.str("trus");
+  iss.clear();
+  err = goodbit;
+  end = ng.get(iss.rdbuf(), 0, iss, err, b);
+  VERIFY( *end == 's' );
+  VERIFY( err == failbit );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-1/testsuite/22_locale/num_get/get/wchar_t/8.cc libstdc++-v3/testsuite/22_locale/num_get/get/wchar_t/8.cc
--- libstdc++-v3-1/testsuite/22_locale/num_get/get/wchar_t/8.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/num_get/get/wchar_t/8.cc	2003-12-13 23:32:47.000000000 +0100
@@ -0,0 +1,70 @@
+// 2003-12-13  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2003 Free Software Foundation
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 22.2.2.1.1  num_get members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  using namespace std;
+  typedef istreambuf_iterator<wchar_t> iterator_type;
+
+  bool test __attribute__((unused)) = true;
+
+  bool b;
+
+  // cache the num_get facet
+  wistringstream iss;
+  const num_get<wchar_t>& ng = use_facet<num_get<wchar_t> >(iss.getloc()); 
+  const ios_base::iostate goodbit = ios_base::goodbit;
+  const ios_base::iostate failbit = ios_base::failbit;
+  ios_base::iostate err;
+  iterator_type end;
+
+  iss.setf(ios_base::boolalpha);
+  iss.str(L"faLse");
+  err = goodbit;
+  end = ng.get(iss.rdbuf(), 0, iss, err, b);
+  VERIFY( *end == L'L' );
+  VERIFY( err == failbit );
+
+  iss.str(L"falsr");
+  iss.clear();  
+  err = goodbit;
+  end = ng.get(iss.rdbuf(), 0, iss, err, b);
+  VERIFY( *end == L'r' );
+  VERIFY( err == failbit );
+
+  iss.str(L"trus");
+  iss.clear();
+  err = goodbit;
+  end = ng.get(iss.rdbuf(), 0, iss, err, b);
+  VERIFY( *end == L's' );
+  VERIFY( err == failbit );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

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