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/RFC] Fix libstdc++/12988


Hi,

the below, trivial change, widen(array) -> widen(char), as
per 22.2.2.2.2, p14, fixes the testcases.

Of course _M_insert_float will be eventually replaced by the
code that Jerry is preparing, but the testcases for float
types will stay ;)

About performance (the widen calls are cached, anyway),
benchmarking reveals that, on x86 at least, for the common case
of no user overridden do_widen and plain chars, calling once
widen(array) (which involved a memcopy) and calling widen(char)
(nop) in a loop are similarly expensive.

Tested x86-linux... will wait a bit...

Paolo.

////////////
2003-11-14  Paolo Carlini  <pcarlini@suse.de>

	PR libstdc++/12988
	* include/bits/locale_facets.h
	(__numpunct_cache<>::_M_cache): According to 22.2.2.2.2, p14,
	use widen(char) not widen(array) for _M_atoms_out;
	* include/bits/locale_facets.tcc (num_put<>::_M_insert_float):
	Likewise, for __ws.
	* testsuite/22_locale/num_put/put/char/12988.cc: New.
	* testsuite/22_locale/num_put/put/wchar_t/12988.cc: Ditto.
diff -urN libstdc++-v3-orig/include/bits/locale_facets.h libstdc++-v3/include/bits/locale_facets.h
--- libstdc++-v3-orig/include/bits/locale_facets.h	2003-11-04 03:06:59.000000000 +0100
+++ libstdc++-v3/include/bits/locale_facets.h	2003-11-14 13:58:54.000000000 +0100
@@ -674,8 +674,8 @@
       _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);
+      for (size_t __i = 0; __i < __num_base::_S_oend; ++__i)
+	_M_atoms_out[__i] = __ct.widen(__num_base::_S_atoms_out[__i]);
       _M_atoms_out[__num_base::_S_oend] = _CharT();
       __ct.widen(__num_base::_S_atoms_in, 
 		 __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
diff -urN libstdc++-v3-orig/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
--- libstdc++-v3-orig/include/bits/locale_facets.tcc	2003-11-09 20:15:25.000000000 +0100
+++ libstdc++-v3/include/bits/locale_facets.tcc	2003-11-14 13:47:23.000000000 +0100
@@ -955,7 +955,8 @@
 
       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
 							   * __len));
-      __ctype.widen(__cs, __cs + __len, __ws);
+      for (int __i = 0; __i < __len; ++__i)
+	__ws[__i] = __ctype.widen(__cs[__i]);
       
       // Replace decimal point.
       const _CharT __cdec = __ctype.widen('.');
diff -urN libstdc++-v3-orig/testsuite/22_locale/num_put/put/char/12988.cc libstdc++-v3/testsuite/22_locale/num_put/put/char/12988.cc
--- libstdc++-v3-orig/testsuite/22_locale/num_put/put/char/12988.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/num_put/put/char/12988.cc	2003-11-14 14:46:53.000000000 +0100
@@ -0,0 +1,61 @@
+// 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.2.1  num_put members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+struct Ctype: std::ctype<char>
+{
+  char do_widen(char c) const {
+    return 'A' + c % 26;
+  }
+};
+
+// libstdc++/12988
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  ostringstream oss;
+  oss.imbue(locale(locale::classic(), new Ctype));
+  const num_put<char>& np = use_facet<num_put<char> >(oss.getloc());
+
+  const string empty;
+  string result;
+  long inum = 123;
+  double fnum = 123.456;
+
+  np.put(oss.rdbuf(), oss, '+', inum);
+  result = oss.str();
+  VERIFY( result == "XYZ" );
+
+  oss.clear();
+  oss.str(empty);
+  np.put(oss.rdbuf(), oss, '+', fnum);
+  result = oss.str();
+  VERIFY( result == "XYZ.ABC" );
+}
+
+int main()
+{
+  test01();
+}
diff -urN libstdc++-v3-orig/testsuite/22_locale/num_put/put/wchar_t/12988.cc libstdc++-v3/testsuite/22_locale/num_put/put/wchar_t/12988.cc
--- libstdc++-v3-orig/testsuite/22_locale/num_put/put/wchar_t/12988.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/num_put/put/wchar_t/12988.cc	2003-11-14 14:47:09.000000000 +0100
@@ -0,0 +1,61 @@
+// 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.2.1  num_put members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+struct Ctype: std::ctype<wchar_t>
+{
+  wchar_t do_widen(char c) const {
+    return L'A' + c % 26;
+  }
+};
+
+// libstdc++/12988
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  wostringstream oss;
+  oss.imbue(locale(locale::classic(), new Ctype));
+  const num_put<wchar_t>& np = use_facet<num_put<wchar_t> >(oss.getloc());
+
+  const wstring empty;
+  wstring result;
+  long inum = 123;
+  double fnum = 123.456;
+
+  np.put(oss.rdbuf(), oss, '+', inum);
+  result = oss.str();
+  VERIFY( result == L"XYZ" );
+
+  oss.clear();
+  oss.str(empty);
+  np.put(oss.rdbuf(), oss, '+', fnum);
+  result = oss.str();
+  VERIFY( result == L"XYZ.ABC" );
+}
+
+int main()
+{
+  test01();
+}

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