Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / spirit / home / support / char_encoding / standard_wide.hpp
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/assert.hpp>
19 #include <boost/cstdint.hpp>
20 #include <boost/spirit/home/support/assert_msg.hpp>
21
22 namespace boost { namespace spirit { namespace traits
23 {
24     template <std::size_t N>
25     struct wchar_t_size
26     {
27         BOOST_SPIRIT_ASSERT_MSG(N == 1 || N == 2 || N == 4,
28             not_supported_size_of_wchar_t, ());
29     };
30
31     template <> struct wchar_t_size<1> { enum { mask = 0xff }; };
32     template <> struct wchar_t_size<2> { enum { mask = 0xffff }; };
33     template <> struct wchar_t_size<4> { enum { mask = 0xffffffff }; };
34
35 }}}
36
37 namespace boost { namespace spirit { namespace char_encoding
38 {
39     ///////////////////////////////////////////////////////////////////////////
40     //  Test characters for specified conditions (using std wchar_t functions)
41     ///////////////////////////////////////////////////////////////////////////
42
43     struct standard_wide
44     {
45         typedef wchar_t char_type;
46         typedef wchar_t classify_type;
47
48         template <typename Char>
49         static typename std::char_traits<Char>::int_type
50         to_int_type(Char ch)
51         {
52             return std::char_traits<Char>::to_int_type(ch);
53         }
54
55         template <typename Char>
56         static Char
57         to_char_type(typename std::char_traits<Char>::int_type ch)
58         {
59             return std::char_traits<Char>::to_char_type(ch);
60         }
61
62         static bool
63         ischar(int ch)
64         {
65             // we have to watch out for sign extensions (casting is there to
66             // silence certain compilers complaining about signed/unsigned
67             // mismatch)
68             return (
69                 std::size_t(0) ==
70                     std::size_t(ch & ~traits::wchar_t_size<sizeof(wchar_t)>::mask) ||
71                 std::size_t(~0) ==
72                     std::size_t(ch | traits::wchar_t_size<sizeof(wchar_t)>::mask)
73             ) != 0;     // any wchar_t, but no other bits set
74         }
75
76         // *** Note on assertions: The precondition is that the calls to
77         // these functions do not violate the required range of ch (type int)
78         // which is that strict_ischar(ch) should be true. It is the
79         // responsibility of the caller to make sure this precondition is not
80         // violated.
81
82         static bool
83         strict_ischar(int ch)
84         {
85             // ch should be representable as a wchar_t
86             return ch >= WCHAR_MIN && ch <= WCHAR_MAX;
87         }
88
89         static bool
90         isalnum(wchar_t ch)
91         {
92             using namespace std;
93             BOOST_ASSERT(strict_ischar(ch));
94             return iswalnum(to_int_type(ch)) != 0;
95         }
96
97         static bool
98         isalpha(wchar_t ch)
99         {
100             using namespace std;
101             BOOST_ASSERT(strict_ischar(ch));
102             return iswalpha(to_int_type(ch)) != 0;
103         }
104
105         static bool
106         iscntrl(wchar_t ch)
107         {
108             using namespace std;
109             BOOST_ASSERT(strict_ischar(ch));
110             return iswcntrl(to_int_type(ch)) != 0;
111         }
112
113         static bool
114         isdigit(wchar_t ch)
115         {
116             using namespace std;
117             BOOST_ASSERT(strict_ischar(ch));
118             return iswdigit(to_int_type(ch)) != 0;
119         }
120
121         static bool
122         isgraph(wchar_t ch)
123         {
124             using namespace std;
125             BOOST_ASSERT(strict_ischar(ch));
126             return iswgraph(to_int_type(ch)) != 0;
127         }
128
129         static bool
130         islower(wchar_t ch)
131         {
132             using namespace std;
133             BOOST_ASSERT(strict_ischar(ch));
134             return iswlower(to_int_type(ch)) != 0;
135         }
136
137         static bool
138         isprint(wchar_t ch)
139         {
140             using namespace std;
141             return iswprint(to_int_type(ch)) != 0;
142         }
143
144         static bool
145         ispunct(wchar_t ch)
146         {
147             using namespace std;
148             BOOST_ASSERT(strict_ischar(ch));
149             return iswpunct(to_int_type(ch)) != 0;
150         }
151
152         static bool
153         isspace(wchar_t ch)
154         {
155             using namespace std;
156             BOOST_ASSERT(strict_ischar(ch));
157             return iswspace(to_int_type(ch)) != 0;
158         }
159
160         static bool
161         isupper(wchar_t ch)
162         {
163             using namespace std;
164             BOOST_ASSERT(strict_ischar(ch));
165             return iswupper(to_int_type(ch)) != 0;
166         }
167
168         static bool
169         isxdigit(wchar_t ch)
170         {
171             using namespace std;
172             BOOST_ASSERT(strict_ischar(ch));
173             return iswxdigit(to_int_type(ch)) != 0;
174         }
175
176         static bool
177         isblank BOOST_PREVENT_MACRO_SUBSTITUTION (wchar_t ch)
178         {
179             BOOST_ASSERT(strict_ischar(ch));
180             return (ch == L' ' || ch == L'\t');
181         }
182
183         ///////////////////////////////////////////////////////////////////////
184         //  Simple character conversions
185         ///////////////////////////////////////////////////////////////////////
186
187         static wchar_t
188         tolower(wchar_t ch)
189         {
190             using namespace std;
191             BOOST_ASSERT(strict_ischar(ch));
192             return isupper(ch) ?
193                 to_char_type<wchar_t>(towlower(to_int_type(ch))) : ch;
194         }
195
196         static wchar_t
197         toupper(wchar_t ch)
198         {
199             using namespace std;
200             BOOST_ASSERT(strict_ischar(ch));
201             return islower(ch) ?
202                 to_char_type<wchar_t>(towupper(to_int_type(ch))) : ch;
203         }
204
205         static ::boost::uint32_t
206         toucs4(int ch)
207         {
208             BOOST_ASSERT(strict_ischar(ch));
209             return ch;
210         }
211     };
212 }}}
213
214 #endif