![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /usr/include/boost/spirit/home/x3/char/detail/ |
Upload File : |
/*============================================================================= Copyright (c) 2001-2014 Joel de Guzman Copyright (c) 2001-2011 Hartmut Kaiser Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #if !defined(BOOST_SPIRIT_X3_CAST_CHAR_NOVEMBER_10_2006_0907AM) #define BOOST_SPIRIT_X3_CAST_CHAR_NOVEMBER_10_2006_0907AM #include <boost/type_traits/is_signed.hpp> #include <boost/type_traits/make_unsigned.hpp> #include <boost/type_traits/make_signed.hpp> namespace boost { namespace spirit { namespace x3 { namespace detail { // 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) { 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)); } } else { // source and target has same signedness return TargetChar(ch); // just cast } } }}}} #endif