1 /*=============================================================================
2 Copyright (c) 1998-2003 Joel de Guzman
3 Copyright (c) 2003 Martin Wille
4 http://spirit.sourceforge.net/
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #if !defined(BOOST_SPIRIT_PRIMITIVES_IPP)
11 #define BOOST_SPIRIT_PRIMITIVES_IPP
14 #if !defined(BOOST_NO_CWCTYPE)
18 #include <string> // char_traits
20 #if defined(BOOST_MSVC)
21 # pragma warning (push)
22 # pragma warning(disable:4800)
25 namespace boost { namespace spirit {
27 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
29 template <typename DrivedT> struct char_parser;
33 template <typename IteratorT>
35 get_last(IteratorT first)
52 typedef typename ScannerT::iterator_t iterator_t;
53 iterator_t saved = scan.first;
54 std::size_t slen = str_last - str_first;
56 while (str_first != str_last)
58 if (scan.at_end() || (*str_first != *scan))
59 return scan.no_match();
64 return scan.create_match(slen, nil_t(), saved, scan.first);
67 ///////////////////////////////////////////////////////////////////////////
69 // Conversion from char_type to int_type
71 ///////////////////////////////////////////////////////////////////////////
73 // Use char_traits for char and wchar_t only, as these are the only
74 // specializations provided in the standard. Other types are on their
77 // For UDT, one may override:
95 // in a namespace suitable for Argument Dependent lookup or in
96 // namespace std (disallowed by the standard).
98 template <typename CharT>
99 struct char_type_char_traits_helper
101 typedef CharT char_type;
102 typedef typename std::char_traits<CharT>::int_type int_type;
104 static int_type to_int_type(CharT c)
106 return std::char_traits<CharT>::to_int_type(c);
109 static char_type to_char_type(int_type i)
111 return std::char_traits<CharT>::to_char_type(i);
115 template <typename CharT>
116 struct char_traits_helper
118 typedef CharT char_type;
119 typedef CharT int_type;
121 static CharT & to_int_type(CharT & c)
126 static CharT & to_char_type(CharT & c)
133 struct char_traits_helper<char>
134 : char_type_char_traits_helper<char>
138 #if !defined(BOOST_NO_CWCTYPE)
141 struct char_traits_helper<wchar_t>
142 : char_type_char_traits_helper<wchar_t>
148 template <typename CharT>
149 inline typename char_traits_helper<CharT>::int_type
152 return char_traits_helper<CharT>::to_int_type(c);
155 template <typename CharT>
157 to_char_type(typename char_traits_helper<CharT>::int_type c)
159 return char_traits_helper<CharT>::to_char_type(c);
162 ///////////////////////////////////////////////////////////////////////
164 // Convenience functions
166 ///////////////////////////////////////////////////////////////////////
168 template <typename CharT>
173 return isalnum(to_int_type(c)) ? true : false;
176 template <typename CharT>
181 return isalpha(to_int_type(c)) ? true : false;
184 template <typename CharT>
189 return iscntrl(to_int_type(c)) ? true : false;
192 template <typename CharT>
197 return isdigit(to_int_type(c)) ? true : false;
200 template <typename CharT>
205 return isgraph(to_int_type(c)) ? true : false;
208 template <typename CharT>
213 return islower(to_int_type(c)) ? true : false;
216 template <typename CharT>
221 return isprint(to_int_type(c)) ? true : false;
224 template <typename CharT>
229 return ispunct(to_int_type(c)) ? true : false;
232 template <typename CharT>
237 return isspace(to_int_type(c)) ? true : false;
240 template <typename CharT>
245 return isupper(to_int_type(c)) ? true : false;
248 template <typename CharT>
253 return isxdigit(to_int_type(c)) ? true : false;
256 template <typename CharT>
260 return (c == ' ' || c == '\t');
263 template <typename CharT>
268 return to_char_type<CharT>(tolower(to_int_type(c)));
271 template <typename CharT>
276 return to_char_type<CharT>(toupper(to_int_type(c)));
279 #if !defined(BOOST_NO_CWCTYPE)
285 return iswalnum(to_int_type(c)) ? true : false;
292 return iswalpha(to_int_type(c)) ? true : false;
299 return iswcntrl(to_int_type(c)) ? true : false;
306 return iswdigit(to_int_type(c)) ? true : false;
313 return iswgraph(to_int_type(c)) ? true : false;
320 return iswlower(to_int_type(c)) ? true : false;
327 return iswprint(to_int_type(c)) ? true : false;
334 return iswpunct(to_int_type(c)) ? true : false;
341 return iswspace(to_int_type(c)) ? true : false;
348 return iswupper(to_int_type(c)) ? true : false;
355 return iswxdigit(to_int_type(c)) ? true : false;
361 return (c == L' ' || c == L'\t');
368 return to_char_type<wchar_t>(towlower(to_int_type(c)));
375 return to_char_type<wchar_t>(towupper(to_int_type(c)));
378 #endif // !defined(BOOST_NO_CWCTYPE)
382 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
384 }} // namespace boost::spirit::impl
387 #pragma warning (pop)