This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch] Some work on money_get::do_get
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ at gcc dot gnu dot org
- Cc: Benjamin Kosnik <bkoz at redhat dot com>
- Date: Thu, 23 Oct 2003 13:33:58 +0200
- Subject: [Patch] Some work on money_get::do_get
Hi,
this all started trying to avoid this loop:
while (__tmp_units.size() > 1 && __tmp_units[0] == __ctype.widen('0'))
__tmp_units.erase(__tmp_units.begin());
that I didn't like (repeated calls to widen, repetead calls to erase...).
In the process, I noticed two conformance issues (see testcases):
1- According to 22.2.6.1.2, p4, "... preceded by a minus sign if and
only if the result is negative" (see first test).
2- We tried to fail whenever __tmp_units was empty, but failed to do
it when _sign.size() && __sign == __neg_sign (see second test).
I find the final result quite clean and efficient ;)
Tested x86-linux, will wait a little for comments, as usual.
Paolo.
////////////
2003-10-23 Paolo Carlini <pcarlini@suse.de>
* include/bits/locale_facets.tcc (money_get<>::do_get(...,
string_type&)): Use find_first_not_of to strip leading
zeros; if __tmp_units == "0" never prefix it with '-';
always fail if __tmp_units is empty.
* testsuite/22_locale/money_get/get/char/10.cc: New.
* testsuite/22_locale/money_get/get/wchar_t/10.cc: Ditto.
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 Wed Oct 22 22:46:40 2003
--- libstdc++-v3/include/bits/locale_facets.tcc Thu Oct 23 10:10:25 2003
*************** namespace std
*** 1284,1321 ****
__testvalid = false;
}
! // Strip leading zeros.
! while (__tmp_units.size() > 1 && __tmp_units[0] == __ctype.widen('0'))
! __tmp_units.erase(__tmp_units.begin());
!
! if (__sign.size() && __sign == __neg_sign)
! __tmp_units.insert(__tmp_units.begin(), __ctype.widen('-'));
! // Test for grouping fidelity.
! if (__grouping.size() && __grouping_tmp.size())
{
! if (!std::__verify_grouping(__grouping, __grouping_tmp))
! __testvalid = false;
}
! // Iff no more characters are available.
! if (__c == __eof)
! __err |= ios_base::eofbit;
!
! // Iff not enough digits were supplied after the decimal-point.
! if (__testdecfound)
{
! const int __frac = __intl ? __mpt.frac_digits()
! : __mpf.frac_digits();
! if (__frac > 0)
{
! if (__sep_pos != __frac)
__testvalid = false;
}
}
// Iff valid sequence is not recognized.
! if (!__testvalid || !__tmp_units.size())
__err |= ios_base::failbit;
else
// Use the "swap trick" to copy __tmp_units into __units.
--- 1284,1332 ----
__testvalid = false;
}
! const char_type __zero = __ctype.widen('0');
! // Strip leading zeros.
! if (__tmp_units.size() > 1)
{
! const size_type __first = __tmp_units.find_first_not_of(__zero);
! const bool __only_zeros = __first == string_type::npos;
! __tmp_units.erase(0, __only_zeros ? __tmp_units.size() - 1
! : __first);
}
! if (__tmp_units.size())
{
! // 22.2.6.1.2, p4
! if (__sign.size() && __sign == __neg_sign
! && __tmp_units[0] != __zero)
! __tmp_units.insert(__tmp_units.begin(), __ctype.widen('-'));
!
! // Test for grouping fidelity.
! if (__grouping.size() && __grouping_tmp.size())
{
! if (!std::__verify_grouping(__grouping, __grouping_tmp))
! __testvalid = false;
! }
!
! // Iff not enough digits were supplied after the decimal-point.
! if (__testdecfound)
! {
! const int __frac = __intl ? __mpt.frac_digits()
! : __mpf.frac_digits();
! if (__frac > 0 && __sep_pos != __frac)
__testvalid = false;
}
}
+ else
+ __testvalid = false;
+
+ // Iff no more characters are available.
+ if (__c == __eof)
+ __err |= ios_base::eofbit;
// Iff valid sequence is not recognized.
! if (!__testvalid)
__err |= ios_base::failbit;
else
// Use the "swap trick" to copy __tmp_units into __units.
diff -prN libstdc++-v3-orig/testsuite/22_locale/money_get/get/char/10.cc libstdc++-v3/testsuite/22_locale/money_get/get/char/10.cc
*** libstdc++-v3-orig/testsuite/22_locale/money_get/get/char/10.cc Thu Jan 1 01:00:00 1970
--- libstdc++-v3/testsuite/22_locale/money_get/get/char/10.cc Thu Oct 23 10:45:07 2003
***************
*** 0 ****
--- 1,62 ----
+ // 2003-10-23 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.6.1.1 money_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;
+
+ locale loc_us = __gnu_test::try_named_locale("en_US");
+
+ iterator_type end;
+ istringstream iss;
+ iss.imbue(loc_us);
+
+ const money_get<char>& mon_get = use_facet<money_get<char> >(iss.getloc());
+
+ iss.str("-$0 ");
+ iterator_type is_it(iss);
+ string extracted_amount;
+ ios_base::iostate err = ios_base::goodbit;
+ mon_get.get(is_it, end, false, iss, err, extracted_amount);
+ VERIFY( extracted_amount == "0" );
+ VERIFY( err == ios_base::goodbit );
+
+ iss.str("-$ ");
+ iterator_type is_it_2(iss);
+ extracted_amount.clear();
+ mon_get.get(is_it_2, end, false, iss, err, extracted_amount);
+ VERIFY( extracted_amount.empty() );
+ VERIFY( err == ios_base::failbit );
+ }
+
+ int main()
+ {
+ test01();
+ return 0;
+ }
diff -prN libstdc++-v3-orig/testsuite/22_locale/money_get/get/wchar_t/10.cc libstdc++-v3/testsuite/22_locale/money_get/get/wchar_t/10.cc
*** libstdc++-v3-orig/testsuite/22_locale/money_get/get/wchar_t/10.cc Thu Jan 1 01:00:00 1970
--- libstdc++-v3/testsuite/22_locale/money_get/get/wchar_t/10.cc Thu Oct 23 12:17:41 2003
***************
*** 0 ****
--- 1,62 ----
+ // 2003-10-23 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.6.1.1 money_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;
+
+ locale loc_us = __gnu_test::try_named_locale("en_US");
+
+ iterator_type end;
+ wistringstream iss;
+ iss.imbue(loc_us);
+
+ const money_get<wchar_t>& mon_get = use_facet<money_get<wchar_t> >(iss.getloc());
+
+ iss.str(L"-$0 ");
+ iterator_type is_it(iss);
+ wstring extracted_amount;
+ ios_base::iostate err = ios_base::goodbit;
+ mon_get.get(is_it, end, false, iss, err, extracted_amount);
+ VERIFY( extracted_amount == L"0" );
+ VERIFY( err == ios_base::goodbit );
+
+ iss.str(L"-$ ");
+ iterator_type is_it_2(iss);
+ extracted_amount.clear();
+ mon_get.get(is_it_2, end, false, iss, err, extracted_amount);
+ VERIFY( extracted_amount.empty() );
+ VERIFY( err == ios_base::failbit );
+ }
+
+ int main()
+ {
+ test01();
+ return 0;
+ }