1 /*============================================================================= 2 Copyright (c) 2001-2011 Hartmut Kaiser 3 Copyright (c) 2001-2011 Joel de Guzman 4 5 Distributed under the Boost Software License, Version 1.0. (See accompanying 6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 =============================================================================*/ 8 #if !defined(BOOST_SPIRIT_STANDARD_WIDE_NOVEMBER_10_2006_0913AM) 9 #define BOOST_SPIRIT_STANDARD_WIDE_NOVEMBER_10_2006_0913AM 10 11 #if defined(_MSC_VER) 12 #pragma once 13 #endif 14 15 #include <cwctype> 16 #include <string> 17 18 #include <boost/cstdint.hpp> 19 #include <boost/spirit/home/support/assert_msg.hpp> 20 21 namespace boost { namespace spirit { namespace traits 22 { 23 template <std::size_t N> 24 struct wchar_t_size 25 { 26 BOOST_SPIRIT_ASSERT_MSG(N == 1 || N == 2 || N == 4, 27 not_supported_size_of_wchar_t, ()); 28 }; 29 30 template <> struct wchar_t_size<1> { enum { mask = 0xff }; }; 31 template <> struct wchar_t_size<2> { enum { mask = 0xffff }; }; 32 template <> struct wchar_t_size<4> { enum { mask = 0xffffffff }; }; 33 34 }}} 35 36 namespace boost { namespace spirit { namespace char_encoding 37 { 38 /////////////////////////////////////////////////////////////////////////// 39 // Test characters for specified conditions (using std wchar_t functions) 40 /////////////////////////////////////////////////////////////////////////// 41 42 struct standard_wide 43 { 44 typedef wchar_t char_type; 45 46 template <typename Char> 47 static typename std::char_traits<Char>::int_type to_int_typeboost::spirit::char_encoding::standard_wide48 to_int_type(Char ch) 49 { 50 return std::char_traits<Char>::to_int_type(ch); 51 } 52 53 template <typename Char> 54 static Char to_char_typeboost::spirit::char_encoding::standard_wide55 to_char_type(typename std::char_traits<Char>::int_type ch) 56 { 57 return std::char_traits<Char>::to_char_type(ch); 58 } 59 60 static bool ischarboost::spirit::char_encoding::standard_wide61 ischar(int ch) 62 { 63 // we have to watch out for sign extensions (casting is there to 64 // silence certain compilers complaining about signed/unsigned 65 // mismatch) 66 return ( 67 std::size_t(0) == 68 std::size_t(ch & ~traits::wchar_t_size<sizeof(wchar_t)>::mask) || 69 std::size_t(~0) == 70 std::size_t(ch | traits::wchar_t_size<sizeof(wchar_t)>::mask) 71 ) ? true : false; // any wchar_t, but no other bits set 72 } 73 74 static bool isalnumboost::spirit::char_encoding::standard_wide75 isalnum(wchar_t ch) 76 { 77 using namespace std; 78 return iswalnum(to_int_type(ch)) ? true : false; 79 } 80 81 static bool isalphaboost::spirit::char_encoding::standard_wide82 isalpha(wchar_t ch) 83 { 84 using namespace std; 85 return iswalpha(to_int_type(ch)) ? true : false; 86 } 87 88 static bool iscntrlboost::spirit::char_encoding::standard_wide89 iscntrl(wchar_t ch) 90 { 91 using namespace std; 92 return iswcntrl(to_int_type(ch)) ? true : false; 93 } 94 95 static bool isdigitboost::spirit::char_encoding::standard_wide96 isdigit(wchar_t ch) 97 { 98 using namespace std; 99 return iswdigit(to_int_type(ch)) ? true : false; 100 } 101 102 static bool isgraphboost::spirit::char_encoding::standard_wide103 isgraph(wchar_t ch) 104 { 105 using namespace std; 106 return iswgraph(to_int_type(ch)) ? true : false; 107 } 108 109 static bool islowerboost::spirit::char_encoding::standard_wide110 islower(wchar_t ch) 111 { 112 using namespace std; 113 return iswlower(to_int_type(ch)) ? true : false; 114 } 115 116 static bool isprintboost::spirit::char_encoding::standard_wide117 isprint(wchar_t ch) 118 { 119 using namespace std; 120 return iswprint(to_int_type(ch)) ? true : false; 121 } 122 123 static bool ispunctboost::spirit::char_encoding::standard_wide124 ispunct(wchar_t ch) 125 { 126 using namespace std; 127 return iswpunct(to_int_type(ch)) ? true : false; 128 } 129 130 static bool isspaceboost::spirit::char_encoding::standard_wide131 isspace(wchar_t ch) 132 { 133 using namespace std; 134 return iswspace(to_int_type(ch)) ? true : false; 135 } 136 137 static bool isupperboost::spirit::char_encoding::standard_wide138 isupper(wchar_t ch) 139 { 140 using namespace std; 141 return iswupper(to_int_type(ch)) ? true : false; 142 } 143 144 static bool isxdigitboost::spirit::char_encoding::standard_wide145 isxdigit(wchar_t ch) 146 { 147 using namespace std; 148 return iswxdigit(to_int_type(ch)) ? true : false; 149 } 150 151 static bool BOOST_PREVENT_MACRO_SUBSTITUTIONboost::spirit::char_encoding::standard_wide152 isblank BOOST_PREVENT_MACRO_SUBSTITUTION (wchar_t ch) 153 { 154 return (ch == L' ' || ch == L'\t'); 155 } 156 157 /////////////////////////////////////////////////////////////////////// 158 // Simple character conversions 159 /////////////////////////////////////////////////////////////////////// 160 161 static wchar_t tolowerboost::spirit::char_encoding::standard_wide162 tolower(wchar_t ch) 163 { 164 using namespace std; 165 return isupper(ch) ? 166 to_char_type<wchar_t>(towlower(to_int_type(ch))) : ch; 167 } 168 169 static wchar_t toupperboost::spirit::char_encoding::standard_wide170 toupper(wchar_t ch) 171 { 172 using namespace std; 173 return islower(ch) ? 174 to_char_type<wchar_t>(towupper(to_int_type(ch))) : ch; 175 } 176 177 static ::boost::uint32_t toucs4boost::spirit::char_encoding::standard_wide178 toucs4(int ch) 179 { 180 return ch; 181 } 182 }; 183 }}} 184 185 #endif 186 187