standard_wide.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <boost/type_traits/make_unsigned.hpp>
  18. namespace boost { namespace spirit { namespace traits
  19. {
  20. template <std::size_t N>
  21. struct wchar_t_size
  22. {
  23. BOOST_SPIRIT_ASSERT_MSG(N == 1 || N == 2 || N == 4,
  24. not_supported_size_of_wchar_t, ());
  25. };
  26. template <> struct wchar_t_size<1> { enum { mask = 0xff }; };
  27. template <> struct wchar_t_size<2> { enum { mask = 0xffff }; };
  28. template <> struct wchar_t_size<4> { enum { mask = 0xffffffff }; };
  29. }}}
  30. namespace boost { namespace spirit { namespace char_encoding
  31. {
  32. ///////////////////////////////////////////////////////////////////////////
  33. // Test characters for specified conditions (using std wchar_t functions)
  34. ///////////////////////////////////////////////////////////////////////////
  35. struct standard_wide
  36. {
  37. typedef wchar_t char_type;
  38. typedef wchar_t classify_type;
  39. template <typename Char>
  40. static typename std::char_traits<Char>::int_type
  41. to_int_type(Char ch)
  42. {
  43. return std::char_traits<Char>::to_int_type(ch);
  44. }
  45. template <typename Char>
  46. static Char
  47. to_char_type(typename std::char_traits<Char>::int_type ch)
  48. {
  49. return std::char_traits<Char>::to_char_type(ch);
  50. }
  51. static bool
  52. ischar(int ch)
  53. {
  54. // we have to watch out for sign extensions (casting is there to
  55. // silence certain compilers complaining about signed/unsigned
  56. // mismatch)
  57. return (
  58. std::size_t(0) ==
  59. std::size_t(ch & ~traits::wchar_t_size<sizeof(wchar_t)>::mask) ||
  60. std::size_t(~0) ==
  61. std::size_t(ch | traits::wchar_t_size<sizeof(wchar_t)>::mask)
  62. ) != 0; // any wchar_t, but no other bits set
  63. }
  64. static bool
  65. isalnum(wchar_t ch)
  66. {
  67. using namespace std;
  68. return iswalnum(to_int_type(ch)) != 0;
  69. }
  70. static bool
  71. isalpha(wchar_t ch)
  72. {
  73. using namespace std;
  74. return iswalpha(to_int_type(ch)) != 0;
  75. }
  76. static bool
  77. iscntrl(wchar_t ch)
  78. {
  79. using namespace std;
  80. return iswcntrl(to_int_type(ch)) != 0;
  81. }
  82. static bool
  83. isdigit(wchar_t ch)
  84. {
  85. using namespace std;
  86. return iswdigit(to_int_type(ch)) != 0;
  87. }
  88. static bool
  89. isgraph(wchar_t ch)
  90. {
  91. using namespace std;
  92. return iswgraph(to_int_type(ch)) != 0;
  93. }
  94. static bool
  95. islower(wchar_t ch)
  96. {
  97. using namespace std;
  98. return iswlower(to_int_type(ch)) != 0;
  99. }
  100. static bool
  101. isprint(wchar_t ch)
  102. {
  103. using namespace std;
  104. return iswprint(to_int_type(ch)) != 0;
  105. }
  106. static bool
  107. ispunct(wchar_t ch)
  108. {
  109. using namespace std;
  110. return iswpunct(to_int_type(ch)) != 0;
  111. }
  112. static bool
  113. isspace(wchar_t ch)
  114. {
  115. using namespace std;
  116. return iswspace(to_int_type(ch)) != 0;
  117. }
  118. static bool
  119. isupper(wchar_t ch)
  120. {
  121. using namespace std;
  122. return iswupper(to_int_type(ch)) != 0;
  123. }
  124. static bool
  125. isxdigit(wchar_t ch)
  126. {
  127. using namespace std;
  128. return iswxdigit(to_int_type(ch)) != 0;
  129. }
  130. static bool
  131. isblank BOOST_PREVENT_MACRO_SUBSTITUTION (wchar_t ch)
  132. {
  133. return (ch == L' ' || ch == L'\t');
  134. }
  135. ///////////////////////////////////////////////////////////////////////
  136. // Simple character conversions
  137. ///////////////////////////////////////////////////////////////////////
  138. static wchar_t
  139. tolower(wchar_t ch)
  140. {
  141. using namespace std;
  142. return isupper(ch) ?
  143. to_char_type<wchar_t>(towlower(to_int_type(ch))) : ch;
  144. }
  145. static wchar_t
  146. toupper(wchar_t ch)
  147. {
  148. using namespace std;
  149. return islower(ch) ?
  150. to_char_type<wchar_t>(towupper(to_int_type(ch))) : ch;
  151. }
  152. static ::boost::uint32_t
  153. toucs4(wchar_t ch)
  154. {
  155. return static_cast<make_unsigned<wchar_t>::type>(ch);
  156. }
  157. };
  158. }}}
  159. #endif