Boost logo

Boost :

Subject: Re: [boost] [Convert] std::isspace requires unsigned #28
From: Vladimir Batov (Vladimir.Batov_at_[hidden])
Date: 2017-03-21 00:16:30


> On Mon, Mar 20, 2017 at 9:07 PM, Vladimir Batov via Boost
> <boost_at_[hidden]> wrote:
>> On 2017-03-20 22:13, Peter Dimov via Boost wrote:
>>> ...
>>> The analyzer is correct; passing char to isspace is a well-known bug,
>>> should be cast to unsigned char. The accepted range of the argument
>>> to
>>> isspace is -1..255 (assuming 8 bit character table), with -1 being
>>> EOF, and the default promotion from char to int doesn't do the right
>>> thing when char is signed.
>>
>> Thanks, Peter, for clarifications. Clearly, the analyzer is correct.
>> My
>> interpretation was that it was the documented isspace() behavior. So,
>> I felt
>> somewhat uncomfortable "correcting"/changing the documented behavior.
>> Now
>> that you mention that it is a well-known bug to be (unfortunately)
>> addressed
>> I did just that. Thanks again. Much appreciated.

Damn confused.

WAS: std::isspace(ch);
THEN: std::isspace(static_cast<unsigned char>(ch));

THEN I realized I needed wchar_t support and therefore needed also
std::iswspace(). So, for such a seemingly trivial thing I ended up with
the following ugliness:

namespace cnv
{
     template<typename char_type>
     struct char_test {};

     template<>
     struct char_test<char>
     {
         static bool is_space(char ch)
         {
             return std::isspace(static_cast<unsigned char>(ch));
         }
     };
     template<>
     struct char_test<wchar_t>
     {
         static bool is_space(wchar_t ch)
         {
             return std::iswspace(ch);
         }
     };
     template<typename char_type>
     bool
     is_space(char_type ch)
     {
         return char_test<char_type>::is_space(ch);
     }
}

That raises two questions.

1) Is it really the only way to go or I went senile or fell behind
language-wise?
2) What about std::iswspace? Does it also need static_cast?

Apologies for seemingly naive questions but I really feel startled.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk