parse comma delimited text string

D

Dietmar Kuehl

Alf said:
result = static_cast<char>( std::toupper( s ) );


The above line shall read

result = std::toupper( static_cast<unsigned char>(s));

While the compiler automatically takes care of the conversion from
'char' to 'int', it could produce negative values on systems where
'char' is a signed type without the explicit conversion to 'unsigned
char'. However, 'std::toupper(int)' only accepts positive values
making it necessary to convert the 'char' to an 'unsigned char'
first.
 
D

Daniel T.

Dietmar Kuehl said:
Note that this does *NOT* work! The argument to 'std::tolower()'
has to be an *unsigned* value. However, on platforms where 'char'
is signed, the argument could expand to a negative value! In
portable code, the only valid call to 'std::tolower(int)' with a
'char' looks like this:

std::tolower(static_cast<unsigned char>(c));

The only possible variation is how the 'char' is first cast to
an 'unsigned char'.

That is only necessary if one is using an extended character set. For
all ASCII characters, "tolower( c )" will work just fine. In order to
write portable code, one must not use an extended character set, making
the cast will not magically make it portable.
 
D

Daniel T.

Dietmar Kuehl said:
Injecting existing filtering stream buffers into the processing is
pretty straight forward. Of course, writing them is not necessarily
so but it is not that hard either. The real advantage I see in the
filtering stream buffer over your code is that it encapsulates the
complete solution, especially if it is also equipped with a simple
input stream which automatically maintains the stream buffer.

I read up on streambuf's last night. Although, your solution does work,
I think it is inappropriate. Stream Buffers are supposed to represent
buffering strategies for different kinds of streams, not conversion
strategies.

Both of our codes encapsulated the complete solution.
 
D

Dietmar Kuehl

Daniel said:
I read up on streambuf's last night.

Just out if interest: where did you read up on them?
Although, your solution does work,
I think it is inappropriate. Stream Buffers are supposed to represent
buffering strategies for different kinds of streams, not conversion
strategies.

Stream buffers are intended to represent streams of characters.
When streaming characters, it is quite common to transform them
in some way. Actually, this is the basis of UNIX' approach to
filters. My code effectively just did the moral equivalent of
"tr 'A-Z,' 'a-z '" which is, IMO, an entirely appropriate use
of streams.

In fact, even at the very basis of file streams, (i.e. in the
class template 'std::basic_filebuf') a heavy conversion mechanism
is employed which converts bytes read from a file into characters
used within an application or vice versa. This uses 'std::codecvt'
facets and it would be an easy typing exercise to inject the
conversion I implemented in the stream buffer into a corresponding
facet.
Both of our codes encapsulated the complete solution.

Well, I somewhat disagree with this statement but then, the
problem was trivial enough that it may not be worth to encapsulate
the solution further than you did.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Daniel said:
That is only necessary if one is using an extended character set. For
all ASCII characters, "tolower( c )" will work just fine. In order to
write portable code, one must not use an extended character set, making
the cast will not magically make it portable.

As far as I know, standard C++ does not privilege the ascii charset in any
way, nor any other 0-127 charset.

Of course you have less problems if you know for sure that all your users
use ascii. But you will also have less users.
 
A

Alf P. Steinbach

* Dietmar Kuehl:
Alf said:
result = static_cast<char>( std::toupper( s ) );


The above line shall read

result = std::toupper( static_cast<unsigned char>(s));


Thanks for that correction, it was a screw-up.

While the compiler automatically takes care of the conversion from
'char' to 'int', it could produce negative values on systems where
'char' is a signed type without the explicit conversion to 'unsigned
char'. However, 'std::toupper(int)' only accepts positive values

And EOF. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top