standard_wide.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_STANDARD_WIDE_NOVEMBER_10_2006_0913AM)
  8. #define BOOST_SPIRIT_STANDARD_WIDE_NOVEMBER_10_2006_0913AM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <cwctype>
  13. #include <string>
  14. #include <boost/assert.hpp>
  15. #include <boost/cstdint.hpp>
  16. #include <boost/spirit/home/support/assert_msg.hpp>
  17. namespace boost { namespace spirit { namespace traits
  18. {
  19. template <std::size_t N>
  20. struct wchar_t_size
  21. {
  22. BOOST_SPIRIT_ASSERT_MSG(N == 1 || N == 2 || N == 4,
  23. not_supported_size_of_wchar_t, ());
  24. };
  25. template <> struct wchar_t_size<1> { enum { mask = 0xff }; };
  26. template <> struct wchar_t_size<2> { enum { mask = 0xffff }; };
  27. template <> struct wchar_t_size<4> { enum { mask = 0xffffffff }; };
  28. }}}
  29. namespace boost { namespace spirit { namespace char_encoding
  30. {
  31. ///////////////////////////////////////////////////////////////////////////
  32. // Test characters for specified conditions (using std wchar_t functions)
  33. ///////////////////////////////////////////////////////////////////////////
  34. struct standard_wide
  35. {
  36. typedef wchar_t char_type;
  37. typedef wchar_t classify_type;
  38. template <typename Char>
  39. static typename std::char_traits<Char>::int_type
  40. to_int_type(Char ch)
  41. {
  42. return std::char_traits<Char>::to_int_type(ch);
  43. }
  44. template <typename Char>
  45. static Char
  46. to_char_type(typename std::char_traits<Char>::int_type ch)
  47. {
  48. return std::char_traits<Char>::to_char_type(ch);
  49. }
  50. static bool
  51. ischar(int ch)
  52. {
  53. // we have to watch out for sign extensions (casting is there to
  54. // silence certain compilers complaining about signed/unsigned
  55. // mismatch)
  56. return (
  57. std::size_t(0) ==
  58. std::size_t(ch & ~traits::wchar_t_size<sizeof(wchar_t)>::mask) ||
  59. std::size_t(~0) ==
  60. std::size_t(ch | traits::wchar_t_size<sizeof(wchar_t)>::mask)
  61. ) != 0; // any wchar_t, but no other bits set
  62. }
  63. // *** Note on assertions: The precondition is that the calls to
  64. // these functions do not violate the required range of ch (type int)
  65. // which is that strict_ischar(ch) should be true. It is the
  66. // responsibility of the caller to make sure this precondition is not
  67. // violated.
  68. static bool
  69. strict_ischar(int ch)
  70. {
  71. // ch should be representable as a wchar_t
  72. return ch >= WCHAR_MIN && ch <= WCHAR_MAX;
  73. }
  74. static bool
  75. isalnum(wchar_t ch)
  76. {
  77. using namespace std;
  78. BOOST_ASSERT(strict_ischar(ch));
  79. return iswalnum(to_int_type(ch)) != 0;
  80. }
  81. static bool
  82. isalpha(wchar_t ch)
  83. {
  84. using namespace std;
  85. BOOST_ASSERT(strict_ischar(ch));
  86. return iswalpha(to_int_type(ch)) != 0;
  87. }
  88. static bool
  89. iscntrl(wchar_t ch)
  90. {
  91. using namespace std;
  92. BOOST_ASSERT(strict_ischar(ch));
  93. return iswcntrl(to_int_type(ch)) != 0;
  94. }
  95. static bool
  96. isdigit(wchar_t ch)
  97. {
  98. using namespace std;
  99. BOOST_ASSERT(strict_ischar(ch));
  100. return iswdigit(to_int_type(ch)) != 0;
  101. }
  102. static bool
  103. isgraph(wchar_t ch)
  104. {
  105. using namespace std;
  106. BOOST_ASSERT(strict_ischar(ch));
  107. return iswgraph(to_int_type(ch)) != 0;
  108. }
  109. static bool
  110. islower(wchar_t ch)
  111. {
  112. using namespace std;
  113. BOOST_ASSERT(strict_ischar(ch));
  114. return iswlower(to_int_type(ch)) != 0;
  115. }
  116. static bool
  117. isprint(wchar_t ch)
  118. {
  119. using namespace std;
  120. return iswprint(to_int_type(ch)) != 0;
  121. }
  122. static bool
  123. ispunct(wchar_t ch)
  124. {
  125. using namespace std;
  126. BOOST_ASSERT(strict_ischar(ch));
  127. return iswpunct(to_int_type(ch)) != 0;
  128. }
  129. static bool
  130. isspace(wchar_t ch)
  131. {
  132. using namespace std;
  133. BOOST_ASSERT(strict_ischar(ch));
  134. return iswspace(to_int_type(ch)) != 0;
  135. }
  136. static bool
  137. isupper(wchar_t ch)
  138. {
  139. using namespace std;
  140. BOOST_ASSERT(strict_ischar(ch));
  141. return iswupper(to_int_type(ch)) != 0;
  142. }
  143. static bool
  144. isxdigit(wchar_t ch)
  145. {
  146. using namespace std;
  147. BOOST_ASSERT(strict_ischar(ch));
  148. return iswxdigit(to_int_type(ch)) != 0;
  149. }
  150. static bool
  151. isblank BOOST_PREVENT_MACRO_SUBSTITUTION (wchar_t ch)
  152. {
  153. BOOST_ASSERT(strict_ischar(ch));
  154. return (ch == L' ' || ch == L'\t');
  155. }
  156. ///////////////////////////////////////////////////////////////////////
  157. // Simple character conversions
  158. ///////////////////////////////////////////////////////////////////////
  159. static wchar_t
  160. tolower(wchar_t ch)
  161. {
  162. using namespace std;
  163. BOOST_ASSERT(strict_ischar(ch));
  164. return isupper(ch) ?
  165. to_char_type<wchar_t>(towlower(to_int_type(ch))) : ch;
  166. }
  167. static wchar_t
  168. toupper(wchar_t ch)
  169. {
  170. using namespace std;
  171. BOOST_ASSERT(strict_ischar(ch));
  172. return islower(ch) ?
  173. to_char_type<wchar_t>(towupper(to_int_type(ch))) : ch;
  174. }
  175. static ::boost::uint32_t
  176. toucs4(int ch)
  177. {
  178. BOOST_ASSERT(strict_ischar(ch));
  179. return ch;
  180. }
  181. };
  182. }}}
  183. #endif