basic_chset.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2009 Daniel Nuffer
  4. http://spirit.sourceforge.net/
  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. #ifndef BOOST_SPIRIT_BASIC_CHSET_APRIL_17_2008
  9. #define BOOST_SPIRIT_BASIC_CHSET_APRIL_17_2008
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <bitset>
  15. #include <climits>
  16. #include <boost/spirit/home/support/char_set/range_run.hpp>
  17. namespace boost { namespace spirit { namespace support { namespace detail
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. //
  21. // basic_chset: basic character set implementation using range_run
  22. //
  23. ///////////////////////////////////////////////////////////////////////////
  24. template <typename Char>
  25. struct basic_chset
  26. {
  27. basic_chset() {}
  28. basic_chset(basic_chset const& arg_)
  29. : rr(arg_.rr) {}
  30. bool
  31. test(Char v) const
  32. {
  33. return rr.test(v);
  34. }
  35. void
  36. set(Char from, Char to)
  37. {
  38. rr.set(range<Char>(from, to));
  39. }
  40. void
  41. set(Char c)
  42. {
  43. rr.set(range<Char>(c, c));
  44. }
  45. void
  46. clear(Char from, Char to)
  47. {
  48. rr.clear(range<Char>(from, to));
  49. }
  50. void
  51. clear(Char c)
  52. {
  53. rr.clear(range<Char>(c, c));
  54. }
  55. void
  56. clear()
  57. {
  58. rr.clear();
  59. }
  60. void
  61. inverse()
  62. {
  63. basic_chset inv;
  64. inv.set(
  65. (std::numeric_limits<Char>::min)(),
  66. (std::numeric_limits<Char>::max)()
  67. );
  68. inv -= *this;
  69. swap(inv);
  70. }
  71. void
  72. swap(basic_chset& x)
  73. {
  74. rr.swap(x.rr);
  75. }
  76. basic_chset&
  77. operator|=(basic_chset const& x)
  78. {
  79. typedef typename range_run<Char>::const_iterator const_iterator;
  80. for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter)
  81. rr.set(*iter);
  82. return *this;
  83. }
  84. basic_chset&
  85. operator&=(basic_chset const& x)
  86. {
  87. basic_chset inv;
  88. inv.set(
  89. (std::numeric_limits<Char>::min)(),
  90. (std::numeric_limits<Char>::max)()
  91. );
  92. inv -= x;
  93. *this -= inv;
  94. return *this;
  95. }
  96. basic_chset&
  97. operator-=(basic_chset const& x)
  98. {
  99. typedef typename range_run<Char>::const_iterator const_iterator;
  100. for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter)
  101. rr.clear(*iter);
  102. return *this;
  103. }
  104. basic_chset&
  105. operator^=(basic_chset const& x)
  106. {
  107. basic_chset bma = x;
  108. bma -= *this;
  109. *this -= x;
  110. *this |= bma;
  111. return *this;
  112. }
  113. private: range_run<Char> rr;
  114. };
  115. #if (CHAR_BIT == 8)
  116. ///////////////////////////////////////////////////////////////////////////
  117. //
  118. // basic_chset: specializations for 8 bit chars using std::bitset
  119. //
  120. ///////////////////////////////////////////////////////////////////////////
  121. template <typename Char>
  122. struct basic_chset_8bit
  123. {
  124. basic_chset_8bit() {}
  125. basic_chset_8bit(basic_chset_8bit const& arg_)
  126. : bset(arg_.bset) {}
  127. bool
  128. test(Char v) const
  129. {
  130. return bset.test((unsigned char)v);
  131. }
  132. void
  133. set(Char from, Char to)
  134. {
  135. for (int i = from; i <= to; ++i)
  136. bset.set((unsigned char)i);
  137. }
  138. void
  139. set(Char c)
  140. {
  141. bset.set((unsigned char)c);
  142. }
  143. void
  144. clear(Char from, Char to)
  145. {
  146. for (int i = from; i <= to; ++i)
  147. bset.reset((unsigned char)i);
  148. }
  149. void
  150. clear(Char c)
  151. {
  152. bset.reset((unsigned char)c);
  153. }
  154. void
  155. clear()
  156. {
  157. bset.reset();
  158. }
  159. void
  160. inverse()
  161. {
  162. bset.flip();
  163. }
  164. void
  165. swap(basic_chset_8bit& x)
  166. {
  167. std::swap(bset, x.bset);
  168. }
  169. basic_chset_8bit&
  170. operator|=(basic_chset_8bit const& x)
  171. {
  172. bset |= x.bset;
  173. return *this;
  174. }
  175. basic_chset_8bit&
  176. operator&=(basic_chset_8bit const& x)
  177. {
  178. bset &= x.bset;
  179. return *this;
  180. }
  181. basic_chset_8bit&
  182. operator-=(basic_chset_8bit const& x)
  183. {
  184. bset &= ~x.bset;
  185. return *this;
  186. }
  187. basic_chset_8bit&
  188. operator^=(basic_chset_8bit const& x)
  189. {
  190. bset ^= x.bset;
  191. return *this;
  192. }
  193. private: std::bitset<256> bset;
  194. };
  195. /////////////////////////////////
  196. template <>
  197. struct basic_chset<char>
  198. : basic_chset_8bit<char> {};
  199. /////////////////////////////////
  200. template <>
  201. struct basic_chset<signed char>
  202. : basic_chset_8bit<signed char> {};
  203. /////////////////////////////////
  204. template <>
  205. struct basic_chset<unsigned char>
  206. : basic_chset_8bit<unsigned char> {};
  207. #endif // #if (CHAR_BIT == 8)
  208. }}}}
  209. #endif