This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[PATCH] Fix second half of PR3655
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: libstdc++ at gcc dot gnu dot org
- Cc: bkoz at redhat dot com
- Date: Thu, 29 Nov 2001 19:22:10 +0100
- Subject: [PATCH] Fix second half of PR3655
Hi again,
now for the second part of the padding issues evidenced by PR3655. These are summarized
in:
http://gcc.gnu.org/ml/libstdc++/2001-11/msg00308.html
I have followed Benjamin suggestion of adding a (bool) parameter to __pad, in order to
enable the helper function to deal correctly with both num-type and const char*-type
objects, thus implementing the corresponding rules prescribed by the Standard.
After all, I think that the additional parameter confers robusteness to the padding
algorithm, easing the fix of any additional issue which may surface in the future.
At this point, I have not tried to play tricks to simulate default-parameters for
non-member templates, which would have reduced the amount of new code, at the expense of
clarity: the user of __pad has to explicitly choose between two different padding
strategies.
I'm not sure that, in principle, the new testcase really belongs to the same test file as
the other (for PR3655), but in this way it is much more clear the contrast between the two
situations.
Booted and tested i686-pc-linux-gnu, as usual.
Cheers,
Paolo.
/////////////////////
Paolo Carlini <pcarlini@unitus.it>
Benjamin Kosnik <bkoz@redhat.com>
* include/bits/locale_facets.tcc (__pad): Add bool parameter to correctly pad
internal-adjusted const char*-type objects.
* include/bits/ostream.tcc (operator<<(_CharT , char, const _CharT*, const char*,
const basic_string&): Update calls.
* src/locale-inst.tcc: Update declarations.
* testsuite/27_io/ostream_inserter_arith.cc (test04): Add test.
diff -urN gcc-vanilla/libstdc++-v3/include/bits/locale_facets.tcc
gcc/libstdc++-v3/include/bits/locale_facets.tcc
--- gcc-vanilla/libstdc++-v3/include/bits/locale_facets.tcc Thu Nov 29 08:57:05 2001
+++ gcc/libstdc++-v3/include/bits/locale_facets.tcc Thu Nov 29 18:40:07 2001
@@ -814,7 +814,7 @@
streamsize __w = __io.width();
if (__w > static_cast<streamsize>(__len))
{
- __pad(__io, __fill, __ws2, __ws, __w, __len);
+ __pad(__io, __fill, __ws2, __ws, __w, __len, true);
__len = static_cast<int>(__w);
// Switch strings.
__ws = __ws2;
@@ -1913,11 +1913,13 @@
// Assumes
// __newlen > __oldlen
// __news is allocated for __newlen size
- // Used by both num_put and ostream inserters.
+ // Used by both num_put and ostream inserters: if __numtype, internal-adjusted
+ // objects are padded according to the rules below concerning 0[xX] and +-,
+ // otherwise, exactly as right-adjusted ones are.
template<typename _CharT, typename _Traits>
void
__pad(ios_base& __io, _CharT __fill, _CharT* __news, const _CharT* __olds,
- const streamsize __newlen, const streamsize __oldlen)
+ const streamsize __newlen, const streamsize __oldlen, const bool __numtype)
{
typedef _CharT char_type;
typedef _Traits traits_type;
@@ -1940,7 +1942,7 @@
__beglen = __oldlen;
__end = __pads;
}
- else if (__adjust == ios_base::internal)
+ else if (__adjust == ios_base::internal && __numtype)
{
// Pad after the sign, if there is one.
// Pad after 0[xX], if there is one.
@@ -1997,10 +1999,10 @@
template<typename _CharT>
void
__pad(ios_base& __io, _CharT __fill, _CharT* __news, const _CharT* __olds,
- const streamsize __newlen, const streamsize __oldlen)
+ const streamsize __newlen, const streamsize __oldlen, const bool __numtype)
{
return __pad<_CharT, char_traits<_CharT> >(__io, __fill, __news,
- __olds, __newlen, __oldlen);
+ __olds, __newlen, __oldlen, __numtype);
}
// Used by both numeric and monetary facets.
diff -urN gcc-vanilla/libstdc++-v3/include/bits/ostream.tcc
gcc/libstdc++-v3/include/bits/ostream.tcc
--- gcc-vanilla/libstdc++-v3/include/bits/ostream.tcc Wed Nov 28 12:57:17 2001
+++ gcc/libstdc++-v3/include/bits/ostream.tcc Thu Nov 29 15:21:58 2001
@@ -481,7 +481,7 @@
streamsize __len = 1;
if (__w > __len)
{
- __pad(__out, __out.fill(), __pads, &__c, __w, __len);
+ __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
__len = __w;
}
__out.write(__pads, __len);
@@ -516,7 +516,7 @@
streamsize __len = 1;
if (__w > __len)
{
- __pad(__out, __out.fill(), __pads, &__c, __w, __len);
+ __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
__len = __w;
}
__out.write(__pads, __len);
@@ -549,7 +549,7 @@
streamsize __len = static_cast<streamsize>(_Traits::length(__s));
if (__w > __len)
{
- __pad(__out, __out.fill(), __pads, __s, __w, __len);
+ __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
__s = __pads;
__len = __w;
}
@@ -595,7 +595,7 @@
if (__w > __len)
{
- __pad(__out, __out.fill(), __pads, __ws, __w, __len);
+ __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
__str = __pads;
__len = __w;
}
@@ -630,7 +630,7 @@
streamsize __len = static_cast<streamsize>(_Traits::length(__s));
if (__w > __len)
{
- __pad(__out, __out.fill(), __pads, __s, __w, __len);
+ __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
__s = __pads;
__len = __w;
}
@@ -668,7 +668,7 @@
#endif
if (__w > __len)
{
- __pad(__out, __out.fill(), __pads, __s, __w, __len);
+ __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
__s = __pads;
__len = __w;
}
diff -urN gcc-vanilla/libstdc++-v3/src/locale-inst.cc gcc/libstdc++-v3/src/locale-inst.cc
--- gcc-vanilla/libstdc++-v3/src/locale-inst.cc Wed Nov 28 12:57:19 2001
+++ gcc/libstdc++-v3/src/locale-inst.cc Thu Nov 29 15:27:14 2001
@@ -337,12 +337,12 @@
template
void
__pad<char>(ios_base&, char, char*, const char *, streamsize,
- streamsize);
+ streamsize, bool);
template
void
__pad<char, char_traits<char> >(ios_base&, char, char*,
- const char *, streamsize, streamsize);
+ const char *, streamsize, streamsize, bool);
#ifdef _GLIBCPP_USE_WCHAR_T
template
@@ -363,13 +363,13 @@
template
void
__pad<wchar_t>(ios_base&, wchar_t, wchar_t*, const wchar_t*,
- streamsize, streamsize);
+ streamsize, streamsize, bool);
template
void
__pad<wchar_t, char_traits<wchar_t> >(ios_base&, wchar_t, wchar_t*,
const wchar_t*,
- streamsize, streamsize);
+ streamsize, streamsize, bool);
#endif // _GLIBCPP_USE_WCHAR_T
template
diff -urN gcc-vanilla/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc
gcc/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc
--- gcc-vanilla/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc Thu Nov 29 08:57:05
2001
+++ gcc/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc Thu Nov 29 18:44:37 2001
@@ -312,11 +312,15 @@
int
test04()
{
- stringbuf strbuf;
- ostream o(&strbuf);
+ stringbuf strbuf1, strbuf2;
+ ostream o1(&strbuf1), o2(&strbuf2);
- o << hex << showbase << setw(6) << internal << 255;
- VERIFY( strbuf.str() == "0x ff" );
+ o1 << hex << showbase << setw(6) << internal << 0xff;
+ VERIFY( strbuf1.str() == "0x ff" );
+
+ // ... vs internal-adjusted const char*-type objects
+ o2 << hex << showbase << setw(6) << internal << "0xff";
+ VERIFY( strbuf2.str() == " 0xff" );
return 0;
}