Topic: effect of std::ios_base::uppercase?


Author: Dietmar Kuehl <dietmar.kuehl@claas-solutions.de>
Date: 2000/02/18
Raw View
Hi,
In article <88fgga$4qo$1@nnrp1.deja.com>,
  Marcus Barnes <marcus_aurelius@my-deja.com> wrote:
> I was hoping I could encode an uppercase string using a
> std::stringstream.

String don't do such a thing. Instead, you would use the 'std::ctype'
facet for something like this:

  template <typename cT>
  std::basic_string<cT> uppercase(std::basic_string<cT> const& str) {
    typedef std::ctype<cT> CType;
    CType const& ct = std::use_facet<CType>(std::locale());
    std::basic_string<cT> rc(str);
    typename std::basic_string<cT>::iterator end = rc.end();
    for (typename std::basic_string<cT>::iterator it = rc.begin();
         it != end; ++it)
      *it = ct.toupper(*it);
    return rc;
  }

> #include <sstream>
>
> std::string
> Uppercase ( std::wstring const& what )
> {
>   std::stringstream codec;
>
>   codec.setf ( std::ios_base::uppercase );
>   codec << what;

This would something rather different (if it works at all) because you
are trying to write a wide character string to a narrow character
stream.

>   27.4.2.1.2  Type ios_base::fmtflags             [lib.ios::fmtflags]
>
>        |uppercase    replaces certain lowercase letters with their |
>        |             uppercase equivalents in generated output.    |
>        +-----------------------------------------------------------+
>
> while in the Microsoft documents (and Dinkumware's) it says this:
>
> o uppercase, to insert uppercase equivalents of lowercase letters in
>   certain insertions.
>   ^^^^^^^
>
> So it seems there's some ambiguity about which formatted I/O
> operations
> are affected by this flag. Is this correct behavior by VC++ or not?

The "certain insertions" referred to are actually those formatting
functions which produce letters. These are effectively the numeric
formatting function writing hexadecimal and exponsents: When procuding
a hexadecimal number, the letters a to f and the letter x (if the base
is shown) will be capatilized depending on the setting of the
'uppercase' flag. Correspondingly, the letter e for the exponent in
floating point formatting might be uppercase.

The decription of the flag's behavior at the definition point in the
standard is more or less informative. The relevant sections are the
decription of numeric formatting (the 'num_put' facet in Chapter 22)
and formatted I/O in general. While the former mentions the use of
'uppercase' to create a suitable format specifier, the latter never
mention it.
--
<mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Marcus Barnes <marcus_aurelius@my-deja.com>
Date: 2000/02/24
Raw View
In article <88hfo5$glv$1@nnrp1.deja.com>,
  Dietmar Kuehl <dietmar.kuehl@claas-solutions.de> wrote:
>
> String don't do such a thing. Instead, you would use the 'std::ctype'
> facet for something like this:

[cut]

I was hoping that the Standard library wouldn't make me write the
toupper() loop.

[cut]

> This would something rather different (if it works at all) because you
> are trying to write a wide character string to a narrow character
> stream.

True. That was a typo of mine though. I did not intend to mix string and
wstring in the example.

Thanks!
--
Regards, Marcus


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Marcus Barnes <marcus_aurelius@my-deja.com>
Date: 2000/02/17
Raw View
Hi All,

I was hoping I could encode an uppercase string using a
std::stringstream. This is the code I thought would do it:

=====
#include <sstream>

std::string
Uppercase ( std::wstring const& what )
{
  std::stringstream codec;

  codec.setf ( std::ios_base::uppercase );
  codec << what;
  return codec.str ();
}
=====

But using the VC++ 6.0 Standard library (Dinkumware's), the insertion
doesn't invoke ctype::toupper() and the string remains unchanged in
this respect. In the 1996 Draft Standard I find this comment about
uppercase:

  27.4.2.1.2  Type ios_base::fmtflags             [lib.ios::fmtflags]

       |uppercase    replaces certain lowercase letters with their |
       |             uppercase equivalents in generated output.    |
       +-----------------------------------------------------------+

while in the Microsoft documents (and Dinkumware's) it says this:

o uppercase, to insert uppercase equivalents of lowercase letters in
  certain insertions.
  ^^^^^^^

So it seems there's some ambiguity about which formatted I/O operations
are affected by this flag. Is this correct behavior by VC++ or not?

--
Regards, Marcus

--
Regards, Marcus


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/02/18
Raw View
Marcus Barnes wrote:
>
> Hi All,
>
> I was hoping I could encode an uppercase string using a
> std::stringstream. This is the code I thought would do it:
>
> =====
> #include <sstream>
>
> std::string
> Uppercase ( std::wstring const& what )
> {
>   std::stringstream codec;
>
>   codec.setf ( std::ios_base::uppercase );
>   codec << what;
>   return codec.str ();
> }
> =====
>
> But using the VC++ 6.0 Standard library (Dinkumware's), the insertion
> doesn't invoke ctype::toupper() and the string remains unchanged in
> this respect. In the 1996 Draft Standard I find this comment about
> uppercase:
>
>   27.4.2.1.2  Type ios_base::fmtflags             [lib.ios::fmtflags]
>
>        |uppercase    replaces certain lowercase letters with their |
>        |             uppercase equivalents in generated output.    |
>        +-----------------------------------------------------------+
>
> while in the Microsoft documents (and Dinkumware's) it says this:
>
> o uppercase, to insert uppercase equivalents of lowercase letters in
>   certain insertions.
>   ^^^^^^^
>
> So it seems there's some ambiguity about which formatted I/O operations
> are affected by this flag. Is this correct behavior by VC++ or not?

Specifically, 'uppercase' is only supposed to control the letters
printed as part of numeric values - the extra letters that appear when
printing in bases higher than 10, and the 'e' or 'E' used to define the
exponent. It's a single flag that corresponds to the distinction in
printf() formats between %X and %x, %E and %e, %G and %g. It's not
intended to be a general-purpose case conversion. See Tables 57 and 58,
in section 22.2.2.2.2 p7-8.

I have to agree that the description of uppercase is far from clear, but
that is what it means.
You'll have to invoke toupper() directly. Note that
std::ctype<charT>::toupper() is overloaded to accept a charT* range,
doing an in-place conversion, so you don't have to write the loop
yourself.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]