Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60226 - trunk/boost/spirit/home/support
From: joel_at_[hidden]
Date: 2010-03-06 06:26:40


Author: djowel
Date: 2010-03-06 06:26:39 EST (Sat, 06 Mar 2010)
New Revision: 60226
URL: http://svn.boost.org/trac/boost/changeset/60226

Log:
some tweaks (casting chars)
Text files modified:
   trunk/boost/spirit/home/support/char_class.hpp | 22 ++++++++++------------
   1 files changed, 10 insertions(+), 12 deletions(-)

Modified: trunk/boost/spirit/home/support/char_class.hpp
==============================================================================
--- trunk/boost/spirit/home/support/char_class.hpp (original)
+++ trunk/boost/spirit/home/support/char_class.hpp 2010-03-06 06:26:39 EST (Sat, 06 Mar 2010)
@@ -29,32 +29,30 @@
 
 namespace boost { namespace spirit { namespace detail
 {
- // Here's the thing... typical encodings except ASCII deals
- // with unsigned integers > 127. ASCII uses only 127, but most
- // char and wchar_t are signed. Thus, when you convert from, say,
- // char with value > 127 it becomes negative (e.g. char 233 is
- // -23). When you cast this to an unsigned int with 32 bits,
- // you get 4294967273! The trick is to cast to an unsigned version
- // of the source char first before casting to the target.
- // {P.S. Don't worry about the code, the optimizer will
- // optimize the if-else branches}
+ // Here's the thing... typical encodings (except ASCII) deal with unsigned
+ // integers > 127. ASCII uses only 127. Yet, most char and wchar_t are signed.
+ // Thus, a char with value > 127 is negative (e.g. char 233 is -23). When you
+ // cast this to an unsigned int with 32 bits, you get 4294967273!
+ //
+ // The trick is to cast to an unsigned version of the source char first
+ // before casting to the target. {P.S. Don't worry about the code, the
+ // optimizer will optimize the if-else branches}
 
     template <typename TargetChar, typename SourceChar>
     TargetChar cast_char(SourceChar ch)
     {
         if (is_signed<TargetChar>::value != is_signed<SourceChar>::value)
         {
- typedef typename make_unsigned<SourceChar>::type USourceChar;
- typedef typename make_signed<SourceChar>::type SSourceChar;
-
             if (is_signed<SourceChar>::value)
             {
                  // source is signed, target is unsigned
+ typedef typename make_unsigned<SourceChar>::type USourceChar;
                 return TargetChar(USourceChar(ch));
             }
             else
             {
                  // source is unsigned, target is signed
+ typedef typename make_signed<SourceChar>::type SSourceChar;
                 return TargetChar(SSourceChar(ch));
             }
         }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk